Source for javax.swing.JFrame

   1: /* JFrame.java --
   2:    Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package javax.swing;
  40: 
  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;
  52: 
  53: import javax.accessibility.Accessible;
  54: import javax.accessibility.AccessibleContext;
  55: 
  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.
  62:  *
  63:  * Also, unlike <code>java.awt.Frame</code>s, JFrames support the
  64:  * Swing Pluggable Look &amp; Feel architecture.
  65:  * 
  66:  * @author Ronald Veldema (rveldema@cs.vu.nl)
  67:  */
  68: public class JFrame extends Frame
  69:   implements WindowConstants, RootPaneContainer, Accessible
  70: {
  71:   /**
  72:    * Provides accessibility support for <code>JFrame</code>s.
  73:    */
  74:   protected class AccessibleJFrame extends Frame.AccessibleAWTFrame
  75:   {
  76:     /**
  77:      * Creates a new instance of <code>AccessibleJFrame</code>.
  78:      */
  79:     protected AccessibleJFrame()
  80:     {
  81:       super();
  82:       // Nothing to do here.
  83:     }
  84:   }
  85: 
  86:   /**
  87:    * A flag for {@link #setDefaultCloseOperation(int)}, indicating that the
  88:    * application should be exited, when this <code>JFrame</code> is closed.
  89:    *
  90:    * @since 1.3
  91:    */
  92:   public static final int EXIT_ON_CLOSE = 3;
  93: 
  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;
  99:   
 100:   /**
 101:    * @specnote rootPaneCheckingEnabled is false to comply with J2SE 5.0
 102:    */
 103:   protected boolean rootPaneCheckingEnabled = false;
 104: 
 105:   public JFrame()
 106:   {
 107:     super("JFrame");
 108:     frameInit();
 109:   }
 110: 
 111:   public JFrame(String title)
 112:   {
 113:     super(title);
 114:     frameInit();
 115:   }
 116: 
 117:   /**
 118:    * Creates a new JFrame in the specified {@link GraphicsConfiguration}
 119:    * and with an empty title.
 120:    *
 121:    * @param gc the <code>GraphicsConfiguration</code> that is used for
 122:    *     the new <code>JFrame</code>
 123:    *
 124:    * @see Frame#Frame(GraphicsConfiguration)
 125:    */
 126:   public JFrame(GraphicsConfiguration gc)
 127:   {
 128:     super(gc);
 129:     frameInit();
 130:   }
 131: 
 132:   /**
 133:    * Creates a new JFrame in the specified {@link GraphicsConfiguration}
 134:    * and with the specified title.
 135:    *
 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>
 139:    *
 140:    * @see Frame#Frame(String, GraphicsConfiguration)
 141:    */
 142:   public JFrame(String title, GraphicsConfiguration gc)
 143:   {
 144:     super(title, gc);
 145:     frameInit();
 146:   }
 147: 
 148:   protected void frameInit()
 149:   {
 150:     super.setLayout(new BorderLayout(1, 1));
 151:     enableEvents(AWTEvent.WINDOW_EVENT_MASK);
 152:     getRootPane(); // will do set/create
 153: 
 154:     // Setup the defaultLookAndFeelDecoration if requested.
 155:     if (isDefaultLookAndFeelDecorated()
 156:         && UIManager.getLookAndFeel().getSupportsWindowDecorations())
 157:       {
 158:         setUndecorated(true);
 159:         getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
 160:       }
 161: 
 162:     // We're now done the init stage.
 163:     setRootPaneCheckingEnabled(true);
 164:   }
 165: 
 166:   public Dimension getPreferredSize()
 167:   {
 168:     return super.getPreferredSize();
 169:   }
 170: 
 171:   public JMenuBar getJMenuBar()
 172:   {
 173:     return getRootPane().getJMenuBar();
 174:   }
 175: 
 176:   public void setJMenuBar(JMenuBar menubar)
 177:   {
 178:     getRootPane().setJMenuBar(menubar);
 179:   }
 180: 
 181:   public void setLayout(LayoutManager manager)
 182:   {
 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);
 189:   }
 190: 
 191:   public void setLayeredPane(JLayeredPane layeredPane)
 192:   {
 193:     getRootPane().setLayeredPane(layeredPane);
 194:   }
 195: 
 196:   public JLayeredPane getLayeredPane()
 197:   {
 198:     return getRootPane().getLayeredPane();
 199:   }
 200: 
 201:   public JRootPane getRootPane()
 202:   {
 203:     if (rootPane == null)
 204:       setRootPane(createRootPane());
 205:     return rootPane;
 206:   }
 207: 
 208:   protected void setRootPane(JRootPane root)
 209:   {
 210:     if (rootPane != null)
 211:       remove(rootPane);
 212: 
 213:     rootPane = root;
 214:     add(rootPane, BorderLayout.CENTER);
 215:   }
 216: 
 217:   protected JRootPane createRootPane()
 218:   {
 219:     return new JRootPane();
 220:   }
 221: 
 222:   public Container getContentPane()
 223:   {
 224:     return getRootPane().getContentPane();
 225:   }
 226: 
 227:   public void setContentPane(Container contentPane)
 228:   {
 229:     getRootPane().setContentPane(contentPane);
 230:   }
 231: 
 232:   public Component getGlassPane()
 233:   {
 234:     return getRootPane().getGlassPane();
 235:   }
 236: 
 237:   public void setGlassPane(Component glassPane)
 238:   {
 239:     getRootPane().setGlassPane(glassPane);
 240:   }
 241: 
 242:   protected void addImpl(Component comp, Object constraints, int index)
 243:   {
 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);
 250:   }
 251: 
 252:   public void remove(Component comp)
 253:   {
 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);
 260:   }
 261: 
 262:   protected boolean isRootPaneCheckingEnabled()
 263:   {
 264:     return rootPaneCheckingEnabled;
 265:   }
 266: 
 267:   protected void setRootPaneCheckingEnabled(boolean enabled)
 268:   {
 269:     rootPaneCheckingEnabled = enabled;
 270:   }
 271: 
 272:   public void update(Graphics g)
 273:   {
 274:     paint(g);
 275:   }
 276: 
 277:   protected void processKeyEvent(KeyEvent e)
 278:   {
 279:     super.processKeyEvent(e);
 280:   }
 281: 
 282:   public static void setDefaultLookAndFeelDecorated(boolean decorated)
 283:   {
 284:     defaultLookAndFeelDecorated = decorated;
 285:   }
 286: 
 287:   public static boolean isDefaultLookAndFeelDecorated()
 288:   {
 289:     return defaultLookAndFeelDecorated;
 290:   }
 291: 
 292:   public AccessibleContext getAccessibleContext()
 293:   {
 294:     if (accessibleContext == null)
 295:       accessibleContext = new AccessibleJFrame();
 296:     return accessibleContext;
 297:   }
 298: 
 299:   public int getDefaultCloseOperation()
 300:   {
 301:     return close_action;
 302:   }
 303: 
 304:   protected String paramString()
 305:   {
 306:     return "JFrame";
 307:   }
 308: 
 309:   protected void processWindowEvent(WindowEvent e)
 310:   {
 311:     super.processWindowEvent(e);
 312:     switch (e.getID())
 313:       {
 314:       case WindowEvent.WINDOW_CLOSING:
 315:         {
 316:       switch (close_action)
 317:         {
 318:         case EXIT_ON_CLOSE:
 319:           {
 320:         System.exit(0);
 321:         break;
 322:           }
 323:         case DISPOSE_ON_CLOSE:
 324:           {
 325:         dispose();
 326:         break;
 327:           }
 328:         case HIDE_ON_CLOSE:
 329:           {
 330:         setVisible(false);
 331:         break;
 332:           }
 333:         case DO_NOTHING_ON_CLOSE:
 334:           break;
 335:         }
 336:       break;
 337:         }
 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;
 345:       }
 346:   }
 347: 
 348:   /**
 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.
 360:    */
 361:   public void setDefaultCloseOperation(int operation)
 362:   {
 363:     SecurityManager sm = System.getSecurityManager();
 364:     if (sm != null && operation == EXIT_ON_CLOSE)
 365:       sm.checkExit(0);
 366: 
 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");
 370: 
 371:     close_action = operation;
 372:   }
 373: }