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