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