Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / JFrame.java
blobd2512056085eb498ae9e04c8922f300abb646d60
1 /* JFrame.java --
2 Copyright (C) 2002, 2004, 2005 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.
90 * @since 1.3
92 public static final int EXIT_ON_CLOSE = 3;
94 private static final long serialVersionUID = -3362141868504252139L;
95 private static boolean defaultLookAndFeelDecorated;
96 private int close_action = HIDE_ON_CLOSE;
97 protected AccessibleContext accessibleContext;
98 protected JRootPane rootPane;
101 * @specnote rootPaneCheckingEnabled is false to comply with J2SE 5.0
103 protected boolean rootPaneCheckingEnabled = false;
105 public JFrame()
107 super("JFrame");
108 frameInit();
111 public JFrame(String title)
113 super(title);
114 frameInit();
118 * Creates a new JFrame in the specified {@link GraphicsConfiguration}
119 * and with an empty title.
121 * @param gc the <code>GraphicsConfiguration</code> that is used for
122 * the new <code>JFrame</code>
124 * @see Frame#Frame(GraphicsConfiguration)
126 public JFrame(GraphicsConfiguration gc)
128 super(gc);
129 frameInit();
133 * Creates a new JFrame in the specified {@link GraphicsConfiguration}
134 * and with the specified title.
136 * @param title the title for the new <code>JFrame</code>
137 * @param gc the <code>GraphicsConfiguration</code> that is used for
138 * the new <code>JFrame</code>
140 * @see Frame#Frame(String, GraphicsConfiguration)
142 public JFrame(String title, GraphicsConfiguration gc)
144 super(title, gc);
145 frameInit();
148 protected void frameInit()
150 super.setLayout(new BorderLayout(1, 1));
151 enableEvents(AWTEvent.WINDOW_EVENT_MASK);
152 getRootPane(); // will do set/create
154 // Setup the defaultLookAndFeelDecoration if requested.
155 if (isDefaultLookAndFeelDecorated()
156 && UIManager.getLookAndFeel().getSupportsWindowDecorations())
158 setUndecorated(true);
159 getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
162 // We're now done the init stage.
163 setRootPaneCheckingEnabled(true);
166 public Dimension getPreferredSize()
168 return super.getPreferredSize();
171 public JMenuBar getJMenuBar()
173 return getRootPane().getJMenuBar();
176 public void setJMenuBar(JMenuBar menubar)
178 getRootPane().setJMenuBar(menubar);
181 public void setLayout(LayoutManager manager)
183 // Check if we're in initialization stage. If so, call super.setLayout
184 // otherwise, valid calls go to the content pane.
185 if (isRootPaneCheckingEnabled())
186 getContentPane().setLayout(manager);
187 else
188 super.setLayout(manager);
191 public void setLayeredPane(JLayeredPane layeredPane)
193 getRootPane().setLayeredPane(layeredPane);
196 public JLayeredPane getLayeredPane()
198 return getRootPane().getLayeredPane();
201 public JRootPane getRootPane()
203 if (rootPane == null)
204 setRootPane(createRootPane());
205 return rootPane;
208 protected void setRootPane(JRootPane root)
210 if (rootPane != null)
211 remove(rootPane);
213 rootPane = root;
214 add(rootPane, BorderLayout.CENTER);
217 protected JRootPane createRootPane()
219 return new JRootPane();
222 public Container getContentPane()
224 return getRootPane().getContentPane();
227 public void setContentPane(Container contentPane)
229 getRootPane().setContentPane(contentPane);
232 public Component getGlassPane()
234 return getRootPane().getGlassPane();
237 public void setGlassPane(Component glassPane)
239 getRootPane().setGlassPane(glassPane);
242 protected void addImpl(Component comp, Object constraints, int index)
244 // If we're adding in the initialization stage use super.add.
245 // Otherwise pass the add onto the content pane.
246 if (isRootPaneCheckingEnabled())
247 getContentPane().add(comp,constraints,index);
248 else
249 super.addImpl(comp, constraints, index);
252 public void remove(Component comp)
254 // If we're removing the root pane, use super.remove. Otherwise
255 // pass it on to the content pane instead.
256 if (comp==rootPane)
257 super.remove(rootPane);
258 else
259 getContentPane().remove(comp);
262 protected boolean isRootPaneCheckingEnabled()
264 return rootPaneCheckingEnabled;
267 protected void setRootPaneCheckingEnabled(boolean enabled)
269 rootPaneCheckingEnabled = enabled;
272 public void update(Graphics g)
274 paint(g);
277 protected void processKeyEvent(KeyEvent e)
279 super.processKeyEvent(e);
282 public static void setDefaultLookAndFeelDecorated(boolean decorated)
284 defaultLookAndFeelDecorated = decorated;
287 public static boolean isDefaultLookAndFeelDecorated()
289 return defaultLookAndFeelDecorated;
292 public AccessibleContext getAccessibleContext()
294 if (accessibleContext == null)
295 accessibleContext = new AccessibleJFrame();
296 return accessibleContext;
299 public int getDefaultCloseOperation()
301 return close_action;
304 protected String paramString()
306 return "JFrame";
309 protected void processWindowEvent(WindowEvent e)
311 super.processWindowEvent(e);
312 switch (e.getID())
314 case WindowEvent.WINDOW_CLOSING:
316 switch (close_action)
318 case EXIT_ON_CLOSE:
320 System.exit(0);
321 break;
323 case DISPOSE_ON_CLOSE:
325 dispose();
326 break;
328 case HIDE_ON_CLOSE:
330 setVisible(false);
331 break;
333 case DO_NOTHING_ON_CLOSE:
334 break;
336 break;
338 case WindowEvent.WINDOW_CLOSED:
339 case WindowEvent.WINDOW_OPENED:
340 case WindowEvent.WINDOW_ICONIFIED:
341 case WindowEvent.WINDOW_DEICONIFIED:
342 case WindowEvent.WINDOW_ACTIVATED:
343 case WindowEvent.WINDOW_DEACTIVATED:
344 break;
349 * Defines what happens when this frame is closed. Can be one off
350 * <code>EXIT_ON_CLOSE</code>,
351 * <code>DISPOSE_ON_CLOSE</code>,
352 * <code>HIDE_ON_CLOSE</code> or
353 * <code>DO_NOTHING_ON_CLOSE</code>.
354 * The default is <code>HIDE_ON_CLOSE</code>.
355 * When <code>EXIT_ON_CLOSE</code> is specified this method calls
356 * <code>SecurityManager.checkExit(0)</code> which might throw a
357 * <code>SecurityException</code>. When the specified operation is
358 * not one of the above a <code>IllegalArgumentException</code> is
359 * thrown.
361 public void setDefaultCloseOperation(int operation)
363 SecurityManager sm = System.getSecurityManager();
364 if (sm != null && operation == EXIT_ON_CLOSE)
365 sm.checkExit(0);
367 if (operation != EXIT_ON_CLOSE && operation != DISPOSE_ON_CLOSE
368 && operation != HIDE_ON_CLOSE && operation != DO_NOTHING_ON_CLOSE)
369 throw new IllegalArgumentException("defaultCloseOperation must be EXIT_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or DO_NOTHING_ON_CLOSE");
371 close_action = operation;