Merge with trank @ 137446
[official-gcc.git] / libjava / classpath / javax / swing / JFrame.java
blobe54d453b8df4d049df278af2a332701b8f418898
1 /* JFrame.java --
2 Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing;
41 import java.awt.AWTEvent;
42 import java.awt.BorderLayout;
43 import java.awt.Component;
44 import java.awt.Container;
45 import java.awt.Dimension;
46 import java.awt.Frame;
47 import java.awt.Graphics;
48 import java.awt.GraphicsConfiguration;
49 import java.awt.LayoutManager;
50 import java.awt.event.KeyEvent;
51 import java.awt.event.WindowEvent;
53 import javax.accessibility.Accessible;
54 import javax.accessibility.AccessibleContext;
56 /**
57 * A window that supports window decorations (titlebar and borders).
58 * This is an extension of {@link java.awt.Frame} that provides support
59 * for the Swing architecture. Most importantly it contains a {@link JRootPane}
60 * as it's only top-level child, that manages the content pane, the menu and
61 * a glass pane.
63 * Also, unlike <code>java.awt.Frame</code>s, JFrames support the
64 * Swing Pluggable Look &amp; Feel architecture.
66 * @author Ronald Veldema (rveldema@cs.vu.nl)
68 public class JFrame extends Frame
69 implements WindowConstants, RootPaneContainer, Accessible
71 /**
72 * Provides accessibility support for <code>JFrame</code>s.
74 protected class AccessibleJFrame extends Frame.AccessibleAWTFrame
76 /**
77 * Creates a new instance of <code>AccessibleJFrame</code>.
79 protected AccessibleJFrame()
81 super();
82 // Nothing to do here.
86 /**
87 * A flag for {@link #setDefaultCloseOperation(int)}, indicating that the
88 * application should be exited, when this <code>JFrame</code> is closed.
89 * Note that in version 1.4, the equivalent constant has been added to
90 * {@link WindowConstants}.
92 * @since 1.3
94 public static final int EXIT_ON_CLOSE = 3;
96 private static final long serialVersionUID = -3362141868504252139L;
97 private static boolean defaultLookAndFeelDecorated;
98 private int closeAction = HIDE_ON_CLOSE;
99 protected AccessibleContext accessibleContext;
100 protected JRootPane rootPane;
103 * @specnote rootPaneCheckingEnabled is false to comply with J2SE 5.0
105 protected boolean rootPaneCheckingEnabled = false;
108 * Creates a new frame with an empty string for the title.
110 public JFrame()
112 super("");
113 frameInit();
117 * Creates a new <code>JFrame</code> with the specified title.
119 * @param title the frame title (<code>null</code> permitted).
121 public JFrame(String title)
123 super(title);
124 frameInit();
128 * Creates a new JFrame in the specified {@link GraphicsConfiguration}
129 * and with an empty title.
131 * @param gc the <code>GraphicsConfiguration</code> that is used for
132 * the new <code>JFrame</code>
134 * @see Frame#Frame(GraphicsConfiguration)
136 public JFrame(GraphicsConfiguration gc)
138 super(gc);
139 frameInit();
143 * Creates a new JFrame in the specified {@link GraphicsConfiguration}
144 * and with the specified title.
146 * @param title the title for the new <code>JFrame</code>
147 * @param gc the <code>GraphicsConfiguration</code> that is used for
148 * the new <code>JFrame</code>
150 * @see Frame#Frame(String, GraphicsConfiguration)
152 public JFrame(String title, GraphicsConfiguration gc)
154 super(title, gc);
155 frameInit();
158 protected void frameInit()
160 // We need to explicitly enable events here so that our processKeyEvent()
161 // and processWindowEvent() gets called.
162 enableEvents(AWTEvent.WINDOW_EVENT_MASK | AWTEvent.KEY_EVENT_MASK);
164 super.setLayout(new BorderLayout());
165 setBackground(UIManager.getDefaults().getColor("control"));
166 enableEvents(AWTEvent.WINDOW_EVENT_MASK);
167 getRootPane(); // will do set/create
169 // Setup the defaultLookAndFeelDecoration if requested.
170 if (isDefaultLookAndFeelDecorated()
171 && UIManager.getLookAndFeel().getSupportsWindowDecorations())
173 setUndecorated(true);
174 getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
177 // We're now done the init stage.
178 setRootPaneCheckingEnabled(true);
181 public Dimension getPreferredSize()
183 return super.getPreferredSize();
186 public JMenuBar getJMenuBar()
188 return getRootPane().getJMenuBar();
191 public void setJMenuBar(JMenuBar menubar)
193 getRootPane().setJMenuBar(menubar);
196 public void setLayout(LayoutManager manager)
198 // Check if we're in initialization stage. If so, call super.setLayout
199 // otherwise, valid calls go to the content pane.
200 if (isRootPaneCheckingEnabled())
201 getContentPane().setLayout(manager);
202 else
203 super.setLayout(manager);
206 public void setLayeredPane(JLayeredPane layeredPane)
208 getRootPane().setLayeredPane(layeredPane);
211 public JLayeredPane getLayeredPane()
213 return getRootPane().getLayeredPane();
216 public JRootPane getRootPane()
218 if (rootPane == null)
219 setRootPane(createRootPane());
220 return rootPane;
223 protected void setRootPane(JRootPane root)
225 if (rootPane != null)
226 remove(rootPane);
228 rootPane = root;
229 add(rootPane, BorderLayout.CENTER);
232 protected JRootPane createRootPane()
234 return new JRootPane();
237 public Container getContentPane()
239 return getRootPane().getContentPane();
242 public void setContentPane(Container contentPane)
244 getRootPane().setContentPane(contentPane);
247 public Component getGlassPane()
249 return getRootPane().getGlassPane();
252 public void setGlassPane(Component glassPane)
254 getRootPane().setGlassPane(glassPane);
257 protected void addImpl(Component comp, Object constraints, int index)
259 // If we're adding in the initialization stage use super.add.
260 // Otherwise pass the add onto the content pane.
261 if (isRootPaneCheckingEnabled() && comp != rootPane)
262 getContentPane().add(comp,constraints,index);
263 else
264 super.addImpl(comp, constraints, index);
267 public void remove(Component comp)
269 // If we're removing the root pane, use super.remove. Otherwise
270 // pass it on to the content pane instead.
271 if (comp==rootPane)
272 super.remove(rootPane);
273 else
274 getContentPane().remove(comp);
277 protected boolean isRootPaneCheckingEnabled()
279 return rootPaneCheckingEnabled;
282 protected void setRootPaneCheckingEnabled(boolean enabled)
284 rootPaneCheckingEnabled = enabled;
287 public void update(Graphics g)
289 paint(g);
292 protected void processKeyEvent(KeyEvent e)
294 super.processKeyEvent(e);
297 public static void setDefaultLookAndFeelDecorated(boolean decorated)
299 defaultLookAndFeelDecorated = decorated;
302 public static boolean isDefaultLookAndFeelDecorated()
304 return defaultLookAndFeelDecorated;
308 * Returns the object that provides accessibility features for this
309 * <code>JFrame</code>.
311 * @return The accessible context (an instance of {@link AccessibleJFrame}).
313 public AccessibleContext getAccessibleContext()
315 if (accessibleContext == null)
316 accessibleContext = new AccessibleJFrame();
317 return accessibleContext;
321 * Returns a code for the default operation when the frame is closed. The
322 * default value is {@link WindowConstants#HIDE_ON_CLOSE}.
324 * @return One of: {@link WindowConstants#DO_NOTHING_ON_CLOSE},
325 * {@link WindowConstants#HIDE_ON_CLOSE},
326 * {@link WindowConstants#DISPOSE_ON_CLOSE}, {@link #EXIT_ON_CLOSE}.
328 * @see #setDefaultCloseOperation(int)
330 public int getDefaultCloseOperation()
332 return closeAction;
336 * Returns a string describing the attributes for the <code>JFrame</code>,
337 * for use in debugging. The return value is guaranteed to be
338 * non-<code>null</code>, but the format may vary between implementations.
340 * @return A string describing the attributes of the <code>JFrame</code>.
342 protected String paramString()
344 StringBuffer sb = new StringBuffer(super.paramString());
345 sb.append(",defaultCloseOperation=");
346 sb.append(SwingUtilities.convertWindowConstantToString(
347 getDefaultCloseOperation()));
348 sb.append(",rootPane=");
349 if (rootPane != null)
350 sb.append(rootPane);
351 sb.append(",rootPaneCheckingEnabled=").append(rootPaneCheckingEnabled);
352 return sb.toString();
355 protected void processWindowEvent(WindowEvent e)
357 super.processWindowEvent(e);
358 if (e.getID() == WindowEvent.WINDOW_CLOSING)
360 switch (closeAction)
362 case EXIT_ON_CLOSE:
363 System.exit(0);
364 break;
365 case DISPOSE_ON_CLOSE:
366 dispose();
367 break;
368 case HIDE_ON_CLOSE:
369 setVisible(false);
370 break;
371 case DO_NOTHING_ON_CLOSE:
372 break;
378 * Sets the default operation that is performed when this frame is closed.
379 * The default is <code>HIDE_ON_CLOSE</code>. When
380 * <code>EXIT_ON_CLOSE</code> is specified this method calls
381 * <code>SecurityManager.checkExit(0)</code> which might throw a
382 * <code>SecurityException</code>.
384 * @param operation a code for the operation (one of:
385 * {@link WindowConstants#DO_NOTHING_ON_CLOSE},
386 * {@link WindowConstants#HIDE_ON_CLOSE},
387 * {@link WindowConstants#DISPOSE_ON_CLOSE} and
388 * {@link WindowConstants#EXIT_ON_CLOSE}).
390 * @throws IllegalArgumentException if <code>operation</code> is not one of
391 * the specified codes.
393 * @see #getDefaultCloseOperation()
395 public void setDefaultCloseOperation(int operation)
397 SecurityManager sm = System.getSecurityManager();
398 if (sm != null && operation == EXIT_ON_CLOSE)
399 sm.checkExit(0);
401 if (operation != EXIT_ON_CLOSE && operation != DISPOSE_ON_CLOSE
402 && operation != HIDE_ON_CLOSE && operation != DO_NOTHING_ON_CLOSE)
403 throw new IllegalArgumentException("operation must be EXIT_ON_CLOSE, "
404 + "HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or DO_NOTHING_ON_CLOSE");
406 closeAction = operation;