KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > client > widgets > jazzy > JSpellForm


1 /*
2  * put your module comment here
3  * formatted with JxBeauty (c) johann.langhofer@nextra.at
4  */

5
6 /*
7  * Changes 11 Jan 2003 Anthony Roy:
8  *
9  * 1) Changed checkText from a JTextArea to a JTextField (lines 51 and 115)
10  * 2) Altered the ADD_CMD action (Line 196). Now adds the misspelled word to the dictionary unless
11  * a new word is typed which does not match the current suggestion. A confirm dialog is shown.
12  */

13 package org.lucane.client.widgets.jazzy;
14
15 import com.swabunga.spell.engine.Word;
16 import com.swabunga.spell.event.SpellCheckEvent;
17
18 import javax.swing.*;
19 import javax.swing.event.EventListenerList JavaDoc;
20 import javax.swing.event.ListSelectionEvent JavaDoc;
21 import javax.swing.event.ListSelectionListener JavaDoc;
22
23 import org.lucane.client.util.Translation;
24
25 import java.awt.*;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28
29
30 /** Implementation of a spell check form.
31  * <p>This needs to layed out correctly but for the most part it works.</p>
32  *
33  * @author Jason Height (jheight@chariot.net.au)
34  */

35 public class JSpellForm extends JPanel implements ActionListener JavaDoc, ListSelectionListener JavaDoc {
36   /** The Ignore button click action command*/
37   public static final String JavaDoc IGNORE_CMD = "IGNORE";
38   /** The Ignore All button click action command*/
39   public static final String JavaDoc IGNOREALL_CMD = "IGNOREALL";
40   /** The Add button click action command*/
41   public static final String JavaDoc ADD_CMD = "ADD";
42   /** The Replace button click action command*/
43   public static final String JavaDoc REPLACE_CMD = "REPLACE";
44   /** The Replace All button click action command*/
45   public static final String JavaDoc REPLACEALL_CMD = "REPLACEALL";
46   /** The Cancel button click action command*/
47   public static final String JavaDoc CANCEL_CMD = "CANCEL";
48   /** The resource for the Suggestions label*/
49   private static final String JavaDoc SUGGESTIONS_RES = "SUGGESTIONS";
50   private static final String JavaDoc INVALIDWORD_RES = "INVALIDWORD";
51   /** Add word confirm*/
52   public static final String JavaDoc ADDWORD_1 = "ADDWORD_1";
53   public static final String JavaDoc ADDWORD_2 = "ADDWORD_2";
54   public static final String JavaDoc ADDWORD_3 = "ADDWORD_3";
55   
56   private JLabel wrongWordLabel;
57
58   /* Accessible GUI Components */
59   protected JList suggestList;
60   protected JTextField checkText;
61   /* The current spell check event */
62   protected SpellCheckEvent spellEvent;
63   /** The listener list (holds actionlisteners) */
64   protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
65   //protected ResourceBundle messages;
66

67   /** Panel constructor */
68   public JSpellForm() {
69     //messages = ResourceBundle.getBundle("com.swabunga.spell.swing.messages", Locale.getDefault());
70
initialiseGUI();
71   }
72
73   /** Helper method to create a JButton with a command, a text label and a listener*/
74   private static final JButton createButton(String JavaDoc command, String JavaDoc text, ActionListener JavaDoc listener) {
75     JButton btn = new JButton(text);
76     btn.setActionCommand(command);
77     btn.addActionListener(listener);
78     return btn;
79   }
80
81   /** Creates the buttons on the left hand side of the panel*/
82   protected JPanel makeEastPanel() {
83     JPanel jPanel1 = new JPanel();
84     BoxLayout layout = new BoxLayout(jPanel1, BoxLayout.Y_AXIS);
85     jPanel1.setLayout(layout);
86
87     JButton ignoreBtn = createButton(IGNORE_CMD, Translation.tr("jazzy."+IGNORE_CMD), this);
88     ignoreBtn.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
89     jPanel1.add(ignoreBtn);
90
91     JButton ignoreAllBtn = createButton(IGNOREALL_CMD, Translation.tr("jazzy."+IGNOREALL_CMD), this);
92     ignoreAllBtn.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
93     jPanel1.add(ignoreAllBtn);
94
95     JButton addBtn = createButton(ADD_CMD, Translation.tr("jazzy."+ADD_CMD), this);
96     addBtn.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
97     jPanel1.add(addBtn);
98
99     JButton changeBtn = createButton(REPLACE_CMD, Translation.tr("jazzy."+REPLACE_CMD), this);
100     changeBtn.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
101     jPanel1.add(changeBtn);
102
103     JButton changeAllBtn = createButton(REPLACEALL_CMD, Translation.tr("jazzy."+REPLACEALL_CMD), this);
104     changeAllBtn.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
105     jPanel1.add(changeAllBtn);
106
107     JButton cancelBtn = createButton(CANCEL_CMD, Translation.tr("jazzy."+CANCEL_CMD), this);
108     cancelBtn.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
109     jPanel1.add(cancelBtn);
110
111     return jPanel1;
112   }
113
114   protected JPanel makeCentrePanel() {
115     JPanel jPanel2 = new JPanel();
116     jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.Y_AXIS));
117     JPanel jPanel3 = new JPanel();
118     JLabel lbl1 = new JLabel(Translation.tr("jazzy."+INVALIDWORD_RES));
119     wrongWordLabel = new JLabel("");
120     wrongWordLabel.setForeground(Color.red); //Changed Color.RED to Color.red for 1.3 compatibility.
121
jPanel3.add(lbl1);
122     jPanel3.add(wrongWordLabel);
123     jPanel2.add(jPanel3);
124     checkText = new JTextField();
125     jPanel2.add(checkText);
126     JLabel lbl2 = new JLabel(Translation.tr("jazzy."+SUGGESTIONS_RES));
127     jPanel2.add(lbl2);
128     suggestList = new JList();
129     suggestList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
130     jPanel2.add(new JScrollPane(suggestList));
131     suggestList.addListSelectionListener(this);
132     return jPanel2;
133   }
134
135   /** Called by the constructor to initialise the GUI*/
136   protected void initialiseGUI() {
137     setLayout(new BorderLayout());
138     this.add(makeEastPanel(), BorderLayout.EAST);
139     this.add(makeCentrePanel(), BorderLayout.CENTER);
140   }
141
142   /** Register an action listener */
143   public void addActionListener(ActionListener JavaDoc l) {
144     listenerList.add(ActionListener JavaDoc.class, l);
145   }
146
147   /** Deregister an action listener*/
148   public void removeActionListener(ActionListener JavaDoc l) {
149     listenerList.remove(ActionListener JavaDoc.class, l);
150   }
151
152   protected void fireActionEvent(ActionEvent JavaDoc e) {
153     // Guaranteed to return a non-null array
154
Object JavaDoc[] listeners = listenerList.getListenerList();
155     // Process the listeners last to first, notifying
156
// those that are interested in this event
157
for (int i = listeners.length - 2; i >= 0; i -= 2) {
158       if (listeners[i] == ActionListener JavaDoc.class) {
159         ((ActionListener JavaDoc) listeners[i + 1]).actionPerformed(e);
160       }
161     }
162   }
163
164   /** Sets the current spell check event that is being shown to the user*/
165   public void setSpellEvent(SpellCheckEvent event) {
166     spellEvent = event;
167     DefaultListModel m = new DefaultListModel();
168     java.util.List JavaDoc suggestions = event.getSuggestions();
169     for (int i = 0; i < suggestions.size(); i++) {
170       m.addElement(suggestions.get(i));
171     }
172     suggestList.setModel(m);
173     wrongWordLabel.setText(event.getInvalidWord());
174     if (m.size() > 0) {
175       suggestList.setSelectedIndex(0);
176       checkText.setText(((Word) m.get(0)).getWord());
177     } else {
178       checkText.setText(event.getInvalidWord());
179     }
180   }
181
182   /** Fired when a value in the list is selected*/
183   public void valueChanged(ListSelectionEvent JavaDoc e) {
184     if (!e.getValueIsAdjusting()) {
185       Object JavaDoc selectedValue = suggestList.getSelectedValue();
186       if (selectedValue != null)
187         checkText.setText(selectedValue.toString());
188     }
189   }
190
191   /** Fired when a button is selected */
192   public void actionPerformed(ActionEvent JavaDoc e) {
193     if (IGNORE_CMD.equals(e.getActionCommand())) {
194       spellEvent.ignoreWord(false);
195     } else if (IGNOREALL_CMD.equals(e.getActionCommand())) {
196       spellEvent.ignoreWord(true);
197     } else if (REPLACE_CMD.equals(e.getActionCommand())) {
198       spellEvent.replaceWord(checkText.getText(), false);
199     } else if (REPLACEALL_CMD.equals(e.getActionCommand())) {
200       spellEvent.replaceWord(checkText.getText(), true);
201     } else if (ADD_CMD.equals(e.getActionCommand())) {
202       String JavaDoc inField = checkText.getText();
203       Object JavaDoc selObj = suggestList.getSelectedValue();
204       String JavaDoc selected = (selObj == null ? "" : selObj.toString());
205       String JavaDoc addString = (inField.equals(selected) ? spellEvent.getInvalidWord() : inField);
206
207       int n = JOptionPane.showConfirmDialog(this, Translation.tr("jazzy."+ADDWORD_1) + " '" + addString + "' " +
208             Translation.tr("jazzy."+ADDWORD_2), Translation.tr("jazzy."+ADDWORD_3), JOptionPane.YES_NO_OPTION);
209
210       if (n == JOptionPane.YES_OPTION) {
211         spellEvent.addToDictionary(addString);
212       } else {
213         return;
214       }
215     } else if (CANCEL_CMD.equals(e.getActionCommand())) {
216       spellEvent.cancel();
217     }
218     fireActionEvent(e);
219   }
220
221   public static void main(String JavaDoc[] args) {
222     try {
223       JSpellForm pane = new JSpellForm();
224       JFrame frm = new JFrame(Translation.tr("jazzy.title"));
225       frm.getContentPane().add(pane);
226       frm.setSize(300, 300);
227       frm.setVisible(true);
228       frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
229     } catch (Exception JavaDoc ex) {
230       ex.printStackTrace();
231     }
232   }
233 }
234
235
236
237
Popular Tags