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:   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:     // We're now done the init stage.
 154:     setRootPaneCheckingEnabled(true);
 155:   }
 156: 
 157:   public Dimension getPreferredSize()
 158:   {
 159:     return super.getPreferredSize();
 160:   }
 161: 
 162:   public JMenuBar getJMenuBar()
 163:   {
 164:     return getRootPane().getJMenuBar();
 165:   }
 166: 
 167:   public void setJMenuBar(JMenuBar menubar)
 168:   {
 169:     getRootPane().setJMenuBar(menubar);
 170:   }
 171: 
 172:   public void setLayout(LayoutManager manager)
 173:   {
 174:     // Check if we're in initialization stage.  If so, call super.setLayout
 175:     // otherwise, valid calls go to the content pane.
 176:     if (isRootPaneCheckingEnabled())
 177:       getContentPane().setLayout(manager);
 178:     else
 179:       super.setLayout(manager);
 180:   }
 181: 
 182:   public void setLayeredPane(JLayeredPane layeredPane)
 183:   {
 184:     getRootPane().setLayeredPane(layeredPane);
 185:   }
 186: 
 187:   public JLayeredPane getLayeredPane()
 188:   {
 189:     return getRootPane().getLayeredPane();
 190:   }
 191: 
 192:   public JRootPane getRootPane()
 193:   {
 194:     if (rootPane == null)
 195:       setRootPane(createRootPane());
 196:     return rootPane;
 197:   }
 198: 
 199:   protected void setRootPane(JRootPane root)
 200:   {
 201:     if (rootPane != null)
 202:       remove(rootPane);
 203: 
 204:     rootPane = root;
 205:     add(rootPane, BorderLayout.CENTER);
 206:   }
 207: 
 208:   protected JRootPane createRootPane()
 209:   {
 210:     return new JRootPane();
 211:   }
 212: 
 213:   public Container getContentPane()
 214:   {
 215:     return getRootPane().getContentPane();
 216:   }
 217: 
 218:   public void setContentPane(Container contentPane)
 219:   {
 220:     getRootPane().setContentPane(contentPane);
 221:   }
 222: 
 223:   public Component getGlassPane()
 224:   {
 225:     return getRootPane().getGlassPane();
 226:   }
 227: 
 228:   public void setGlassPane(Component glassPane)
 229:   {
 230:     getRootPane().setGlassPane(glassPane);
 231:   }
 232: 
 233:   protected void addImpl(Component comp, Object constraints, int index)
 234:   {
 235:     // If we're adding in the initialization stage use super.add.
 236:     // Otherwise pass the add onto the content pane.
 237:     if (isRootPaneCheckingEnabled())
 238:       getContentPane().add(comp,constraints,index);
 239:     else
 240:       super.addImpl(comp, constraints, index);
 241:   }
 242: 
 243:   public void remove(Component comp)
 244:   {
 245:     // If we're removing the root pane, use super.remove. Otherwise
 246:     // pass it on to the content pane instead.
 247:     if (comp==rootPane)
 248:       super.remove(rootPane);
 249:     else
 250:       getContentPane().remove(comp);
 251:   }
 252: 
 253:   protected boolean isRootPaneCheckingEnabled()
 254:   {
 255:     return rootPaneCheckingEnabled;
 256:   }
 257: 
 258:   protected void setRootPaneCheckingEnabled(boolean enabled)
 259:   {
 260:     rootPaneCheckingEnabled = enabled;
 261:   }
 262: 
 263:   public void update(Graphics g)
 264:   {
 265:     paint(g);
 266:   }
 267: 
 268:   protected void processKeyEvent(KeyEvent e)
 269:   {
 270:     super.processKeyEvent(e);
 271:   }
 272: 
 273:   public static void setDefaultLookAndFeelDecorated(boolean decorated)
 274:   {
 275:     defaultLookAndFeelDecorated = decorated;
 276:   }
 277: 
 278:   public static boolean isDefaultLookAndFeelDecorated()
 279:   {
 280:     return defaultLookAndFeelDecorated;
 281:   }
 282: 
 283:   public AccessibleContext getAccessibleContext()
 284:   {
 285:     if (accessibleContext == null)
 286:       accessibleContext = new AccessibleJFrame();
 287:     return accessibleContext;
 288:   }
 289: 
 290:   public int getDefaultCloseOperation()
 291:   {
 292:     return close_action;
 293:   }
 294: 
 295:   protected String paramString()
 296:   {
 297:     return "JFrame";
 298:   }
 299: 
 300:   protected void processWindowEvent(WindowEvent e)
 301:   {
 302:     super.processWindowEvent(e);
 303:     switch (e.getID())
 304:       {
 305:       case WindowEvent.WINDOW_CLOSING:
 306:         {
 307:       switch (close_action)
 308:         {
 309:         case EXIT_ON_CLOSE:
 310:           {
 311:         System.exit(0);
 312:         break;
 313:           }
 314:         case DISPOSE_ON_CLOSE:
 315:           {
 316:         dispose();
 317:         break;
 318:           }
 319:         case HIDE_ON_CLOSE:
 320:           {
 321:         setVisible(false);
 322:         break;
 323:           }
 324:         case DO_NOTHING_ON_CLOSE:
 325:           break;
 326:         }
 327:       break;
 328:         }
 329:       case WindowEvent.WINDOW_CLOSED:
 330:       case WindowEvent.WINDOW_OPENED:
 331:       case WindowEvent.WINDOW_ICONIFIED:
 332:       case WindowEvent.WINDOW_DEICONIFIED:
 333:       case WindowEvent.WINDOW_ACTIVATED:
 334:       case WindowEvent.WINDOW_DEACTIVATED:
 335:     break;
 336:       }
 337:   }
 338: 
 339:   /**
 340:    * Defines what happens when this frame is closed. Can be one off
 341:    * <code>EXIT_ON_CLOSE</code>,
 342:    * <code>DISPOSE_ON_CLOSE</code>,
 343:    * <code>HIDE_ON_CLOSE</code> or
 344:    * <code>DO_NOTHING_ON_CLOSE</code>.
 345:    * The default is <code>HIDE_ON_CLOSE</code>.
 346:    * When <code>EXIT_ON_CLOSE</code> is specified this method calls
 347:    * <code>SecurityManager.checkExit(0)</code> which might throw a
 348:    * <code>SecurityException</code>. When the specified operation is
 349:    * not one of the above a <code>IllegalArgumentException</code> is
 350:    * thrown.
 351:    */
 352:   public void setDefaultCloseOperation(int operation)
 353:   {
 354:     SecurityManager sm = System.getSecurityManager();
 355:     if (sm != null && operation == EXIT_ON_CLOSE)
 356:       sm.checkExit(0);
 357: 
 358:     if (operation != EXIT_ON_CLOSE && operation != DISPOSE_ON_CLOSE
 359:         && operation != HIDE_ON_CLOSE && operation != DO_NOTHING_ON_CLOSE)
 360:       throw new IllegalArgumentException("defaultCloseOperation must be EXIT_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or DO_NOTHING_ON_CLOSE");
 361: 
 362:     close_action = operation;
 363:   }
 364: }