001    /* JAPI - (Yet anothr (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 javax.swing.JDialog;
027    import static javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW;
028    import static javax.swing.KeyStroke.getKeyStroke;
029    
030    /** An Action implementation that disposes a window when activated.
031     * Usually, you'd put an instance of this class in an actionmap, eventually pointing an inputmap to it.
032     * Usage example:
033     * <pre>
034     *  JDialog d = new JDialog();
035     *  DisposeAction da = new DisposeAction(d);
036     *  d.getRootPane().getActionMap().put("close", da);
037     *  d.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "close");
038     * </pre>
039     * The convenience method {@link #install(JDialog)} will do exactly that for an existing JDialog.
040     * @author <a href="mailto:Christian.Hujer@itcqis.com">Christian Hujer</a>
041     */
042    public final class DisposeAction extends AbstractAction {
043    
044        /** The window to be disposed.
045         * @serial include
046         */
047        private Window window;
048    
049        /** Install the DisposeAction to a JDialog for the ESCAPE key.
050         * For most users of this method, the return value is irrelevant and can be ignored.
051         * @param dialog JDialog to install to
052         * @return the created DisposeAction
053         */
054        public static DisposeAction install(final JDialog dialog) {
055            final DisposeAction da = new DisposeAction(dialog);
056            dialog.getRootPane().getActionMap().put("close", da);
057            dialog.getRootPane().getInputMap(WHEN_IN_FOCUSED_WINDOW).put(getKeyStroke("ESCAPE"), "close");
058            return da;
059        }
060    
061        /** Create a DisposeAction.
062         * @param window Window to be disposed when this action is activated
063         */
064        public DisposeAction(final Window window) {
065            this.window = window;
066        }
067    
068        /** {@inheritDoc} */
069        public void actionPerformed(final ActionEvent e) {
070            window.dispose();
071        }
072    
073        /** {@inheritDoc} */
074        @Override protected Object clone() throws CloneNotSupportedException {
075            return super.clone();
076        }
077    
078    } // class DisposeAction