001 /* JAPI - (Yet another (hopefully) useful) Java API
002 *
003 * Copyright (C) 2004-2006 Christian Hujer
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License as
007 * published by the Free Software Foundation; either version 2 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful, but
011 * WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013 * General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018 * 02111-1307, USA.
019 */
020
021 package net.sf.japi.swing;
022
023 import java.awt.Window;
024 import java.awt.event.ActionEvent;
025 import javax.swing.AbstractAction;
026 import static javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW;
027 import javax.swing.JDialog;
028 import static javax.swing.KeyStroke.getKeyStroke;
029 import org.jetbrains.annotations.NotNull;
030
031 /** An Action implementation that disposes a window when activated.
032 * Usually, you'd put an instance of this class in an actionmap, eventually pointing an inputmap to it.
033 * Usage example:
034 * <pre>
035 * JDialog d = new JDialog();
036 * DisposeAction da = new DisposeAction(d);
037 * d.getRootPane().getActionMap().put("close", da);
038 * d.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "close");
039 * </pre>
040 * The convenience method {@link #install(JDialog)} will do exactly that for an existing JDialog.
041 * @todo basically this is the same as using an ActionFactory with dispose as method, so why not use that?
042 * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
043 */
044 public final class DisposeAction extends AbstractAction {
045
046 /** The window to be disposed.
047 * @serial include
048 */
049 private Window window;
050
051 /** Install the DisposeAction to a JDialog for the ESCAPE key.
052 * For most users of this method, the return value is irrelevant and can be ignored.
053 * @param dialog JDialog to install to
054 * @return the created DisposeAction
055 */
056 public static DisposeAction install(@NotNull final JDialog dialog) {
057 final DisposeAction da = new DisposeAction(dialog);
058 dialog.getRootPane().getActionMap().put("close", da);
059 dialog.getRootPane().getInputMap(WHEN_IN_FOCUSED_WINDOW).put(getKeyStroke("ESCAPE"), "close");
060 return da;
061 }
062
063 /** Create a DisposeAction.
064 * @param window Window to be disposed when this action is activated
065 */
066 public DisposeAction(@NotNull final Window window) {
067 this.window = window;
068 }
069
070 /** {@inheritDoc} */
071 public void actionPerformed(@NotNull final ActionEvent e) {
072 window.dispose();
073 }
074
075 /** {@inheritDoc} */
076 @Override protected Object clone() throws CloneNotSupportedException {
077 return super.clone();
078 }
079
080 } // class DisposeAction