libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / javax / swing / JFrame.java
blob0c956b3969ef39feae78b6a2bd3f923b79a8bcc6
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 gnu.java.lang.CPStringBuilder;
43 import java.awt.AWTEvent;
44 import java.awt.BorderLayout;
45 import java.awt.Component;
46 import java.awt.Container;
47 import java.awt.Dimension;
48 import java.awt.Frame;
49 import java.awt.Graphics;
50 import java.awt.GraphicsConfiguration;
51 import java.awt.LayoutManager;
52 import java.awt.event.KeyEvent;
53 import java.awt.event.WindowEvent;
55 import javax.accessibility.Accessible;
56 import javax.accessibility.AccessibleContext;
58 /**
59 * A window that supports window decorations (titlebar and borders).
60 * This is an extension of {@link java.awt.Frame} that provides support
61 * for the Swing architecture. Most importantly it contains a {@link JRootPane}
62 * as it's only top-level child, that manages the content pane, the menu and
63 * a glass pane.
65 * Also, unlike <code>java.awt.Frame</code>s, JFrames support the
66 * Swing Pluggable Look &amp; Feel architecture.
68 * @author Ronald Veldema (rveldema@cs.vu.nl)
70 public class JFrame extends Frame
71 implements WindowConstants, RootPaneContainer, Accessible
73 /**
74 * Provides accessibility support for <code>JFrame</code>s.
76 protected class AccessibleJFrame extends Frame.AccessibleAWTFrame
78 /**
79 * Creates a new instance of <code>AccessibleJFrame</code>.
81 protected AccessibleJFrame()
83 super();
84 // Nothing to do here.
88 /**
89 * A flag for {@link #setDefaultCloseOperation(int)}, indicating that the
90 * application should be exited, when this <code>JFrame</code> is closed.
91 * Note that in version 1.4, the equivalent constant has been added to
92 * {@link WindowConstants}.
94 * @since 1.3
96 public static final int EXIT_ON_CLOSE = 3;
98 private static final long serialVersionUID = -3362141868504252139L;
99 private static boolean defaultLookAndFeelDecorated;
100 private int closeAction = HIDE_ON_CLOSE;
101 protected AccessibleContext accessibleContext;
102 protected JRootPane rootPane;
105 * @specnote rootPaneCheckingEnabled is false to comply with J2SE 5.0
107 protected boolean rootPaneCheckingEnabled = false;
110 * Creates a new frame with an empty string for the title.
112 public JFrame()
114 super("");
115 frameInit();
119 * Creates a new <code>JFrame</code> with the specified title.
121 * @param title the frame title (<code>null</code> permitted).
123 public JFrame(String title)
125 super(title);
126 frameInit();
130 * Creates a new JFrame in the specified {@link GraphicsConfiguration}
131 * and with an empty title.
133 * @param gc the <code>GraphicsConfiguration</code> that is used for
134 * the new <code>JFrame</code>
136 * @see Frame#Frame(GraphicsConfiguration)
138 public JFrame(GraphicsConfiguration gc)
140 super(gc);
141 frameInit();
145 * Creates a new JFrame in the specified {@link GraphicsConfiguration}
146 * and with the specified title.
148 * @param title the title for the new <code>JFrame</code>
149 * @param gc the <code>GraphicsConfiguration</code> that is used for
150 * the new <code>JFrame</code>
152 * @see Frame#Frame(String, GraphicsConfiguration)
154 public JFrame(String title, GraphicsConfiguration gc)
156 super(title, gc);
157 frameInit();
160 protected void frameInit()
162 // We need to explicitly enable events here so that our processKeyEvent()
163 // and processWindowEvent() gets called.
164 enableEvents(AWTEvent.WINDOW_EVENT_MASK | AWTEvent.KEY_EVENT_MASK);
166 super.setLayout(new BorderLayout());
167 setBackground(UIManager.getDefaults().getColor("control"));
168 enableEvents(AWTEvent.WINDOW_EVENT_MASK);
169 getRootPane(); // will do set/create
171 // Setup the defaultLookAndFeelDecoration if requested.
172 if (isDefaultLookAndFeelDecorated()
173 && UIManager.getLookAndFeel().getSupportsWindowDecorations())
175 setUndecorated(true);
176 getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
179 // We're now done the init stage.
180 setRootPaneCheckingEnabled(true);
183 public Dimension getPreferredSize()
185 return super.getPreferredSize();
188 public JMenuBar getJMenuBar()
190 return getRootPane().getJMenuBar();
193 public void setJMenuBar(JMenuBar menubar)
195 getRootPane().setJMenuBar(menubar);
198 public void setLayout(LayoutManager manager)
200 // Check if we're in initialization stage. If so, call super.setLayout
201 // otherwise, valid calls go to the content pane.
202 if (isRootPaneCheckingEnabled())
203 getContentPane().setLayout(manager);
204 else
205 super.setLayout(manager);
208 public void setLayeredPane(JLayeredPane layeredPane)
210 getRootPane().setLayeredPane(layeredPane);
213 public JLayeredPane getLayeredPane()
215 return getRootPane().getLayeredPane();
218 public JRootPane getRootPane()
220 if (rootPane == null)
221 setRootPane(createRootPane());
222 return rootPane;
225 protected void setRootPane(JRootPane root)
227 if (rootPane != null)
228 remove(rootPane);
230 rootPane = root;
231 add(rootPane, BorderLayout.CENTER);
234 protected JRootPane createRootPane()
236 return new JRootPane();
239 public Container getContentPane()
241 return getRootPane().getContentPane();
244 public void setContentPane(Container contentPane)
246 getRootPane().setContentPane(contentPane);
249 public Component getGlassPane()
251 return getRootPane().getGlassPane();
254 public void setGlassPane(Component glassPane)
256 getRootPane().setGlassPane(glassPane);
259 protected void addImpl(Component comp, Object constraints, int index)
261 // If we're adding in the initialization stage use super.add.
262 // Otherwise pass the add onto the content pane.
263 if (isRootPaneCheckingEnabled() && comp != rootPane)
264 getContentPane().add(comp,constraints,index);
265 else
266 super.addImpl(comp, constraints, index);
269 public void remove(Component comp)
271 // If we're removing the root pane, use super.remove. Otherwise
272 // pass it on to the content pane instead.
273 if (comp==rootPane)
274 super.remove(rootPane);
275 else
276 getContentPane().remove(comp);
279 protected boolean isRootPaneCheckingEnabled()
281 return rootPaneCheckingEnabled;
284 protected void setRootPaneCheckingEnabled(boolean enabled)
286 rootPaneCheckingEnabled = enabled;
289 public void update(Graphics g)
291 paint(g);
294 protected void processKeyEvent(KeyEvent e)
296 super.processKeyEvent(e);
299 public static void setDefaultLookAndFeelDecorated(boolean decorated)
301 defaultLookAndFeelDecorated = decorated;
304 public static boolean isDefaultLookAndFeelDecorated()
306 return defaultLookAndFeelDecorated;
310 * Returns the object that provides accessibility features for this
311 * <code>JFrame</code>.
313 * @return The accessible context (an instance of {@link AccessibleJFrame}).
315 public AccessibleContext getAccessibleContext()
317 if (accessibleContext == null)
318 accessibleContext = new AccessibleJFrame();
319 return accessibleContext;
323 * Returns a code for the default operation when the frame is closed. The
324 * default value is {@link WindowConstants#HIDE_ON_CLOSE}.
326 * @return One of: {@link WindowConstants#DO_NOTHING_ON_CLOSE},
327 * {@link WindowConstants#HIDE_ON_CLOSE},
328 * {@link WindowConstants#DISPOSE_ON_CLOSE}, {@link #EXIT_ON_CLOSE}.
330 * @see #setDefaultCloseOperation(int)
332 public int getDefaultCloseOperation()
334 return closeAction;
338 * Returns a string describing the attributes for the <code>JFrame</code>,
339 * for use in debugging. The return value is guaranteed to be
340 * non-<code>null</code>, but the format may vary between implementations.
342 * @return A string describing the attributes of the <code>JFrame</code>.
344 protected String paramString()
346 CPStringBuilder sb = new CPStringBuilder(super.paramString());
347 sb.append(",defaultCloseOperation=");
348 sb.append(SwingUtilities.convertWindowConstantToString(
349 getDefaultCloseOperation()));
350 sb.append(",rootPane=");
351 if (rootPane != null)
352 sb.append(rootPane);
353 sb.append(",rootPaneCheckingEnabled=").append(rootPaneCheckingEnabled);
354 return sb.toString();
357 protected void processWindowEvent(WindowEvent e)
359 super.processWindowEvent(e);
360 if (e.getID() == WindowEvent.WINDOW_CLOSING)
362 switch (closeAction)
364 case EXIT_ON_CLOSE:
365 System.exit(0);
366 break;
367 case DISPOSE_ON_CLOSE:
368 dispose();
369 break;
370 case HIDE_ON_CLOSE:
371 setVisible(false);
372 break;
373 case DO_NOTHING_ON_CLOSE:
374 break;
380 * Sets the default operation that is performed when this frame is closed.
381 * The default is <code>HIDE_ON_CLOSE</code>. When
382 * <code>EXIT_ON_CLOSE</code> is specified this method calls
383 * <code>SecurityManager.checkExit(0)</code> which might throw a
384 * <code>SecurityException</code>.
386 * @param operation a code for the operation (one of:
387 * {@link WindowConstants#DO_NOTHING_ON_CLOSE},
388 * {@link WindowConstants#HIDE_ON_CLOSE},
389 * {@link WindowConstants#DISPOSE_ON_CLOSE} and
390 * {@link WindowConstants#EXIT_ON_CLOSE}).
392 * @throws IllegalArgumentException if <code>operation</code> is not one of
393 * the specified codes.
395 * @see #getDefaultCloseOperation()
397 public void setDefaultCloseOperation(int operation)
399 SecurityManager sm = System.getSecurityManager();
400 if (sm != null && operation == EXIT_ON_CLOSE)
401 sm.checkExit(0);
403 if (operation != EXIT_ON_CLOSE && operation != DISPOSE_ON_CLOSE
404 && operation != HIDE_ON_CLOSE && operation != DO_NOTHING_ON_CLOSE)
405 throw new IllegalArgumentException("operation must be EXIT_ON_CLOSE, "
406 + "HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or DO_NOTHING_ON_CLOSE");
408 closeAction = operation;