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