001: /*
002: * put your module comment here
003: * formatted with JxBeauty (c) johann.langhofer@nextra.at
004: */
005:
006: package com.xoetrope.carousel.langed;
007:
008: import java.util.Locale;
009: import java.util.ResourceBundle;
010:
011: import java.awt.BorderLayout;
012: import java.awt.Dimension;
013: import java.awt.event.ActionEvent;
014: import java.awt.event.ActionListener;
015: import javax.swing.BoxLayout;
016: import javax.swing.DefaultListModel;
017: import javax.swing.JButton;
018: import javax.swing.JLabel;
019: import javax.swing.JList;
020: import javax.swing.JPanel;
021: import javax.swing.JScrollPane;
022: import javax.swing.JTextArea;
023: import javax.swing.ListSelectionModel;
024: import javax.swing.event.EventListenerList;
025: import javax.swing.event.ListSelectionEvent;
026: import javax.swing.event.ListSelectionListener;
027:
028: import com.swabunga.spell.engine.Word;
029: import com.swabunga.spell.event.SpellCheckEvent;
030:
031: /** Implementation of a spell check form.
032: * <p>This needs to layed out correctly but for the most part it works.</p>
033: *
034: * @author Jason Height (jheight@chariot.net.au)
035: */
036: public class JSpellForm extends JPanel implements ActionListener,
037: ListSelectionListener {
038: /** The Ignore button click action command*/
039: public static final String IGNORE_CMD = "IGNORE";
040: /** The Ignore All button click action command*/
041: public static final String IGNOREALL_CMD = "IGNOREALL";
042: /** The Add button click action command*/
043: public static final String ADD_CMD = "ADD";
044: /** The Replace button click action command*/
045: public static final String REPLACE_CMD = "REPLACE";
046: /** The Replace All button click action command*/
047: public static final String REPLACEALL_CMD = "REPLACEALL";
048: /** The Cancel button click action command*/
049: public static final String CANCEL_CMD = "CANCEL";
050: /** The resource for the Suggestions label*/
051: private static final String SUGGESTIONS_RES = "SUGGESTIONS";
052: private static final String INVALIDWORD_RES = "INVALIDWORD";
053:
054: /* Accessible GUI Components */
055: protected JList suggestList;
056: protected JTextArea checkText;
057: /* The current spell check event */
058: protected SpellCheckEvent spellEvent;
059: /** The listener list (holds actionlisteners) */
060: protected EventListenerList listenerList = new EventListenerList();
061: protected ResourceBundle messages;
062:
063: /** Panel constructor */
064: public JSpellForm() {
065: messages = ResourceBundle.getBundle(
066: "com.swabunga.spell.swing.messages", Locale
067: .getDefault());
068: initialiseGUI();
069: }
070:
071: /** Helper method to create a JButton with a command, a text label and a listener*/
072: private static final JButton createButton(String command,
073: String text, ActionListener listener) {
074: JButton btn = new JButton(text);
075: btn.setActionCommand(command);
076: btn.addActionListener(listener);
077: return btn;
078: }
079:
080: /** Creates the buttons on the left hand side of the panel*/
081: protected JPanel makeEastPanel() {
082: JPanel jPanel1 = new JPanel();
083: BoxLayout layout = new BoxLayout(jPanel1, BoxLayout.Y_AXIS);
084: jPanel1.setLayout(layout);
085:
086: JButton ignoreBtn = createButton(IGNORE_CMD, messages
087: .getString(IGNORE_CMD), this );
088: ignoreBtn.setMaximumSize(new Dimension(Short.MAX_VALUE,
089: Short.MAX_VALUE));
090: jPanel1.add(ignoreBtn);
091:
092: JButton ignoreAllBtn = createButton(IGNOREALL_CMD, messages
093: .getString(IGNOREALL_CMD), this );
094: ignoreAllBtn.setMaximumSize(new Dimension(Short.MAX_VALUE,
095: Short.MAX_VALUE));
096: jPanel1.add(ignoreAllBtn);
097:
098: JButton addBtn = createButton(ADD_CMD, messages
099: .getString(ADD_CMD), this );
100: addBtn.setMaximumSize(new Dimension(Short.MAX_VALUE,
101: Short.MAX_VALUE));
102: jPanel1.add(addBtn);
103:
104: JButton changeBtn = createButton(REPLACE_CMD, messages
105: .getString(REPLACE_CMD), this );
106: changeBtn.setMaximumSize(new Dimension(Short.MAX_VALUE,
107: Short.MAX_VALUE));
108: jPanel1.add(changeBtn);
109:
110: JButton changeAllBtn = createButton(REPLACEALL_CMD, messages
111: .getString(REPLACEALL_CMD), this );
112: changeAllBtn.setMaximumSize(new Dimension(Short.MAX_VALUE,
113: Short.MAX_VALUE));
114: jPanel1.add(changeAllBtn);
115:
116: JButton cancelBtn = createButton(CANCEL_CMD, messages
117: .getString(CANCEL_CMD), this );
118: cancelBtn.setMaximumSize(new Dimension(Short.MAX_VALUE,
119: Short.MAX_VALUE));
120: jPanel1.add(cancelBtn);
121:
122: return jPanel1;
123: }
124:
125: protected JPanel makeCentrePanel() {
126: JPanel jPanel2 = new JPanel();
127: jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.Y_AXIS));
128: JLabel lbl1 = new JLabel(messages.getString(INVALIDWORD_RES));
129: jPanel2.add(lbl1);
130: checkText = new JTextArea();
131: jPanel2.add(new JScrollPane(checkText));
132: JLabel lbl2 = new JLabel(messages.getString(SUGGESTIONS_RES));
133: jPanel2.add(lbl2);
134: suggestList = new JList();
135: suggestList
136: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
137: jPanel2.add(new JScrollPane(suggestList));
138: suggestList.addListSelectionListener(this );
139: return jPanel2;
140: }
141:
142: /** Called by the constructor to initialise the GUI*/
143: protected void initialiseGUI() {
144: setLayout(new BorderLayout());
145: this .add(makeEastPanel(), BorderLayout.EAST);
146: this .add(makeCentrePanel(), BorderLayout.CENTER);
147: }
148:
149: /** Register an action listener */
150: public void addActionListener(ActionListener l) {
151: listenerList.add(ActionListener.class, l);
152: }
153:
154: /** Deregister an action listener*/
155: public void removeActionListener(ActionListener l) {
156: listenerList.remove(ActionListener.class, l);
157: }
158:
159: protected void fireActionEvent(ActionEvent e) {
160: // Guaranteed to return a non-null array
161: Object[] listeners = listenerList.getListenerList();
162: // Process the listeners last to first, notifying
163: // those that are interested in this event
164: for (int i = listeners.length - 2; i >= 0; i -= 2) {
165: if (listeners[i] == ActionListener.class) {
166: ((ActionListener) listeners[i + 1]).actionPerformed(e);
167: }
168: }
169: }
170:
171: /** Sets the current spell check event that is being shown to the user*/
172: public void setSpellEvent(SpellCheckEvent event) {
173: spellEvent = event;
174: DefaultListModel m = new DefaultListModel();
175: java.util.List suggestions = event.getSuggestions();
176: for (int i = 0; i < suggestions.size(); i++) {
177: m.addElement(suggestions.get(i));
178: }
179: suggestList.setModel(m);
180: if (m.size() > 0) {
181: suggestList.setSelectedIndex(0);
182: checkText.setText(((Word) m.get(0)).getWord());
183: } else {
184: checkText.setText(event.getInvalidWord());
185: }
186: }
187:
188: /** Fired when a value in the list is selected*/
189: public void valueChanged(ListSelectionEvent e) {
190: if (!e.getValueIsAdjusting()) {
191: Object selectedValue = suggestList.getSelectedValue();
192: if (selectedValue != null)
193: checkText.setText(selectedValue.toString());
194: }
195: }
196:
197: /** Fired when a button is selected */
198: public void actionPerformed(ActionEvent e) {
199: if (IGNORE_CMD.equals(e.getActionCommand())) {
200: spellEvent.ignoreWord(false);
201: } else if (IGNOREALL_CMD.equals(e.getActionCommand())) {
202: spellEvent.ignoreWord(true);
203: } else if (REPLACE_CMD.equals(e.getActionCommand())) {
204: spellEvent.replaceWord(checkText.getText(), false);
205: } else if (REPLACEALL_CMD.equals(e.getActionCommand())) {
206: spellEvent.replaceWord(checkText.getText(), true);
207: } else if (ADD_CMD.equals(e.getActionCommand())) {
208: spellEvent.addToDictionary(checkText.getText());
209: } else if (CANCEL_CMD.equals(e.getActionCommand())) {
210: spellEvent.cancel();
211: }
212: fireActionEvent(e);
213: }
214: }
|