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.AccessibleContext;
  54: 
  55: /**
  56:  * A window that supports window decorations (titlebar and borders).
  57:  * This is an extension of {@link java.awt.Frame} that provides support
  58:  * for the Swing architecture. Most importantly it contains a {@link JRootPane}
  59:  * as it's only top-level child, that manages the content pane, the menu and
  60:  * a glass pane.
  61:  *
  62:  * Also, unlike <code>java.awt.Frame</code>s, JFrames support the
  63:  * Swing Pluggable Look &amp; Feel architecture.
  64:  * 
  65:  * @author Ronald Veldema (rveldema@cs.vu.nl)
  66:  */
  67: public class JFrame extends Frame
  68:   implements WindowConstants, RootPaneContainer
  69: {
  70:   private static final long serialVersionUID = -3362141868504252139L;
  71:   private static boolean defaultLookAndFeelDecorated;
  72:   private int close_action = HIDE_ON_CLOSE;
  73:   protected AccessibleContext accessibleContext;
  74:   protected JRootPane rootPane;
  75:   
  76:   /**
  77:    * @specnote rootPaneCheckingEnabled is false to comply with J2SE 5.0
  78:    */
  79:   protected boolean rootPaneCheckingEnabled = false;
  80: 
  81:   /**
  82:    * Tells us if we're in the initialization stage.
  83:    * If so, adds go to top-level Container, otherwise they go
  84:    * to the content pane for this container.
  85:    */
  86:   private boolean initStageDone = false;
  87: 
  88:   public JFrame()
  89:   {
  90:     super("JFrame");
  91:     frameInit();
  92:   }
  93: 
  94:   public JFrame(String title)
  95:   {
  96:     super(title);
  97:     frameInit();
  98:   }
  99: 
 100:   /**
 101:    * Creates a new JFrame in the specified {@link GraphicsConfiguration}
 102:    * and with an empty title.
 103:    *
 104:    * @param gc the <code>GraphicsConfiguration</code> that is used for
 105:    *     the new <code>JFrame</code>
 106:    *
 107:    * @see Frame#Frame(GraphicsConfiguration)
 108:    */
 109:   public JFrame(GraphicsConfiguration gc)
 110:   {
 111:     super(gc);
 112:     frameInit();
 113:   }
 114: 
 115:   /**
 116:    * Creates a new JFrame in the specified {@link GraphicsConfiguration}
 117:    * and with the specified title.
 118:    *
 119:    * @param title the title for the new <code>JFrame</code>
 120:    * @param gc the <code>GraphicsConfiguration</code> that is used for
 121:    *     the new <code>JFrame</code>
 122:    *
 123:    * @see Frame#Frame(String, GraphicsConfiguration)
 124:    */
 125:   public JFrame(String title, GraphicsConfiguration gc)
 126:   {
 127:     super(title, gc);
 128:     frameInit();
 129:   }
 130: 
 131:   protected void frameInit()
 132:   {
 133:     super.setLayout(new BorderLayout(1, 1));
 134:     enableEvents(AWTEvent.WINDOW_EVENT_MASK);
 135:     getRootPane(); // will do set/create
 136:     // We're now done the init stage.
 137:     initStageDone = true;
 138:   }
 139: 
 140:   public Dimension getPreferredSize()
 141:   {
 142:     return super.getPreferredSize();
 143:   }
 144: 
 145:   public JMenuBar getJMenuBar()
 146:   {
 147:     return getRootPane().getJMenuBar();
 148:   }
 149: 
 150:   public void setJMenuBar(JMenuBar menubar)
 151:   {
 152:     getRootPane().setJMenuBar(menubar);
 153:   }
 154: 
 155:   public void setLayout(LayoutManager manager)
 156:   {
 157:     // Check if we're in initialization stage.  If so, call super.setLayout
 158:     // otherwise, valid calls go to the content pane.
 159:     if (initStageDone)
 160:       {
 161:         if (isRootPaneCheckingEnabled())
 162:           throw new Error("Cannot set layout. Use getContentPane().setLayout()"
 163:                            + " instead.");
 164:         getContentPane().setLayout(manager);
 165:       }
 166:     else
 167:       super.setLayout(manager);
 168:   }
 169: 
 170:   public void setLayeredPane(JLayeredPane layeredPane)
 171:   {
 172:     getRootPane().setLayeredPane(layeredPane);
 173:   }
 174: 
 175:   public JLayeredPane getLayeredPane()
 176:   {
 177:     return getRootPane().getLayeredPane();
 178:   }
 179: 
 180:   public JRootPane getRootPane()
 181:   {
 182:     if (rootPane == null)
 183:       setRootPane(createRootPane());
 184:     return rootPane;
 185:   }
 186: 
 187:   protected void setRootPane(JRootPane root)
 188:   {
 189:     if (rootPane != null)
 190:       remove(rootPane);
 191: 
 192:     rootPane = root;
 193:     add(rootPane, BorderLayout.CENTER);
 194:   }
 195: 
 196:   protected JRootPane createRootPane()
 197:   {
 198:     return new JRootPane();
 199:   }
 200: 
 201:   public Container getContentPane()
 202:   {
 203:     return getRootPane().getContentPane();
 204:   }
 205: 
 206:   public void setContentPane(Container contentPane)
 207:   {
 208:     getRootPane().setContentPane(contentPane);
 209:   }
 210: 
 211:   public Component getGlassPane()
 212:   {
 213:     return getRootPane().getGlassPane();
 214:   }
 215: 
 216:   public void setGlassPane(Component glassPane)
 217:   {
 218:     getRootPane().setGlassPane(glassPane);
 219:   }
 220: 
 221:   protected void addImpl(Component comp, Object constraints, int index)
 222:   {
 223:     // If we're adding in the initialization stage use super.add.
 224:     // Otherwise pass the add onto the content pane.
 225:     if (!initStageDone)
 226:       super.addImpl(comp, constraints, index);
 227:     else
 228:       {
 229:         if (isRootPaneCheckingEnabled())
 230:           throw new Error("rootPaneChecking is enabled - adding components "
 231:                            + "disallowed.");
 232:         getContentPane().add(comp,constraints,index);
 233:       }
 234:   }
 235: 
 236:   public void remove(Component comp)
 237:   {
 238:     // If we're removing the root pane, use super.remove. Otherwise
 239:     // pass it on to the content pane instead.
 240:     if (comp==rootPane)
 241:       super.remove(rootPane);
 242:     else
 243:       getContentPane().remove(comp);
 244:   }
 245: 
 246:   protected boolean isRootPaneCheckingEnabled()
 247:   {
 248:     return rootPaneCheckingEnabled;
 249:   }
 250: 
 251:   protected void setRootPaneCheckingEnabled(boolean enabled)
 252:   {
 253:     rootPaneCheckingEnabled = enabled;
 254:   }
 255: 
 256:   public void update(Graphics g)
 257:   {
 258:     paint(g);
 259:   }
 260: 
 261:   protected void processKeyEvent(KeyEvent e)
 262:   {
 263:     super.processKeyEvent(e);
 264:   }
 265: 
 266:   public static void setDefaultLookAndFeelDecorated(boolean decorated)
 267:   {
 268:     defaultLookAndFeelDecorated = decorated;
 269:   }
 270: 
 271:   public static boolean isDefaultLookAndFeelDecorated()
 272:   {
 273:     return defaultLookAndFeelDecorated;
 274:   }
 275: 
 276:   public AccessibleContext getAccessibleContext()
 277:   {
 278:     return accessibleContext;
 279:   }
 280: 
 281:   public int getDefaultCloseOperation()
 282:   {
 283:     return close_action;
 284:   }
 285: 
 286:   protected String paramString()
 287:   {
 288:     return "JFrame";
 289:   }
 290: 
 291:   protected void processWindowEvent(WindowEvent e)
 292:   {
 293:     super.processWindowEvent(e);
 294:     switch (e.getID())
 295:       {
 296:       case WindowEvent.WINDOW_CLOSING:
 297:         {
 298:       switch (close_action)
 299:         {
 300:         case EXIT_ON_CLOSE:
 301:           {
 302:         System.exit(0);
 303:         break;
 304:           }
 305:         case DISPOSE_ON_CLOSE:
 306:           {
 307:         dispose();
 308:         break;
 309:           }
 310:         case HIDE_ON_CLOSE:
 311:           {
 312:         setVisible(false);
 313:         break;
 314:           }
 315:         case DO_NOTHING_ON_CLOSE:
 316:           break;
 317:         }
 318:       break;
 319:         }
 320:       case WindowEvent.WINDOW_CLOSED:
 321:       case WindowEvent.WINDOW_OPENED:
 322:       case WindowEvent.WINDOW_ICONIFIED:
 323:       case WindowEvent.WINDOW_DEICONIFIED:
 324:       case WindowEvent.WINDOW_ACTIVATED:
 325:       case WindowEvent.WINDOW_DEACTIVATED:
 326:     break;
 327:       }
 328:   }
 329: 
 330:   /**
 331:    * Defines what happens when this frame is closed. Can be one off
 332:    * <code>EXIT_ON_CLOSE</code>,
 333:    * <code>DISPOSE_ON_CLOSE</code>,
 334:    * <code>HIDE_ON_CLOSE</code> or
 335:    * <code>DO_NOTHING_ON_CLOSE</code>.
 336:    * The default is <code>HIDE_ON_CLOSE</code>.
 337:    * When <code>EXIT_ON_CLOSE</code> is specified this method calls
 338:    * <code>SecurityManager.checkExit(0)</code> which might throw a
 339:    * <code>SecurityException</code>. When the specified operation is
 340:    * not one of the above a <code>IllegalArgumentException</code> is
 341:    * thrown.
 342:    */
 343:   public void setDefaultCloseOperation(int operation)
 344:   {
 345:     SecurityManager sm = System.getSecurityManager();
 346:     if (sm != null && operation == EXIT_ON_CLOSE)
 347:       sm.checkExit(0);
 348: 
 349:     if (operation != EXIT_ON_CLOSE && operation != DISPOSE_ON_CLOSE
 350:         && operation != HIDE_ON_CLOSE && operation != DO_NOTHING_ON_CLOSE)
 351:       throw new IllegalArgumentException("defaultCloseOperation must be EXIT_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or DO_NOTHING_ON_CLOSE");
 352: 
 353:     close_action = operation;
 354:   }
 355: }