Source for javax.swing.JApplet

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