001: /*
002: * put your module comment here
003: * formatted with JxBeauty (c) johann.langhofer@nextra.at
004: */
005:
006: /*
007: * Changes 11 Jan 2003 Anthony Roy:
008: *
009: * 1) Changed checkText from a JTextArea to a JTextField (lines 51 and 115)
010: * 2) Altered the ADD_CMD action (Line 196). Now adds the misspelled word to the dictionary unless
011: * a new word is typed which does not match the current suggestion. A confirm dialog is shown.
012: */
013: package org.lucane.client.widgets.jazzy;
014:
015: import com.swabunga.spell.engine.Word;
016: import com.swabunga.spell.event.SpellCheckEvent;
017:
018: import javax.swing.*;
019: import javax.swing.event.EventListenerList;
020: import javax.swing.event.ListSelectionEvent;
021: import javax.swing.event.ListSelectionListener;
022:
023: import org.lucane.client.util.Translation;
024:
025: import java.awt.*;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028:
029: /** Implementation of a spell check form.
030: * <p>This needs to layed out correctly but for the most part it works.</p>
031: *
032: * @author Jason Height (jheight@chariot.net.au)
033: */
034: public class JSpellForm extends JPanel implements ActionListener,
035: ListSelectionListener {
036: /** The Ignore button click action command*/
037: public static final String IGNORE_CMD = "IGNORE";
038: /** The Ignore All button click action command*/
039: public static final String IGNOREALL_CMD = "IGNOREALL";
040: /** The Add button click action command*/
041: public static final String ADD_CMD = "ADD";
042: /** The Replace button click action command*/
043: public static final String REPLACE_CMD = "REPLACE";
044: /** The Replace All button click action command*/
045: public static final String REPLACEALL_CMD = "REPLACEALL";
046: /** The Cancel button click action command*/
047: public static final String CANCEL_CMD = "CANCEL";
048: /** The resource for the Suggestions label*/
049: private static final String SUGGESTIONS_RES = "SUGGESTIONS";
050: private static final String INVALIDWORD_RES = "INVALIDWORD";
051: /** Add word confirm*/
052: public static final String ADDWORD_1 = "ADDWORD_1";
053: public static final String ADDWORD_2 = "ADDWORD_2";
054: public static final String ADDWORD_3 = "ADDWORD_3";
055:
056: private JLabel wrongWordLabel;
057:
058: /* Accessible GUI Components */
059: protected JList suggestList;
060: protected JTextField checkText;
061: /* The current spell check event */
062: protected SpellCheckEvent spellEvent;
063: /** The listener list (holds actionlisteners) */
064: protected EventListenerList listenerList = new EventListenerList();
065:
066: //protected ResourceBundle messages;
067:
068: /** Panel constructor */
069: public JSpellForm() {
070: //messages = ResourceBundle.getBundle("com.swabunga.spell.swing.messages", Locale.getDefault());
071: initialiseGUI();
072: }
073:
074: /** Helper method to create a JButton with a command, a text label and a listener*/
075: private static final JButton createButton(String command,
076: String text, ActionListener listener) {
077: JButton btn = new JButton(text);
078: btn.setActionCommand(command);
079: btn.addActionListener(listener);
080: return btn;
081: }
082:
083: /** Creates the buttons on the left hand side of the panel*/
084: protected JPanel makeEastPanel() {
085: JPanel jPanel1 = new JPanel();
086: BoxLayout layout = new BoxLayout(jPanel1, BoxLayout.Y_AXIS);
087: jPanel1.setLayout(layout);
088:
089: JButton ignoreBtn = createButton(IGNORE_CMD, Translation
090: .tr("jazzy." + IGNORE_CMD), this );
091: ignoreBtn.setMaximumSize(new Dimension(Short.MAX_VALUE,
092: Short.MAX_VALUE));
093: jPanel1.add(ignoreBtn);
094:
095: JButton ignoreAllBtn = createButton(IGNOREALL_CMD, Translation
096: .tr("jazzy." + IGNOREALL_CMD), this );
097: ignoreAllBtn.setMaximumSize(new Dimension(Short.MAX_VALUE,
098: Short.MAX_VALUE));
099: jPanel1.add(ignoreAllBtn);
100:
101: JButton addBtn = createButton(ADD_CMD, Translation.tr("jazzy."
102: + ADD_CMD), this );
103: addBtn.setMaximumSize(new Dimension(Short.MAX_VALUE,
104: Short.MAX_VALUE));
105: jPanel1.add(addBtn);
106:
107: JButton changeBtn = createButton(REPLACE_CMD, Translation
108: .tr("jazzy." + REPLACE_CMD), this );
109: changeBtn.setMaximumSize(new Dimension(Short.MAX_VALUE,
110: Short.MAX_VALUE));
111: jPanel1.add(changeBtn);
112:
113: JButton changeAllBtn = createButton(REPLACEALL_CMD, Translation
114: .tr("jazzy." + REPLACEALL_CMD), this );
115: changeAllBtn.setMaximumSize(new Dimension(Short.MAX_VALUE,
116: Short.MAX_VALUE));
117: jPanel1.add(changeAllBtn);
118:
119: JButton cancelBtn = createButton(CANCEL_CMD, Translation
120: .tr("jazzy." + CANCEL_CMD), this );
121: cancelBtn.setMaximumSize(new Dimension(Short.MAX_VALUE,
122: Short.MAX_VALUE));
123: jPanel1.add(cancelBtn);
124:
125: return jPanel1;
126: }
127:
128: protected JPanel makeCentrePanel() {
129: JPanel jPanel2 = new JPanel();
130: jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.Y_AXIS));
131: JPanel jPanel3 = new JPanel();
132: JLabel lbl1 = new JLabel(Translation.tr("jazzy."
133: + INVALIDWORD_RES));
134: wrongWordLabel = new JLabel("");
135: wrongWordLabel.setForeground(Color.red); //Changed Color.RED to Color.red for 1.3 compatibility.
136: jPanel3.add(lbl1);
137: jPanel3.add(wrongWordLabel);
138: jPanel2.add(jPanel3);
139: checkText = new JTextField();
140: jPanel2.add(checkText);
141: JLabel lbl2 = new JLabel(Translation.tr("jazzy."
142: + SUGGESTIONS_RES));
143: jPanel2.add(lbl2);
144: suggestList = new JList();
145: suggestList
146: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
147: jPanel2.add(new JScrollPane(suggestList));
148: suggestList.addListSelectionListener(this );
149: return jPanel2;
150: }
151:
152: /** Called by the constructor to initialise the GUI*/
153: protected void initialiseGUI() {
154: setLayout(new BorderLayout());
155: this .add(makeEastPanel(), BorderLayout.EAST);
156: this .add(makeCentrePanel(), BorderLayout.CENTER);
157: }
158:
159: /** Register an action listener */
160: public void addActionListener(ActionListener l) {
161: listenerList.add(ActionListener.class, l);
162: }
163:
164: /** Deregister an action listener*/
165: public void removeActionListener(ActionListener l) {
166: listenerList.remove(ActionListener.class, l);
167: }
168:
169: protected void fireActionEvent(ActionEvent e) {
170: // Guaranteed to return a non-null array
171: Object[] listeners = listenerList.getListenerList();
172: // Process the listeners last to first, notifying
173: // those that are interested in this event
174: for (int i = listeners.length - 2; i >= 0; i -= 2) {
175: if (listeners[i] == ActionListener.class) {
176: ((ActionListener) listeners[i + 1]).actionPerformed(e);
177: }
178: }
179: }
180:
181: /** Sets the current spell check event that is being shown to the user*/
182: public void setSpellEvent(SpellCheckEvent event) {
183: spellEvent = event;
184: DefaultListModel m = new DefaultListModel();
185: java.util.List suggestions = event.getSuggestions();
186: for (int i = 0; i < suggestions.size(); i++) {
187: m.addElement(suggestions.get(i));
188: }
189: suggestList.setModel(m);
190: wrongWordLabel.setText(event.getInvalidWord());
191: if (m.size() > 0) {
192: suggestList.setSelectedIndex(0);
193: checkText.setText(((Word) m.get(0)).getWord());
194: } else {
195: checkText.setText(event.getInvalidWord());
196: }
197: }
198:
199: /** Fired when a value in the list is selected*/
200: public void valueChanged(ListSelectionEvent e) {
201: if (!e.getValueIsAdjusting()) {
202: Object selectedValue = suggestList.getSelectedValue();
203: if (selectedValue != null)
204: checkText.setText(selectedValue.toString());
205: }
206: }
207:
208: /** Fired when a button is selected */
209: public void actionPerformed(ActionEvent e) {
210: if (IGNORE_CMD.equals(e.getActionCommand())) {
211: spellEvent.ignoreWord(false);
212: } else if (IGNOREALL_CMD.equals(e.getActionCommand())) {
213: spellEvent.ignoreWord(true);
214: } else if (REPLACE_CMD.equals(e.getActionCommand())) {
215: spellEvent.replaceWord(checkText.getText(), false);
216: } else if (REPLACEALL_CMD.equals(e.getActionCommand())) {
217: spellEvent.replaceWord(checkText.getText(), true);
218: } else if (ADD_CMD.equals(e.getActionCommand())) {
219: String inField = checkText.getText();
220: Object selObj = suggestList.getSelectedValue();
221: String selected = (selObj == null ? "" : selObj.toString());
222: String addString = (inField.equals(selected) ? spellEvent
223: .getInvalidWord() : inField);
224:
225: int n = JOptionPane.showConfirmDialog(this , Translation
226: .tr("jazzy." + ADDWORD_1)
227: + " '"
228: + addString
229: + "' "
230: + Translation.tr("jazzy." + ADDWORD_2), Translation
231: .tr("jazzy." + ADDWORD_3),
232: JOptionPane.YES_NO_OPTION);
233:
234: if (n == JOptionPane.YES_OPTION) {
235: spellEvent.addToDictionary(addString);
236: } else {
237: return;
238: }
239: } else if (CANCEL_CMD.equals(e.getActionCommand())) {
240: spellEvent.cancel();
241: }
242: fireActionEvent(e);
243: }
244:
245: public static void main(String[] args) {
246: try {
247: JSpellForm pane = new JSpellForm();
248: JFrame frm = new JFrame(Translation.tr("jazzy.title"));
249: frm.getContentPane().add(pane);
250: frm.setSize(300, 300);
251: frm.setVisible(true);
252: frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
253: } catch (Exception ex) {
254: ex.printStackTrace();
255: }
256: }
257: }
|