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