1 30 31 package org.columba.mail.spellcheck.cswilly; 32 33 import java.awt.Component ; 34 import java.awt.Dialog ; 35 import java.awt.Dimension ; 36 import java.awt.Frame ; 37 import java.awt.Point ; 38 import java.awt.event.ActionEvent ; 39 import java.awt.event.ActionListener ; 40 import java.awt.event.KeyEvent ; 41 import java.util.List ; 42 43 import javax.swing.AbstractAction ; 44 import javax.swing.Box ; 45 import javax.swing.JButton ; 46 import javax.swing.JComponent ; 47 import javax.swing.JDialog ; 48 import javax.swing.JLabel ; 49 import javax.swing.JList ; 50 import javax.swing.JOptionPane ; 51 import javax.swing.JPanel ; 52 import javax.swing.JRootPane ; 53 import javax.swing.JScrollPane ; 54 import javax.swing.JTextField ; 55 import javax.swing.KeyStroke ; 56 import javax.swing.ListSelectionModel ; 57 import javax.swing.event.ListSelectionEvent ; 58 import javax.swing.event.ListSelectionListener ; 59 60 86 87 public class ValidationDialog extends JDialog { 88 89 91 private static Point _location = new Point (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 _changeToTextField; 106 107 private final String _originalWord; 108 109 private final List _suggestions; 110 111 private JList _suggestionsJList; 112 113 private UserAction _userAction = CANCEL; 114 115 private String _title = "Spell Check, Release R004"; 116 117 public ValidationDialog(Frame owner, String originalWord, List suggestions) { 118 119 super(owner); 120 121 _originalWord = originalWord; 122 123 _suggestions = suggestions; 124 125 _init(); 126 127 } 128 129 public ValidationDialog(Dialog owner, String originalWord, List suggestions) { 130 131 super(owner); 132 133 _originalWord = originalWord; 134 135 _suggestions = suggestions; 136 137 _init(); 138 139 } 140 141 public ValidationDialog(String originalWord, List suggestions) { 142 143 super(); 144 145 _originalWord = originalWord; 146 147 _suggestions = suggestions; 148 149 _init(); 150 151 } 152 153 public void dispose() { 154 155 157 _location = getLocation(); 158 159 super.dispose(); 160 161 } 162 163 public UserAction getUserAction() { 164 165 return _userAction; 166 167 } 168 169 178 179 public String getSelectedWord() { 180 181 return _changeToTextField.getText(); 182 183 } 184 185 189 190 protected JRootPane createRootPane() { 191 192 KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); 193 194 JRootPane rootPane = new JRootPane (); 195 196 ActionListener actionListener = new CloseDialogActionListener(); 197 198 rootPane.registerKeyboardAction(actionListener, stroke, 199 200 JComponent.WHEN_IN_FOCUSED_WINDOW); 201 202 return rootPane; 203 204 } 205 206 209 210 private void _addRow(Box mainBox, JComponent labelComponent, 211 212 Component component) { 213 214 Box hBox = Box.createHorizontalBox(); 215 216 mainBox.add(hBox); 217 218 Dimension labelComponentDim = new Dimension (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 248 249 private static JButton _configButton(AbstractAction action) { 250 251 JButton retButton = new JButton (action); 252 253 Object value; 254 255 value = action.getValue(AbstractAction.MNEMONIC_KEY); 256 257 if (value != null) { 258 259 int MnemonicKey = ((Integer ) 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 toolTip = (String ) value; 270 271 retButton.setToolTipText(toolTip); 272 273 } 274 275 retButton.setMinimumSize(new Dimension (85, 276 retButton.getPreferredSize().height)); 277 278 retButton.setMaximumSize(new Dimension (85, 279 retButton.getPreferredSize().height)); 280 281 retButton.setPreferredSize(new Dimension (85, retButton 282 .getPreferredSize().height)); 283 284 return retButton; 285 286 } 287 288 291 292 private void _init() { 293 294 setModal(true); 295 296 setTitle(_title); 297 298 300 302 304 JButton aboutButton = _configButton(new AboutAction()); 305 306 JButton addButton = _configButton(new AddAction()); 307 308 addButton.setEnabled(false); 309 310 JButton cancelButton = _configButton(new CancelAction()); 311 312 JButton changeButton = _configButton(new ChangeAction()); 313 314 JButton changeAllButton = _configButton(new ChangeAllAction()); 315 316 JButton ignoreButton = _configButton(new IgnoreAction()); 317 318 JButton ignoreAllButton = _configButton(new IgnoreAllAction()); 319 320 322 324 326 _changeToTextField = new JTextField (); 327 328 _changeToTextField.setMinimumSize(new Dimension (200, 329 330 _changeToTextField.getPreferredSize().height)); 331 332 _changeToTextField.setMaximumSize(new Dimension (Integer.MAX_VALUE, 333 334 _changeToTextField.getPreferredSize().height)); 335 336 JTextField originalWordTextField = new JTextField (_originalWord); 337 338 originalWordTextField.setMinimumSize(new Dimension (200, 339 340 originalWordTextField.getPreferredSize().height)); 341 342 originalWordTextField.setMaximumSize(new Dimension (Integer.MAX_VALUE, 343 344 originalWordTextField.getPreferredSize().height)); 345 346 348 350 352 _suggestionsJList = new JList (_suggestions.toArray()); 353 354 _suggestionsJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 355 356 _suggestionsJList 357 .addListSelectionListener(new MyListSelectionListener()); 358 359 _suggestionsJList.setMinimumSize(new Dimension (200, 300)); 360 361 _suggestionsJList.setMaximumSize(new Dimension (Integer.MAX_VALUE, 362 363 Integer.MAX_VALUE)); 364 365 _suggestionsJList.setPreferredSize(new Dimension (200, 300)); 366 367 JScrollPane suggestionsJScrollPane = new JScrollPane (_suggestionsJList); 368 369 371 374 376 378 380 Box mainBox = Box.createVerticalBox(); 381 382 getContentPane().add(mainBox); 383 384 Box hBox; 385 386 JLabel jLabel; 387 388 jLabel = new JLabel (" Not in Dictionary:"); 389 390 _addRow(mainBox, jLabel, originalWordTextField); 391 392 jLabel = new JLabel (" Change to:"); 393 394 _addRow(mainBox, jLabel, _changeToTextField); 395 396 jLabel = new JLabel (" Suggestions:"); 397 398 hBox = Box.createHorizontalBox(); 399 400 402 405 hBox.add(suggestionsJScrollPane); 406 407 hBox.add(Box.createHorizontalGlue()); 408 409 getRootPane().setDefaultButton(ignoreButton); 410 411 JPanel buttonPanel = new JPanel (); 412 413 buttonPanel.setPreferredSize(new Dimension (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 449 } 450 451 454 455 public static class UserAction { 456 457 private final String _name; 458 459 private UserAction(String name) { 460 461 _name = name; 462 463 } 464 465 public String toString() { 466 467 return _name; 468 469 } 470 471 } 472 473 475 477 479 480 private class AboutAction extends AbstractAction { 481 482 private AboutAction() { 483 484 super("About..."); 485 486 } 487 488 public void actionPerformed(ActionEvent event) { 489 490 String 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 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 { 510 511 private AddAction() { 512 513 super("Add"); 514 515 putValue(MNEMONIC_KEY, new Integer (KeyEvent.VK_A)); 516 517 putValue(ACCELERATOR_KEY, new Integer (KeyEvent.VK_A)); 518 519 } 520 521 public void actionPerformed(ActionEvent event) { 522 523 _userAction = ADD; 524 525 dispose(); 526 527 } 528 529 } 530 531 532 private class CancelAction extends AbstractAction { 533 534 private CancelAction() { 535 536 super("Cancel"); 537 538 } 539 540 public void actionPerformed(ActionEvent event) { 541 542 _userAction = CANCEL; 543 544 dispose(); 545 546 } 547 548 } 549 550 551 private class ChangeAction extends AbstractAction { 552 553 private ChangeAction() { 554 555 super("Change"); 556 557 putValue(MNEMONIC_KEY, new Integer (KeyEvent.VK_C)); 558 559 putValue(ACCELERATOR_KEY, new Integer (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 event) { 569 570 _userAction = CHANGE; 571 572 dispose(); 573 574 } 575 576 } 577 578 579 private class ChangeAllAction extends AbstractAction { 580 581 private ChangeAllAction() { 582 583 super("Change All"); 584 585 putValue(MNEMONIC_KEY, new Integer (KeyEvent.VK_L)); 586 587 putValue(ACCELERATOR_KEY, new Integer (KeyEvent.VK_L)); 588 589 } 590 591 public void actionPerformed(ActionEvent event) { 592 593 _userAction = CHANGE_ALL; 594 595 dispose(); 596 597 } 598 599 } 600 601 private class IgnoreAction extends AbstractAction { 602 603 private IgnoreAction() { 604 605 super("Ignore"); 606 607 putValue(MNEMONIC_KEY, new Integer (KeyEvent.VK_I)); 608 609 putValue(ACCELERATOR_KEY, new Integer (KeyEvent.VK_I)); 610 611 } 612 613 public void actionPerformed(ActionEvent event) { 614 615 _userAction = IGNORE; 616 617 dispose(); 618 619 } 620 621 } 622 623 private class IgnoreAllAction extends AbstractAction { 624 625 private IgnoreAllAction() { 626 627 super("Ignore All"); 628 629 631 633 putValue(MNEMONIC_KEY, new Integer (KeyEvent.VK_G)); 634 635 putValue(ACCELERATOR_KEY, new Integer (KeyEvent.VK_G)); 636 637 } 638 639 public void actionPerformed(ActionEvent event) { 640 641 _userAction = IGNORE_ALL; 642 643 dispose(); 644 645 } 646 647 } 648 649 private class CloseDialogActionListener implements ActionListener { 650 651 public void actionPerformed(ActionEvent actionEvent) { 652 653 _userAction = CANCEL; 654 655 dispose(); 656 657 } 658 659 } 660 661 private class MyListSelectionListener implements ListSelectionListener { 662 663 public void valueChanged(ListSelectionEvent e) { 664 665 int selectedIndex = _suggestionsJList.getSelectedIndex(); 666 667 if (selectedIndex >= 0) { 668 669 _changeToTextField.setText((String ) _suggestions.get( 670 671 selectedIndex)); 672 673 } 674 675 } 676 677 } 678 679 } 680 | Popular Tags |