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.applet.Applet; 28 import java.awt.AWTEvent; 29 import java.awt.BorderLayout; 30 import java.awt.Color; 31 import java.awt.Component; 32 import java.awt.Container; 33 import java.awt.Graphics; 34 import java.awt.HeadlessException; 35 import java.awt.LayoutManager; 36 37 import javax.accessibility.Accessible; 38 import javax.accessibility.AccessibleContext; 39 40 /** 41 * An extended version of <code>java.applet.Applet</code> that adds support for 42 * the JFC/Swing component architecture. 43 * You can find task-oriented documentation about using <code>JApplet</code> 44 * in <em>The Java Tutorial</em>, 45 * in the section 46 * <a 47 href="http://docs.oracle.com/javase/tutorial/uiswing/components/applet.html">How to Make Applets</a>. 48 * <p> 49 * The <code>JApplet</code> class is slightly incompatible with 50 * <code>java.applet.Applet</code>. <code>JApplet</code> contains a 51 * <code>JRootPane</code> as its only child. The <code>contentPane</code> 52 * should be the parent of any children of the <code>JApplet</code>. 53 * As a convenience, the {@code add}, {@code remove}, and {@code setLayout} 54 * methods of this class are overridden, so that they delegate calls 55 * to the corresponding methods of the {@code ContentPane}. 70 * <p> 71 * Please see the <code>JRootPane</code> documentation for a 72 * complete description of the <code>contentPane</code>, <code>glassPane</code>, 73 * and <code>layeredPane</code> properties. 74 * <p> 75 * <strong>Warning:</strong> Swing is not thread safe. For more 76 * information see <a 77 * href="package-summary.html#threading">Swing's Threading 78 * Policy</a>. 79 * <p> 80 * <strong>Warning:</strong> 81 * Serialized objects of this class will not be compatible with 82 * future Swing releases. The current serialization support is 83 * appropriate for short term storage or RMI between applications running 84 * the same version of Swing. As of 1.4, support for long term storage 85 * of all JavaBeans™ 86 * has been added to the <code>java.beans</code> package. 87 * Please see {@link java.beans.XMLEncoder}. 88 * 89 * @see javax.swing.RootPaneContainer 90 * @beaninfo 91 * attribute: isContainer true 92 * attribute: containerDelegate getContentPane 93 * description: Swing's Applet subclass. 94 * 95 * @author Arnaud Weber 96 * @since 1.2 97 */ 98 @SuppressWarnings("serial") // Same-version serialization only 99 public class JApplet extends Applet implements Accessible, 100 RootPaneContainer, 101 TransferHandler.HasGetTransferHandler 102 { 103 /** 104 * @see #getRootPane 105 * @see #setRootPane 106 */ 107 protected JRootPane rootPane; 108 109 /** 110 * If true then calls to <code>add</code> and <code>setLayout</code> 111 * will be forwarded to the <code>contentPane</code>. This is initially 112 * false, but is set to true when the <code>JApplet</code> is constructed. 113 * 114 * @see #isRootPaneCheckingEnabled 115 * @see #setRootPaneCheckingEnabled 116 * @see javax.swing.RootPaneContainer 117 */ 186 * {@code null} or not a user-set drop target, this method will change the 187 * drop target as follows: If {@code newHandler} is {@code null} it will 188 * clear the drop target. If not {@code null} it will install a new 189 * {@code DropTarget}. 190 * <p> 191 * Note: When used with {@code JApplet}, {@code TransferHandler} only 192 * provides data import capability, as the data export related methods 193 * are currently typed to {@code JComponent}. 194 * <p> 195 * Please see 196 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html"> 197 * How to Use Drag and Drop and Data Transfer</a>, a section in 198 * <em>The Java Tutorial</em>, for more information. 199 * 200 * @param newHandler the new {@code TransferHandler} 201 * 202 * @see TransferHandler 203 * @see #getTransferHandler 204 * @see java.awt.Component#setDropTarget 205 * @since 1.6 206 * 207 * @beaninfo 208 * bound: true 209 * hidden: true 210 * description: Mechanism for transfer of data into the component 211 */ 212 public void setTransferHandler(TransferHandler newHandler) { 213 TransferHandler oldHandler = transferHandler; 214 transferHandler = newHandler; 215 SwingUtilities.installSwingDropTargetAsNecessary(this, transferHandler); 216 firePropertyChange("transferHandler", oldHandler, newHandler); 217 } 218 219 /** 220 * Gets the <code>transferHandler</code> property. 221 * 222 * @return the value of the <code>transferHandler</code> property 223 * 224 * @see TransferHandler 225 * @see #setTransferHandler 226 * @since 1.6 227 */ 228 public TransferHandler getTransferHandler() { 229 return transferHandler; 230 } 231 232 /** 233 * Just calls <code>paint(g)</code>. This method was overridden to 234 * prevent an unnecessary call to clear the background. 235 */ 236 public void update(Graphics g) { 237 paint(g); 238 } 239 240 /** 241 * Sets the menubar for this applet. 242 * @param menuBar the menubar being placed in the applet 243 * 244 * @see #getJMenuBar 245 * 246 * @beaninfo 247 * hidden: true 248 * description: The menubar for accessing pulldown menus from this applet. 249 */ 250 public void setJMenuBar(final JMenuBar menuBar) { 251 getRootPane().setJMenuBar(menuBar); 252 } 253 254 /** 255 * Returns the menubar set on this applet. 256 * 257 * @return the menubar set on this applet 258 * @see #setJMenuBar 259 */ 260 public JMenuBar getJMenuBar() { 261 return getRootPane().getJMenuBar(); 262 } 263 264 265 /** 266 * Returns whether calls to <code>add</code> and 267 * <code>setLayout</code> are forwarded to the <code>contentPane</code>. 268 * 269 * @return true if <code>add</code> and <code>setLayout</code> 274 * @see #setRootPaneCheckingEnabled 275 * @see javax.swing.RootPaneContainer 276 */ 277 protected boolean isRootPaneCheckingEnabled() { 278 return rootPaneCheckingEnabled; 279 } 280 281 282 /** 283 * Sets whether calls to <code>add</code> and 284 * <code>setLayout</code> are forwarded to the <code>contentPane</code>. 285 * 286 * @param enabled true if <code>add</code> and <code>setLayout</code> 287 * are forwarded, false if they should operate directly on the 288 * <code>JApplet</code>. 289 * 290 * @see #addImpl 291 * @see #setLayout 292 * @see #isRootPaneCheckingEnabled 293 * @see javax.swing.RootPaneContainer 294 * @beaninfo 295 * hidden: true 296 * description: Whether the add and setLayout methods are forwarded 297 */ 298 protected void setRootPaneCheckingEnabled(boolean enabled) { 299 rootPaneCheckingEnabled = enabled; 300 } 301 302 303 /** 304 * Adds the specified child <code>Component</code>. 305 * This method is overridden to conditionally forward calls to the 306 * <code>contentPane</code>. 307 * By default, children are added to the <code>contentPane</code> instead 308 * of the frame, refer to {@link javax.swing.RootPaneContainer} for 309 * details. 310 * 311 * @param comp the component to be enhanced 312 * @param constraints the constraints to be respected 313 * @param index the index 314 * @exception IllegalArgumentException if <code>index</code> is invalid 315 * @exception IllegalArgumentException if adding the container's parent 316 * to itself 317 * @exception IllegalArgumentException if adding a window to a container 360 * @param manager the <code>LayoutManager</code> 361 * @see #setRootPaneCheckingEnabled 362 * @see javax.swing.RootPaneContainer 363 */ 364 public void setLayout(LayoutManager manager) { 365 if(isRootPaneCheckingEnabled()) { 366 getContentPane().setLayout(manager); 367 } 368 else { 369 super.setLayout(manager); 370 } 371 } 372 373 374 /** 375 * Returns the rootPane object for this applet. 376 * 377 * @see #setRootPane 378 * @see RootPaneContainer#getRootPane 379 */ 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 * @beaninfo 392 * hidden: true 393 * description: the RootPane object for this applet. 394 */ 395 protected void setRootPane(JRootPane root) { 396 if(rootPane != null) { 397 remove(rootPane); 398 } 399 rootPane = root; 400 if(rootPane != null) { 401 boolean checkingEnabled = isRootPaneCheckingEnabled(); 402 try { 403 setRootPaneCheckingEnabled(false); 404 add(rootPane, BorderLayout.CENTER); 405 } 406 finally { 407 setRootPaneCheckingEnabled(checkingEnabled); 408 } 409 } 410 } 411 412 413 /** 414 * Returns the contentPane object for this applet. 415 * 416 * @see #setContentPane 417 * @see RootPaneContainer#getContentPane 418 */ 419 public Container getContentPane() { 420 return getRootPane().getContentPane(); 421 } 422 423 /** 424 * Sets the contentPane property. This method is called by the constructor. 425 * @param contentPane the contentPane object for this applet 426 * 427 * @exception java.awt.IllegalComponentStateException (a runtime 428 * exception) if the content pane parameter is null 429 * @see #getContentPane 430 * @see RootPaneContainer#setContentPane 431 * 432 * @beaninfo 433 * hidden: true 434 * description: The client area of the applet where child 435 * components are normally inserted. 436 */ 437 public void setContentPane(Container contentPane) { 438 getRootPane().setContentPane(contentPane); 439 } 440 441 /** 442 * Returns the layeredPane object for this applet. 443 * 444 * @exception java.awt.IllegalComponentStateException (a runtime 445 * exception) if the layered pane parameter is null 446 * @see #setLayeredPane 447 * @see RootPaneContainer#getLayeredPane 448 */ 449 public JLayeredPane getLayeredPane() { 450 return getRootPane().getLayeredPane(); 451 } 452 453 /** 454 * Sets the layeredPane property. This method is called by the constructor. 455 * @param layeredPane the layeredPane object for this applet 456 * 457 * @see #getLayeredPane 458 * @see RootPaneContainer#setLayeredPane 459 * 460 * @beaninfo 461 * hidden: true 462 * description: The pane which holds the various applet layers. 463 */ 464 public void setLayeredPane(JLayeredPane layeredPane) { 465 getRootPane().setLayeredPane(layeredPane); 466 } 467 468 /** 469 * Returns the glassPane object for this applet. 470 * 471 * @see #setGlassPane 472 * @see RootPaneContainer#getGlassPane 473 */ 474 public Component getGlassPane() { 475 return getRootPane().getGlassPane(); 476 } 477 478 /** 479 * Sets the glassPane property. 480 * This method is called by the constructor. 481 * @param glassPane the glassPane object for this applet 482 * 483 * @see #getGlassPane 484 * @see RootPaneContainer#setGlassPane 485 * 486 * @beaninfo 487 * hidden: true 488 * description: A transparent pane used for menu rendering. 489 */ 490 public void setGlassPane(Component glassPane) { 491 getRootPane().setGlassPane(glassPane); 492 } 493 494 /** 495 * {@inheritDoc} 496 * 497 * @since 1.6 498 */ 499 public Graphics getGraphics() { 500 JComponent.getGraphicsInvoked(this); 501 return super.getGraphics(); 502 } 503 504 /** 505 * Repaints the specified rectangle of this component within 506 * <code>time</code> milliseconds. Refer to <code>RepaintManager</code> 507 * for details on how the repaint is handled. 508 * 509 * @param time maximum time in milliseconds before update 510 * @param x the <i>x</i> coordinate 511 * @param y the <i>y</i> coordinate 512 * @param width the width 513 * @param height the height 514 * @see RepaintManager 515 * @since 1.6 516 */ 517 public void repaint(long time, int x, int y, int width, int height) { 518 if (RepaintManager.HANDLE_TOP_LEVEL_PAINT) { | 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.applet.Applet; 28 import java.awt.AWTEvent; 29 import java.awt.BorderLayout; 30 import java.awt.Color; 31 import java.awt.Component; 32 import java.awt.Container; 33 import java.awt.Graphics; 34 import java.awt.HeadlessException; 35 import java.awt.LayoutManager; 36 import java.beans.BeanProperty; 37 import java.beans.JavaBean; 38 39 import javax.accessibility.Accessible; 40 import javax.accessibility.AccessibleContext; 41 42 /** 43 * An extended version of <code>java.applet.Applet</code> that adds support for 44 * the JFC/Swing component architecture. 45 * You can find task-oriented documentation about using <code>JApplet</code> 46 * in <em>The Java Tutorial</em>, 47 * in the section 48 * <a 49 href="http://docs.oracle.com/javase/tutorial/uiswing/components/applet.html">How to Make Applets</a>. 50 * <p> 51 * The <code>JApplet</code> class is slightly incompatible with 52 * <code>java.applet.Applet</code>. <code>JApplet</code> contains a 53 * <code>JRootPane</code> as its only child. The <code>contentPane</code> 54 * should be the parent of any children of the <code>JApplet</code>. 55 * As a convenience, the {@code add}, {@code remove}, and {@code setLayout} 56 * methods of this class are overridden, so that they delegate calls 57 * to the corresponding methods of the {@code ContentPane}. 72 * <p> 73 * Please see the <code>JRootPane</code> documentation for a 74 * complete description of the <code>contentPane</code>, <code>glassPane</code>, 75 * and <code>layeredPane</code> properties. 76 * <p> 77 * <strong>Warning:</strong> Swing is not thread safe. For more 78 * information see <a 79 * href="package-summary.html#threading">Swing's Threading 80 * Policy</a>. 81 * <p> 82 * <strong>Warning:</strong> 83 * Serialized objects of this class will not be compatible with 84 * future Swing releases. The current serialization support is 85 * appropriate for short term storage or RMI between applications running 86 * the same version of Swing. As of 1.4, support for long term storage 87 * of all JavaBeans™ 88 * has been added to the <code>java.beans</code> package. 89 * Please see {@link java.beans.XMLEncoder}. 90 * 91 * @see javax.swing.RootPaneContainer 92 * 93 * @author Arnaud Weber 94 * @since 1.2 95 */ 96 @JavaBean(defaultProperty = "JMenuBar", description = "Swing's Applet subclass.") 97 @SwingContainer(delegate = "getContentPane") 98 @SuppressWarnings("serial") // Same-version serialization only 99 public class JApplet extends Applet implements Accessible, 100 RootPaneContainer, 101 TransferHandler.HasGetTransferHandler 102 { 103 /** 104 * @see #getRootPane 105 * @see #setRootPane 106 */ 107 protected JRootPane rootPane; 108 109 /** 110 * If true then calls to <code>add</code> and <code>setLayout</code> 111 * will be forwarded to the <code>contentPane</code>. This is initially 112 * false, but is set to true when the <code>JApplet</code> is constructed. 113 * 114 * @see #isRootPaneCheckingEnabled 115 * @see #setRootPaneCheckingEnabled 116 * @see javax.swing.RootPaneContainer 117 */ 186 * {@code null} or not a user-set drop target, this method will change the 187 * drop target as follows: If {@code newHandler} is {@code null} it will 188 * clear the drop target. If not {@code null} it will install a new 189 * {@code DropTarget}. 190 * <p> 191 * Note: When used with {@code JApplet}, {@code TransferHandler} only 192 * provides data import capability, as the data export related methods 193 * are currently typed to {@code JComponent}. 194 * <p> 195 * Please see 196 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html"> 197 * How to Use Drag and Drop and Data Transfer</a>, a section in 198 * <em>The Java Tutorial</em>, for more information. 199 * 200 * @param newHandler the new {@code TransferHandler} 201 * 202 * @see TransferHandler 203 * @see #getTransferHandler 204 * @see java.awt.Component#setDropTarget 205 * @since 1.6 206 */ 207 @BeanProperty(hidden = true, description 208 = "Mechanism for transfer of data into the component") 209 public void setTransferHandler(TransferHandler newHandler) { 210 TransferHandler oldHandler = transferHandler; 211 transferHandler = newHandler; 212 SwingUtilities.installSwingDropTargetAsNecessary(this, transferHandler); 213 firePropertyChange("transferHandler", oldHandler, newHandler); 214 } 215 216 /** 217 * Gets the <code>transferHandler</code> property. 218 * 219 * @return the value of the <code>transferHandler</code> property 220 * 221 * @see TransferHandler 222 * @see #setTransferHandler 223 * @since 1.6 224 */ 225 public TransferHandler getTransferHandler() { 226 return transferHandler; 227 } 228 229 /** 230 * Just calls <code>paint(g)</code>. This method was overridden to 231 * prevent an unnecessary call to clear the background. 232 */ 233 public void update(Graphics g) { 234 paint(g); 235 } 236 237 /** 238 * Sets the menubar for this applet. 239 * @param menuBar the menubar being placed in the applet 240 * 241 * @see #getJMenuBar 242 */ 243 @BeanProperty(bound = false, hidden = true, description 244 = "The menubar for accessing pulldown menus from this applet.") 245 public void setJMenuBar(final JMenuBar menuBar) { 246 getRootPane().setJMenuBar(menuBar); 247 } 248 249 /** 250 * Returns the menubar set on this applet. 251 * 252 * @return the menubar set on this applet 253 * @see #setJMenuBar 254 */ 255 public JMenuBar getJMenuBar() { 256 return getRootPane().getJMenuBar(); 257 } 258 259 260 /** 261 * Returns whether calls to <code>add</code> and 262 * <code>setLayout</code> are forwarded to the <code>contentPane</code>. 263 * 264 * @return true if <code>add</code> and <code>setLayout</code> 269 * @see #setRootPaneCheckingEnabled 270 * @see javax.swing.RootPaneContainer 271 */ 272 protected boolean isRootPaneCheckingEnabled() { 273 return rootPaneCheckingEnabled; 274 } 275 276 277 /** 278 * Sets whether calls to <code>add</code> and 279 * <code>setLayout</code> are forwarded to the <code>contentPane</code>. 280 * 281 * @param enabled true if <code>add</code> and <code>setLayout</code> 282 * are forwarded, false if they should operate directly on the 283 * <code>JApplet</code>. 284 * 285 * @see #addImpl 286 * @see #setLayout 287 * @see #isRootPaneCheckingEnabled 288 * @see javax.swing.RootPaneContainer 289 */ 290 @BeanProperty(hidden = true, description 291 = "Whether the add and setLayout methods are forwarded") 292 protected void setRootPaneCheckingEnabled(boolean enabled) { 293 rootPaneCheckingEnabled = enabled; 294 } 295 296 297 /** 298 * Adds the specified child <code>Component</code>. 299 * This method is overridden to conditionally forward calls to the 300 * <code>contentPane</code>. 301 * By default, children are added to the <code>contentPane</code> instead 302 * of the frame, refer to {@link javax.swing.RootPaneContainer} for 303 * details. 304 * 305 * @param comp the component to be enhanced 306 * @param constraints the constraints to be respected 307 * @param index the index 308 * @exception IllegalArgumentException if <code>index</code> is invalid 309 * @exception IllegalArgumentException if adding the container's parent 310 * to itself 311 * @exception IllegalArgumentException if adding a window to a container 354 * @param manager the <code>LayoutManager</code> 355 * @see #setRootPaneCheckingEnabled 356 * @see javax.swing.RootPaneContainer 357 */ 358 public void setLayout(LayoutManager manager) { 359 if(isRootPaneCheckingEnabled()) { 360 getContentPane().setLayout(manager); 361 } 362 else { 363 super.setLayout(manager); 364 } 365 } 366 367 368 /** 369 * Returns the rootPane object for this applet. 370 * 371 * @see #setRootPane 372 * @see RootPaneContainer#getRootPane 373 */ 374 @BeanProperty(bound = false, hidden = true, description 375 = "the RootPane object for this applet.") 376 public JRootPane getRootPane() { 377 return rootPane; 378 } 379 380 381 /** 382 * Sets the rootPane property. This method is called by the constructor. 383 * @param root the rootPane object for this applet 384 * 385 * @see #getRootPane 386 */ 387 protected void setRootPane(JRootPane root) { 388 if(rootPane != null) { 389 remove(rootPane); 390 } 391 rootPane = root; 392 if(rootPane != null) { 393 boolean checkingEnabled = isRootPaneCheckingEnabled(); 394 try { 395 setRootPaneCheckingEnabled(false); 396 add(rootPane, BorderLayout.CENTER); 397 } 398 finally { 399 setRootPaneCheckingEnabled(checkingEnabled); 400 } 401 } 402 } 403 404 405 /** 406 * Returns the contentPane object for this applet. 407 * 408 * @see #setContentPane 409 * @see RootPaneContainer#getContentPane 410 */ 411 public Container getContentPane() { 412 return getRootPane().getContentPane(); 413 } 414 415 /** 416 * Sets the contentPane property. This method is called by the constructor. 417 * @param contentPane the contentPane object for this applet 418 * 419 * @exception java.awt.IllegalComponentStateException (a runtime 420 * exception) if the content pane parameter is null 421 * @see #getContentPane 422 * @see RootPaneContainer#setContentPane 423 */ 424 @BeanProperty(bound = false, hidden = true, description 425 = "The client area of the applet where child components are normally inserted.") 426 public void setContentPane(Container contentPane) { 427 getRootPane().setContentPane(contentPane); 428 } 429 430 /** 431 * Returns the layeredPane object for this applet. 432 * 433 * @exception java.awt.IllegalComponentStateException (a runtime 434 * exception) if the layered pane parameter is null 435 * @see #setLayeredPane 436 * @see RootPaneContainer#getLayeredPane 437 */ 438 public JLayeredPane getLayeredPane() { 439 return getRootPane().getLayeredPane(); 440 } 441 442 /** 443 * Sets the layeredPane property. This method is called by the constructor. 444 * @param layeredPane the layeredPane object for this applet 445 * 446 * @see #getLayeredPane 447 * @see RootPaneContainer#setLayeredPane 448 */ 449 @BeanProperty(bound = false, hidden = true, description 450 = "The pane which holds the various applet layers.") 451 public void setLayeredPane(JLayeredPane layeredPane) { 452 getRootPane().setLayeredPane(layeredPane); 453 } 454 455 /** 456 * Returns the glassPane object for this applet. 457 * 458 * @see #setGlassPane 459 * @see RootPaneContainer#getGlassPane 460 */ 461 public Component getGlassPane() { 462 return getRootPane().getGlassPane(); 463 } 464 465 /** 466 * Sets the glassPane property. 467 * This method is called by the constructor. 468 * @param glassPane the glassPane object for this applet 469 * 470 * @see #getGlassPane 471 * @see RootPaneContainer#setGlassPane 472 */ 473 @BeanProperty(bound = false, hidden = true, description 474 = "A transparent pane used for menu rendering.") 475 public void setGlassPane(Component glassPane) { 476 getRootPane().setGlassPane(glassPane); 477 } 478 479 /** 480 * {@inheritDoc} 481 * 482 * @since 1.6 483 */ 484 @BeanProperty(bound = false) 485 public Graphics getGraphics() { 486 JComponent.getGraphicsInvoked(this); 487 return super.getGraphics(); 488 } 489 490 /** 491 * Repaints the specified rectangle of this component within 492 * <code>time</code> milliseconds. Refer to <code>RepaintManager</code> 493 * for details on how the repaint is handled. 494 * 495 * @param time maximum time in milliseconds before update 496 * @param x the <i>x</i> coordinate 497 * @param y the <i>y</i> coordinate 498 * @param width the width 499 * @param height the height 500 * @see RepaintManager 501 * @since 1.6 502 */ 503 public void repaint(long time, int x, int y, int width, int height) { 504 if (RepaintManager.HANDLE_TOP_LEVEL_PAINT) { |