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:     public 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:   /**
 106:    * Tells us if we're in the initialization stage.
 107:    * If so, adds go to top-level Container, otherwise they go
 108:    * to the content pane for this container.
 109:    */
 110:   private boolean initStageDone = false;
 111: 
 112:   public JFrame()
 113:   {
 114:     super("JFrame");
 115:     frameInit();
 116:   }
 117: 
 118:   public JFrame(String title)
 119:   {
 120:     super(title);
 121:     frameInit();
 122:   }
 123: 
 124:   /**
 125:    * Creates a new JFrame in the specified {@link GraphicsConfiguration}
 126:    * and with an empty title.
 127:    *
 128:    * @param gc the <code>GraphicsConfiguration</code> that is used for
 129:    *     the new <code>JFrame</code>
 130:    *
 131:    * @see Frame#Frame(GraphicsConfiguration)
 132:    */
 133:   public JFrame(GraphicsConfiguration gc)
 134:   {
 135:     super(gc);
 136:     frameInit();
 137:   }
 138: 
 139:   /**
 140:    * Creates a new JFrame in the specified {@link GraphicsConfiguration}
 141:    * and with the specified title.
 142:    *
 143:    * @param title the title for the new <code>JFrame</code>
 144:    * @param gc the <code>GraphicsConfiguration</code> that is used for
 145:    *     the new <code>JFrame</code>
 146:    *
 147:    * @see Frame#Frame(String, GraphicsConfiguration)
 148:    */
 149:   public JFrame(String title, GraphicsConfiguration gc)
 150:   {
 151:     super(title, gc);
 152:     frameInit();
 153:   }
 154: 
 155:   protected void frameInit()
 156:   {
 157:     super.setLayout(new BorderLayout(1, 1));
 158:     enableEvents(AWTEvent.WINDOW_EVENT_MASK);
 159:     getRootPane(); // will do set/create
 160:     // We're now done the init stage.
 161:     initStageDone = true;
 162:   }
 163: 
 164:   public Dimension getPreferredSize()
 165:   {
 166:     return super.getPreferredSize();
 167:   }
 168: 
 169:   public JMenuBar getJMenuBar()
 170:   {
 171:     return getRootPane().getJMenuBar();
 172:   }
 173: 
 174:   public void setJMenuBar(JMenuBar menubar)
 175:   {
 176:     getRootPane().setJMenuBar(menubar);
 177:   }
 178: 
 179:   public void setLayout(LayoutManager manager)
 180:   {
 181:     // Check if we're in initialization stage.  If so, call super.setLayout
 182:     // otherwise, valid calls go to the content pane.
 183:     if (initStageDone)
 184:       {
 185:         if (isRootPaneCheckingEnabled())
 186:           throw new Error("Cannot set layout. Use getContentPane().setLayout()"
 187:                            + " instead.");
 188:         getContentPane().setLayout(manager);
 189:       }
 190:     else
 191:       super.setLayout(manager);
 192:   }
 193: 
 194:   public void setLayeredPane(JLayeredPane layeredPane)
 195:   {
 196:     getRootPane().setLayeredPane(layeredPane);
 197:   }
 198: 
 199:   public JLayeredPane getLayeredPane()
 200:   {
 201:     return getRootPane().getLayeredPane();
 202:   }
 203: 
 204:   public JRootPane getRootPane()
 205:   {
 206:     if (rootPane == null)
 207:       setRootPane(createRootPane());
 208:     return rootPane;
 209:   }
 210: 
 211:   protected void setRootPane(JRootPane root)
 212:   {
 213:     if (rootPane != null)
 214:       remove(rootPane);
 215: 
 216:     rootPane = root;
 217:     add(rootPane, BorderLayout.CENTER);
 218:   }
 219: 
 220:   protected JRootPane createRootPane()
 221:   {
 222:     return new JRootPane();
 223:   }
 224: 
 225:   public Container getContentPane()
 226:   {
 227:     return getRootPane().getContentPane();
 228:   }
 229: 
 230:   public void setContentPane(Container contentPane)
 231:   {
 232:     getRootPane().setContentPane(contentPane);
 233:   }
 234: 
 235:   public Component getGlassPane()
 236:   {
 237:     return getRootPane().getGlassPane();
 238:   }
 239: 
 240:   public void setGlassPane(Component glassPane)
 241:   {
 242:     getRootPane().setGlassPane(glassPane);
 243:   }
 244: 
 245:   protected void addImpl(Component comp, Object constraints, int index)
 246:   {
 247:     // If we're adding in the initialization stage use super.add.
 248:     // Otherwise pass the add onto the content pane.
 249:     if (!initStageDone)
 250:       super.addImpl(comp, constraints, index);
 251:     else
 252:       {
 253:         if (isRootPaneCheckingEnabled())
 254:           throw new Error("rootPaneChecking is enabled - adding components "
 255:                            + "disallowed.");
 256:         getContentPane().add(comp,constraints,index);
 257:       }
 258:   }
 259: 
 260:   public void remove(Component comp)
 261:   {
 262:     // If we're removing the root pane, use super.remove. Otherwise
 263:     // pass it on to the content pane instead.
 264:     if (comp==rootPane)
 265:       super.remove(rootPane);
 266:     else
 267:       getContentPane().remove(comp);
 268:   }
 269: 
 270:   protected boolean isRootPaneCheckingEnabled()
 271:   {
 272:     return rootPaneCheckingEnabled;
 273:   }
 274: 
 275:   protected void setRootPaneCheckingEnabled(boolean enabled)
 276:   {
 277:     rootPaneCheckingEnabled = enabled;
 278:   }
 279: 
 280:   public void update(Graphics g)
 281:   {
 282:     paint(g);
 283:   }
 284: 
 285:   protected void processKeyEvent(KeyEvent e)
 286:   {
 287:     super.processKeyEvent(e);
 288:   }
 289: 
 290:   public static void setDefaultLookAndFeelDecorated(boolean decorated)
 291:   {
 292:     defaultLookAndFeelDecorated = decorated;
 293:   }
 294: 
 295:   public static boolean isDefaultLookAndFeelDecorated()
 296:   {
 297:     return defaultLookAndFeelDecorated;
 298:   }
 299: 
 300:   public AccessibleContext getAccessibleContext()
 301:   {
 302:     if (accessibleContext == null)
 303:       accessibleContext = new AccessibleJFrame();
 304:     return accessibleContext;
 305:   }
 306: 
 307:   public int getDefaultCloseOperation()
 308:   {
 309:     return close_action;
 310:   }
 311: 
 312:   protected String paramString()
 313:   {
 314:     return "JFrame";
 315:   }
 316: 
 317:   protected void processWindowEvent(WindowEvent e)
 318:   {
 319:     super.processWindowEvent(e);
 320:     switch (e.getID())
 321:       {
 322:       case WindowEvent.WINDOW_CLOSING:
 323:         {
 324:       switch (close_action)
 325:         {
 326:         case EXIT_ON_CLOSE:
 327:           {
 328:         System.exit(0);
 329:         break;
 330:           }
 331:         case DISPOSE_ON_CLOSE:
 332:           {
 333:         dispose();
 334:         break;
 335:           }
 336:         case HIDE_ON_CLOSE:
 337:           {
 338:         setVisible(false);
 339:         break;
 340:           }
 341:         case DO_NOTHING_ON_CLOSE:
 342:           break;
 343:         }
 344:       break;
 345:         }
 346:       case WindowEvent.WINDOW_CLOSED:
 347:       case WindowEvent.WINDOW_OPENED:
 348:       case WindowEvent.WINDOW_ICONIFIED:
 349:       case WindowEvent.WINDOW_DEICONIFIED:
 350:       case WindowEvent.WINDOW_ACTIVATED:
 351:       case WindowEvent.WINDOW_DEACTIVATED:
 352:     break;
 353:       }
 354:   }
 355: 
 356:   /**
 357:    * Defines what happens when this frame is closed. Can be one off
 358:    * <code>EXIT_ON_CLOSE</code>,
 359:    * <code>DISPOSE_ON_CLOSE</code>,
 360:    * <code>HIDE_ON_CLOSE</code> or
 361:    * <code>DO_NOTHING_ON_CLOSE</code>.
 362:    * The default is <code>HIDE_ON_CLOSE</code>.
 363:    * When <code>EXIT_ON_CLOSE</code> is specified this method calls
 364:    * <code>SecurityManager.checkExit(0)</code> which might throw a
 365:    * <code>SecurityException</code>. When the specified operation is
 366:    * not one of the above a <code>IllegalArgumentException</code> is
 367:    * thrown.
 368:    */
 369:   public void setDefaultCloseOperation(int operation)
 370:   {
 371:     SecurityManager sm = System.getSecurityManager();
 372:     if (sm != null && operation == EXIT_ON_CLOSE)
 373:       sm.checkExit(0);
 374: 
 375:     if (operation != EXIT_ON_CLOSE && operation != DISPOSE_ON_CLOSE
 376:         && operation != HIDE_ON_CLOSE && operation != DO_NOTHING_ON_CLOSE)
 377:       throw new IllegalArgumentException("defaultCloseOperation must be EXIT_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or DO_NOTHING_ON_CLOSE");
 378: 
 379:     close_action = operation;
 380:   }
 381: }