KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > swixml > XDialog


1 /*--
2  $Id: XDialog.java,v 1.2 2004/08/20 05:59:30 wolfpaulus Exp $
3
4  Copyright (C) 2003-2004 Wolf Paulus.
5  All rights reserved.
6
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions
9  are met:
10
11  1. Redistributions of source code must retain the above copyright
12  notice, this list of conditions, and the following disclaimer.
13
14  2. Redistributions in binary form must reproduce the above copyright
15  notice, this list of conditions, and the disclaimer that follows
16  these conditions in the documentation and/or other materials provided
17  with the distribution.
18
19  3. The end-user documentation included with the redistribution,
20  if any, must include the following acknowledgment:
21         "This product includes software developed by the
22          SWIXML Project (http://www.swixml.org/)."
23  Alternately, this acknowledgment may appear in the software itself,
24  if and wherever such third-party acknowledgments normally appear.
25
26  4. The name "Swixml" must not be used to endorse or promote products
27  derived from this software without prior written permission. For
28  written permission, please contact <info_AT_swixml_DOT_org>
29
30  5. Products derived from this software may not be called "Swixml",
31  nor may "Swixml" appear in their name, without prior written
32  permission from the Swixml Project Management.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  DISCLAIMED. IN NO EVENT SHALL THE SWIXML PROJECT OR ITS
38  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  SUCH DAMAGE.
46  ====================================================================
47
48  This software consists of voluntary contributions made by many
49  individuals on behalf of the Swixml Project and was originally
50  created by Wolf Paulus <wolf_AT_swixml_DOT_org>. For more information
51  on the Swixml Project, please see <http://www.swixml.org/>.
52 */

53 package org.swixml;
54
55 import javax.swing.*;
56 import java.awt.*;
57 import java.awt.event.ActionEvent JavaDoc;
58 import java.awt.event.ActionListener JavaDoc;
59 import java.awt.event.KeyEvent JavaDoc;
60
61 /**
62  * XDialog simply extends JDialog to allow instantiation with a parent frame
63  *
64  * @author <a HREF="mailto:wolf@paulus.com">Wolf Paulus</a>
65  * @version $Revision: 1.2 $
66  */

67 public class XDialog extends JDialog {
68   /**
69    * Creates a non-modal dialog without a title and without a specified
70    * <code>Frame</code> owner. A shared, hidden frame will be
71    * set as the owner of the dialog.
72    * <p>
73    * This constructor sets the component's locale property to the value
74    * returned by <code>JComponent.getDefaultLocale</code>.
75    * </p>
76    * <p>
77    * setLocationRelativeTo is called for the instanced dialog if a parent
78    * could be provided by the SwingEngine.
79    * </p>
80    * @exception HeadlessException if GraphicsEnvironment.isHeadless()
81    * returns true.
82    * @see GraphicsEnvironment#isHeadless
83    * @see JComponent#getDefaultLocale
84    * @see Window#setLocationRelativeTo
85    */

86   public XDialog() throws HeadlessException {
87     super( SwingEngine.getAppFrame() != null && SwingEngine.getAppFrame().isDisplayable() ? SwingEngine.getAppFrame() : null );
88   }
89
90   /**
91    * Makes the Dialog visible. If the dialog and/or its owner
92    * are not yet displayable, both are made displayable. The
93    * dialog will be validated prior to being made visible.
94    * If the dialog is already visible, this will bring the dialog
95    * to the front.
96    * <p>
97    * If the dialog is modal and is not already visible, this call will
98    * not return until the dialog is hidden by calling <code>hide</code> or
99    * <code>dispose</code>. It is permissible to show modal dialogs from
100    * the event dispatching thread because the toolkit will ensure that
101    * another event pump runs while the one which invoked this method
102    * is blocked.
103    * @see Component#hide
104    * @see Component#isDisplayable
105    * @see Component#validate
106    * @see Dialog#isModal
107    */

108   public void show() {
109     this.setLocationRelativeTo( SwingUtilities.windowForComponent( this ) );
110     super.show();
111   }
112
113   /**
114    * Sets the application frame system icon.
115    * <pre><b>Note:</b><br>
116    * The provided icon is only applied if an enclosing frame doesn't really exists yet or does not have an icon set.
117    * </pre>
118    * @param image <code>Image</code> the image to become the app's system icon.
119    */

120   public synchronized void setIconImage( Image image ) {
121     Frame f = JOptionPane.getFrameForComponent( this );
122     if (f != null && f.getIconImage() == null) {
123       f.setIconImage( image );
124     }
125   }
126
127   /**
128    * Overwrites the <code>createRootPane</code> method to install Escape key handling.
129    * <pre>
130    * When using the JDialog window through a JOptionPane, you do not have to install the Escape key handling,
131    * as the basic look-and-feel class for the option pane (BasicOptionPaneUI) already does this for you.
132    * </pre>
133    *
134    * @return <code>JRootPane</code> - the rootpane with some keyboard actions registered.
135    *
136    */

137   protected JRootPane createRootPane() {
138     ActionListener JavaDoc actionListener = new ActionListener JavaDoc() {
139       public void actionPerformed( ActionEvent JavaDoc actionEvent ) {
140         setVisible( false );
141       }
142     };
143     JRootPane rootPane = new JRootPane();
144     KeyStroke stroke = KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 );
145     rootPane.registerKeyboardAction( actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW );
146     return rootPane;
147   }
148 }
149
Popular Tags