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