001 /* JWindow.java --
002 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package javax.swing;
040
041 import java.awt.AWTEvent;
042 import java.awt.BorderLayout;
043 import java.awt.Component;
044 import java.awt.Container;
045 import java.awt.Dimension;
046 import java.awt.Frame;
047 import java.awt.Graphics;
048 import java.awt.GraphicsConfiguration;
049 import java.awt.LayoutManager;
050 import java.awt.Window;
051 import java.awt.event.KeyEvent;
052
053 import javax.accessibility.Accessible;
054 import javax.accessibility.AccessibleContext;
055
056 /**
057 * Unlike JComponent derivatives, JWindow inherits from
058 * java.awt.Window. But also lets a look-and-feel component to its work.
059 *
060 * @author Ronald Veldema (rveldema@cs.vu.nl)
061 */
062 public class JWindow extends Window implements Accessible, RootPaneContainer
063 {
064 /**
065 * Provides accessibility support for <code>JWindow</code>.
066 */
067 protected class AccessibleJWindow extends Window.AccessibleAWTWindow
068 {
069 /**
070 * Creates a new instance of <code>AccessibleJWindow</code>.
071 */
072 protected AccessibleJWindow()
073 {
074 super();
075 // Nothing to do here.
076 }
077 }
078
079 private static final long serialVersionUID = 5420698392125238833L;
080
081 protected JRootPane rootPane;
082
083 /**
084 * @specnote rootPaneCheckingEnabled is false to comply with J2SE 5.0
085 */
086 protected boolean rootPaneCheckingEnabled = false;
087
088 protected AccessibleContext accessibleContext;
089
090 /**
091 * Creates a new <code>JWindow</code> that has a shared invisible owner frame
092 * as its parent.
093 */
094 public JWindow()
095 {
096 super(SwingUtilities.getOwnerFrame(null));
097 windowInit();
098 }
099
100 /**
101 * Creates a new <code>JWindow</code> that uses the specified graphics
102 * environment. This can be used to open a window on a different screen for
103 * example.
104 *
105 * @param gc the graphics environment to use
106 */
107 public JWindow(GraphicsConfiguration gc)
108 {
109 super(SwingUtilities.getOwnerFrame(null), gc);
110 windowInit();
111 }
112
113 /**
114 * Creates a new <code>JWindow</code> that has the specified
115 * <code>owner</code> frame. If <code>owner</code> is <code>null</code>, then
116 * an invisible shared owner frame is installed as owner frame.
117 *
118 * @param owner the owner frame of this window; if <code>null</code> a shared
119 * invisible owner frame is used
120 */
121 public JWindow(Frame owner)
122 {
123 super(SwingUtilities.getOwnerFrame(owner));
124 windowInit();
125 }
126
127 /**
128 * Creates a new <code>JWindow</code> that has the specified
129 * <code>owner</code> window. If <code>owner</code> is <code>null</code>,
130 * then an invisible shared owner frame is installed as owner frame.
131 *
132 * @param owner the owner window of this window; if <code>null</code> a
133 * shared invisible owner frame is used
134 */
135 public JWindow(Window owner)
136 {
137 super(SwingUtilities.getOwnerFrame(owner));
138 windowInit();
139 }
140
141 /**
142 * Creates a new <code>JWindow</code> for the given graphics configuration
143 * and that has the specified <code>owner</code> window. If
144 * <code>owner</code> is <code>null</code>, then an invisible shared owner
145 * frame is installed as owner frame.
146 *
147 * The <code>gc</code> parameter can be used to open the window on a
148 * different screen for example.
149 *
150 * @param owner the owner window of this window; if <code>null</code> a
151 * shared invisible owner frame is used
152 * @param gc the graphics configuration to use
153 */
154 public JWindow(Window owner, GraphicsConfiguration gc)
155 {
156 super(SwingUtilities.getOwnerFrame(owner), gc);
157 windowInit();
158 }
159
160 protected void windowInit()
161 {
162 // We need to explicitly enable events here so that our processKeyEvent()
163 // and processWindowEvent() gets called.
164 enableEvents(AWTEvent.KEY_EVENT_MASK);
165
166 super.setLayout(new BorderLayout(1, 1));
167 getRootPane(); // will do set/create
168 // Now we're done init stage, adds and layouts go to content pane.
169 setRootPaneCheckingEnabled(true);
170 }
171
172 public Dimension getPreferredSize()
173 {
174 return super.getPreferredSize();
175 }
176
177 public void setLayout(LayoutManager manager)
178 {
179 // Check if we're in initialization stage. If so, call super.setLayout
180 // otherwise, valid calls go to the content pane.
181 if (isRootPaneCheckingEnabled())
182 getContentPane().setLayout(manager);
183 else
184 super.setLayout(manager);
185 }
186
187 public void setLayeredPane(JLayeredPane layeredPane)
188 {
189 getRootPane().setLayeredPane(layeredPane);
190 }
191
192 public JLayeredPane getLayeredPane()
193 {
194 return getRootPane().getLayeredPane();
195 }
196
197 public JRootPane getRootPane()
198 {
199 if (rootPane == null)
200 setRootPane(createRootPane());
201 return rootPane;
202 }
203
204 protected void setRootPane(JRootPane root)
205 {
206 if (rootPane != null)
207 remove(rootPane);
208
209 rootPane = root;
210 add(rootPane, BorderLayout.CENTER);
211 }
212
213 protected JRootPane createRootPane()
214 {
215 return new JRootPane();
216 }
217
218 public Container getContentPane()
219 {
220 return getRootPane().getContentPane();
221 }
222
223 public void setContentPane(Container contentPane)
224 {
225 getRootPane().setContentPane(contentPane);
226 }
227
228 public Component getGlassPane()
229 {
230 return getRootPane().getGlassPane();
231 }
232
233 public void setGlassPane(Component glassPane)
234 {
235 getRootPane().setGlassPane(glassPane);
236 }
237
238
239 protected void addImpl(Component comp, Object constraints, int index)
240 {
241 // If we're adding in the initialization stage use super.add.
242 // otherwise pass the add onto the content pane.
243 if (isRootPaneCheckingEnabled())
244 getContentPane().add(comp, constraints, index);
245 else
246 super.addImpl(comp, constraints, index);
247 }
248
249 public void remove(Component comp)
250 {
251 // If we're removing the root pane, use super.remove. Otherwise
252 // pass it on to the content pane instead.
253 if (comp == rootPane)
254 super.remove(rootPane);
255 else
256 getContentPane().remove(comp);
257 }
258
259 protected boolean isRootPaneCheckingEnabled()
260 {
261 return rootPaneCheckingEnabled;
262 }
263
264 protected void setRootPaneCheckingEnabled(boolean enabled)
265 {
266 rootPaneCheckingEnabled = enabled;
267 }
268
269 public void update(Graphics g)
270 {
271 paint(g);
272 }
273
274 protected void processKeyEvent(KeyEvent e)
275 {
276 super.processKeyEvent(e);
277 }
278
279 public AccessibleContext getAccessibleContext()
280 {
281 if (accessibleContext == null)
282 accessibleContext = new AccessibleJWindow();
283 return accessibleContext;
284 }
285
286 protected String paramString()
287 {
288 return "JWindow";
289 }
290 }