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™ 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 * @since 1.2 93 */ 94 @SuppressWarnings("serial") // Same-version serialization only 95 public class JApplet extends Applet implements Accessible, 96 RootPaneContainer, 97 TransferHandler.HasGetTransferHandler 98 { 99 /** 100 * @see #getRootPane 101 * @see #setRootPane 102 */ 103 protected JRootPane rootPane; 104 105 /** 106 * If true then calls to <code>add</code> and <code>setLayout</code> 107 * will be forwarded to the <code>contentPane</code>. This is initially 108 * false, but is set to true when the <code>JApplet</code> is constructed. 109 * 110 * @see #isRootPaneCheckingEnabled 111 * @see #setRootPaneCheckingEnabled 112 * @see javax.swing.RootPaneContainer 113 */ 114 protected boolean rootPaneCheckingEnabled = false; 115 116 /** 117 * The <code>TransferHandler</code> for this applet. 118 */ 119 private TransferHandler transferHandler; 120 121 /** 122 * Creates a swing applet instance. 123 * <p> 124 * This constructor sets the component's locale property to the value 125 * returned by <code>JComponent.getDefaultLocale</code>. 126 * 127 * @exception HeadlessException if GraphicsEnvironment.isHeadless() 128 * returns true. 129 * @see java.awt.GraphicsEnvironment#isHeadless 130 * @see JComponent#getDefaultLocale 131 */ 132 public JApplet() throws HeadlessException { 133 super(); 134 // Check the timerQ and restart if necessary. 135 TimerQueue q = TimerQueue.sharedInstance(); 136 if(q != null) { 137 q.startIfNeeded(); 138 } 139 140 /* Workaround for bug 4155072. The shared double buffer image 141 * may hang on to a reference to this applet; unfortunately 142 * Image.getGraphics() will continue to call JApplet.getForeground() 143 * and getBackground() even after this applet has been destroyed. 144 * So we ensure that these properties are non-null here. 145 */ 146 setForeground(Color.black); 147 setBackground(Color.white); 148 149 setLocale( JComponent.getDefaultLocale() ); 150 setLayout(new BorderLayout()); 151 setRootPane(createRootPane()); 152 setRootPaneCheckingEnabled(true); 153 154 setFocusTraversalPolicyProvider(true); 155 sun.awt.SunToolkit.checkAndSetPolicy(this); 156 157 enableEvents(AWTEvent.KEY_EVENT_MASK); 158 } 159 160 /** 161 * Called by the constructor methods to create the default rootPane. 162 * 163 * @return a new {@code JRootPane} 164 */ 165 protected JRootPane createRootPane() { 166 JRootPane rp = new JRootPane(); 167 // NOTE: this uses setOpaque vs LookAndFeel.installProperty as there 168 // is NO reason for the RootPane not to be opaque. For painting to 169 // work the contentPane must be opaque, therefor the RootPane can 170 // also be opaque. 171 rp.setOpaque(true); 172 return rp; 173 } 174 175 /** 176 * Sets the {@code transferHandler} property, which is a mechanism to 177 * support transfer of data into this component. Use {@code null} 178 * if the component does not support data transfer operations. 179 * <p> 180 * If the system property {@code suppressSwingDropSupport} is {@code false} 181 * (the default) and the current drop target on this component is either 182 * {@code null} or not a user-set drop target, this method will change the 183 * drop target as follows: If {@code newHandler} is {@code null} it will 184 * clear the drop target. If not {@code null} it will install a new 185 * {@code DropTarget}. 186 * <p> 187 * Note: When used with {@code JApplet}, {@code TransferHandler} only 188 * provides data import capability, as the data export related methods 189 * are currently typed to {@code JComponent}. 190 * <p> 191 * Please see 192 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html"> 193 * How to Use Drag and Drop and Data Transfer</a>, a section in 194 * <em>The Java Tutorial</em>, for more information. 195 * 196 * @param newHandler the new {@code TransferHandler} 197 * 198 * @see TransferHandler 199 * @see #getTransferHandler 200 * @see java.awt.Component#setDropTarget 201 * @since 1.6 202 * 203 * @beaninfo 204 * bound: true 205 * hidden: true 206 * description: Mechanism for transfer of data into the component 207 */ 208 public void setTransferHandler(TransferHandler newHandler) { 209 TransferHandler oldHandler = transferHandler; 210 transferHandler = newHandler; 211 SwingUtilities.installSwingDropTargetAsNecessary(this, transferHandler); 212 firePropertyChange("transferHandler", oldHandler, newHandler); 213 } 214 215 /** 216 * Gets the <code>transferHandler</code> property. 217 * 218 * @return the value of the <code>transferHandler</code> property 219 * 220 * @see TransferHandler 221 * @see #setTransferHandler 222 * @since 1.6 223 */ 224 public TransferHandler getTransferHandler() { 225 return transferHandler; 226 } 227 228 /** 229 * Just calls <code>paint(g)</code>. This method was overridden to 230 * prevent an unnecessary call to clear the background. 231 */ 232 public void update(Graphics g) { 233 paint(g); 234 } 235 236 /** 237 * Sets the menubar for this applet. 238 * @param menuBar the menubar being placed in the applet 239 * 240 * @see #getJMenuBar 241 * 242 * @beaninfo 243 * hidden: true 244 * description: The menubar for accessing pulldown menus from this applet. 245 */ 246 @SuppressWarnings("deprecation") 247 public void setJMenuBar(JMenuBar menuBar) { 248 getRootPane().setMenuBar(menuBar); 249 } 250 251 /** 252 * Returns the menubar set on this applet. 253 * 254 * @return the menubar set on this applet 255 * @see #setJMenuBar 256 */ 257 @SuppressWarnings("deprecation") 258 public JMenuBar getJMenuBar() { 259 return getRootPane().getMenuBar(); 260 } 261 262 263 /** 264 * Returns whether calls to <code>add</code> and 265 * <code>setLayout</code> are forwarded to the <code>contentPane</code>. 266 * 267 * @return true if <code>add</code> and <code>setLayout</code> 268 * are forwarded; false otherwise 269 * 270 * @see #addImpl 271 * @see #setLayout 272 * @see #setRootPaneCheckingEnabled 273 * @see javax.swing.RootPaneContainer 274 */ 275 protected boolean isRootPaneCheckingEnabled() { 276 return rootPaneCheckingEnabled; 277 } 278 279 280 /** 281 * Sets whether calls to <code>add</code> and 282 * <code>setLayout</code> are forwarded to the <code>contentPane</code>. 283 * 284 * @param enabled true if <code>add</code> and <code>setLayout</code> 285 * are forwarded, false if they should operate directly on the 286 * <code>JApplet</code>. 287 * 288 * @see #addImpl 289 * @see #setLayout 290 * @see #isRootPaneCheckingEnabled 291 * @see javax.swing.RootPaneContainer 292 * @beaninfo 293 * hidden: true 294 * description: Whether the add and setLayout methods are forwarded 295 */ 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 public JRootPane getRootPane() { 379 return rootPane; 380 } 381 382 383 /** 384 * Sets the rootPane property. This method is called by the constructor. 385 * @param root the rootPane object for this applet 386 * 387 * @see #getRootPane 388 * 389 * @beaninfo 390 * hidden: true 391 * description: the RootPane object for this applet. 392 */ 393 protected void setRootPane(JRootPane root) { 394 if(rootPane != null) { 395 remove(rootPane); 396 } 397 rootPane = root; 398 if(rootPane != null) { 399 boolean checkingEnabled = isRootPaneCheckingEnabled(); 400 try { 401 setRootPaneCheckingEnabled(false); 402 add(rootPane, BorderLayout.CENTER); 403 } 404 finally { 405 setRootPaneCheckingEnabled(checkingEnabled); 406 } 407 } 408 } 409 410 411 /** 412 * Returns the contentPane object for this applet. 413 * 414 * @see #setContentPane 415 * @see RootPaneContainer#getContentPane 416 */ 417 public Container getContentPane() { 418 return getRootPane().getContentPane(); 419 } 420 421 /** 422 * Sets the contentPane property. This method is called by the constructor. 423 * @param contentPane the contentPane object for this applet 424 * 425 * @exception java.awt.IllegalComponentStateException (a runtime 426 * exception) if the content pane parameter is null 427 * @see #getContentPane 428 * @see RootPaneContainer#setContentPane 429 * 430 * @beaninfo 431 * hidden: true 432 * description: The client area of the applet where child 433 * components are normally inserted. 434 */ 435 public void setContentPane(Container contentPane) { 436 getRootPane().setContentPane(contentPane); 437 } 438 439 /** 440 * Returns the layeredPane object for this applet. 441 * 442 * @exception java.awt.IllegalComponentStateException (a runtime 443 * exception) if the layered pane parameter is null 444 * @see #setLayeredPane 445 * @see RootPaneContainer#getLayeredPane 446 */ 447 public JLayeredPane getLayeredPane() { 448 return getRootPane().getLayeredPane(); 449 } 450 451 /** 452 * Sets the layeredPane property. This method is called by the constructor. 453 * @param layeredPane the layeredPane object for this applet 454 * 455 * @see #getLayeredPane 456 * @see RootPaneContainer#setLayeredPane 457 * 458 * @beaninfo 459 * hidden: true 460 * description: The pane which holds the various applet layers. 461 */ 462 public void setLayeredPane(JLayeredPane layeredPane) { 463 getRootPane().setLayeredPane(layeredPane); 464 } 465 466 /** 467 * Returns the glassPane object for this applet. 468 * 469 * @see #setGlassPane 470 * @see RootPaneContainer#getGlassPane 471 */ 472 public Component getGlassPane() { 473 return getRootPane().getGlassPane(); 474 } 475 476 /** 477 * Sets the glassPane property. 478 * This method is called by the constructor. 479 * @param glassPane the glassPane object for this applet 480 * 481 * @see #getGlassPane 482 * @see RootPaneContainer#setGlassPane 483 * 484 * @beaninfo 485 * hidden: true 486 * description: A transparent pane used for menu rendering. 487 */ 488 public void setGlassPane(Component glassPane) { 489 getRootPane().setGlassPane(glassPane); 490 } 491 492 /** 493 * {@inheritDoc} 494 * 495 * @since 1.6 496 */ 497 public Graphics getGraphics() { 498 JComponent.getGraphicsInvoked(this); 499 return super.getGraphics(); 500 } 501 502 /** 503 * Repaints the specified rectangle of this component within 504 * <code>time</code> milliseconds. Refer to <code>RepaintManager</code> 505 * for details on how the repaint is handled. 506 * 507 * @param time maximum time in milliseconds before update 508 * @param x the <i>x</i> coordinate 509 * @param y the <i>y</i> coordinate 510 * @param width the width 511 * @param height the height 512 * @see RepaintManager 513 * @since 1.6 514 */ 515 public void repaint(long time, int x, int y, int width, int height) { 516 if (RepaintManager.HANDLE_TOP_LEVEL_PAINT) { 517 RepaintManager.currentManager(this).addDirtyRegion( 518 this, x, y, width, height); 519 } 520 else { 521 super.repaint(time, x, y, width, height); 522 } 523 } 524 525 /** 526 * Returns a string representation of this JApplet. This method 527 * is intended to be used only for debugging purposes, and the 528 * content and format of the returned string may vary between 529 * implementations. The returned string may be empty but may not 530 * be <code>null</code>. 531 * 532 * @return a string representation of this JApplet. 533 */ 534 protected String paramString() { 535 String rootPaneString = (rootPane != null ? 536 rootPane.toString() : ""); 537 String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ? 538 "true" : "false"); 539 540 return super.paramString() + 541 ",rootPane=" + rootPaneString + 542 ",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString; 543 } 544 545 546 547 ///////////////// 548 // Accessibility support 549 //////////////// 550 551 /** 552 * {@code AccessibleContext} associated with this {@code JApplet} 553 */ 554 protected AccessibleContext accessibleContext = null; 555 556 /** 557 * Gets the AccessibleContext associated with this JApplet. 558 * For JApplets, the AccessibleContext takes the form of an 559 * AccessibleJApplet. 560 * A new AccessibleJApplet instance is created if necessary. 561 * 562 * @return an AccessibleJApplet that serves as the 563 * AccessibleContext of this JApplet 564 */ 565 public AccessibleContext getAccessibleContext() { 566 if (accessibleContext == null) { 567 accessibleContext = new AccessibleJApplet(); 568 } 569 return accessibleContext; 570 } 571 572 /** 573 * This class implements accessibility support for the 574 * <code>JApplet</code> class. 575 */ 576 protected class AccessibleJApplet extends AccessibleApplet { 577 // everything moved to new parent, AccessibleApplet 578 } 579 }