KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > spellcheck > cswilly > ValidationDialog


1 /*
2  * $Revision: 1.1 $
3  * $Date: 2006/06/10 13:32:32 $
4  * $Author: fdietz $
5  *
6  * Copyright (C) 2001 C. Scott Willy
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  *
22  * Modified 2004/09/24
23  * By: Kelvin Eldridge
24  * Changes: Changed Title to 004.
25  * Changed buttons to all be the same size 85xpreferred component height.
26  * Add a space in front of labels to shift text from window border.
27  *
28  *
29  */

30
31 package org.columba.mail.spellcheck.cswilly;
32
33 import java.awt.Component JavaDoc;
34 import java.awt.Dialog JavaDoc;
35 import java.awt.Dimension JavaDoc;
36 import java.awt.Frame JavaDoc;
37 import java.awt.Point JavaDoc;
38 import java.awt.event.ActionEvent JavaDoc;
39 import java.awt.event.ActionListener JavaDoc;
40 import java.awt.event.KeyEvent JavaDoc;
41 import java.util.List JavaDoc;
42
43 import javax.swing.AbstractAction JavaDoc;
44 import javax.swing.Box JavaDoc;
45 import javax.swing.JButton JavaDoc;
46 import javax.swing.JComponent JavaDoc;
47 import javax.swing.JDialog JavaDoc;
48 import javax.swing.JLabel JavaDoc;
49 import javax.swing.JList JavaDoc;
50 import javax.swing.JOptionPane JavaDoc;
51 import javax.swing.JPanel JavaDoc;
52 import javax.swing.JRootPane JavaDoc;
53 import javax.swing.JScrollPane JavaDoc;
54 import javax.swing.JTextField JavaDoc;
55 import javax.swing.KeyStroke JavaDoc;
56 import javax.swing.ListSelectionModel JavaDoc;
57 import javax.swing.event.ListSelectionEvent JavaDoc;
58 import javax.swing.event.ListSelectionListener JavaDoc;
59
60 /**
61  * Dialog to the user to determine what to do about a misspelled word.
62  * <p>
63  * <p>
64  * Features of the dialog box:
65  * </p>
66  * <ul>
67  * <li>the dialog box is modal</li>
68  * <li>the title of the dialog box is set to "Spell Check"</li>
69  * <li>the user action change be determined using the getUserAction() method</li>
70  * <li>the possible user actions are modelled using the instances of the class
71  * UserAction (which is an enum-like thingy). The possible instances are give by
72  * public static text fields (e.g. ADD, and CANCEL) of type UserAction.</li>
73  * <li>the dialog box can be closed using the escape key (user action is
74  * CANCEL)</li>
75  * <li>there is a default button (<cite>Ignore</cite>)</li>
76  * <li>the buttons all have mnemonic keys</li>
77  * <li>??? todo set focus to <cite>Change to</cite> text field when dialog is
78  * opened</li>
79  * <li>??? todo add tool tips to all visual components</li>
80  * <li>??? todo move text to properties file</li>
81  * </ul>
82  * <p>
83  * Escape closes dialog
84  * (http://www.javaworld.com/javaworld/javatips/jw-javatip72.html)
85  */

