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

Print this page




 106     private final Rectangle bounds = new Rectangle();
 107     private Region region;
 108 
 109     // Component state. Should be accessed under the state lock
 110     private boolean visible = false;
 111     private boolean enabled = true;
 112 
 113     private Color background;
 114     private Color foreground;
 115     private Font font;
 116 
 117     /**
 118      * Paint area to coalesce all the paint events and store the target dirty
 119      * area.
 120      */
 121     private final RepaintArea targetPaintArea;
 122 
 123     //   private volatile boolean paintPending;
 124     private volatile boolean isLayouting;
 125 
 126     private D delegate = null;
 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     private final class DelegateContainer extends Container {
 137         {
 138             enableEvents(0xFFFFFFFF);
 139         }
 140 
 141         DelegateContainer() {
 142             super();
 143         }
 144 
 145         @Override
 146         public boolean isLightweight() {
 147             return false;
 148         }
 149 
 150         @Override
 151         public Point getLocation() {
 152             return getLocationOnScreen();
 153         }
 154 
 155         @Override


 250                     Field field = Toolkit.class.getDeclaredField("eventListener");
 251                     field.setAccessible(true);
 252                     field.set(toolkit, listener);
 253                 } catch (Exception e) {
 254                     throw new InternalError(e.toString());
 255                 }
 256                 return null;
 257             }
 258         });
 259     }
 260 
 261     /**
 262      * This method is called under getDelegateLock().
 263      * Overridden in subclasses.
 264      */
 265     protected D createDelegate() {
 266         return null;
 267     }
 268 
 269     protected final D getDelegate() {
 270         synchronized (getStateLock()) {
 271             return delegate;
 272         }
 273     }
 274 
 275     protected Component getDelegateFocusOwner() {
 276         return getDelegate();
 277     }
 278 
 279     /**
 280      * Initializes this peer. The call to initialize() is not placed to
 281      * LWComponentPeer ctor to let the subclass ctor to finish completely first.
 282      * Instead, it's the LWToolkit object who is responsible for initialization.
 283      * Note that we call setVisible() at the end of initialization.
 284      */
 285     public final void initialize() {
 286         platformComponent.initialize(getPlatformWindow());
 287         initializeImpl();
 288         setVisible(target.isVisible());
 289     }
 290 
 291     /**
 292      * Fetching general properties from the target. Should be overridden in
 293      * subclasses to initialize specific peers properties.


 681             font = f;
 682         }
 683         final D delegate = getDelegate();
 684         if (delegate != null) {
 685             synchronized (getDelegateLock()) {
 686                 // delegate will repaint the target
 687                 delegate.setFont(f);
 688             }
 689         } else {
 690             repaintPeer();
 691         }
 692     }
 693 
 694     protected final Font getFont() {
 695         synchronized (getStateLock()) {
 696             return font;
 697         }
 698     }
 699 
 700     @Override
 701     public FontMetrics getFontMetrics(Font f) {
 702         // Borrow the metrics from the top-level window
 703 //        return getWindowPeer().getFontMetrics(f);
 704         // Obtain the metrics from the offscreen window where this peer is
 705         // mostly drawn to.
 706         // TODO: check for "use platform metrics" settings
 707         Graphics g = getWindowPeer().getGraphics();
 708         try {
 709             if (g != null) {

 710                 return g.getFontMetrics(f);
 711             } else {
 712                 synchronized (getDelegateLock()) {
 713                     return delegateContainer.getFontMetrics(f);
 714                 }
 715             }
 716         } finally {
 717             if (g != null) {
 718                 g.dispose();
 719             }
 720         }



 721     }
 722 
 723     @Override
 724     public void setEnabled(final boolean e) {
 725         boolean status = e;
 726         final LWComponentPeer cp = getContainerPeer();
 727         if (cp != null) {
 728             status &= cp.isEnabled();
 729         }
 730         synchronized (getStateLock()) {
 731             if (enabled == status) {
 732                 return;
 733             }
 734             enabled = status;
 735         }
 736 
 737         final D delegate = getDelegate();
 738 
 739         if (delegate != null) {
 740             synchronized (getDelegateLock()) {


 830      * Should be overridden in subclasses which use complex Swing components.
 831      */
 832     @Override
 833     public void layout() {
 834         // TODO: not implemented
 835     }
 836 
 837     @Override
 838     public boolean isObscured() {
 839         // TODO: not implemented
 840         return false;
 841     }
 842 
 843     @Override
 844     public boolean canDetermineObscurity() {
 845         // TODO: not implemented
 846         return false;
 847     }
 848 
 849     /**
 850      * Should be overridden in subclasses to forward the request
 851      * to the Swing helper component, if required.

 852      */
 853     @Override
 854     public Dimension getPreferredSize() {
 855         // It looks like a default implementation for all toolkits
 856         return getMinimumSize();



 857     }
 858 
 859     /*
 860      * Should be overridden in subclasses to forward the request
 861      * to the Swing helper component.

 862      */
 863     @Override
 864     public Dimension getMinimumSize() {
 865         D delegate = getDelegate();
 866 
 867         if (delegate == null) {
 868             // Is it a correct default value?
 869             return getBounds().getSize();
 870         } else {
 871             synchronized (getDelegateLock()) {
 872                 return delegate.getMinimumSize();
 873             }














 874         }

 875     }
 876 
 877     @Override
 878     public void updateCursorImmediately() {
 879         getLWToolkit().getCursorManager().updateCursor();
 880     }
 881 
 882     @Override
 883     public boolean isFocusable() {
 884         // Overridden in focusable subclasses like buttons
 885         return false;
 886     }
 887 
 888     @Override
 889     public boolean requestFocus(Component lightweightChild, boolean temporary,
 890                                 boolean focusedWindowChangeAllowed, long time,
 891                                 CausedFocusEvent.Cause cause)
 892     {
 893         if (focusLog.isLoggable(PlatformLogger.FINEST)) {
 894             focusLog.finest("lightweightChild=" + lightweightChild + ", temporary=" + temporary +




 106     private final Rectangle bounds = new Rectangle();
 107     private Region region;
 108 
 109     // Component state. Should be accessed under the state lock
 110     private boolean visible = false;
 111     private boolean enabled = true;
 112 
 113     private Color background;
 114     private Color foreground;
 115     private Font font;
 116 
 117     /**
 118      * Paint area to coalesce all the paint events and store the target dirty
 119      * area.
 120      */
 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


 255                     Field field = Toolkit.class.getDeclaredField("eventListener");
 256                     field.setAccessible(true);
 257                     field.set(toolkit, listener);
 258                 } catch (Exception e) {
 259                     throw new InternalError(e.toString());
 260                 }
 261                 return null;
 262             }
 263         });
 264     }
 265 
 266     /**
 267      * This method is called under getDelegateLock().
 268      * Overridden in subclasses.
 269      */
 270     protected D createDelegate() {
 271         return null;
 272     }
 273 
 274     protected final D getDelegate() {

 275         return delegate;
 276     }

 277 
 278     protected Component getDelegateFocusOwner() {
 279         return getDelegate();
 280     }
 281 
 282     /**
 283      * Initializes this peer. The call to initialize() is not placed to
 284      * LWComponentPeer ctor to let the subclass ctor to finish completely first.
 285      * Instead, it's the LWToolkit object who is responsible for initialization.
 286      * Note that we call setVisible() at the end of initialization.
 287      */
 288     public final void initialize() {
 289         platformComponent.initialize(getPlatformWindow());
 290         initializeImpl();
 291         setVisible(target.isVisible());
 292     }
 293 
 294     /**
 295      * Fetching general properties from the target. Should be overridden in
 296      * subclasses to initialize specific peers properties.


 684             font = f;
 685         }
 686         final D delegate = getDelegate();
 687         if (delegate != null) {
 688             synchronized (getDelegateLock()) {
 689                 // delegate will repaint the target
 690                 delegate.setFont(f);
 691             }
 692         } else {
 693             repaintPeer();
 694         }
 695     }
 696 
 697     protected final Font getFont() {
 698         synchronized (getStateLock()) {
 699             return font;
 700         }
 701     }
 702 
 703     @Override
 704     public FontMetrics getFontMetrics(final Font f) {
 705         // Borrow the metrics from the top-level window
 706 //        return getWindowPeer().getFontMetrics(f);
 707         // Obtain the metrics from the offscreen window where this peer is
 708         // mostly drawn to.
 709         // TODO: check for "use platform metrics" settings
 710         final Graphics g = getOnscreenGraphics();

 711         if (g != null) {
 712             try {
 713                 return g.getFontMetrics(f);





 714             } finally {

 715                 g.dispose();
 716             }
 717         }
 718         synchronized (getDelegateLock()) {
 719             return delegateContainer.getFontMetrics(f);
 720         }
 721     }
 722 
 723     @Override
 724     public void setEnabled(final boolean e) {
 725         boolean status = e;
 726         final LWComponentPeer cp = getContainerPeer();
 727         if (cp != null) {
 728             status &= cp.isEnabled();
 729         }
 730         synchronized (getStateLock()) {
 731             if (enabled == status) {
 732                 return;
 733             }
 734             enabled = status;
 735         }
 736 
 737         final D delegate = getDelegate();
 738 
 739         if (delegate != null) {
 740             synchronized (getDelegateLock()) {


 830      * Should be overridden in subclasses which use complex Swing components.
 831      */
 832     @Override
 833     public void layout() {
 834         // TODO: not implemented
 835     }
 836 
 837     @Override
 838     public boolean isObscured() {
 839         // TODO: not implemented
 840         return false;
 841     }
 842 
 843     @Override
 844     public boolean canDetermineObscurity() {
 845         // TODO: not implemented
 846         return false;
 847     }
 848 
 849     /**
 850      * Determines the preferred size of the component. By default forwards the
 851      * request to the Swing helper component. Should be overridden in subclasses
 852      * if required.
 853      */
 854     @Override
 855     public Dimension getPreferredSize() {
 856         final Dimension size;
 857         synchronized (getDelegateLock()) {
 858             size = getDelegate().getPreferredSize();
 859         }
 860         return validateSize(size);
 861     }
 862 
 863     /**
 864      * Determines the minimum size of the component. By default forwards the
 865      * request to the Swing helper component. Should be overridden in subclasses
 866      * if required.
 867      */
 868     @Override
 869     public Dimension getMinimumSize() {
 870         final Dimension size;





 871         synchronized (getDelegateLock()) {
 872             size = getDelegate().getMinimumSize();
 873         }
 874         return validateSize(size);
 875     }
 876 
 877     /**
 878      * In some situations delegates can return empty minimum/preferred size.
 879      * (For example: empty JLabel, etc), but awt components never should be
 880      * empty. In the XPeers or WPeers we use some magic constants, but here we
 881      * try to use something more useful,
 882      */
 883     private Dimension validateSize(final Dimension size) {
 884         if (size.width == 0 || size.height == 0) {
 885             final FontMetrics fm = getFontMetrics(getFont());
 886             size.width = fm.charWidth(WIDE_CHAR);
 887             size.height = fm.getHeight();
 888         }
 889         return size;
 890     }
 891 
 892     @Override
 893     public void updateCursorImmediately() {
 894         getLWToolkit().getCursorManager().updateCursor();
 895     }
 896 
 897     @Override
 898     public boolean isFocusable() {
 899         // Overridden in focusable subclasses like buttons
 900         return false;
 901     }
 902 
 903     @Override
 904     public boolean requestFocus(Component lightweightChild, boolean temporary,
 905                                 boolean focusedWindowChangeAllowed, long time,
 906                                 CausedFocusEvent.Cause cause)
 907     {
 908         if (focusLog.isLoggable(PlatformLogger.FINEST)) {
 909             focusLog.finest("lightweightChild=" + lightweightChild + ", temporary=" + temporary +