KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > EnhancedDialog


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
20 package org.gjt.sp.jedit.gui;
21
22 import javax.swing.*;
23 import java.awt.event.*;
24 import java.awt.*;
25
26 /**
27  * A dialog box that handles window closing, the ENTER key and the ESCAPE
28  * key for you. All you have to do is implement ok() (called when
29  * Enter is pressed) and cancel() (called when Escape is pressed, or window
30  * is closed).
31  * @author Slava Pestov
32  * @version $Id: EnhancedDialog.java 5339 2006-01-25 23:12:07Z spestov $
33  */

34 public abstract class EnhancedDialog extends JDialog
35 {
36     public EnhancedDialog(Frame parent, String JavaDoc title, boolean modal)
37     {
38         super(parent,title,modal);
39         _init();
40     }
41     
42     public EnhancedDialog(Dialog parent, String JavaDoc title, boolean modal)
43     {
44         super(parent,title,modal);
45         _init();
46     }
47
48     public boolean getEnterEnabled()
49     {
50         return enterEnabled;
51     }
52
53     public void setEnterEnabled(boolean enterEnabled)
54     {
55         this.enterEnabled = enterEnabled;
56     }
57     
58     public abstract void ok();
59     public abstract void cancel();
60
61     //{{{ Private members
62
private void _init()
63     {
64         ((Container)getLayeredPane()).addContainerListener(
65             new ContainerHandler());
66         getContentPane().addContainerListener(new ContainerHandler());
67
68         keyHandler = new KeyHandler();
69         addKeyListener(keyHandler);
70         addWindowListener(new WindowHandler());
71
72         setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
73         
74         enterEnabled = true;
75     }
76     //}}}
77

78     // protected members
79
protected KeyHandler keyHandler;
80     protected boolean enterEnabled;
81
82     // Recursively adds our key listener to sub-components
83
class ContainerHandler extends ContainerAdapter
84     {
85         public void componentAdded(ContainerEvent evt)
86         {
87             componentAdded(evt.getChild());
88         }
89
90         public void componentRemoved(ContainerEvent evt)
91         {
92             componentRemoved(evt.getChild());
93         }
94
95         private void componentAdded(Component comp)
96         {
97             comp.addKeyListener(keyHandler);
98             if(comp instanceof Container)
99             {
100                 Container cont = (Container)comp;
101                 cont.addContainerListener(this);
102                 Component[] comps = cont.getComponents();
103                 for(int i = 0; i < comps.length; i++)
104                 {
105                     componentAdded(comps[i]);
106                 }
107             }
108         }
109
110         private void componentRemoved(Component comp)
111         {
112             comp.removeKeyListener(keyHandler);
113             if(comp instanceof Container)
114             {
115                 Container cont = (Container)comp;
116                 cont.removeContainerListener(this);
117                 Component[] comps = cont.getComponents();
118                 for(int i = 0; i < comps.length; i++)
119                 {
120                     componentRemoved(comps[i]);
121                 }
122             }
123         }
124     }
125
126     class KeyHandler extends KeyAdapter
127     {
128         public void keyPressed(KeyEvent evt)
129         {
130             if(evt.isConsumed())
131                 return;
132
133             if(evt.getKeyCode() == KeyEvent.VK_ENTER
134                 && enterEnabled)
135             {
136                 Component comp = getFocusOwner();
137                 while(comp != null)
138                 {
139                     if(comp instanceof JComboBox)
140                     {
141                         JComboBox combo = (JComboBox)comp;
142                         if(combo.isEditable())
143                         {
144                             Object JavaDoc selected = combo.getEditor().getItem();
145                             if(selected != null)
146                                 combo.setSelectedItem(selected);
147                         }
148                         break;
149                     }
150
151                     comp = comp.getParent();
152                 }
153
154                 ok();
155                 evt.consume();
156             }
157             else if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
158             {
159                 cancel();
160                 evt.consume();
161             }
162         }
163     }
164
165     class WindowHandler extends WindowAdapter
166     {
167         public void windowClosing(WindowEvent evt)
168         {
169             cancel();
170         }
171     }
172 }
173
Popular Tags