86
87 public class ValidationDialog extends JDialog JavaDoc {
88
89     // ??? bad to have release hardocoded here. Fix later...right.
90

91     private static Point JavaDoc _location = new Point JavaDoc(100, 100);
92
93     public static final UserAction ADD = new UserAction("Add");
94
95     public static final UserAction CANCEL = new UserAction("Cancel");
96
97     public static final UserAction CHANGE = new UserAction("Change");
98
99     public static final UserAction CHANGE_ALL = new UserAction("Change All");
100
101     public static final UserAction IGNORE = new UserAction("Ignore");
102
103     public static final UserAction IGNORE_ALL = new UserAction("Ignore All");
104
105     private JTextField JavaDoc _changeToTextField;
106
107     private final String JavaDoc _originalWord;
108
109     private final List JavaDoc _suggestions;
110
111     private JList JavaDoc _suggestionsJList;
112
113     private UserAction _userAction = CANCEL;
114
115     private String JavaDoc _title = "Spell Check, Release R004";
116
117     public ValidationDialog(Frame JavaDoc owner, String JavaDoc originalWord, List JavaDoc suggestions) {
118
119         super(owner);
120
121         _originalWord = originalWord;
122
123         _suggestions = suggestions;
124
125         _init();
126
127     }
128
129     public ValidationDialog(Dialog JavaDoc owner, String JavaDoc originalWord, List JavaDoc suggestions) {
130
131         super(owner);
132
133         _originalWord = originalWord;
134
135         _suggestions = suggestions;
136
137         _init();
138
139     }
140
141     public ValidationDialog(String JavaDoc originalWord, List JavaDoc suggestions) {
142
143         super();
144
145         _originalWord = originalWord;
146
147         _suggestions = suggestions;
148
149         _init();
150
151     }
152
153     public void dispose() {
154
155         // save current location statically for next time
156

157         _location = getLocation();
158
159         super.dispose();
160
161     }
162
163     public UserAction getUserAction() {
164
165         return _userAction;
166
167     }
168
169     /**
170      * Returns the replacement word selected by the user
171      * <p>
172      * The returned value only makes sense if the user action is either CHANGE
173      * or CHANGE_ALL. Should be ignored for any other action.
174      * <p>
175      *
176      * @return the replacement word selected by the user as a String
177      */

178
179     public String JavaDoc getSelectedWord() {
180
181         return _changeToTextField.getText();
182
183     }
184
185     /**
186      * Overriden to register {@link CloseDialogActionListener} to be called when
187      * the escape key is pressed.
188      */

189
190     protected JRootPane JavaDoc createRootPane() {
191
192         KeyStroke JavaDoc stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
193
194         JRootPane JavaDoc rootPane = new JRootPane JavaDoc();
195
196         ActionListener JavaDoc actionListener = new CloseDialogActionListener();
197
198         rootPane.registerKeyboardAction(actionListener, stroke,
199
200         JComponent.WHEN_IN_FOCUSED_WINDOW);
201
202         return rootPane;
203
204     }
205
206     /**
207      * Convenience method add lableled component
208      */

209
210     private void _addRow(Box JavaDoc mainBox, JComponent JavaDoc labelComponent,
211
212     Component JavaDoc component) {
213
214         Box JavaDoc hBox = Box.createHorizontalBox();
215
216         mainBox.add(hBox);
217
218         Dimension JavaDoc labelComponentDim = new Dimension JavaDoc(100,
219
220         labelComponent.getPreferredSize().height);
221
222         labelComponent.setPreferredSize(labelComponentDim);
223
224         labelComponent.setMinimumSize(labelComponentDim);
225
226         labelComponent.setMaximumSize(labelComponentDim);
227
228         hBox.add(labelComponent);
229
230         hBox.add(Box.createHorizontalGlue());
231
232         hBox.add(component);
233
234         hBox.add(Box.createHorizontalGlue());
235
236     }
237
238     /**
239      * Convenience method to create and config buttons from an action
240      * <p>
241      * After creating the JButton, sets the Mnemonic and ToolTipText by looking
242      * for the AbstractFilterAction.MNEMONIC_KEY and
243      * AbstractFilterAction.SHORT_DESCRIPTION keys in <code>action</code>.
244      * <p>
245      *
246      * @return a new, freshly configured JButton
247      */

248
249     private static JButton JavaDoc _configButton(AbstractAction JavaDoc action) {
250
251         JButton JavaDoc retButton = new JButton JavaDoc(action);
252
253         Object JavaDoc value;
254
255         value = action.getValue(AbstractAction.MNEMONIC_KEY);
256
257         if (value != null) {
258
259             int MnemonicKey = ((Integer JavaDoc) value).intValue();
260
261             retButton.setMnemonic(MnemonicKey);
262
263         }
264
265         value = action.getValue(AbstractAction.SHORT_DESCRIPTION);
266
267         if (value != null) {
268
269             String JavaDoc toolTip = (String JavaDoc) value;
270
271             retButton.setToolTipText(toolTip);
272
273         }
274
275         retButton.setMinimumSize(new Dimension JavaDoc(85,
276                 retButton.getPreferredSize().height));
277
278         retButton.setMaximumSize(new Dimension JavaDoc(85,
279                 retButton.getPreferredSize().height));
280
281         retButton.setPreferredSize(new Dimension JavaDoc(85, retButton
282                 .getPreferredSize().height));
283
284         return retButton;
285
286     }
287
288     /**
289      * Initializes the dialog box
290      */

291
292     private void _init() {
293
294         setModal(true);
295
296         setTitle(_title);
297
298         // --
299

300         // -- Buttons
301

302         // --
303

304         JButton JavaDoc aboutButton = _configButton(new AboutAction());
305
306         JButton JavaDoc addButton = _configButton(new AddAction());
307
308         addButton.setEnabled(false);
309
310         JButton JavaDoc cancelButton = _configButton(new CancelAction());
311
312         JButton JavaDoc changeButton = _configButton(new ChangeAction());
313
314         JButton JavaDoc changeAllButton = _configButton(new ChangeAllAction());
315
316         JButton JavaDoc ignoreButton = _configButton(new IgnoreAction());
317
318         JButton JavaDoc ignoreAllButton = _configButton(new IgnoreAllAction());
319
320         // --
321

322         // -- Text Fields
323

324         // --
325

326         _changeToTextField = new JTextField JavaDoc();
327
328         _changeToTextField.setMinimumSize(new Dimension JavaDoc(200,
329
330         _changeToTextField.getPreferredSize().height));
331
332         _changeToTextField.setMaximumSize(new Dimension JavaDoc(Integer.MAX_VALUE,
333
334         _changeToTextField.getPreferredSize().height));
335
336         JTextField JavaDoc originalWordTextField = new JTextField JavaDoc(_originalWord);
337
338         originalWordTextField.setMinimumSize(new Dimension JavaDoc(200,
339
340         originalWordTextField.getPreferredSize().height));
341
342         originalWordTextField.setMaximumSize(new Dimension JavaDoc(Integer.MAX_VALUE,
343
344         originalWordTextField.getPreferredSize().height));
345
346         // --
347

348         // -- Other components
349

350         // --
351

352         _suggestionsJList = new JList JavaDoc(_suggestions.toArray());
353
354         _suggestionsJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
355
356         _suggestionsJList
357                 .addListSelectionListener(new MyListSelectionListener());
358
359         _suggestionsJList.setMinimumSize(new Dimension JavaDoc(200, 300));
360
361         _suggestionsJList.setMaximumSize(new Dimension JavaDoc(Integer.MAX_VALUE,
362
363         Integer.MAX_VALUE));
364
365         _suggestionsJList.setPreferredSize(new Dimension JavaDoc(200, 300));
366
367         JScrollPane JavaDoc suggestionsJScrollPane = new JScrollPane JavaDoc(_suggestionsJList);
368
369         // suggestionsJScrollPane.setPreferredSize(
370

371         // new Dimension( suggestionsJScrollPane.getPreferredSize().width, 75 )
372
// );
373

374         // --
375

376         // -- Overall Dialog box
377

378         // --
379

380         Box JavaDoc mainBox = Box.createVerticalBox();
381
382         getContentPane().add(mainBox);
383
384         Box JavaDoc hBox;
385
386         JLabel JavaDoc jLabel;
387
388         jLabel = new JLabel JavaDoc(" Not in Dictionary:");
389
390         _addRow(mainBox, jLabel, originalWordTextField);
391
392         jLabel = new JLabel JavaDoc(" Change to:");
393
394         _addRow(mainBox, jLabel, _changeToTextField);
395
396         jLabel = new JLabel JavaDoc(" Suggestions:");
397
398         hBox = Box.createHorizontalBox();
399
400         // suggestionsJScrollPane.setMinimumSize( new Dimension( 200, 300 ) );
401

402         // suggestionsJScrollPane.setMaximumSize( new Dimension(
403
// Integer.MAX_VALUE, Integer.MAX_VALUE ) );
404

405         hBox.add(suggestionsJScrollPane);
406
407         hBox.add(Box.createHorizontalGlue());
408
409         getRootPane().setDefaultButton(ignoreButton);
410
411         JPanel JavaDoc buttonPanel = new JPanel JavaDoc();
412
413         buttonPanel.setPreferredSize(new Dimension JavaDoc(200, 100));
414
415         buttonPanel.add(ignoreButton);
416
417         buttonPanel.add(ignoreAllButton);
418
419         buttonPanel.add(changeButton);
420
421         buttonPanel.add(changeAllButton);
422
423         buttonPanel.add(addButton);
424
425         buttonPanel.add(cancelButton);
426
427         buttonPanel.add(aboutButton);
428
429         hBox.add(buttonPanel);
430
431         hBox.add(Box.createHorizontalGlue());
432
433         _addRow(mainBox, jLabel, hBox);
434
435         if (_location != null) {
436
437             setLocation(_location);
438
439         }
440
441         pack();
442
443         _suggestionsJList.setSelectedIndex(0);
444
445         _suggestionsJList.grabFocus();
446
447         // setSize( 750, getPreferredSize().height );
448

449     }
450
451     /**
452      * Models a enum of UserActions
453      */

454
455     public static class UserAction {
456
457         private final String JavaDoc _name;
458
459         private UserAction(String JavaDoc name) {
460
461             _name = name;
462
463         }
464
465         public String JavaDoc toString() {
466
467             return _name;
468
469         }
470
471     }
472
473     // --
474

475     // -- Availables Actions
476

477     // --
478

479     
480     private class AboutAction extends AbstractAction JavaDoc {
481
482         private AboutAction() {
483
484             super("About...");
485
486         }
487
488         public void actionPerformed(ActionEvent JavaDoc event) {
489
490             String JavaDoc msg = "Based on interfacing Java with Aspell.\n" +
491
492             "Hacked by C. Scott Willy to scratch an itch.\n" +
493
494             "Copyright 2001 Scott Willy\n" +
495
496             "http://www.geocities.com/cswilly/spellcheck/";
497
498             String JavaDoc title = "About " + _title;
499
500             JOptionPane.showMessageDialog(ValidationDialog.this, msg, title,
501
502             JOptionPane.INFORMATION_MESSAGE);
503
504         }
505
506     }
507
508     
509     private class AddAction extends AbstractAction JavaDoc {
510
511         private AddAction() {
512
513             super("Add");
514
515             putValue(MNEMONIC_KEY, new Integer JavaDoc(KeyEvent.VK_A));
516
517             putValue(ACCELERATOR_KEY, new Integer JavaDoc(KeyEvent.VK_A));
518
519         }
520
521         public void actionPerformed(ActionEvent JavaDoc event) {
522
523             _userAction = ADD;
524
525             dispose();
526
527         }
528
529     }
530
531     
532     private class CancelAction extends AbstractAction JavaDoc {
533
534         private CancelAction() {
535
536             super("Cancel");
537
538         }
539
540         public void actionPerformed(ActionEvent JavaDoc event) {
541
542             _userAction = CANCEL;
543
544             dispose();
545
546         }
547
548     }
549
550     
551     private class ChangeAction extends AbstractAction JavaDoc {
552
553         private ChangeAction() {
554
555             super("Change");
556
557             putValue(MNEMONIC_KEY, new Integer JavaDoc(KeyEvent.VK_C));
558
559             putValue(ACCELERATOR_KEY, new Integer JavaDoc(KeyEvent.VK_C));
560
561             putValue(
562                     SHORT_DESCRIPTION,
563
564                     "Replaces the word not in the Not in Dictionary text field with the word in the Change to text field.");
565
566         }
567
568         public void actionPerformed(ActionEvent JavaDoc event) {
569
570             _userAction = CHANGE;
571
572             dispose();
573
574         }
575
576     }
577
578     
579     private class ChangeAllAction extends AbstractAction JavaDoc {
580
581         private ChangeAllAction() {
582
583             super("Change All");
584
585             putValue(MNEMONIC_KEY, new Integer JavaDoc(KeyEvent.VK_L));
586
587             putValue(ACCELERATOR_KEY, new Integer JavaDoc(KeyEvent.VK_L));
588
589         }
590
591         public void actionPerformed(ActionEvent JavaDoc event) {
592
593             _userAction = CHANGE_ALL;
594
595             dispose();
596
597         }
598
599     }
600
601     private class IgnoreAction extends AbstractAction JavaDoc {
602
603         private IgnoreAction() {
604
605             super("Ignore");
606
607             putValue(MNEMONIC_KEY, new Integer JavaDoc(KeyEvent.VK_I));
608
609             putValue(ACCELERATOR_KEY, new Integer JavaDoc(KeyEvent.VK_I));
610
611         }
612
613         public void actionPerformed(ActionEvent JavaDoc event) {
614
615             _userAction = IGNORE;
616
617             dispose();
618
619         }
620
621     }
622
623     private class IgnoreAllAction extends AbstractAction JavaDoc {
624
625         private IgnoreAllAction() {
626
627             super("Ignore All");
628
629             // putValue( MNEMONIC_KEY, new Integer(KeyEvent.VK_G) );
630

631             // putValue( ACCELERATOR_KEY, new Integer(KeyEvent.VK_G) );
632

633             putValue(MNEMONIC_KEY, new Integer JavaDoc(KeyEvent.VK_G));
634
635             putValue(ACCELERATOR_KEY, new Integer JavaDoc(KeyEvent.VK_G));
636
637         }
638
639         public void actionPerformed(ActionEvent JavaDoc event) {
640
641             _userAction = IGNORE_ALL;
642
643             dispose();
644
645         }
646
647     }
648
649     private class CloseDialogActionListener implements ActionListener JavaDoc {
650
651         public void actionPerformed(ActionEvent JavaDoc actionEvent) {
652
653             _userAction = CANCEL;
654
655             dispose();
656
657         }
658
659     }
660
661     private class MyListSelectionListener implements ListSelectionListener JavaDoc {
662
663         public void valueChanged(ListSelectionEvent JavaDoc e) {
664
665             int selectedIndex = _suggestionsJList.getSelectedIndex();
666
667             if (selectedIndex >= 0) {
668
669                 _changeToTextField.setText((String JavaDoc) _suggestions.get(
670
671                 selectedIndex));
672
673             }
674
675         }
676
677     }
678
679 }
680
Popular Tags