Source for javax.swing.JWindow

   1: /* JWindow.java --
   2:    Copyright (C) 2002, 2003, 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.BorderLayout;
  42: import java.awt.Component;
  43: import java.awt.Container;
  44: import java.awt.Dimension;
  45: import java.awt.Frame;
  46: import java.awt.Graphics;
  47: import java.awt.GraphicsConfiguration;
  48: import java.awt.LayoutManager;
  49: import java.awt.Window;
  50: import java.awt.event.KeyEvent;
  51: 
  52: import javax.accessibility.Accessible;
  53: import javax.accessibility.AccessibleContext;
  54: 
  55: /**
  56:  * Unlike JComponent derivatives, JWindow inherits from
  57:  * java.awt.Window. But also lets a look-and-feel component to its work.
  58:  *
  59:  * @author Ronald Veldema (rveldema@cs.vu.nl)
  60:  */
  61: public class JWindow extends Window implements Accessible, RootPaneContainer
  62: {
  63:   /**
  64:    * Provides accessibility support for <code>JWindow</code>.
  65:    */
  66:   protected class AccessibleJWindow extends Window.AccessibleAWTWindow
  67:   {
  68:     /**
  69:      * Creates a new instance of <code>AccessibleJWindow</code>.
  70:      */
  71:     public AccessibleJWindow()
  72:     {
  73:       super();
  74:       // Nothing to do here.
  75:     }
  76:   }
  77: 
  78:   private static final long serialVersionUID = 5420698392125238833L;
  79:   
  80:   protected JRootPane rootPane;
  81: 
  82:   /**
  83:    * @specnote rootPaneCheckingEnabled is false to comply with J2SE 5.0
  84:    */
  85:   protected boolean rootPaneCheckingEnabled = false;
  86: 
  87:   protected AccessibleContext accessibleContext;
  88: 
  89:   /**
  90:    * Tells us if we're in the initialization stage.
  91:    * If so, adds go to top-level Container, otherwise they go
  92:    * to the content pane for this container.
  93:    */
  94:   private boolean initStageDone = false;
  95: 
  96:   public JWindow()
  97:   {
  98:     super(SwingUtilities.getOwnerFrame());
  99:     windowInit();
 100:   }
 101: 
 102:   public JWindow(GraphicsConfiguration gc)
 103:   {
 104:     super(SwingUtilities.getOwnerFrame(), gc);
 105:     windowInit();
 106:   }
 107:   
 108:   public JWindow(Frame owner)
 109:   {
 110:     super(owner);
 111:     windowInit();
 112:   }
 113: 
 114:   public JWindow(Window owner)
 115:   {
 116:     super(owner);
 117:     windowInit();
 118:   }
 119: 
 120:   public JWindow(Window owner, GraphicsConfiguration gc)
 121:   {
 122:     super(owner, gc);
 123:     windowInit();
 124:   }
 125: 
 126:   protected void windowInit()
 127:   {
 128:     super.setLayout(new BorderLayout(1, 1));
 129:     getRootPane(); // will do set/create
 130:     // Now we're done init stage, adds and layouts go to content pane.
 131:     initStageDone = true;
 132:   }
 133: 
 134:   public Dimension getPreferredSize()
 135:   {
 136:     return super.getPreferredSize();
 137:   }
 138: 
 139:   public void setLayout(LayoutManager manager)
 140:   {
 141:     // Check if we're in initialization stage.  If so, call super.setLayout
 142:     // otherwise, valid calls go to the content pane.
 143:     if (initStageDone)
 144:       {
 145:         if (isRootPaneCheckingEnabled())
 146:           throw new Error("Cannot set layout. Use getContentPane().setLayout()"
 147:                            + " instead.");
 148:         getContentPane().setLayout(manager);
 149:       }
 150:     else
 151:       super.setLayout(manager);
 152:   }
 153: 
 154:   public void setLayeredPane(JLayeredPane layeredPane)
 155:   {
 156:     getRootPane().setLayeredPane(layeredPane);
 157:   }
 158: 
 159:   public JLayeredPane getLayeredPane()
 160:   {
 161:     return getRootPane().getLayeredPane();
 162:   }
 163: 
 164:   public JRootPane getRootPane()
 165:   {
 166:     if (rootPane == null)
 167:       setRootPane(createRootPane());
 168:     return rootPane;
 169:   }
 170: 
 171:   protected void setRootPane(JRootPane root)
 172:   {
 173:     if (rootPane != null)
 174:       remove(rootPane);
 175: 
 176:     rootPane = root;
 177:     add(rootPane, BorderLayout.CENTER);
 178:   }
 179: 
 180:   protected JRootPane createRootPane()
 181:   {
 182:     return new JRootPane();
 183:   }
 184: 
 185:   public Container getContentPane()
 186:   {
 187:     return getRootPane().getContentPane();
 188:   }
 189: 
 190:   public void setContentPane(Container contentPane)
 191:   {
 192:     getRootPane().setContentPane(contentPane);
 193:   }
 194: 
 195:   public Component getGlassPane()
 196:   {
 197:     return getRootPane().getGlassPane();
 198:   }
 199: 
 200:   public void setGlassPane(Component glassPane)
 201:   {
 202:     getRootPane().setGlassPane(glassPane);
 203:   }
 204: 
 205: 
 206:   protected void addImpl(Component comp, Object constraints, int index)
 207:   {
 208:     // If we're adding in the initialization stage use super.add.
 209:     // otherwise pass the add onto the content pane.
 210:     if (!initStageDone)
 211:       super.addImpl(comp, constraints, index);
 212:     else
 213:       {
 214:         if (isRootPaneCheckingEnabled())
 215:           throw new Error("Do not use add() on JWindow directly. Use "
 216:                           + "getContentPane().add() instead");
 217:         getContentPane().add(comp, constraints, index);
 218:       }
 219:   }
 220: 
 221:   public void remove(Component comp)
 222:   {
 223:     // If we're removing the root pane, use super.remove.  Otherwise
 224:     // pass it on to the content pane instead.
 225:     if (comp == rootPane)
 226:       super.remove(rootPane);
 227:     else
 228:       getContentPane().remove(comp);
 229:   }
 230: 
 231:   protected boolean isRootPaneCheckingEnabled()
 232:   {
 233:     return rootPaneCheckingEnabled;
 234:   }
 235: 
 236:   protected void setRootPaneCheckingEnabled(boolean enabled)
 237:   {
 238:     rootPaneCheckingEnabled = enabled;
 239:   }
 240: 
 241:   public void update(Graphics g)
 242:   {
 243:     paint(g);
 244:   }
 245: 
 246:   protected void processKeyEvent(KeyEvent e)
 247:   {
 248:     super.processKeyEvent(e);
 249:   }
 250: 
 251:   public AccessibleContext getAccessibleContext()
 252:   {
 253:     if (accessibleContext == null)
 254:       accessibleContext = new AccessibleJWindow();
 255:     return accessibleContext;
 256:   }
 257: 
 258:   protected String paramString()
 259:   {
 260:     return "JWindow";
 261:   }
 262: }