Source for javax.swing.JFrame

   1: /* JFrame.java --
   2:    Copyright (C) 2002, 2004, 2005, 2006,  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:    * Note that in version 1.4, the equivalent constant has been added to
  90:    * {@link WindowConstants}.
  91:    *
  92:    * @since 1.3
  93:    */
  94:   public static final int EXIT_ON_CLOSE = 3;
  95: 
  96:   private static final long serialVersionUID = -3362141868504252139L;
  97:   private static boolean defaultLookAndFeelDecorated;
  98:   private int closeAction = HIDE_ON_CLOSE;
  99:   protected AccessibleContext accessibleContext;
 100:   protected JRootPane rootPane;
 101:   
 102:   /**
 103:    * @specnote rootPaneCheckingEnabled is false to comply with J2SE 5.0
 104:    */
 105:   protected boolean rootPaneCheckingEnabled = false;
 106: 
 107:   /**
 108:    * Creates a new frame with an empty string for the title.
 109:    */
 110:   public JFrame()
 111:   {
 112:     super("");
 113:     frameInit();
 114:   }
 115: 
 116:   /**
 117:    * Creates a new <code>JFrame</code> with the specified title.
 118:    * 
 119:    * @param title  the frame title (<code>null</code> permitted).
 120:    */
 121:   public JFrame(String title)
 122:   {
 123:     super(title);
 124:     frameInit();
 125:   }
 126: 
 127:   /**
 128:    * Creates a new JFrame in the specified {@link GraphicsConfiguration}
 129:    * and with an empty title.
 130:    *
 131:    * @param gc the <code>GraphicsConfiguration</code> that is used for
 132:    *     the new <code>JFrame</code>
 133:    *
 134:    * @see Frame#Frame(GraphicsConfiguration)
 135:    */
 136:   public JFrame(GraphicsConfiguration gc)
 137:   {
 138:     super(gc);
 139:     frameInit();
 140:   }
 141: 
 142:   /**
 143:    * Creates a new JFrame in the specified {@link GraphicsConfiguration}
 144:    * and with the specified title.
 145:    *
 146:    * @param title the title for the new <code>JFrame</code>
 147:    * @param gc the <code>GraphicsConfiguration</code> that is used for
 148:    *     the new <code>JFrame</code>
 149:    *
 150:    * @see Frame#Frame(String, GraphicsConfiguration)
 151:    */
 152:   public JFrame(String title, GraphicsConfiguration gc)
 153:   {
 154:     super(title, gc);
 155:     frameInit();
 156:   }
 157: 
 158:   protected void frameInit()
 159:   {
 160:     super.setLayout(new BorderLayout());
 161:     setBackground(UIManager.getDefaults().getColor("control"));
 162:     enableEvents(AWTEvent.WINDOW_EVENT_MASK);
 163:     getRootPane(); // will do set/create
 164: 
 165:     // Setup the defaultLookAndFeelDecoration if requested.
 166:     if (isDefaultLookAndFeelDecorated()
 167:         && UIManager.getLookAndFeel().getSupportsWindowDecorations())
 168:       {
 169:         setUndecorated(true);
 170:         getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
 171:       }
 172: 
 173:     // We're now done the init stage.
 174:     setRootPaneCheckingEnabled(true);
 175:   }
 176: 
 177:   public Dimension getPreferredSize()
 178:   {
 179:     return super.getPreferredSize();
 180:   }
 181: 
 182:   public JMenuBar getJMenuBar()
 183:   {
 184:     return getRootPane().getJMenuBar();
 185:   }
 186: 
 187:   public void setJMenuBar(JMenuBar menubar)
 188:   {
 189:     getRootPane().setJMenuBar(menubar);
 190:   }
 191: 
 192:   public void setLayout(LayoutManager manager)
 193:   {
 194:     // Check if we're in initialization stage.  If so, call super.setLayout
 195:     // otherwise, valid calls go to the content pane.
 196:     if (isRootPaneCheckingEnabled())
 197:       getContentPane().setLayout(manager);
 198:     else
 199:       super.setLayout(manager);
 200:   }
 201: 
 202:   public void setLayeredPane(JLayeredPane layeredPane)
 203:   {
 204:     getRootPane().setLayeredPane(layeredPane);
 205:   }
 206: 
 207:   public JLayeredPane getLayeredPane()
 208:   {
 209:     return getRootPane().getLayeredPane();
 210:   }
 211: 
 212:   public JRootPane getRootPane()
 213:   {
 214:     if (rootPane == null)
 215:       setRootPane(createRootPane());
 216:     return rootPane;
 217:   }
 218: 
 219:   protected void setRootPane(JRootPane root)
 220:   {
 221:     if (rootPane != null)
 222:       remove(rootPane);
 223: 
 224:     rootPane = root;
 225:     add(rootPane, BorderLayout.CENTER);
 226:   }
 227: 
 228:   protected JRootPane createRootPane()
 229:   {
 230:     return new JRootPane();
 231:   }
 232: 
 233:   public Container getContentPane()
 234:   {
 235:     return getRootPane().getContentPane();
 236:   }
 237: 
 238:   public void setContentPane(Container contentPane)
 239:   {
 240:     getRootPane().setContentPane(contentPane);
 241:   }
 242: 
 243:   public Component getGlassPane()
 244:   {
 245:     return getRootPane().getGlassPane();
 246:   }
 247: 
 248:   public void setGlassPane(Component glassPane)
 249:   {
 250:     getRootPane().setGlassPane(glassPane);
 251:   }
 252: 
 253:   protected void addImpl(Component comp, Object constraints, int index)
 254:   {
 255:     // If we're adding in the initialization stage use super.add.
 256:     // Otherwise pass the add onto the content pane.
 257:     if (isRootPaneCheckingEnabled())
 258:       getContentPane().add(comp,constraints,index);
 259:     else
 260:       super.addImpl(comp, constraints, index);
 261:   }
 262: 
 263:   public void remove(Component comp)
 264:   {
 265:     // If we're removing the root pane, use super.remove. Otherwise
 266:     // pass it on to the content pane instead.
 267:     if (comp==rootPane)
 268:       super.remove(rootPane);
 269:     else
 270:       getContentPane().remove(comp);
 271:   }
 272: 
 273:   protected boolean isRootPaneCheckingEnabled()
 274:   {
 275:     return rootPaneCheckingEnabled;
 276:   }
 277: 
 278:   protected void setRootPaneCheckingEnabled(boolean enabled)
 279:   {
 280:     rootPaneCheckingEnabled = enabled;
 281:   }
 282: 
 283:   public void update(Graphics g)
 284:   {
 285:     paint(g);
 286:   }
 287: 
 288:   protected void processKeyEvent(KeyEvent e)
 289:   {
 290:     super.processKeyEvent(e);
 291:   }
 292: 
 293:   public static void setDefaultLookAndFeelDecorated(boolean decorated)
 294:   {
 295:     defaultLookAndFeelDecorated = decorated;
 296:   }
 297: 
 298:   public static boolean isDefaultLookAndFeelDecorated()
 299:   {
 300:     return defaultLookAndFeelDecorated;
 301:   }
 302: 
 303:   /**
 304:    * Returns the object that provides accessibility features for this 
 305:    * <code>JFrame</code>.
 306:    *
 307:    * @return The accessible context (an instance of {@link AccessibleJFrame}).
 308:    */
 309:   public AccessibleContext getAccessibleContext()
 310:   {
 311:     if (accessibleContext == null)
 312:       accessibleContext = new AccessibleJFrame();
 313:     return accessibleContext;
 314:   }
 315: 
 316:   /**
 317:    * Returns a code for the default operation when the frame is closed.  The
 318:    * default value is {@link WindowConstants#HIDE_ON_CLOSE}.
 319:    * 
 320:    * @return One of: {@link WindowConstants#DO_NOTHING_ON_CLOSE},
 321:    *     {@link WindowConstants#HIDE_ON_CLOSE}, 
 322:    *     {@link WindowConstants#DISPOSE_ON_CLOSE}, {@link #EXIT_ON_CLOSE}.
 323:    * 
 324:    * @see #setDefaultCloseOperation(int)
 325:    */
 326:   public int getDefaultCloseOperation()
 327:   {
 328:     return closeAction;
 329:   }
 330: 
 331:   /**
 332:    * Returns a string describing the attributes for the <code>JFrame</code>,
 333:    * for use in debugging.  The return value is guaranteed to be 
 334:    * non-<code>null</code>, but the format may vary between implementations.
 335:    * 
 336:    * @return A string describing the attributes of the <code>JFrame</code>.
 337:    */
 338:   protected String paramString()
 339:   {
 340:     StringBuffer sb = new StringBuffer(super.paramString());
 341:     sb.append(",defaultCloseOperation=");
 342:     sb.append(SwingUtilities.convertWindowConstantToString(
 343:         getDefaultCloseOperation()));
 344:     sb.append(",rootPane=");
 345:     if (rootPane != null)
 346:       sb.append(rootPane);
 347:     sb.append(",rootPaneCheckingEnabled=").append(rootPaneCheckingEnabled);
 348:     return sb.toString();
 349:   }
 350: 
 351:   protected void processWindowEvent(WindowEvent e)
 352:   {
 353:     super.processWindowEvent(e);
 354:     switch (e.getID())
 355:       {
 356:       case WindowEvent.WINDOW_CLOSING:
 357:         {
 358:       switch (closeAction)
 359:         {
 360:         case EXIT_ON_CLOSE:
 361:           {
 362:         System.exit(0);
 363:         break;
 364:           }
 365:         case DISPOSE_ON_CLOSE:
 366:           {
 367:         dispose();
 368:         break;
 369:           }
 370:         case HIDE_ON_CLOSE:
 371:           {
 372:         setVisible(false);
 373:         break;
 374:           }
 375:         case DO_NOTHING_ON_CLOSE:
 376:           break;
 377:         }
 378:       break;
 379:         }
 380:       case WindowEvent.WINDOW_CLOSED:
 381:       case WindowEvent.WINDOW_OPENED:
 382:       case WindowEvent.WINDOW_ICONIFIED:
 383:       case WindowEvent.WINDOW_DEICONIFIED:
 384:       case WindowEvent.WINDOW_ACTIVATED:
 385:       case WindowEvent.WINDOW_DEACTIVATED:
 386:     break;
 387:       }
 388:   }
 389: 
 390:   /**
 391:    * Sets the default operation that is performed when this frame is closed.
 392:    * The default is <code>HIDE_ON_CLOSE</code>.  When 
 393:    * <code>EXIT_ON_CLOSE</code> is specified this method calls
 394:    * <code>SecurityManager.checkExit(0)</code> which might throw a
 395:    * <code>SecurityException</code>.
 396:    * 
 397:    * @param operation  a code for the operation (one of: 
 398:    *     {@link WindowConstants#DO_NOTHING_ON_CLOSE}, 
 399:    *     {@link WindowConstants#HIDE_ON_CLOSE}, 
 400:    *     {@link WindowConstants#DISPOSE_ON_CLOSE} and 
 401:    *     {@link WindowConstants#EXIT_ON_CLOSE}).
 402:    * 
 403:    * @throws IllegalArgumentException if <code>operation</code> is not one of
 404:    *     the specified codes.
 405:    * 
 406:    * @see #getDefaultCloseOperation()
 407:    */
 408:   public void setDefaultCloseOperation(int operation)
 409:   {
 410:     SecurityManager sm = System.getSecurityManager();
 411:     if (sm != null && operation == EXIT_ON_CLOSE)
 412:       sm.checkExit(0);
 413: 
 414:     if (operation != EXIT_ON_CLOSE && operation != DISPOSE_ON_CLOSE
 415:         && operation != HIDE_ON_CLOSE && operation != DO_NOTHING_ON_CLOSE)
 416:       throw new IllegalArgumentException("operation must be EXIT_ON_CLOSE, " 
 417:           + "HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or DO_NOTHING_ON_CLOSE");
 418: 
 419:     closeAction = operation;
 420:   }
 421: }