/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/EnhancedDialog.java

# · Java · 147 lines · 101 code · 17 blank · 29 comment · 15 complexity · 5597a1ef2ae040f435f8e0eeb6fc6353 MD5 · raw file

  1. /*
  2. * EnhancedDialog.java - Handles OK/Cancel for you
  3. * Copyright (C) 1998, 1999, 2001 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package org.gjt.sp.jedit.gui;
  20. import javax.swing.*;
  21. import java.awt.event.*;
  22. import java.awt.*;
  23. /**
  24. * A dialog box that handles window closing, the ENTER key and the ESCAPE
  25. * key for you. All you have to do is implement ok() (called when
  26. * Enter is pressed) and cancel() (called when Escape is pressed, or window
  27. * is closed).
  28. * @author Slava Pestov
  29. * @version $Id: EnhancedDialog.java 3791 2001-09-02 05:36:59Z spestov $
  30. */
  31. public abstract class EnhancedDialog extends JDialog
  32. {
  33. public EnhancedDialog(Frame parent, String title, boolean modal)
  34. {
  35. super(parent,title,modal);
  36. ((Container)getLayeredPane()).addContainerListener(
  37. new ContainerHandler());
  38. getContentPane().addContainerListener(new ContainerHandler());
  39. keyHandler = new KeyHandler();
  40. addKeyListener(keyHandler);
  41. addWindowListener(new WindowHandler());
  42. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  43. }
  44. public abstract void ok();
  45. public abstract void cancel();
  46. // protected members
  47. protected KeyHandler keyHandler;
  48. // Recursively adds our key listener to sub-components
  49. class ContainerHandler extends ContainerAdapter
  50. {
  51. public void componentAdded(ContainerEvent evt)
  52. {
  53. componentAdded(evt.getChild());
  54. }
  55. public void componentRemoved(ContainerEvent evt)
  56. {
  57. componentRemoved(evt.getChild());
  58. }
  59. private void componentAdded(Component comp)
  60. {
  61. comp.addKeyListener(keyHandler);
  62. if(comp instanceof Container)
  63. {
  64. Container cont = (Container)comp;
  65. cont.addContainerListener(this);
  66. Component[] comps = cont.getComponents();
  67. for(int i = 0; i < comps.length; i++)
  68. {
  69. componentAdded(comps[i]);
  70. }
  71. }
  72. }
  73. private void componentRemoved(Component comp)
  74. {
  75. comp.removeKeyListener(keyHandler);
  76. if(comp instanceof Container)
  77. {
  78. Container cont = (Container)comp;
  79. cont.removeContainerListener(this);
  80. Component[] comps = cont.getComponents();
  81. for(int i = 0; i < comps.length; i++)
  82. {
  83. componentRemoved(comps[i]);
  84. }
  85. }
  86. }
  87. }
  88. class KeyHandler extends KeyAdapter
  89. {
  90. public void keyPressed(KeyEvent evt)
  91. {
  92. if(evt.isConsumed())
  93. return;
  94. if(evt.getKeyCode() == KeyEvent.VK_ENTER)
  95. {
  96. // crusty workaround
  97. Component comp = getFocusOwner();
  98. while(comp != null)
  99. {
  100. if(comp instanceof JComboBox)
  101. {
  102. JComboBox combo = (JComboBox)comp;
  103. if(combo.isEditable())
  104. {
  105. Object selected = combo.getEditor().getItem();
  106. if(selected != null)
  107. combo.setSelectedItem(selected);
  108. }
  109. break;
  110. }
  111. comp = comp.getParent();
  112. }
  113. ok();
  114. evt.consume();
  115. }
  116. else if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
  117. {
  118. cancel();
  119. evt.consume();
  120. }
  121. }
  122. }
  123. class WindowHandler extends WindowAdapter
  124. {
  125. public void windowClosing(WindowEvent evt)
  126. {
  127. cancel();
  128. }
  129. }
  130. }