2 * put your module comment here
3 * formatted with JxBeauty (c) johann.langhofer@nextra.at
6 package com
.swabunga
.spell
.swing
;
8 import com
.swabunga
.spell
.engine
.Word
;
9 import com
.swabunga
.spell
.event
.*;
11 import javax
.swing
.text
.*;
12 import javax
.swing
.event
.*;
15 import java
.awt
.event
.*;
18 /** Implementation of a spell check form.
19 * <p>This needs to layed out correctly but for the most part it works.</p>
21 * @author Jason Height (jheight@chariot.net.au)
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.
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";
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
;
58 /** Panel constructor */
59 public JSpellForm () {
60 messages
= ResourceBundle
.getBundle("com.swabunga.spell.swing.messages", Locale
.getDefault());
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
);
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
);
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
);
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
);
86 JButton addBtn
= createButton(ADD_CMD
, messages
.getString(ADD_CMD
), this);
87 addBtn
.setMaximumSize( new Dimension(Short
.MAX_VALUE
, Short
.MAX_VALUE
));
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
);
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
);
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
);
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
));
110 checkText
= new JTextArea();
111 jPanel2
.add(new JScrollPane(checkText
));
112 JLabel lbl2
= new JLabel(messages
.getString(SUGGESTIONS_RES
));
114 suggestList
= new JList();
115 suggestList
.setSelectionMode(ListSelectionModel
.SINGLE_SELECTION
);
116 jPanel2
.add(new JScrollPane(suggestList
));
117 suggestList
.addListSelectionListener(this);
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
);
128 /** Register an action listener */
129 public void addActionListener (ActionListener l
) {
130 listenerList
.add(ActionListener
.class, l
);
133 /** Deregister an action listener*/
134 public void removeActionListener (ActionListener l
) {
135 listenerList
.remove(ActionListener
.class, l
);
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
);
150 /** Sets the current spell check event that is being shown to the user*/
151 public void setSpellEvent (SpellCheckEvent 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
));
158 suggestList
.setModel(m
);
160 suggestList
.setSelectedIndex(0);
162 checkText
.setText(event
.getInvalidWord());
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();
172 /** Fired when a button is selected */
173 public void actionPerformed (ActionEvent e
) {
174 if (IGNORE_CMD
.equals(e
.getActionCommand())) {
175 spellEvent
.ignoreWord(false
);
177 else if (IGNOREALL_CMD
.equals(e
.getActionCommand())) {
178 spellEvent
.ignoreWord(true
);
180 else if (REPLACE_CMD
.equals(e
.getActionCommand())) {
181 // Howard Kistler START
182 String replacementWord
;
183 if(suggestList
.getSelectedValue() != null
)
185 replacementWord
= suggestList
.getSelectedValue().toString();
189 replacementWord
= checkText
.getText();
191 spellEvent
.replaceWord(replacementWord
, false
);
192 // Howard Kistler END
194 else if (REPLACEALL_CMD
.equals(e
.getActionCommand())) {
195 // Howard Kistler START
196 String replacementWord
;
197 if(suggestList
.getSelectedValue() != null
)
199 replacementWord
= suggestList
.getSelectedValue().toString();
203 replacementWord
= checkText
.getText();
205 spellEvent
.replaceWord(replacementWord
, true
);
206 // Howard Kistler END
208 else if (ADD_CMD
.equals(e
.getActionCommand())) {
209 spellEvent
.addToDictionary(checkText
.getText());
211 else if (CANCEL_CMD
.equals(e
.getActionCommand())) {
217 public static void main (String
[] args
) {
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();