PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/gui/EnhancedDialog.java

#
Java | 160 lines | 109 code | 20 blank | 31 comment | 15 complexity | 2705fb76ce15f0ec1df090017d0fe65e MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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 4352 2002-10-07 21:13:20Z 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. _init();
  37. }
  38. public EnhancedDialog(Dialog parent, String title, boolean modal)
  39. {
  40. super(parent,title,modal);
  41. _init();
  42. }
  43. public abstract void ok();
  44. public abstract void cancel();
  45. //{{{ Private members
  46. private void _init() {
  47. ((Container)getLayeredPane()).addContainerListener(
  48. new ContainerHandler());
  49. getContentPane().addContainerListener(new ContainerHandler());
  50. keyHandler = new KeyHandler();
  51. addKeyListener(keyHandler);
  52. addWindowListener(new WindowHandler());
  53. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  54. }
  55. //}}}
  56. // protected members
  57. protected KeyHandler keyHandler;
  58. // Recursively adds our key listener to sub-components
  59. class ContainerHandler extends ContainerAdapter
  60. {
  61. public void componentAdded(ContainerEvent evt)
  62. {
  63. componentAdded(evt.getChild());
  64. }
  65. public void componentRemoved(ContainerEvent evt)
  66. {
  67. componentRemoved(evt.getChild());
  68. }
  69. private void componentAdded(Component comp)
  70. {
  71. comp.addKeyListener(keyHandler);
  72. if(comp instanceof Container)
  73. {
  74. Container cont = (Container)comp;
  75. cont.addContainerListener(this);
  76. Component[] comps = cont.getComponents();
  77. for(int i = 0; i < comps.length; i++)
  78. {
  79. componentAdded(comps[i]);
  80. }
  81. }
  82. }
  83. private void componentRemoved(Component comp)
  84. {
  85. comp.removeKeyListener(keyHandler);
  86. if(comp instanceof Container)
  87. {
  88. Container cont = (Container)comp;
  89. cont.removeContainerListener(this);
  90. Component[] comps = cont.getComponents();
  91. for(int i = 0; i < comps.length; i++)
  92. {
  93. componentRemoved(comps[i]);
  94. }
  95. }
  96. }
  97. }
  98. class KeyHandler extends KeyAdapter
  99. {
  100. public void keyPressed(KeyEvent evt)
  101. {
  102. if(evt.isConsumed())
  103. return;
  104. if(evt.getKeyCode() == KeyEvent.VK_ENTER)
  105. {
  106. // crusty workaround
  107. Component comp = getFocusOwner();
  108. while(comp != null)
  109. {
  110. if(comp instanceof JComboBox)
  111. {
  112. JComboBox combo = (JComboBox)comp;
  113. if(combo.isEditable())
  114. {
  115. Object selected = combo.getEditor().getItem();
  116. if(selected != null)
  117. combo.setSelectedItem(selected);
  118. }
  119. break;
  120. }
  121. comp = comp.getParent();
  122. }
  123. ok();
  124. evt.consume();
  125. }
  126. else if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
  127. {
  128. cancel();
  129. evt.consume();
  130. }
  131. }
  132. }
  133. class WindowHandler extends WindowAdapter
  134. {
  135. public void windowClosing(WindowEvent evt)
  136. {
  137. cancel();
  138. }
  139. }
  140. }