1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 import java.applet.Applet;
  30 import java.beans.PropertyChangeListener;
  31 import java.util.Locale;
  32 import java.util.Vector;
  33 import java.io.Serializable;
  34 import javax.accessibility.*;
  35 
  36 /**
  37  * An extended version of <code>java.applet.Applet</code> that adds support for
  38  * the JFC/Swing component architecture.
  39  * You can find task-oriented documentation about using <code>JApplet</code>
  40  * in <em>The Java Tutorial</em>,
  41  * in the section
  42  * <a
  43  href="http://docs.oracle.com/javase/tutorial/uiswing/components/applet.html">How to Make Applets</a>.
  44  * <p>
  45  * The <code>JApplet</code> class is slightly incompatible with
  46  * <code>java.applet.Applet</code>.  <code>JApplet</code> contains a
  47  * <code>JRootPane</code> as its only child.  The <code>contentPane</code>
  48  * should be the parent of any children of the <code>JApplet</code>.
  49  * As a convenience, the {@code add}, {@code remove}, and {@code setLayout}
  50  * methods of this class are overridden, so that they delegate calls
  51  * to the corresponding methods of the {@code ContentPane}.
  52  * For example, you can add a child component to an applet as follows:
  53  * <pre>
  54  *       applet.add(child);
  55  * </pre>
  56  *
  57  * And the child will be added to the <code>contentPane</code>.
  58  * The <code>contentPane</code> will always be non-<code>null</code>.
  59  * Attempting to set it to <code>null</code> will cause the
  60  * <code>JApplet</code> to throw an exception. The default
  61  * <code>contentPane</code> will have a <code>BorderLayout</code>
  62  * manager set on it.
  63  * Refer to {@link javax.swing.RootPaneContainer}
  64  * for details on adding, removing and setting the <code>LayoutManager</code>
  65  * of a <code>JApplet</code>.
  66  * <p>
  67  * Please see the <code>JRootPane</code> documentation for a
  68  * complete description of the <code>contentPane</code>, <code>glassPane</code>,
  69  * and <code>layeredPane</code> properties.
  70  * <p>
  71  * <strong>Warning:</strong> Swing is not thread safe. For more
  72  * information see <a
  73  * href="package-summary.html#threading">Swing's Threading
  74  * Policy</a>.
  75  * <p>
  76  * <strong>Warning:</strong>
  77  * Serialized objects of this class will not be compatible with
  78  * future Swing releases. The current serialization support is
  79  * appropriate for short term storage or RMI between applications running
  80  * the same version of Swing.  As of 1.4, support for long term storage
  81  * of all JavaBeans&trade;
  82  * has been added to the <code>java.beans</code> package.
  83  * Please see {@link java.beans.XMLEncoder}.
  84  *
  85  * @see javax.swing.RootPaneContainer
  86  * @beaninfo
  87  *      attribute: isContainer true
  88  *      attribute: containerDelegate getContentPane
  89  *    description: Swing's Applet subclass.
  90  *
  91  * @author Arnaud Weber
  92  */
  93 @SuppressWarnings("serial") // Same-version serialization only
  94 public class JApplet extends Applet implements Accessible,
  95                                                RootPaneContainer,
  96                                TransferHandler.HasGetTransferHandler
  97 {
  98     /**
  99      * @see #getRootPane
 100      * @see #setRootPane
 101      */
 102     protected JRootPane rootPane;
 103 
 104     /**
 105      * If true then calls to <code>add</code> and <code>setLayout</code>
 106      * will be forwarded to the <code>contentPane</code>. This is initially
 107      * false, but is set to true when the <code>JApplet</code> is constructed.
 108      *
 109      * @see #isRootPaneCheckingEnabled
 110      * @see #setRootPaneCheckingEnabled
 111      * @see javax.swing.RootPaneContainer
 112      */
 113     protected boolean rootPaneCheckingEnabled = false;
 114 
 115     /**
 116      * The <code>TransferHandler</code> for this applet.
 117      */
 118     private TransferHandler transferHandler;
 119 
 120     /**
 121      * Creates a swing applet instance.
 122      * <p>
 123      * This constructor sets the component's locale property to the value
 124      * returned by <code>JComponent.getDefaultLocale</code>.
 125      *
 126      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 127      * returns true.
 128      * @see java.awt.GraphicsEnvironment#isHeadless
 129      * @see JComponent#getDefaultLocale
 130      */
 131     public JApplet() throws HeadlessException {
 132         super();
 133         // Check the timerQ and restart if necessary.
 134         TimerQueue q = TimerQueue.sharedInstance();
 135         if(q != null) {
 136             q.startIfNeeded();
 137         }
 138 
 139         /* Workaround for bug 4155072.  The shared double buffer image
 140          * may hang on to a reference to this applet; unfortunately
 141          * Image.getGraphics() will continue to call JApplet.getForeground()
 142          * and getBackground() even after this applet has been destroyed.
 143          * So we ensure that these properties are non-null here.
 144          */
 145         setForeground(Color.black);
 146         setBackground(Color.white);
 147 
 148         setLocale( JComponent.getDefaultLocale() );
 149         setLayout(new BorderLayout());
 150         setRootPane(createRootPane());
 151         setRootPaneCheckingEnabled(true);
 152 
 153         setFocusTraversalPolicyProvider(true);
 154         sun.awt.SunToolkit.checkAndSetPolicy(this);
 155 
 156         enableEvents(AWTEvent.KEY_EVENT_MASK);
 157     }
 158 
 159 
 160     /** Called by the constructor methods to create the default rootPane. */
 161     protected JRootPane createRootPane() {
 162         JRootPane rp = new JRootPane();
 163         // NOTE: this uses setOpaque vs LookAndFeel.installProperty as there
 164         // is NO reason for the RootPane not to be opaque. For painting to
 165         // work the contentPane must be opaque, therefor the RootPane can
 166         // also be opaque.
 167         rp.setOpaque(true);
 168         return rp;
 169     }
 170 
 171     /**
 172      * Sets the {@code transferHandler} property, which is a mechanism to
 173      * support transfer of data into this component. Use {@code null}
 174      * if the component does not support data transfer operations.
 175      * <p>
 176      * If the system property {@code suppressSwingDropSupport} is {@code false}
 177      * (the default) and the current drop target on this component is either
 178      * {@code null} or not a user-set drop target, this method will change the
 179      * drop target as follows: If {@code newHandler} is {@code null} it will
 180      * clear the drop target. If not {@code null} it will install a new
 181      * {@code DropTarget}.
 182      * <p>
 183      * Note: When used with {@code JApplet}, {@code TransferHandler} only
 184      * provides data import capability, as the data export related methods
 185      * are currently typed to {@code JComponent}.
 186      * <p>
 187      * Please see
 188      * <a href="http://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html">
 189      * How to Use Drag and Drop and Data Transfer</a>, a section in
 190      * <em>The Java Tutorial</em>, for more information.
 191      *
 192      * @param newHandler the new {@code TransferHandler}
 193      *
 194      * @see TransferHandler
 195      * @see #getTransferHandler
 196      * @see java.awt.Component#setDropTarget
 197      * @since 1.6
 198      *
 199      * @beaninfo
 200      *        bound: true
 201      *       hidden: true
 202      *  description: Mechanism for transfer of data into the component
 203      */
 204     public void setTransferHandler(TransferHandler newHandler) {
 205         TransferHandler oldHandler = transferHandler;
 206         transferHandler = newHandler;
 207         SwingUtilities.installSwingDropTargetAsNecessary(this, transferHandler);
 208         firePropertyChange("transferHandler", oldHandler, newHandler);
 209     }
 210 
 211     /**
 212      * Gets the <code>transferHandler</code> property.
 213      *
 214      * @return the value of the <code>transferHandler</code> property
 215      *
 216      * @see TransferHandler
 217      * @see #setTransferHandler
 218      * @since 1.6
 219      */
 220     public TransferHandler getTransferHandler() {
 221         return transferHandler;
 222     }
 223 
 224     /**
 225      * Just calls <code>paint(g)</code>.  This method was overridden to
 226      * prevent an unnecessary call to clear the background.
 227      */
 228     public void update(Graphics g) {
 229         paint(g);
 230     }
 231 
 232    /**
 233     * Sets the menubar for this applet.
 234     * @param menuBar the menubar being placed in the applet
 235     *
 236     * @see #getJMenuBar
 237     *
 238     * @beaninfo
 239     *      hidden: true
 240     * description: The menubar for accessing pulldown menus from this applet.
 241     */
 242     public void setJMenuBar(JMenuBar menuBar) {
 243         getRootPane().setMenuBar(menuBar);
 244     }
 245 
 246    /**
 247     * Returns the menubar set on this applet.
 248     *
 249     * @see #setJMenuBar
 250     */
 251     public JMenuBar getJMenuBar() {
 252         return getRootPane().getMenuBar();
 253     }
 254 
 255 
 256     /**
 257      * Returns whether calls to <code>add</code> and
 258      * <code>setLayout</code> are forwarded to the <code>contentPane</code>.
 259      *
 260      * @return true if <code>add</code> and <code>setLayout</code>
 261      *         are forwarded; false otherwise
 262      *
 263      * @see #addImpl
 264      * @see #setLayout
 265      * @see #setRootPaneCheckingEnabled
 266      * @see javax.swing.RootPaneContainer
 267      */
 268     protected boolean isRootPaneCheckingEnabled() {
 269         return rootPaneCheckingEnabled;
 270     }
 271 
 272 
 273     /**
 274      * Sets whether calls to <code>add</code> and
 275      * <code>setLayout</code> are forwarded to the <code>contentPane</code>.
 276      *
 277      * @param enabled  true if <code>add</code> and <code>setLayout</code>
 278      *        are forwarded, false if they should operate directly on the
 279      *        <code>JApplet</code>.
 280      *
 281      * @see #addImpl
 282      * @see #setLayout
 283      * @see #isRootPaneCheckingEnabled
 284      * @see javax.swing.RootPaneContainer
 285      * @beaninfo
 286      *      hidden: true
 287      * description: Whether the add and setLayout methods are forwarded
 288      */
 289     protected void setRootPaneCheckingEnabled(boolean enabled) {
 290         rootPaneCheckingEnabled = enabled;
 291     }
 292 
 293 
 294     /**
 295      * Adds the specified child <code>Component</code>.
 296      * This method is overridden to conditionally forward calls to the
 297      * <code>contentPane</code>.
 298      * By default, children are added to the <code>contentPane</code> instead
 299      * of the frame, refer to {@link javax.swing.RootPaneContainer} for
 300      * details.
 301      *
 302      * @param comp the component to be enhanced
 303      * @param constraints the constraints to be respected
 304      * @param index the index
 305      * @exception IllegalArgumentException if <code>index</code> is invalid
 306      * @exception IllegalArgumentException if adding the container's parent
 307      *                  to itself
 308      * @exception IllegalArgumentException if adding a window to a container
 309      *
 310      * @see #setRootPaneCheckingEnabled
 311      * @see javax.swing.RootPaneContainer
 312      */
 313     protected void addImpl(Component comp, Object constraints, int index)
 314     {
 315         if(isRootPaneCheckingEnabled()) {
 316             getContentPane().add(comp, constraints, index);
 317         }
 318         else {
 319             super.addImpl(comp, constraints, index);
 320         }
 321     }
 322 
 323     /**
 324      * Removes the specified component from the container. If
 325      * <code>comp</code> is not the <code>rootPane</code>, this will forward
 326      * the call to the <code>contentPane</code>. This will do nothing if
 327      * <code>comp</code> is not a child of the <code>JFrame</code> or
 328      * <code>contentPane</code>.
 329      *
 330      * @param comp the component to be removed
 331      * @throws NullPointerException if <code>comp</code> is null
 332      * @see #add
 333      * @see javax.swing.RootPaneContainer
 334      */
 335     public void remove(Component comp) {
 336         if (comp == rootPane) {
 337             super.remove(comp);
 338         } else {
 339             getContentPane().remove(comp);
 340         }
 341     }
 342 
 343 
 344     /**
 345      * Sets the <code>LayoutManager</code>.
 346      * Overridden to conditionally forward the call to the
 347      * <code>contentPane</code>.
 348      * Refer to {@link javax.swing.RootPaneContainer} for
 349      * more information.
 350      *
 351      * @param manager the <code>LayoutManager</code>
 352      * @see #setRootPaneCheckingEnabled
 353      * @see javax.swing.RootPaneContainer
 354      */
 355     public void setLayout(LayoutManager manager) {
 356         if(isRootPaneCheckingEnabled()) {
 357             getContentPane().setLayout(manager);
 358         }
 359         else {
 360             super.setLayout(manager);
 361         }
 362     }
 363 
 364 
 365     /**
 366      * Returns the rootPane object for this applet.
 367      *
 368      * @see #setRootPane
 369      * @see RootPaneContainer#getRootPane
 370      */
 371     public JRootPane getRootPane() {
 372         return rootPane;
 373     }
 374 
 375 
 376     /**
 377      * Sets the rootPane property.  This method is called by the constructor.
 378      * @param root the rootPane object for this applet
 379      *
 380      * @see #getRootPane
 381      *
 382      * @beaninfo
 383      *   hidden: true
 384      * description: the RootPane object for this applet.
 385      */
 386     protected void setRootPane(JRootPane root) {
 387         if(rootPane != null) {
 388             remove(rootPane);
 389         }
 390         rootPane = root;
 391         if(rootPane != null) {
 392             boolean checkingEnabled = isRootPaneCheckingEnabled();
 393             try {
 394                 setRootPaneCheckingEnabled(false);
 395                 add(rootPane, BorderLayout.CENTER);
 396             }
 397             finally {
 398                 setRootPaneCheckingEnabled(checkingEnabled);
 399             }
 400         }
 401     }
 402 
 403 
 404     /**
 405      * Returns the contentPane object for this applet.
 406      *
 407      * @see #setContentPane
 408      * @see RootPaneContainer#getContentPane
 409      */
 410     public Container getContentPane() {
 411         return getRootPane().getContentPane();
 412     }
 413 
 414    /**
 415      * Sets the contentPane property.  This method is called by the constructor.
 416      * @param contentPane the contentPane object for this applet
 417      *
 418      * @exception java.awt.IllegalComponentStateException (a runtime
 419      *            exception) if the content pane parameter is null
 420      * @see #getContentPane
 421      * @see RootPaneContainer#setContentPane
 422      *
 423      * @beaninfo
 424      *     hidden: true
 425      *     description: The client area of the applet where child
 426      *                  components are normally inserted.
 427      */
 428     public void setContentPane(Container contentPane) {
 429         getRootPane().setContentPane(contentPane);
 430     }
 431 
 432     /**
 433      * Returns the layeredPane object for this applet.
 434      *
 435      * @exception java.awt.IllegalComponentStateException (a runtime
 436      *            exception) if the layered pane parameter is null
 437      * @see #setLayeredPane
 438      * @see RootPaneContainer#getLayeredPane
 439      */
 440     public JLayeredPane getLayeredPane() {
 441         return getRootPane().getLayeredPane();
 442     }
 443 
 444     /**
 445      * Sets the layeredPane property.  This method is called by the constructor.
 446      * @param layeredPane the layeredPane object for this applet
 447      *
 448      * @see #getLayeredPane
 449      * @see RootPaneContainer#setLayeredPane
 450      *
 451      * @beaninfo
 452      *     hidden: true
 453      *     description: The pane which holds the various applet layers.
 454      */
 455     public void setLayeredPane(JLayeredPane layeredPane) {
 456         getRootPane().setLayeredPane(layeredPane);
 457     }
 458 
 459     /**
 460      * Returns the glassPane object for this applet.
 461      *
 462      * @see #setGlassPane
 463      * @see RootPaneContainer#getGlassPane
 464      */
 465     public Component getGlassPane() {
 466         return getRootPane().getGlassPane();
 467     }
 468 
 469     /**
 470      * Sets the glassPane property.
 471      * This method is called by the constructor.
 472      * @param glassPane the glassPane object for this applet
 473      *
 474      * @see #getGlassPane
 475      * @see RootPaneContainer#setGlassPane
 476      *
 477      * @beaninfo
 478      *     hidden: true
 479      *     description: A transparent pane used for menu rendering.
 480      */
 481     public void setGlassPane(Component glassPane) {
 482         getRootPane().setGlassPane(glassPane);
 483     }
 484 
 485     /**
 486      * {@inheritDoc}
 487      *
 488      * @since 1.6
 489      */
 490     public Graphics getGraphics() {
 491         JComponent.getGraphicsInvoked(this);
 492         return super.getGraphics();
 493     }
 494 
 495     /**
 496      * Repaints the specified rectangle of this component within
 497      * <code>time</code> milliseconds.  Refer to <code>RepaintManager</code>
 498      * for details on how the repaint is handled.
 499      *
 500      * @param     time   maximum time in milliseconds before update
 501      * @param     x    the <i>x</i> coordinate
 502      * @param     y    the <i>y</i> coordinate
 503      * @param     width    the width
 504      * @param     height   the height
 505      * @see       RepaintManager
 506      * @since     1.6
 507      */
 508     public void repaint(long time, int x, int y, int width, int height) {
 509         if (RepaintManager.HANDLE_TOP_LEVEL_PAINT) {
 510             RepaintManager.currentManager(this).addDirtyRegion(
 511                               this, x, y, width, height);
 512         }
 513         else {
 514             super.repaint(time, x, y, width, height);
 515         }
 516     }
 517 
 518     /**
 519      * Returns a string representation of this JApplet. This method
 520      * is intended to be used only for debugging purposes, and the
 521      * content and format of the returned string may vary between
 522      * implementations. The returned string may be empty but may not
 523      * be <code>null</code>.
 524      *
 525      * @return  a string representation of this JApplet.
 526      */
 527     protected String paramString() {
 528         String rootPaneString = (rootPane != null ?
 529                                  rootPane.toString() : "");
 530         String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ?
 531                                                 "true" : "false");
 532 
 533         return super.paramString() +
 534         ",rootPane=" + rootPaneString +
 535         ",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString;
 536     }
 537 
 538 
 539 
 540 /////////////////
 541 // Accessibility support
 542 ////////////////
 543 
 544     protected AccessibleContext accessibleContext = null;
 545 
 546     /**
 547      * Gets the AccessibleContext associated with this JApplet.
 548      * For JApplets, the AccessibleContext takes the form of an
 549      * AccessibleJApplet.
 550      * A new AccessibleJApplet instance is created if necessary.
 551      *
 552      * @return an AccessibleJApplet that serves as the
 553      *         AccessibleContext of this JApplet
 554      */
 555     public AccessibleContext getAccessibleContext() {
 556         if (accessibleContext == null) {
 557             accessibleContext = new AccessibleJApplet();
 558         }
 559         return accessibleContext;
 560     }
 561 
 562     /**
 563      * This class implements accessibility support for the
 564      * <code>JApplet</code> class.
 565      */
 566     protected class AccessibleJApplet extends AccessibleApplet {
 567         // everything moved to new parent, AccessibleApplet
 568     }
 569 }