PageRenderTime 111ms CodeModel.GetById 105ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 20package org.gjt.sp.jedit.gui;
 21
 22import javax.swing.*;
 23import java.awt.event.*;
 24import 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 3791 2001-09-02 05:36:59Z spestov $
 33 */
 34public abstract class EnhancedDialog extends JDialog
 35{
 36	public EnhancedDialog(Frame parent, String title, boolean modal)
 37	{
 38		super(parent,title,modal);
 39
 40		((Container)getLayeredPane()).addContainerListener(
 41			new ContainerHandler());
 42		getContentPane().addContainerListener(new ContainerHandler());
 43
 44		keyHandler = new KeyHandler();
 45		addKeyListener(keyHandler);
 46		addWindowListener(new WindowHandler());
 47
 48		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
 49	}
 50
 51	public abstract void ok();
 52	public abstract void cancel();
 53
 54	// protected members
 55	protected KeyHandler keyHandler;
 56
 57	// Recursively adds our key listener to sub-components
 58	class ContainerHandler extends ContainerAdapter
 59	{
 60		public void componentAdded(ContainerEvent evt)
 61		{
 62			componentAdded(evt.getChild());
 63		}
 64
 65		public void componentRemoved(ContainerEvent evt)
 66		{
 67			componentRemoved(evt.getChild());
 68		}
 69
 70		private void componentAdded(Component comp)
 71		{
 72			comp.addKeyListener(keyHandler);
 73			if(comp instanceof Container)
 74			{
 75				Container cont = (Container)comp;
 76				cont.addContainerListener(this);
 77				Component[] comps = cont.getComponents();
 78				for(int i = 0; i < comps.length; i++)
 79				{
 80					componentAdded(comps[i]);
 81				}
 82			}
 83		}
 84
 85		private void componentRemoved(Component comp)
 86		{
 87			comp.removeKeyListener(keyHandler);
 88			if(comp instanceof Container)
 89			{
 90				Container cont = (Container)comp;
 91				cont.removeContainerListener(this);
 92				Component[] comps = cont.getComponents();
 93				for(int i = 0; i < comps.length; i++)
 94				{
 95					componentRemoved(comps[i]);
 96				}
 97			}
 98		}
 99	}
100
101	class KeyHandler extends KeyAdapter
102	{
103		public void keyPressed(KeyEvent evt)
104		{
105			if(evt.isConsumed())
106				return;
107
108			if(evt.getKeyCode() == KeyEvent.VK_ENTER)
109			{
110				// crusty workaround
111				Component comp = getFocusOwner();
112				while(comp != null)
113				{
114					if(comp instanceof JComboBox)
115					{
116						JComboBox combo = (JComboBox)comp;
117						if(combo.isEditable())
118						{
119							Object selected = combo.getEditor().getItem();
120							if(selected != null)
121								combo.setSelectedItem(selected);
122						}
123						break;
124					}
125
126					comp = comp.getParent();
127				}
128
129				ok();
130				evt.consume();
131			}
132			else if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
133			{
134				cancel();
135				evt.consume();
136			}
137		}
138	}
139
140	class WindowHandler extends WindowAdapter
141	{
142		public void windowClosing(WindowEvent evt)
143		{
144			cancel();
145		}
146	}
147}