Originally posted by r035198x
version mayhap ... that local variable definitely is a nono.
kind regards,
Jos
public class Main implements ActionListener { private static final String OPTIONS_BUTTON = "Options..."; private JTextArea textArea = null; private JTextPane resultArea = null; private JComboBox langBox = null; private Map<Language, ConfigurationDialog> configDialogs = new HashMap<Language, ConfigurationDialog>(); public Main() { } public void createAndShowGUI(String inputText) { JFrame frame = new JFrame("Grammar Check Tool"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); textArea = new JTextArea(inputText); // TODO: wrong line number is displayed for lines that are wrapped automatically: textArea.setLineWrap(true); textArea.setWrapStyleWord(true); resultArea = new JTextPane(); resultArea.setContentType("text/html"); resultArea.setText("Results will appear here"); JLabel label = new JLabel("Please type or paste text to check in the top area"); JButton button = new JButton("Check text"); button.setMnemonic('c'); button.addActionListener(this); JButton configButton = new JButton(OPTIONS_BUTTON); configButton.setMnemonic('o'); configButton.addActionListener(this); JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); GridBagConstraints buttonCons = new GridBagConstraints(); buttonCons.gridx = 0; buttonCons.gridy = 0; panel.add(button, buttonCons); buttonCons.gridx = 1; buttonCons.gridy = 0; panel.add(new JLabel(" "), buttonCons); buttonCons.gridx = 2; buttonCons.gridy = 0; langBox = new JComboBox(); /*for (Language lang : Language.LANGUAGES) { if (lang != Language.DEMO) { langBox.addItem(lang); } }*/ //panel.add(langBox, buttonCons); buttonCons.gridx = 3; buttonCons.gridy = 0; buttonCons.insets = new Insets(0, 10, 0, 0); //panel.add(configButton, buttonCons); Container contentPane = frame.getContentPane(); GridBagLayout gridLayout = new GridBagLayout(); contentPane.setLayout(gridLayout); GridBagConstraints cons = new GridBagConstraints(); cons.fill = GridBagConstraints.BOTH; cons.weightx = 10.0f; cons.weighty = 10.0f; cons.gridx = 0; cons.gridy = 0; contentPane.add(new JScrollPane(textArea), cons); cons.gridy = 1; cons.weighty = 5.0f; contentPane.add(new JScrollPane(resultArea), cons); cons.fill = GridBagConstraints.NONE; cons.gridx = 0; cons.gridy = 2; cons.weighty = 0.0f; cons.insets = new Insets(3,3,3,3); //cons.fill = GridBagConstraints.NONE; contentPane.add(label, cons); cons.gridy = 3; contentPane.add(panel, cons); frame.pack(); frame.setSize(600, 600); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { String langName = "English"; Language language = Language.getLanguageForName(langName); ConfigurationDialog configDialog = null; if (configDialogs.containsKey(language)) { configDialog = (ConfigurationDialog)configDialogs.get(language); } else { configDialog = new ConfigurationDialog(null, false); configDialogs.put(language, configDialog); // JOptionPane.showMessageDialog(null,"test"); } JLanguageTool langTool; try { langTool = new JLanguageTool(language, configDialog.getMotherTongue()); langTool.activateDefaultPatternRules(); langTool.activateDefaultFalseFriendRules(); Set disabledRules = configDialog.getDisabledRuleIds(); if (disabledRules != null) { for (Iterator iter = disabledRules.iterator(); iter.hasNext();) { String ruleId = (String) iter.next(); langTool.disableRule(ruleId); } } } catch (IOException ioe) { throw new RuntimeException(ioe); } catch (ParserConfigurationException ex) { throw new RuntimeException(ex); } catch (SAXException ex) { throw new RuntimeException(ex); } if (e.getActionCommand().equals(OPTIONS_BUTTON)) { List rules = langTool.getAllRules(); configDialog.show(rules); } else { if (textArea.getText().trim().equals("")) { textArea.setText("Please insert text to check here"); } else { StringBuffer sb = new StringBuffer(); resultArea.setText("Starting check...<br>\n"); resultArea.repaint(); // FIXME: why doesn't this work? //TODO: resultArea.setCursor(new Cursor(Cursor.WAIT_CURSOR)); sb.append("Starting check in " +langName+ "...<br>\n"); int matches = 0; try { matches = checkText(langTool, textArea.getText(), sb); } catch (Exception ex) { sb.append("<br><br><b><font color=\"red\">" + ex.toString() + "<br>"); StackTraceElement[] elements = ex.getStackTrace(); for (StackTraceElement element : elements) { sb.append(element + "<br>"); } sb.append("</font></b><br>"); ex.printStackTrace(); } sb.append("Check done. " +matches+ " potential problems found<br>\n"); resultArea.setText(sb.toString()); resultArea.setCaretPosition(0); } } } public int checkText(JLanguageTool langTool, String text, StringBuffer sb) throws IOException { long startTime = System.currentTimeMillis(); List ruleMatches = langTool.check(text); long startTimeMatching = System.currentTimeMillis(); int i = 0; for (Iterator iter = ruleMatches.iterator(); iter.hasNext();) { RuleMatch match = (RuleMatch) iter.next(); sb.append("<br>\n<b>" +(i+1)+ ". Line " + (match.getLine() + 1) + ", column " + match.getColumn() + "</b><br>\n"); String msg = match.getMessage(); msg = msg.replaceAll("<suggestion>", "<b>"); msg = msg.replaceAll("</suggestion>", "</b>"); msg = msg.replaceAll("<old>", "<b>"); msg = msg.replaceAll("</old>", "</b>"); sb.append("<b>Message:</b> " + msg + "<br>\n"); sb.append("<b>Context:</b> " + Tools.getContext(match.getFromPos(), match.getToPos(), text)); sb.append("<br>\n"); i++; } long endTime = System.currentTimeMillis(); sb.append("<br>\nTime: " + (endTime - startTime) + "ms (including " + (endTime - startTimeMatching) + "ms for rule matching)<br>\n"); return ruleMatches.size(); } public void openGui (String input) { /*final Main prg = new Main(); prg.createAndShowGUI("They is around. ");*/ JOptionPane.showMessageDialog(null,"open"); createAndShowGUI(input); } }
Java Plug-in 1.6.0_02 Using JRE version 1.6.0_02 Java HotSpot(TM) Client VM User home directory = C:\Documents and Settings\Administrator network: Loading user-defined proxy configuration ... network: Done. network: Loading proxy configuration from Internet Explorer ... network: Done. network: Loading direct proxy configuration ... network: Done. network: Proxy Configuration: No proxy ---------------------------------------------------- c: clear console window f: finalize objects on finalization queue g: garbage collect h: display this help message l: dump classloader list m: print memory usage o: trigger logging p: reload proxy configuration q: hide console r: reload policy configuration s: dump system and deployment properties t: dump thread list v: dump thread stack x: clear classloader cache 0-5: set trace level to <n> ---------------------------------------------------- liveconnect: Invoking JS method: document liveconnect: Invoking JS method: URL basic: Referencing classloader: sun.plugin.ClassLoaderInfo@12498b5, refcount=1 basic: Added progress listener: sun.plugin.util.GrayBoxPainter@f5da06 basic: Loading applet ... basic: Initializing applet ... basic: Starting applet ... basic: completed perf rollup liveconnect: Invoking method: public void test.check(java.lang.String) liveconnect: Needs conversion: java.lang.String --> java.lang.String java.lang.NullPointerException at java.util.Hashtable.put(Unknown Source) at javax.swing.JEditorPane.registerEditorKitForContentType(Unknown Source) at javax.swing.JEditorPane.registerEditorKitForContentType(Unknown Source) at javax.swing.JEditorPane.loadDefaultKitsIfNecessary(Unknown Source) at javax.swing.JEditorPane.getKitTypeRegistry(Unknown Source) at javax.swing.JEditorPane.getEditorKitClassNameForContentType(Unknown Source) at javax.swing.JTextPane.<init>(Unknown Source) at mypack.Main.createAndShowGUI(main.java:84) at mypack.Main.openGui(main.java:238) at test.check(test.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at sun.plugin.javascript.JSInvoke.invoke(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at sun.plugin.javascript.JSClassLoader.invoke(Unknown Source) at sun.plugin.com.MethodDispatcher.invoke(Unknown Source) at sun.plugin.com.DispatchImpl.invokeImpl(Unknown Source) at sun.plugin.com.DispatchImpl$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at sun.plugin.com.DispatchImpl.invoke(Unknown Source) java.lang.Exception: java.lang.NullPointerException at sun.plugin.com.DispatchImpl.invokeImpl(Unknown Source) at sun.plugin.com.DispatchImpl$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at sun.plugin.com.DispatchImpl.invoke(Unknown Source)
Comment