src/share/classes/sun/awt/EmbeddedFrame.java

Print this page




 163      * must call this method on the Applet's AppContext. After that, all the changes
 164      * can be handled automatically, including possible replacement of
 165      * KeyboardFocusManager.
 166      */
 167     public void registerListeners() {
 168         if (appletKFM != null) {
 169             removeTraversingOutListeners(appletKFM);
 170         }
 171         appletKFM = KeyboardFocusManager.getCurrentKeyboardFocusManager();
 172         if (isVisible()) {
 173             addTraversingOutListeners(appletKFM);
 174         }
 175     }
 176 
 177     /**
 178      * Needed to avoid memory leak: we register this EmbeddedFrame as a listener with
 179      * KeyboardFocusManager of applet's AppContext. We don't want the KFM to keep
 180      * reference to our EmbeddedFrame forever if the Frame is no longer in use, so we
 181      * add listeners in show() and remove them in hide().
 182      */

 183     public void show() {
 184         if (appletKFM != null) {
 185             addTraversingOutListeners(appletKFM);
 186         }
 187         super.show();
 188     }
 189 
 190     /**
 191      * Needed to avoid memory leak: we register this EmbeddedFrame as a listener with
 192      * KeyboardFocusManager of applet's AppContext. We don't want the KFM to keep
 193      * reference to our EmbeddedFrame forever if the Frame is no longer in use, so we
 194      * add listeners in show() and remove them in hide().
 195      */

 196     public void hide() {
 197         if (appletKFM != null) {
 198             removeTraversingOutListeners(appletKFM);
 199         }
 200         super.hide();
 201     }
 202 
 203     /**
 204      * Need this method to detect when the focus may have chance to leave the
 205      * focus cycle root which is EmbeddedFrame. Mostly, the code here is copied
 206      * from DefaultKeyboardFocusManager.processKeyEvent with some minor
 207      * modifications.
 208      */
 209     public boolean dispatchKeyEvent(KeyEvent e) {
 210 
 211         // We can't guarantee that this is called on the same AppContext as EmbeddedFrame
 212         // belongs to. That's why we can't use public methods to find current focus cycle
 213         // root. Instead, we access KFM's private field directly.
 214         if (currentCycleRoot == null) {
 215             currentCycleRoot = (Field)AccessController.doPrivileged(new PrivilegedAction() {
 216                 public Object run() {
 217                     try {
 218                         Field unaccessibleRoot = KeyboardFocusManager.class.
 219                                                      getDeclaredField("currentFocusCycleRoot");
 220                         if (unaccessibleRoot != null) {
 221                             unaccessibleRoot.setAccessible(true);
 222                         }
 223                         return unaccessibleRoot;
 224                     } catch (NoSuchFieldException e1) {
 225                         assert false;
 226                     } catch (SecurityException e2) {
 227                         assert false;
 228                     }
 229                     return null;
 230                 }
 231             });
 232         }
 233 
 234         Container currentRoot = null;
 235         if (currentCycleRoot != null) {
 236             try {


 240                 // This is impossible: currentCycleRoot would be null if setAccessible failed.
 241                 assert false;
 242             }
 243         }
 244 
 245         // if we are not in EmbeddedFrame's cycle, we should not try to leave.
 246         if (this != currentRoot) {
 247             return false;
 248         }
 249 
 250         // KEY_TYPED events cannot be focus traversal keys
 251         if (e.getID() == KeyEvent.KEY_TYPED) {
 252             return false;
 253         }
 254 
 255         if (!getFocusTraversalKeysEnabled() || e.isConsumed()) {
 256             return false;
 257         }
 258 
 259         AWTKeyStroke stroke = AWTKeyStroke.getAWTKeyStrokeForEvent(e);
 260         Set toTest;
 261         Component currentFocused = e.getComponent();
 262 
 263         toTest = getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
 264         if (toTest.contains(stroke)) {
 265             // 6581899: performance improvement for SortingFocusTraversalPolicy
 266             Component last = getFocusTraversalPolicy().getLastComponent(this);
 267             if (currentFocused == last || last == null) {
 268                 if (traverseOut(FORWARD)) {
 269                     e.consume();
 270                     return true;
 271                 }
 272             }
 273         }
 274 
 275         toTest = getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
 276         if (toTest.contains(stroke)) {
 277             // 6581899: performance improvement for SortingFocusTraversalPolicy
 278             Component first = getFocusTraversalPolicy().getFirstComponent(this);
 279             if (currentFocused == first || first == null) {
 280                 if (traverseOut(BACKWARD)) {


 340      */
 341     protected boolean traverseOut(boolean direction) {
 342         return false;
 343     }
 344 
 345     /**
 346      * Block modifying any frame attributes, since they aren't applicable
 347      * for EmbeddedFrames.
 348      */
 349     public void setTitle(String title) {}
 350     public void setIconImage(Image image) {}
 351     public void setIconImages(java.util.List<? extends Image> icons) {}
 352     public void setMenuBar(MenuBar mb) {}
 353     public void setResizable(boolean resizable) {}
 354     public void remove(MenuComponent m) {}
 355 
 356     public boolean isResizable() {
 357         return true;
 358     }
 359 

 360     public void addNotify() {
 361         synchronized (getTreeLock()) {
 362             if (getPeer() == null) {
 363                 setPeer(new NullEmbeddedFramePeer());
 364             }
 365             super.addNotify();
 366         }
 367     }
 368 
 369     // These three functions consitute RFE 4100710. Do not remove.

 370     public void setCursorAllowed(boolean isCursorAllowed) {
 371         this.isCursorAllowed = isCursorAllowed;
 372         getPeer().updateCursorImmediately();
 373     }
 374     public boolean isCursorAllowed() {
 375         return isCursorAllowed;
 376     }
 377     public Cursor getCursor() {
 378         return (isCursorAllowed)
 379             ? super.getCursor()
 380             : Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
 381     }
 382 

 383     protected  void setPeer(final ComponentPeer p){
 384         if (fieldPeer == null) {
 385             fieldPeer = (Field)AccessController.doPrivileged(new PrivilegedAction() {
 386                     public Object run() {
 387                         try {
 388                             Field lnkPeer = Component.class.getDeclaredField("peer");
 389                             if (lnkPeer != null) {
 390                                 lnkPeer.setAccessible(true);
 391                             }
 392                             return lnkPeer;
 393                         } catch (NoSuchFieldException e) {
 394                             assert false;
 395                         } catch (SecurityException e) {
 396                             assert false;
 397                         }
 398                         return null;
 399                     }//run
 400                 });
 401         }
 402         try{
 403             if (fieldPeer !=null){
 404                 fieldPeer.set(EmbeddedFrame.this, p);
 405             }
 406         } catch (IllegalAccessException e) {
 407             assert false;
 408         }
 409     };  //setPeer method ends
 410 
 411     /**
 412      * Synthesize native message to activate or deactivate EmbeddedFrame window
 413      * depending on the value of parameter <code>b</code>.
 414      * Peers should override this method if they are to implement
 415      * this functionality.
 416      * @param doActivate  if <code>true</code>, activates the window;
 417      * otherwise, deactivates the window
 418      */
 419     public void synthesizeWindowActivation(boolean doActivate) {}
 420 
 421     /**
 422      * Moves this embedded frame to a new location. The top-left corner of
 423      * the new location is specified by the <code>x</code> and <code>y</code>


 490      * setLocationPrivate() and setBoundsPrivate() were introduced, and they
 491      * work just the same way as setLocation() and setBounds() for usual,
 492      * non-embedded components.
 493      * </p>
 494      * <p>
 495      * Using usual get/setLocation() and get/setBounds() together with new
 496      * get/setLocationPrivate() and get/setBoundsPrivate() is not recommended.
 497      * For example, calling getBoundsPrivate() after setLocation() works fine,
 498      * but getBounds() after setBoundsPrivate() may return unpredictable value.
 499      * </p>
 500      * @param x the new <i>x</i>-coordinate relative to the parent component
 501      * @param y the new <i>y</i>-coordinate relative to the parent component
 502      * @param width the new <code>width</code> of this embedded frame
 503      * @param height the new <code>height</code> of this embedded frame
 504      * @see java.awt.Component#setBounds
 505      * @see #setLocationPrivate
 506      * @see #getLocationPrivate
 507      * @see #getBoundsPrivate
 508      * @since 1.5
 509      */

 510     protected void setBoundsPrivate(int x, int y, int width, int height) {
 511         final FramePeer peer = (FramePeer)getPeer();
 512         if (peer != null) {
 513             peer.setBoundsPrivate(x, y, width, height);
 514         }
 515     }
 516 
 517     /**
 518      * Gets the bounds of this embedded frame as a rectangle specifying the
 519      * width, height and location relative to the native parent component.
 520      * <p>
 521      * setLocation() and setBounds() for EmbeddedFrame really don't move it
 522      * within the native parent. These methods always put embedded frame to
 523      * (0, 0) for backward compatibility. To allow getting location and size
 524      * of embedded frames getLocationPrivate() and getBoundsPrivate() were
 525      * introduced, and they work just the same way as getLocation() and getBounds()
 526      * for ususal, non-embedded components.
 527      * </p>
 528      * <p>
 529      * Using usual get/setLocation() and get/setBounds() together with new
 530      * get/setLocationPrivate() and get/setBoundsPrivate() is not recommended.
 531      * For example, calling getBoundsPrivate() after setLocation() works fine,
 532      * but getBounds() after setBoundsPrivate() may return unpredictable value.
 533      * </p>
 534      * @return a rectangle indicating this embedded frame's bounds
 535      * @see java.awt.Component#getBounds
 536      * @see #setLocationPrivate
 537      * @see #getLocationPrivate
 538      * @see #setBoundsPrivate
 539      * @since 1.6
 540      */

 541     protected Rectangle getBoundsPrivate() {
 542         final FramePeer peer = (FramePeer)getPeer();
 543         if (peer != null) {
 544             return peer.getBoundsPrivate();
 545         }
 546         else {
 547             return getBounds();
 548         }
 549     }
 550 
 551     public void toFront() {}
 552     public void toBack() {}
 553 
 554     public abstract void registerAccelerator(AWTKeyStroke stroke);
 555     public abstract void unregisterAccelerator(AWTKeyStroke stroke);
 556 
 557     /**
 558      * Checks if the component is in an EmbeddedFrame. If so,
 559      * returns the applet found in the hierarchy or null if
 560      * not found.




 163      * must call this method on the Applet's AppContext. After that, all the changes
 164      * can be handled automatically, including possible replacement of
 165      * KeyboardFocusManager.
 166      */
 167     public void registerListeners() {
 168         if (appletKFM != null) {
 169             removeTraversingOutListeners(appletKFM);
 170         }
 171         appletKFM = KeyboardFocusManager.getCurrentKeyboardFocusManager();
 172         if (isVisible()) {
 173             addTraversingOutListeners(appletKFM);
 174         }
 175     }
 176 
 177     /**
 178      * Needed to avoid memory leak: we register this EmbeddedFrame as a listener with
 179      * KeyboardFocusManager of applet's AppContext. We don't want the KFM to keep
 180      * reference to our EmbeddedFrame forever if the Frame is no longer in use, so we
 181      * add listeners in show() and remove them in hide().
 182      */
 183     @SuppressWarnings("deprecation")
 184     public void show() {
 185         if (appletKFM != null) {
 186             addTraversingOutListeners(appletKFM);
 187         }
 188         super.show();
 189     }
 190 
 191     /**
 192      * Needed to avoid memory leak: we register this EmbeddedFrame as a listener with
 193      * KeyboardFocusManager of applet's AppContext. We don't want the KFM to keep
 194      * reference to our EmbeddedFrame forever if the Frame is no longer in use, so we
 195      * add listeners in show() and remove them in hide().
 196      */
 197     @SuppressWarnings("deprecation")
 198     public void hide() {
 199         if (appletKFM != null) {
 200             removeTraversingOutListeners(appletKFM);
 201         }
 202         super.hide();
 203     }
 204 
 205     /**
 206      * Need this method to detect when the focus may have chance to leave the
 207      * focus cycle root which is EmbeddedFrame. Mostly, the code here is copied
 208      * from DefaultKeyboardFocusManager.processKeyEvent with some minor
 209      * modifications.
 210      */
 211     public boolean dispatchKeyEvent(KeyEvent e) {
 212 
 213         // We can't guarantee that this is called on the same AppContext as EmbeddedFrame
 214         // belongs to. That's why we can't use public methods to find current focus cycle
 215         // root. Instead, we access KFM's private field directly.
 216         if (currentCycleRoot == null) {
 217             currentCycleRoot = AccessController.doPrivileged(new PrivilegedAction<Field>() {
 218                 public Field run() {
 219                     try {
 220                         Field unaccessibleRoot = KeyboardFocusManager.class.
 221                                                      getDeclaredField("currentFocusCycleRoot");
 222                         if (unaccessibleRoot != null) {
 223                             unaccessibleRoot.setAccessible(true);
 224                         }
 225                         return unaccessibleRoot;
 226                     } catch (NoSuchFieldException e1) {
 227                         assert false;
 228                     } catch (SecurityException e2) {
 229                         assert false;
 230                     }
 231                     return null;
 232                 }
 233             });
 234         }
 235 
 236         Container currentRoot = null;
 237         if (currentCycleRoot != null) {
 238             try {


 242                 // This is impossible: currentCycleRoot would be null if setAccessible failed.
 243                 assert false;
 244             }
 245         }
 246 
 247         // if we are not in EmbeddedFrame's cycle, we should not try to leave.
 248         if (this != currentRoot) {
 249             return false;
 250         }
 251 
 252         // KEY_TYPED events cannot be focus traversal keys
 253         if (e.getID() == KeyEvent.KEY_TYPED) {
 254             return false;
 255         }
 256 
 257         if (!getFocusTraversalKeysEnabled() || e.isConsumed()) {
 258             return false;
 259         }
 260 
 261         AWTKeyStroke stroke = AWTKeyStroke.getAWTKeyStrokeForEvent(e);
 262         Set<AWTKeyStroke> toTest;
 263         Component currentFocused = e.getComponent();
 264 
 265         toTest = getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
 266         if (toTest.contains(stroke)) {
 267             // 6581899: performance improvement for SortingFocusTraversalPolicy
 268             Component last = getFocusTraversalPolicy().getLastComponent(this);
 269             if (currentFocused == last || last == null) {
 270                 if (traverseOut(FORWARD)) {
 271                     e.consume();
 272                     return true;
 273                 }
 274             }
 275         }
 276 
 277         toTest = getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
 278         if (toTest.contains(stroke)) {
 279             // 6581899: performance improvement for SortingFocusTraversalPolicy
 280             Component first = getFocusTraversalPolicy().getFirstComponent(this);
 281             if (currentFocused == first || first == null) {
 282                 if (traverseOut(BACKWARD)) {


 342      */
 343     protected boolean traverseOut(boolean direction) {
 344         return false;
 345     }
 346 
 347     /**
 348      * Block modifying any frame attributes, since they aren't applicable
 349      * for EmbeddedFrames.
 350      */
 351     public void setTitle(String title) {}
 352     public void setIconImage(Image image) {}
 353     public void setIconImages(java.util.List<? extends Image> icons) {}
 354     public void setMenuBar(MenuBar mb) {}
 355     public void setResizable(boolean resizable) {}
 356     public void remove(MenuComponent m) {}
 357 
 358     public boolean isResizable() {
 359         return true;
 360     }
 361 
 362     @SuppressWarnings("deprecation")
 363     public void addNotify() {
 364         synchronized (getTreeLock()) {
 365             if (getPeer() == null) {
 366                 setPeer(new NullEmbeddedFramePeer());
 367             }
 368             super.addNotify();
 369         }
 370     }
 371 
 372     // These three functions consitute RFE 4100710. Do not remove.
 373     @SuppressWarnings("deprecation")
 374     public void setCursorAllowed(boolean isCursorAllowed) {
 375         this.isCursorAllowed = isCursorAllowed;
 376         getPeer().updateCursorImmediately();
 377     }
 378     public boolean isCursorAllowed() {
 379         return isCursorAllowed;
 380     }
 381     public Cursor getCursor() {
 382         return (isCursorAllowed)
 383             ? super.getCursor()
 384             : Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
 385     }
 386 
 387     @SuppressWarnings("deprecation")
 388     protected void setPeer(final ComponentPeer p){
 389         if (fieldPeer == null) {
 390             fieldPeer = AccessController.doPrivileged(new PrivilegedAction<Field>() {
 391                 public Field run() {
 392                     try {
 393                         Field lnkPeer = Component.class.getDeclaredField("peer");
 394                         if (lnkPeer != null) {
 395                             lnkPeer.setAccessible(true);
 396                         }
 397                         return lnkPeer;
 398                     } catch (NoSuchFieldException e) {
 399                         assert false;
 400                     } catch (SecurityException e) {
 401                         assert false;
 402                     }
 403                     return null;
 404                 }//run
 405             });
 406         }
 407         try{
 408             if (fieldPeer != null){
 409                 fieldPeer.set(EmbeddedFrame.this, p);
 410             }
 411         } catch (IllegalAccessException e) {
 412             assert false;
 413         }
 414     };  //setPeer method ends
 415 
 416     /**
 417      * Synthesize native message to activate or deactivate EmbeddedFrame window
 418      * depending on the value of parameter <code>b</code>.
 419      * Peers should override this method if they are to implement
 420      * this functionality.
 421      * @param doActivate  if <code>true</code>, activates the window;
 422      * otherwise, deactivates the window
 423      */
 424     public void synthesizeWindowActivation(boolean doActivate) {}
 425 
 426     /**
 427      * Moves this embedded frame to a new location. The top-left corner of
 428      * the new location is specified by the <code>x</code> and <code>y</code>


 495      * setLocationPrivate() and setBoundsPrivate() were introduced, and they
 496      * work just the same way as setLocation() and setBounds() for usual,
 497      * non-embedded components.
 498      * </p>
 499      * <p>
 500      * Using usual get/setLocation() and get/setBounds() together with new
 501      * get/setLocationPrivate() and get/setBoundsPrivate() is not recommended.
 502      * For example, calling getBoundsPrivate() after setLocation() works fine,
 503      * but getBounds() after setBoundsPrivate() may return unpredictable value.
 504      * </p>
 505      * @param x the new <i>x</i>-coordinate relative to the parent component
 506      * @param y the new <i>y</i>-coordinate relative to the parent component
 507      * @param width the new <code>width</code> of this embedded frame
 508      * @param height the new <code>height</code> of this embedded frame
 509      * @see java.awt.Component#setBounds
 510      * @see #setLocationPrivate
 511      * @see #getLocationPrivate
 512      * @see #getBoundsPrivate
 513      * @since 1.5
 514      */
 515     @SuppressWarnings("deprecation")
 516     protected void setBoundsPrivate(int x, int y, int width, int height) {
 517         final FramePeer peer = (FramePeer)getPeer();
 518         if (peer != null) {
 519             peer.setBoundsPrivate(x, y, width, height);
 520         }
 521     }
 522 
 523     /**
 524      * Gets the bounds of this embedded frame as a rectangle specifying the
 525      * width, height and location relative to the native parent component.
 526      * <p>
 527      * setLocation() and setBounds() for EmbeddedFrame really don't move it
 528      * within the native parent. These methods always put embedded frame to
 529      * (0, 0) for backward compatibility. To allow getting location and size
 530      * of embedded frames getLocationPrivate() and getBoundsPrivate() were
 531      * introduced, and they work just the same way as getLocation() and getBounds()
 532      * for ususal, non-embedded components.
 533      * </p>
 534      * <p>
 535      * Using usual get/setLocation() and get/setBounds() together with new
 536      * get/setLocationPrivate() and get/setBoundsPrivate() is not recommended.
 537      * For example, calling getBoundsPrivate() after setLocation() works fine,
 538      * but getBounds() after setBoundsPrivate() may return unpredictable value.
 539      * </p>
 540      * @return a rectangle indicating this embedded frame's bounds
 541      * @see java.awt.Component#getBounds
 542      * @see #setLocationPrivate
 543      * @see #getLocationPrivate
 544      * @see #setBoundsPrivate
 545      * @since 1.6
 546      */
 547     @SuppressWarnings("deprecation")
 548     protected Rectangle getBoundsPrivate() {
 549         final FramePeer peer = (FramePeer)getPeer();
 550         if (peer != null) {
 551             return peer.getBoundsPrivate();
 552         }
 553         else {
 554             return getBounds();
 555         }
 556     }
 557 
 558     public void toFront() {}
 559     public void toBack() {}
 560 
 561     public abstract void registerAccelerator(AWTKeyStroke stroke);
 562     public abstract void unregisterAccelerator(AWTKeyStroke stroke);
 563 
 564     /**
 565      * Checks if the component is in an EmbeddedFrame. If so,
 566      * returns the applet found in the hierarchy or null if
 567      * not found.