/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
- /*
- * EnhancedDialog.java - Handles OK/Cancel for you
- * Copyright (C) 1998, 1999, 2001 Slava Pestov
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- package org.gjt.sp.jedit.gui;
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.*;
- /**
- * A dialog box that handles window closing, the ENTER key and the ESCAPE
- * key for you. All you have to do is implement ok() (called when
- * Enter is pressed) and cancel() (called when Escape is pressed, or window
- * is closed).
- * @author Slava Pestov
- * @version $Id: EnhancedDialog.java 4352 2002-10-07 21:13:20Z spestov $
- */
- public abstract class EnhancedDialog extends JDialog
- {
- public EnhancedDialog(Frame parent, String title, boolean modal)
- {
- super(parent,title,modal);
- _init();
- }
-
- public EnhancedDialog(Dialog parent, String title, boolean modal)
- {
- super(parent,title,modal);
- _init();
- }
- public abstract void ok();
- public abstract void cancel();
- //{{{ Private members
-
- private void _init() {
- ((Container)getLayeredPane()).addContainerListener(
- new ContainerHandler());
- getContentPane().addContainerListener(new ContainerHandler());
- keyHandler = new KeyHandler();
- addKeyListener(keyHandler);
- addWindowListener(new WindowHandler());
- setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
- }
- //}}}
-
- // protected members
- protected KeyHandler keyHandler;
- // Recursively adds our key listener to sub-components
- class ContainerHandler extends ContainerAdapter
- {
- public void componentAdded(ContainerEvent evt)
- {
- componentAdded(evt.getChild());
- }
- public void componentRemoved(ContainerEvent evt)
- {
- componentRemoved(evt.getChild());
- }
- private void componentAdded(Component comp)
- {
- comp.addKeyListener(keyHandler);
- if(comp instanceof Container)
- {
- Container cont = (Container)comp;
- cont.addContainerListener(this);
- Component[] comps = cont.getComponents();
- for(int i = 0; i < comps.length; i++)
- {
- componentAdded(comps[i]);
- }
- }
- }
- private void componentRemoved(Component comp)
- {
- comp.removeKeyListener(keyHandler);
- if(comp instanceof Container)
- {
- Container cont = (Container)comp;
- cont.removeContainerListener(this);
- Component[] comps = cont.getComponents();
- for(int i = 0; i < comps.length; i++)
- {
- componentRemoved(comps[i]);
- }
- }
- }
- }
- class KeyHandler extends KeyAdapter
- {
- public void keyPressed(KeyEvent evt)
- {
- if(evt.isConsumed())
- return;
- if(evt.getKeyCode() == KeyEvent.VK_ENTER)
- {
- // crusty workaround
- Component comp = getFocusOwner();
- while(comp != null)
- {
- if(comp instanceof JComboBox)
- {
- JComboBox combo = (JComboBox)comp;
- if(combo.isEditable())
- {
- Object selected = combo.getEditor().getItem();
- if(selected != null)
- combo.setSelectedItem(selected);
- }
- break;
- }
- comp = comp.getParent();
- }
- ok();
- evt.consume();
- }
- else if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
- {
- cancel();
- evt.consume();
- }
- }
- }
- class WindowHandler extends WindowAdapter
- {
- public void windowClosing(WindowEvent evt)
- {
- cancel();
- }
- }
- }