src/macosx/classes/sun/lwawt/LWComponentPeer.java

Print this page




 121     private final RepaintArea targetPaintArea;
 122 
 123     //   private volatile boolean paintPending;
 124     private volatile boolean isLayouting;
 125 
 126     private final D delegate;
 127     private Container delegateContainer;
 128     private Component delegateDropTarget;
 129     private final Object dropTargetLock = new Object();
 130 
 131     private int fNumDropTargets = 0;
 132     private CDropTarget fDropTarget = null;
 133 
 134     private final PlatformComponent platformComponent;
 135 
 136     /**
 137      * Character with reasonable value between the minimum width and maximum.
 138      */
 139     static final char WIDE_CHAR = '0';
 140 





 141     private final class DelegateContainer extends Container {
 142         {
 143             enableEvents(0xFFFFFFFF);
 144         }
 145 
 146         DelegateContainer() {
 147             super();
 148         }
 149 
 150         @Override
 151         public boolean isLightweight() {
 152             return false;
 153         }
 154 
 155         @Override
 156         public Point getLocation() {
 157             return getLocationOnScreen();
 158         }
 159 
 160         @Override


 372     // ---- PEER METHODS ---- //
 373 
 374     @Override
 375     public Toolkit getToolkit() {
 376         return LWToolkit.getLWToolkit();
 377     }
 378 
 379     // Just a helper method
 380     public LWToolkit getLWToolkit() {
 381         return LWToolkit.getLWToolkit();
 382     }
 383 
 384     @Override
 385     public final void dispose() {
 386         if (disposed.compareAndSet(false, true)) {
 387             disposeImpl();
 388         }
 389     }
 390 
 391     protected void disposeImpl() {

 392         LWContainerPeer cp = getContainerPeer();
 393         if (cp != null) {
 394             cp.removeChildPeer(this);
 395         }
 396         platformComponent.dispose();
 397         LWToolkit.targetDisposedPeer(getTarget(), this);
 398     }
 399 
 400     public final boolean isDisposed() {
 401         return disposed.get();
 402     }
 403 
 404     /*
 405      * GraphicsConfiguration is borrowed from the parent peer. The
 406      * return value must not be null.
 407      *
 408      * Overridden in LWWindowPeer.
 409      */
 410     @Override
 411     public GraphicsConfiguration getGraphicsConfiguration() {
 412         // Don't check windowPeer for null as it can only happen
 413         // for windows, but this method is overridden in
 414         // LWWindowPeer and doesn't call super()
 415         return getWindowPeer().getGraphicsConfiguration();
 416     }
 417 






 418     /*
 419      * Overridden in LWWindowPeer to replace its surface
 420      * data and back buffer.
 421      */
 422     @Override
 423     public boolean updateGraphicsData(GraphicsConfiguration gc) {
 424         // TODO: not implemented
 425 //        throw new RuntimeException("Has not been implemented yet.");
 426         return false;
 427     }
 428 
 429     @Override
 430     public final Graphics getGraphics() {
 431         final Graphics g = getOnscreenGraphics();
 432         if (g != null) {
 433             synchronized (getPeerTreeLock()){
 434                 applyConstrain(g);
 435             }
 436         }
 437         return g;


 489     static final Region computeVisibleRect(LWComponentPeer c, Region region) {
 490         final LWContainerPeer p = c.getContainerPeer();
 491         if (p != null) {
 492             final Rectangle r = c.getBounds();
 493             region = region.getTranslatedRegion(r.x, r.y);
 494             region = region.getIntersection(p.getRegion());
 495             region = region.getIntersection(p.getContentSize());
 496             region = p.cutChildren(region, c);
 497             region = computeVisibleRect(p, region);
 498             region = region.getTranslatedRegion(-r.x, -r.y);
 499         }
 500         return region;
 501     }
 502 
 503     @Override
 504     public ColorModel getColorModel() {
 505         // Is it a correct implementation?
 506         return getGraphicsConfiguration().getColorModel();
 507     }
 508 





 509     @Override
 510     public void createBuffers(int numBuffers, BufferCapabilities caps)
 511             throws AWTException {
 512         throw new AWTException("Back buffers are only supported for " +
 513                 "Window or Canvas components.");



 514     }
 515 
 516     /*
 517      * To be overridden in LWWindowPeer and LWCanvasPeer.
 518      */
 519     @Override
 520     public Image getBackBuffer() {
 521         // Return null or throw AWTException?
 522         return null;




 523     }
 524 
 525     @Override
 526     public void flip(int x1, int y1, int x2, int y2,
 527                      BufferCapabilities.FlipContents flipAction) {
 528         // Skip silently or throw AWTException?
 529     }
 530 
 531     @Override
 532     public void destroyBuffers() {
 533         // Do nothing





 534     }
 535 
 536     // Helper method
 537     public void setBounds(Rectangle r) {
 538         setBounds(r.x, r.y, r.width, r.height, SET_BOUNDS);
 539     }
 540 
 541     /**
 542      * This method could be called on the toolkit thread.
 543      */
 544     @Override
 545     public void setBounds(int x, int y, int w, int h, int op) {
 546         setBounds(x, y, w, h, op, true, false);
 547     }
 548 
 549     protected void setBounds(int x, int y, int w, int h, int op, boolean notify,
 550                              final boolean updateTarget) {
 551         Rectangle oldBounds;
 552         synchronized (getStateLock()) {
 553             oldBounds = new Rectangle(bounds);


 625     @Override
 626     public void setBackground(final Color c) {
 627         final Color oldBg = getBackground();
 628         if (oldBg == c || (oldBg != null && oldBg.equals(c))) {
 629             return;
 630         }
 631         synchronized (getStateLock()) {
 632             background = c;
 633         }
 634         final D delegate = getDelegate();
 635         if (delegate != null) {
 636             synchronized (getDelegateLock()) {
 637                 // delegate will repaint the target
 638                 delegate.setBackground(c);
 639             }
 640         } else {
 641             repaintPeer();
 642         }
 643     }
 644 
 645     protected final Color getBackground() {
 646         synchronized (getStateLock()) {
 647             return background;
 648         }
 649     }
 650 
 651     @Override
 652     public void setForeground(final Color c) {
 653         final Color oldFg = getForeground();
 654         if (oldFg == c || (oldFg != null && oldFg.equals(c))) {
 655             return;
 656         }
 657         synchronized (getStateLock()) {
 658             foreground = c;
 659         }
 660         final D delegate = getDelegate();
 661         if (delegate != null) {
 662             synchronized (getDelegateLock()) {
 663                 // delegate will repaint the target
 664                 delegate.setForeground(c);
 665             }


 965                     }
 966                     LWKeyboardFocusManagerPeer.removeLastFocusRequest(getTarget());
 967                     return false;
 968                 }
 969 
 970                 KeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
 971                 Component focusOwner = kfmPeer.getCurrentFocusOwner();
 972                 return LWKeyboardFocusManagerPeer.deliverFocus(lightweightChild,
 973                         getTarget(), temporary,
 974                         focusedWindowChangeAllowed,
 975                         time, cause, focusOwner);
 976 
 977             case LWKeyboardFocusManagerPeer.SNFH_SUCCESS_HANDLED:
 978                 return true;
 979         }
 980 
 981         return false;
 982     }
 983 
 984     @Override
 985     public Image createImage(ImageProducer producer) {
 986         return new ToolkitImage(producer);
 987     }
 988 
 989     @Override
 990     public Image createImage(int w, int h) {
 991         CGraphicsConfig gc = (CGraphicsConfig)getGraphicsConfiguration();
 992         return gc.createAcceleratedImage(getTarget(), w, h);
 993     }
 994 
 995     @Override
 996     public VolatileImage createVolatileImage(int w, int h) {
 997         // TODO: is it a right/complete implementation?
 998         return new SunVolatileImage(getTarget(), w, h);
 999     }
1000 
1001     @Override
1002     public boolean prepareImage(Image img, int w, int h, ImageObserver o) {
1003         // TODO: is it a right/complete implementation?
1004         return getToolkit().prepareImage(img, w, h, o);
1005     }
1006 
1007     @Override
1008     public int checkImage(Image img, int w, int h, ImageObserver o) {
1009         // TODO: is it a right/complete implementation?
1010         return getToolkit().checkImage(img, w, h, o);
1011     }
1012 
1013     @Override
1014     public boolean handlesWheelScrolling() {
1015         // TODO: not implemented
1016         return false;
1017     }


1088                 if (--fNumDropTargets == 0) {
1089                     // Having a null drop target would be an error but let's check just in case:
1090                     if (fDropTarget != null) {
1091                         // Dispose of the drop target:
1092                         fDropTarget.dispose();
1093                         fDropTarget = null;
1094                     } else
1095                         System.err.println("CComponent.removeDropTarget(): current drop target is null.");
1096                 }
1097             }
1098         }
1099     }
1100 
1101     // ---- PEER NOTIFICATIONS ---- //
1102 
1103     /**
1104      * Called when this peer's location has been changed either as a result
1105      * of target.setLocation() or as a result of user actions (window is
1106      * dragged with mouse).
1107      *
1108      * To be overridden in LWWindowPeer to update its GraphicsConfig.
1109      *
1110      * This method could be called on the toolkit thread.
1111      */
1112     protected final void handleMove(final int x, final int y,
1113                                     final boolean updateTarget) {
1114         if (updateTarget) {
1115             AWTAccessor.getComponentAccessor().setLocation(getTarget(), x, y);
1116         }
1117         postEvent(new ComponentEvent(getTarget(),
1118                                      ComponentEvent.COMPONENT_MOVED));
1119     }
1120 
1121     /**
1122      * Called when this peer's size has been changed either as a result of
1123      * target.setSize() or as a result of user actions (window is resized).
1124      *
1125      * To be overridden in LWWindowPeer to update its SurfaceData and
1126      * GraphicsConfig.
1127      *
1128      * This method could be called on the toolkit thread.
1129      */
1130     protected final void handleResize(final int w, final int h,
1131                                       final boolean updateTarget) {









1132         if (updateTarget) {
1133             AWTAccessor.getComponentAccessor().setSize(getTarget(), w, h);
1134         }
1135         postEvent(new ComponentEvent(getTarget(),
1136                                      ComponentEvent.COMPONENT_RESIZED));
1137     }
1138 
1139     protected final void repaintOldNewBounds(final Rectangle oldB) {
1140         repaintParent(oldB);
1141         repaintPeer(getSize());
1142     }
1143 
1144     protected final void repaintParent(final Rectangle oldB) {
1145         final LWContainerPeer cp = getContainerPeer();
1146         if (cp != null) {
1147             // Repaint unobscured part of the parent
1148             cp.repaintPeer(cp.getContentSize().intersection(oldB));
1149         }
1150     }
1151 




 121     private final RepaintArea targetPaintArea;
 122 
 123     //   private volatile boolean paintPending;
 124     private volatile boolean isLayouting;
 125 
 126     private final D delegate;
 127     private Container delegateContainer;
 128     private Component delegateDropTarget;
 129     private final Object dropTargetLock = new Object();
 130 
 131     private int fNumDropTargets = 0;
 132     private CDropTarget fDropTarget = null;
 133 
 134     private final PlatformComponent platformComponent;
 135 
 136     /**
 137      * Character with reasonable value between the minimum width and maximum.
 138      */
 139     static final char WIDE_CHAR = '0';
 140 
 141     /**
 142      * The back buffer provide user with a BufferStrategy.
 143      */
 144     private Image backBuffer;
 145 
 146     private final class DelegateContainer extends Container {
 147         {
 148             enableEvents(0xFFFFFFFF);
 149         }
 150 
 151         DelegateContainer() {
 152             super();
 153         }
 154 
 155         @Override
 156         public boolean isLightweight() {
 157             return false;
 158         }
 159 
 160         @Override
 161         public Point getLocation() {
 162             return getLocationOnScreen();
 163         }
 164 
 165         @Override


 377     // ---- PEER METHODS ---- //
 378 
 379     @Override
 380     public Toolkit getToolkit() {
 381         return LWToolkit.getLWToolkit();
 382     }
 383 
 384     // Just a helper method
 385     public LWToolkit getLWToolkit() {
 386         return LWToolkit.getLWToolkit();
 387     }
 388 
 389     @Override
 390     public final void dispose() {
 391         if (disposed.compareAndSet(false, true)) {
 392             disposeImpl();
 393         }
 394     }
 395 
 396     protected void disposeImpl() {
 397         destroyBuffers();
 398         LWContainerPeer cp = getContainerPeer();
 399         if (cp != null) {
 400             cp.removeChildPeer(this);
 401         }
 402         platformComponent.dispose();
 403         LWToolkit.targetDisposedPeer(getTarget(), this);
 404     }
 405 
 406     public final boolean isDisposed() {
 407         return disposed.get();
 408     }
 409 
 410     /*
 411      * GraphicsConfiguration is borrowed from the parent peer. The
 412      * return value must not be null.
 413      *
 414      * Overridden in LWWindowPeer.
 415      */
 416     @Override
 417     public GraphicsConfiguration getGraphicsConfiguration() {
 418         // Don't check windowPeer for null as it can only happen
 419         // for windows, but this method is overridden in
 420         // LWWindowPeer and doesn't call super()
 421         return getWindowPeer().getGraphicsConfiguration();
 422     }
 423 
 424 
 425     // Just a helper method
 426     public final LWGraphicsConfig getLWGC() {
 427         return (LWGraphicsConfig) getGraphicsConfiguration();
 428     }
 429 
 430     /*
 431      * Overridden in LWWindowPeer to replace its surface
 432      * data and back buffer.
 433      */
 434     @Override
 435     public boolean updateGraphicsData(GraphicsConfiguration gc) {
 436         // TODO: not implemented
 437 //        throw new RuntimeException("Has not been implemented yet.");
 438         return false;
 439     }
 440 
 441     @Override
 442     public final Graphics getGraphics() {
 443         final Graphics g = getOnscreenGraphics();
 444         if (g != null) {
 445             synchronized (getPeerTreeLock()){
 446                 applyConstrain(g);
 447             }
 448         }
 449         return g;


 501     static final Region computeVisibleRect(LWComponentPeer c, Region region) {
 502         final LWContainerPeer p = c.getContainerPeer();
 503         if (p != null) {
 504             final Rectangle r = c.getBounds();
 505             region = region.getTranslatedRegion(r.x, r.y);
 506             region = region.getIntersection(p.getRegion());
 507             region = region.getIntersection(p.getContentSize());
 508             region = p.cutChildren(region, c);
 509             region = computeVisibleRect(p, region);
 510             region = region.getTranslatedRegion(-r.x, -r.y);
 511         }
 512         return region;
 513     }
 514 
 515     @Override
 516     public ColorModel getColorModel() {
 517         // Is it a correct implementation?
 518         return getGraphicsConfiguration().getColorModel();
 519     }
 520 
 521     public boolean isTranslucent() {
 522         // Translucent windows of the top level are supported only
 523         return false;
 524     }
 525 
 526     @Override
 527     public final void createBuffers(int numBuffers, BufferCapabilities caps)
 528             throws AWTException {
 529         getLWGC().assertOperationSupported(numBuffers, caps);
 530         final Image buffer = getLWGC().createBackBuffer(this);
 531         synchronized (getStateLock()) {
 532             backBuffer = buffer;
 533         }
 534     }
 535 



 536     @Override
 537     public final Image getBackBuffer() {
 538         synchronized (getStateLock()) {
 539             if (backBuffer != null) {
 540                 return backBuffer;
 541             }
 542         }
 543         throw new IllegalStateException("Buffers have not been created");
 544     }
 545 
 546     @Override
 547     public final void flip(int x1, int y1, int x2, int y2,
 548                      BufferCapabilities.FlipContents flipAction) {
 549         getLWGC().flip(this, getBackBuffer(), x1, y1, x2, y2, flipAction);
 550     }
 551 
 552     @Override
 553     public final void destroyBuffers() {
 554         final Image oldBB;
 555         synchronized (getStateLock()) {
 556             oldBB = backBuffer;
 557             backBuffer = null;
 558         }
 559         getLWGC().destroyBackBuffer(oldBB);
 560     }
 561 
 562     // Helper method
 563     public void setBounds(Rectangle r) {
 564         setBounds(r.x, r.y, r.width, r.height, SET_BOUNDS);
 565     }
 566 
 567     /**
 568      * This method could be called on the toolkit thread.
 569      */
 570     @Override
 571     public void setBounds(int x, int y, int w, int h, int op) {
 572         setBounds(x, y, w, h, op, true, false);
 573     }
 574 
 575     protected void setBounds(int x, int y, int w, int h, int op, boolean notify,
 576                              final boolean updateTarget) {
 577         Rectangle oldBounds;
 578         synchronized (getStateLock()) {
 579             oldBounds = new Rectangle(bounds);


 651     @Override
 652     public void setBackground(final Color c) {
 653         final Color oldBg = getBackground();
 654         if (oldBg == c || (oldBg != null && oldBg.equals(c))) {
 655             return;
 656         }
 657         synchronized (getStateLock()) {
 658             background = c;
 659         }
 660         final D delegate = getDelegate();
 661         if (delegate != null) {
 662             synchronized (getDelegateLock()) {
 663                 // delegate will repaint the target
 664                 delegate.setBackground(c);
 665             }
 666         } else {
 667             repaintPeer();
 668         }
 669     }
 670 
 671     public final Color getBackground() {
 672         synchronized (getStateLock()) {
 673             return background;
 674         }
 675     }
 676 
 677     @Override
 678     public void setForeground(final Color c) {
 679         final Color oldFg = getForeground();
 680         if (oldFg == c || (oldFg != null && oldFg.equals(c))) {
 681             return;
 682         }
 683         synchronized (getStateLock()) {
 684             foreground = c;
 685         }
 686         final D delegate = getDelegate();
 687         if (delegate != null) {
 688             synchronized (getDelegateLock()) {
 689                 // delegate will repaint the target
 690                 delegate.setForeground(c);
 691             }


 991                     }
 992                     LWKeyboardFocusManagerPeer.removeLastFocusRequest(getTarget());
 993                     return false;
 994                 }
 995 
 996                 KeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
 997                 Component focusOwner = kfmPeer.getCurrentFocusOwner();
 998                 return LWKeyboardFocusManagerPeer.deliverFocus(lightweightChild,
 999                         getTarget(), temporary,
1000                         focusedWindowChangeAllowed,
1001                         time, cause, focusOwner);
1002 
1003             case LWKeyboardFocusManagerPeer.SNFH_SUCCESS_HANDLED:
1004                 return true;
1005         }
1006 
1007         return false;
1008     }
1009 
1010     @Override
1011     public final Image createImage(final ImageProducer producer) {
1012         return new ToolkitImage(producer);
1013     }
1014 
1015     @Override
1016     public final Image createImage(final int width, final int height) {
1017         return getLWGC().createAcceleratedImage(getTarget(), width, height);

1018     }
1019 
1020     @Override
1021     public final VolatileImage createVolatileImage(final int w, final int h) {

1022         return new SunVolatileImage(getTarget(), w, h);
1023     }
1024 
1025     @Override
1026     public boolean prepareImage(Image img, int w, int h, ImageObserver o) {
1027         // TODO: is it a right/complete implementation?
1028         return getToolkit().prepareImage(img, w, h, o);
1029     }
1030 
1031     @Override
1032     public int checkImage(Image img, int w, int h, ImageObserver o) {
1033         // TODO: is it a right/complete implementation?
1034         return getToolkit().checkImage(img, w, h, o);
1035     }
1036 
1037     @Override
1038     public boolean handlesWheelScrolling() {
1039         // TODO: not implemented
1040         return false;
1041     }


1112                 if (--fNumDropTargets == 0) {
1113                     // Having a null drop target would be an error but let's check just in case:
1114                     if (fDropTarget != null) {
1115                         // Dispose of the drop target:
1116                         fDropTarget.dispose();
1117                         fDropTarget = null;
1118                     } else
1119                         System.err.println("CComponent.removeDropTarget(): current drop target is null.");
1120                 }
1121             }
1122         }
1123     }
1124 
1125     // ---- PEER NOTIFICATIONS ---- //
1126 
1127     /**
1128      * Called when this peer's location has been changed either as a result
1129      * of target.setLocation() or as a result of user actions (window is
1130      * dragged with mouse).
1131      *


1132      * This method could be called on the toolkit thread.
1133      */
1134     protected final void handleMove(final int x, final int y,
1135                                     final boolean updateTarget) {
1136         if (updateTarget) {
1137             AWTAccessor.getComponentAccessor().setLocation(getTarget(), x, y);
1138         }
1139         postEvent(new ComponentEvent(getTarget(),
1140                                      ComponentEvent.COMPONENT_MOVED));
1141     }
1142 
1143     /**
1144      * Called when this peer's size has been changed either as a result of
1145      * target.setSize() or as a result of user actions (window is resized).
1146      *



1147      * This method could be called on the toolkit thread.
1148      */
1149     protected final void handleResize(final int w, final int h,
1150                                       final boolean updateTarget) {
1151         Image oldBB = null;
1152         synchronized (getStateLock()) {
1153             if (backBuffer != null) {
1154                 oldBB = backBuffer;
1155                 backBuffer = getLWGC().createBackBuffer(this);
1156             }
1157         }
1158         getLWGC().destroyBackBuffer(oldBB);
1159 
1160         if (updateTarget) {
1161             AWTAccessor.getComponentAccessor().setSize(getTarget(), w, h);
1162         }
1163         postEvent(new ComponentEvent(getTarget(),
1164                                      ComponentEvent.COMPONENT_RESIZED));
1165     }
1166 
1167     protected final void repaintOldNewBounds(final Rectangle oldB) {
1168         repaintParent(oldB);
1169         repaintPeer(getSize());
1170     }
1171 
1172     protected final void repaintParent(final Rectangle oldB) {
1173         final LWContainerPeer cp = getContainerPeer();
1174         if (cp != null) {
1175             // Repaint unobscured part of the parent
1176             cp.repaintPeer(cp.getContentSize().intersection(oldB));
1177         }
1178     }
1179