Initial revision
[old-projects.git] / ekit / com / swabunga / spell / swing / JSpellForm.java
CommitLineData
6dd70280
JL
1/*\r\r
2 * put your module comment here\r\r
3 * formatted with JxBeauty (c) johann.langhofer@nextra.at\r\r
4 */\r\r
5\r\r
6package com.swabunga.spell.swing;\r\r
7\r\r
8import com.swabunga.spell.engine.Word;\r\r
9import com.swabunga.spell.event.*;\r\r
10import javax.swing.*;\r\r
11import javax.swing.text.*;\r\r
12import javax.swing.event.*;\r\r
13import java.io.File;\r\r
14import java.awt.*;\r\r
15import java.awt.event.*;\r\r
16import java.util.*;\r\r
17\r\r
18/** Implementation of a spell check form.\r\r
19 * <p>This needs to layed out correctly but for the most part it works.</p>\r\r
20 *\r\r
21 * @author Jason Height (jheight@chariot.net.au)\r\r
22 *\r\r
23 * NOTE: Modification be Howard Kistler:\r\r
24 * Removed the calls that change the Invalid Word text box, because\r\r
25 * I think that behaviour is counter-intuitive and might even screw\r\r
26 * up other functions that rely on it being the original invalid word.\r\r
27 * If it's going to be like this for good, it should probably be a\r\r
28 * Label instead of a Textfield.\r\r
29 *\r\r
30 */\r\r
31public class JSpellForm extends JPanel\r\r
32 implements ActionListener, ListSelectionListener {\r\r
33 /** The Ignore button click action command*/\r\r
34 public static final String IGNORE_CMD = "IGNORE";\r\r
35 /** The Ignore All button click action command*/\r\r
36 public static final String IGNOREALL_CMD = "IGNOREALL";\r\r
37 /** The Add button click action command*/\r\r
38 public static final String ADD_CMD = "ADD";\r\r
39 /** The Replace button click action command*/\r\r
40 public static final String REPLACE_CMD = "REPLACE";\r\r
41 /** The Replace All button click action command*/\r\r
42 public static final String REPLACEALL_CMD = "REPLACEALL";\r\r
43 /** The Cancel button click action command*/\r\r
44 public static final String CANCEL_CMD = "CANCEL";\r\r
45 /** The resource for the Suggestions label*/\r\r
46 private static final String SUGGESTIONS_RES = "SUGGESTIONS";\r\r
47 private static final String INVALIDWORD_RES = "INVALIDWORD";\r\r
48\r\r
49 /* Accessible GUI Components */\r\r
50 protected JList suggestList;\r\r
51 protected JTextArea checkText;\r\r
52 /* The current spell check event */\r\r
53 protected SpellCheckEvent spellEvent;\r\r
54 /** The listener list (holds actionlisteners) */\r\r
55 protected EventListenerList listenerList = new EventListenerList();\r\r
56 protected ResourceBundle messages;\r\r
57\r\r
58 /** Panel constructor */\r\r
59 public JSpellForm () {\r\r
60 messages = ResourceBundle.getBundle("com.swabunga.spell.swing.messages", Locale.getDefault());\r\r
61 initialiseGUI();\r\r
62 }\r\r
63\r\r
64 /** Helper method to create a JButton with a command, a text label and a listener*/\r\r
65 private static final JButton createButton (String command, String text, ActionListener listener) {\r\r
66 JButton btn = new JButton(text);\r\r
67 btn.setActionCommand(command);\r\r
68 btn.addActionListener(listener);\r\r
69 return btn;\r\r
70 }\r\r
71\r\r
72 /** Creates the buttons on the left hand side of the panel*/\r\r
73 protected JPanel makeEastPanel () {\r\r
74 JPanel jPanel1 = new JPanel();\r\r
75 BoxLayout layout = new BoxLayout(jPanel1, BoxLayout.Y_AXIS);\r\r
76 jPanel1.setLayout(layout);\r\r
77 \r\r
78 JButton ignoreBtn = createButton(IGNORE_CMD, messages.getString(IGNORE_CMD), this);\r\r
79 ignoreBtn.setMaximumSize( new Dimension(Short.MAX_VALUE, Short.MAX_VALUE )); \r\r
80 jPanel1.add(ignoreBtn);\r\r
81 \r\r
82 JButton ignoreAllBtn = createButton(IGNOREALL_CMD, messages.getString(IGNOREALL_CMD), this);\r\r
83 ignoreAllBtn.setMaximumSize( new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));\r\r
84 jPanel1.add(ignoreAllBtn);\r\r
85 \r\r
86 JButton addBtn = createButton(ADD_CMD, messages.getString(ADD_CMD), this);\r\r
87 addBtn.setMaximumSize( new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));\r\r
88 jPanel1.add(addBtn);\r\r
89 \r\r
90 JButton changeBtn = createButton(REPLACE_CMD, messages.getString(REPLACE_CMD), this);\r\r
91 changeBtn.setMaximumSize( new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));\r\r
92 jPanel1.add(changeBtn);\r\r
93 \r\r
94 JButton changeAllBtn = createButton(REPLACEALL_CMD, messages.getString(REPLACEALL_CMD), this);\r\r
95 changeAllBtn.setMaximumSize( new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));\r\r
96 jPanel1.add(changeAllBtn);\r\r
97 \r\r
98 JButton cancelBtn = createButton(CANCEL_CMD, messages.getString(CANCEL_CMD), this);\r\r
99 cancelBtn.setMaximumSize( new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));\r\r
100 jPanel1.add(cancelBtn);\r\r
101 \r\r
102 return jPanel1;\r\r
103 }\r\r
104\r\r
105 protected JPanel makeCentrePanel () {\r\r
106 JPanel jPanel2 = new JPanel();\r\r
107 jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.Y_AXIS));\r\r
108 JLabel lbl1 = new JLabel(messages.getString(INVALIDWORD_RES));\r\r
109 jPanel2.add(lbl1);\r\r
110 checkText = new JTextArea();\r\r
111 jPanel2.add(new JScrollPane(checkText));\r\r
112 JLabel lbl2 = new JLabel(messages.getString(SUGGESTIONS_RES));\r\r
113 jPanel2.add(lbl2);\r\r
114 suggestList = new JList();\r\r
115 suggestList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);\r\r
116 jPanel2.add(new JScrollPane(suggestList));\r\r
117 suggestList.addListSelectionListener(this);\r\r
118 return jPanel2;\r\r
119 }\r\r
120\r\r
121 /** Called by the constructor to initialise the GUI*/\r\r
122 protected void initialiseGUI () {\r\r
123 setLayout(new BorderLayout());\r\r
124 this.add(makeEastPanel(), BorderLayout.EAST);\r\r
125 this.add(makeCentrePanel(), BorderLayout.CENTER);\r\r
126 }\r\r
127\r\r
128 /** Register an action listener */\r\r
129 public void addActionListener (ActionListener l) {\r\r
130 listenerList.add(ActionListener.class, l);\r\r
131 }\r\r
132\r\r
133 /** Deregister an action listener*/\r\r
134 public void removeActionListener (ActionListener l) {\r\r
135 listenerList.remove(ActionListener.class, l);\r\r
136 }\r\r
137\r\r
138 protected void fireActionEvent (ActionEvent e) {\r\r
139 // Guaranteed to return a non-null array\r\r
140 Object[] listeners = listenerList.getListenerList();\r\r
141 // Process the listeners last to first, notifying\r\r
142 // those that are interested in this event\r\r
143 for (int i = listeners.length - 2; i >= 0; i -= 2) {\r\r
144 if (listeners[i] == ActionListener.class) {\r\r
145 ((ActionListener)listeners[i + 1]).actionPerformed(e);\r\r
146 }\r\r
147 }\r\r
148 }\r\r
149\r\r
150 /** Sets the current spell check event that is being shown to the user*/\r\r
151 public void setSpellEvent (SpellCheckEvent event) {\r\r
152 spellEvent = event;\r\r
153 DefaultListModel m = new DefaultListModel();\r\r
154 java.util.List suggestions = event.getSuggestions();\r\r
155 for (int i = 0; i < suggestions.size(); i++) {\r\r
156 m.addElement(suggestions.get(i));\r\r
157 }\r\r
158 suggestList.setModel(m);\r\r
159 if (m.size()>0) {\r\r
160 suggestList.setSelectedIndex(0);\r\r
161 }\r\r
162 checkText.setText(event.getInvalidWord());\r\r
163 }\r\r
164\r\r
165 /** Fired when a value in the list is selected*/\r\r
166 public void valueChanged(ListSelectionEvent e) {\r\r
167 if (!e.getValueIsAdjusting()) {\r\r
168 Object selectedValue = suggestList.getSelectedValue();\r\r
169 }\r\r
170 }\r\r
171\r\r
172 /** Fired when a button is selected */\r\r
173 public void actionPerformed (ActionEvent e) {\r\r
174 if (IGNORE_CMD.equals(e.getActionCommand())) {\r\r
175 spellEvent.ignoreWord(false);\r\r
176 }\r\r
177 else if (IGNOREALL_CMD.equals(e.getActionCommand())) {\r\r
178 spellEvent.ignoreWord(true);\r\r
179 }\r\r
180 else if (REPLACE_CMD.equals(e.getActionCommand())) {\r\r
181 // Howard Kistler START\r\r
182 String replacementWord;\r\r
183 if(suggestList.getSelectedValue() != null)\r\r
184 {\r\r
185 replacementWord = suggestList.getSelectedValue().toString();\r\r
186 }\r\r
187 else\r\r
188 {\r\r
189 replacementWord = checkText.getText();\r\r
190 }\r\r
191 spellEvent.replaceWord(replacementWord, false);\r\r
192 // Howard Kistler END\r\r
193 }\r\r
194 else if (REPLACEALL_CMD.equals(e.getActionCommand())) {\r\r
195 // Howard Kistler START\r\r
196 String replacementWord;\r\r
197 if(suggestList.getSelectedValue() != null)\r\r
198 {\r\r
199 replacementWord = suggestList.getSelectedValue().toString();\r\r
200 }\r\r
201 else\r\r
202 {\r\r
203 replacementWord = checkText.getText();\r\r
204 }\r\r
205 spellEvent.replaceWord(replacementWord, true);\r\r
206 // Howard Kistler END\r\r
207 }\r\r
208 else if (ADD_CMD.equals(e.getActionCommand())) {\r\r
209 spellEvent.addToDictionary(checkText.getText());\r\r
210 }\r\r
211 else if (CANCEL_CMD.equals(e.getActionCommand())) {\r\r
212 spellEvent.cancel();\r\r
213 }\r\r
214 fireActionEvent(e);\r\r
215 }\r\r
216\r\r
217 public static void main (String[] args) {\r\r
218 try {\r\r
219 JSpellForm pane = new JSpellForm();\r\r
220 JFrame frm = new JFrame("Spelling");\r\r
221 frm.getContentPane().add(pane);\r\r
222 frm.setSize(300, 300);\r\r
223 frm.setVisible(true);\r\r
224 frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\r\r
225 } catch (Exception ex) {\r\r
226 ex.printStackTrace();\r\r
227 }\r\r
228 }\r\r
229}\r\r
230\r\r
231\r\r
232\r\r