--- /dev/null
+/*\r\r
+ * put your module comment here\r\r
+ * formatted with JxBeauty (c) johann.langhofer@nextra.at\r\r
+ */\r\r
+\r\r
+package com.swabunga.spell.swing;\r\r
+\r\r
+import com.swabunga.spell.engine.Word;\r\r
+import com.swabunga.spell.event.*;\r\r
+import javax.swing.*;\r\r
+import javax.swing.text.*;\r\r
+import javax.swing.event.*;\r\r
+import java.io.File;\r\r
+import java.awt.*;\r\r
+import java.awt.event.*;\r\r
+import java.util.*;\r\r
+\r\r
+/** Implementation of a spell check form.\r\r
+ * <p>This needs to layed out correctly but for the most part it works.</p>\r\r
+ *\r\r
+ * @author Jason Height (jheight@chariot.net.au)\r\r
+ *\r\r
+ * NOTE: Modification be Howard Kistler:\r\r
+ * Removed the calls that change the Invalid Word text box, because\r\r
+ * I think that behaviour is counter-intuitive and might even screw\r\r
+ * up other functions that rely on it being the original invalid word.\r\r
+ * If it's going to be like this for good, it should probably be a\r\r
+ * Label instead of a Textfield.\r\r
+ *\r\r
+ */\r\r
+public class JSpellForm extends JPanel\r\r
+ implements ActionListener, ListSelectionListener {\r\r
+ /** The Ignore button click action command*/\r\r
+ public static final String IGNORE_CMD = "IGNORE";\r\r
+ /** The Ignore All button click action command*/\r\r
+ public static final String IGNOREALL_CMD = "IGNOREALL";\r\r
+ /** The Add button click action command*/\r\r
+ public static final String ADD_CMD = "ADD";\r\r
+ /** The Replace button click action command*/\r\r
+ public static final String REPLACE_CMD = "REPLACE";\r\r
+ /** The Replace All button click action command*/\r\r
+ public static final String REPLACEALL_CMD = "REPLACEALL";\r\r
+ /** The Cancel button click action command*/\r\r
+ public static final String CANCEL_CMD = "CANCEL";\r\r
+ /** The resource for the Suggestions label*/\r\r
+ private static final String SUGGESTIONS_RES = "SUGGESTIONS";\r\r
+ private static final String INVALIDWORD_RES = "INVALIDWORD";\r\r
+\r\r
+ /* Accessible GUI Components */\r\r
+ protected JList suggestList;\r\r
+ protected JTextArea checkText;\r\r
+ /* The current spell check event */\r\r
+ protected SpellCheckEvent spellEvent;\r\r
+ /** The listener list (holds actionlisteners) */\r\r
+ protected EventListenerList listenerList = new EventListenerList();\r\r
+ protected ResourceBundle messages;\r\r
+\r\r
+ /** Panel constructor */\r\r
+ public JSpellForm () {\r\r
+ messages = ResourceBundle.getBundle("com.swabunga.spell.swing.messages", Locale.getDefault());\r\r
+ initialiseGUI();\r\r
+ }\r\r
+\r\r
+ /** Helper method to create a JButton with a command, a text label and a listener*/\r\r
+ private static final JButton createButton (String command, String text, ActionListener listener) {\r\r
+ JButton btn = new JButton(text);\r\r
+ btn.setActionCommand(command);\r\r
+ btn.addActionListener(listener);\r\r
+ return btn;\r\r
+ }\r\r
+\r\r
+ /** Creates the buttons on the left hand side of the panel*/\r\r
+ protected JPanel makeEastPanel () {\r\r
+ JPanel jPanel1 = new JPanel();\r\r
+ BoxLayout layout = new BoxLayout(jPanel1, BoxLayout.Y_AXIS);\r\r
+ jPanel1.setLayout(layout);\r\r
+ \r\r
+ JButton ignoreBtn = createButton(IGNORE_CMD, messages.getString(IGNORE_CMD), this);\r\r
+ ignoreBtn.setMaximumSize( new Dimension(Short.MAX_VALUE, Short.MAX_VALUE )); \r\r
+ jPanel1.add(ignoreBtn);\r\r
+ \r\r
+ JButton ignoreAllBtn = createButton(IGNOREALL_CMD, messages.getString(IGNOREALL_CMD), this);\r\r
+ ignoreAllBtn.setMaximumSize( new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));\r\r
+ jPanel1.add(ignoreAllBtn);\r\r
+ \r\r
+ JButton addBtn = createButton(ADD_CMD, messages.getString(ADD_CMD), this);\r\r
+ addBtn.setMaximumSize( new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));\r\r
+ jPanel1.add(addBtn);\r\r
+ \r\r
+ JButton changeBtn = createButton(REPLACE_CMD, messages.getString(REPLACE_CMD), this);\r\r
+ changeBtn.setMaximumSize( new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));\r\r
+ jPanel1.add(changeBtn);\r\r
+ \r\r
+ JButton changeAllBtn = createButton(REPLACEALL_CMD, messages.getString(REPLACEALL_CMD), this);\r\r
+ changeAllBtn.setMaximumSize( new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));\r\r
+ jPanel1.add(changeAllBtn);\r\r
+ \r\r
+ JButton cancelBtn = createButton(CANCEL_CMD, messages.getString(CANCEL_CMD), this);\r\r
+ cancelBtn.setMaximumSize( new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));\r\r
+ jPanel1.add(cancelBtn);\r\r
+ \r\r
+ return jPanel1;\r\r
+ }\r\r
+\r\r
+ protected JPanel makeCentrePanel () {\r\r
+ JPanel jPanel2 = new JPanel();\r\r
+ jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.Y_AXIS));\r\r
+ JLabel lbl1 = new JLabel(messages.getString(INVALIDWORD_RES));\r\r
+ jPanel2.add(lbl1);\r\r
+ checkText = new JTextArea();\r\r
+ jPanel2.add(new JScrollPane(checkText));\r\r
+ JLabel lbl2 = new JLabel(messages.getString(SUGGESTIONS_RES));\r\r
+ jPanel2.add(lbl2);\r\r
+ suggestList = new JList();\r\r
+ suggestList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);\r\r
+ jPanel2.add(new JScrollPane(suggestList));\r\r
+ suggestList.addListSelectionListener(this);\r\r
+ return jPanel2;\r\r
+ }\r\r
+\r\r
+ /** Called by the constructor to initialise the GUI*/\r\r
+ protected void initialiseGUI () {\r\r
+ setLayout(new BorderLayout());\r\r
+ this.add(makeEastPanel(), BorderLayout.EAST);\r\r
+ this.add(makeCentrePanel(), BorderLayout.CENTER);\r\r
+ }\r\r
+\r\r
+ /** Register an action listener */\r\r
+ public void addActionListener (ActionListener l) {\r\r
+ listenerList.add(ActionListener.class, l);\r\r
+ }\r\r
+\r\r
+ /** Deregister an action listener*/\r\r
+ public void removeActionListener (ActionListener l) {\r\r
+ listenerList.remove(ActionListener.class, l);\r\r
+ }\r\r
+\r\r
+ protected void fireActionEvent (ActionEvent e) {\r\r
+ // Guaranteed to return a non-null array\r\r
+ Object[] listeners = listenerList.getListenerList();\r\r
+ // Process the listeners last to first, notifying\r\r
+ // those that are interested in this event\r\r
+ for (int i = listeners.length - 2; i >= 0; i -= 2) {\r\r
+ if (listeners[i] == ActionListener.class) {\r\r
+ ((ActionListener)listeners[i + 1]).actionPerformed(e);\r\r
+ }\r\r
+ }\r\r
+ }\r\r
+\r\r
+ /** Sets the current spell check event that is being shown to the user*/\r\r
+ public void setSpellEvent (SpellCheckEvent event) {\r\r
+ spellEvent = event;\r\r
+ DefaultListModel m = new DefaultListModel();\r\r
+ java.util.List suggestions = event.getSuggestions();\r\r
+ for (int i = 0; i < suggestions.size(); i++) {\r\r
+ m.addElement(suggestions.get(i));\r\r
+ }\r\r
+ suggestList.setModel(m);\r\r
+ if (m.size()>0) {\r\r
+ suggestList.setSelectedIndex(0);\r\r
+ }\r\r
+ checkText.setText(event.getInvalidWord());\r\r
+ }\r\r
+\r\r
+ /** Fired when a value in the list is selected*/\r\r
+ public void valueChanged(ListSelectionEvent e) {\r\r
+ if (!e.getValueIsAdjusting()) {\r\r
+ Object selectedValue = suggestList.getSelectedValue();\r\r
+ }\r\r
+ }\r\r
+\r\r
+ /** Fired when a button is selected */\r\r
+ public void actionPerformed (ActionEvent e) {\r\r
+ if (IGNORE_CMD.equals(e.getActionCommand())) {\r\r
+ spellEvent.ignoreWord(false);\r\r
+ }\r\r
+ else if (IGNOREALL_CMD.equals(e.getActionCommand())) {\r\r
+ spellEvent.ignoreWord(true);\r\r
+ }\r\r
+ else if (REPLACE_CMD.equals(e.getActionCommand())) {\r\r
+ // Howard Kistler START\r\r
+ String replacementWord;\r\r
+ if(suggestList.getSelectedValue() != null)\r\r
+ {\r\r
+ replacementWord = suggestList.getSelectedValue().toString();\r\r
+ }\r\r
+ else\r\r
+ {\r\r
+ replacementWord = checkText.getText();\r\r
+ }\r\r
+ spellEvent.replaceWord(replacementWord, false);\r\r
+ // Howard Kistler END\r\r
+ }\r\r
+ else if (REPLACEALL_CMD.equals(e.getActionCommand())) {\r\r
+ // Howard Kistler START\r\r
+ String replacementWord;\r\r
+ if(suggestList.getSelectedValue() != null)\r\r
+ {\r\r
+ replacementWord = suggestList.getSelectedValue().toString();\r\r
+ }\r\r
+ else\r\r
+ {\r\r
+ replacementWord = checkText.getText();\r\r
+ }\r\r
+ spellEvent.replaceWord(replacementWord, true);\r\r
+ // Howard Kistler END\r\r
+ }\r\r
+ else if (ADD_CMD.equals(e.getActionCommand())) {\r\r
+ spellEvent.addToDictionary(checkText.getText());\r\r
+ }\r\r
+ else if (CANCEL_CMD.equals(e.getActionCommand())) {\r\r
+ spellEvent.cancel();\r\r
+ }\r\r
+ fireActionEvent(e);\r\r
+ }\r\r
+\r\r
+ public static void main (String[] args) {\r\r
+ try {\r\r
+ JSpellForm pane = new JSpellForm();\r\r
+ JFrame frm = new JFrame("Spelling");\r\r
+ frm.getContentPane().add(pane);\r\r
+ frm.setSize(300, 300);\r\r
+ frm.setVisible(true);\r\r
+ frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\r\r
+ } catch (Exception ex) {\r\r
+ ex.printStackTrace();\r\r
+ }\r\r
+ }\r\r
+}\r\r
+\r\r
+\r\r
+\r\r