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

Print this page


   1 /*
   2  * Copyright (c) 2011, 2013, 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


  25 
  26 
  27 package sun.lwawt;
  28 
  29 import java.awt.*;
  30 
  31 import java.awt.dnd.DropTarget;
  32 import java.awt.dnd.peer.DropTargetPeer;
  33 import java.awt.event.*;
  34 
  35 import java.awt.image.ColorModel;
  36 import java.awt.image.ImageObserver;
  37 import java.awt.image.ImageProducer;
  38 import java.awt.image.VolatileImage;
  39 
  40 import java.awt.peer.ComponentPeer;
  41 import java.awt.peer.ContainerPeer;
  42 
  43 import java.awt.peer.KeyboardFocusManagerPeer;
  44 import java.util.concurrent.atomic.AtomicBoolean;
  45 import java.lang.reflect.Field;
  46 import java.security.AccessController;
  47 import java.security.PrivilegedAction;
  48 
  49 import sun.awt.*;
  50 
  51 import sun.awt.event.IgnorePaintEvent;
  52 
  53 import sun.awt.image.SunVolatileImage;
  54 import sun.awt.image.ToolkitImage;
  55 
  56 import sun.java2d.SunGraphics2D;
  57 import sun.java2d.opengl.OGLRenderQueue;
  58 import sun.java2d.pipe.Region;
  59 
  60 import sun.util.logging.PlatformLogger;
  61 
  62 import javax.swing.JComponent;
  63 import javax.swing.SwingUtilities;
  64 import javax.swing.RepaintManager;
  65 
  66 import sun.lwawt.macosx.CDropTarget;
  67 


 115     private Region region;
 116 
 117     // Component state. Should be accessed under the state lock
 118     private boolean visible = false;
 119     private boolean enabled = true;
 120 
 121     private Color background;
 122     private Color foreground;
 123     private Font font;
 124 
 125     /**
 126      * Paint area to coalesce all the paint events and store the target dirty
 127      * area.
 128      */
 129     private final RepaintArea targetPaintArea;
 130 
 131     //   private volatile boolean paintPending;
 132     private volatile boolean isLayouting;
 133 
 134     private final D delegate;
 135     private Container delegateContainer;
 136     private Component delegateDropTarget;
 137     private final Object dropTargetLock = new Object();
 138 
 139     private int fNumDropTargets = 0;
 140     private CDropTarget fDropTarget = null;
 141 
 142     private final PlatformComponent platformComponent;
 143 
 144     /**
 145      * Character with reasonable value between the minimum width and maximum.
 146      */
 147     static final char WIDE_CHAR = '0';
 148 
 149     /**
 150      * The back buffer provide user with a BufferStrategy.
 151      */
 152     private Image backBuffer;
 153 
 154     /**
 155      * All Swing delegates use delegateContainer as a parent. This container
 156      * intentionally do not use parent of the peer.
 157      */
 158     @SuppressWarnings("serial")// Safe: outer class is non-serializable.
 159     private final class DelegateContainer extends Container {
 160         {
 161             enableEvents(0xFFFFFFFF);
 162         }
 163 
 164         // Empty non private constructor was added because access to this
 165         // class shouldn't be emulated by a synthetic accessor method.
 166         DelegateContainer() {
 167             super();
 168         }
 169 
 170         @Override
 171         public boolean isLightweight() {
 172             return false;
 173         }
 174 
 175         @Override
 176         public Point getLocation() {
 177             return getLocationOnScreen();
 178         }
 179 
 180         @Override
 181         public Point getLocationOnScreen() {
 182             return LWComponentPeer.this.getLocationOnScreen();


 195 
 196     LWComponentPeer(final T target, final PlatformComponent platformComponent) {
 197         targetPaintArea = new LWRepaintArea();
 198         this.target = target;
 199         this.platformComponent = platformComponent;
 200 
 201         // Container peer is always null for LWWindowPeers, so
 202         // windowPeer is always null for them as well. On the other
 203         // hand, LWWindowPeer shouldn't use windowPeer at all
 204         final Container container = SunToolkit.getNativeContainer(target);
 205         containerPeer = (LWContainerPeer) LWToolkit.targetToPeer(container);
 206         windowPeer = containerPeer != null ? containerPeer.getWindowPeerOrSelf()
 207                                            : null;
 208         // don't bother about z-order here as updateZOrder()
 209         // will be called from addNotify() later anyway
 210         if (containerPeer != null) {
 211             containerPeer.addChildPeer(this);
 212         }
 213 
 214         // the delegate must be created after the target is set
 215         AWTEventListener toolkitListener = null;
 216         synchronized (Toolkit.getDefaultToolkit()) {
 217             try {
 218                 toolkitListener = getToolkitAWTEventListener();
 219                 setToolkitAWTEventListener(null);
 220 
 221                 synchronized (getDelegateLock()) {
 222                     delegate = createDelegate();
 223                     if (delegate != null) {



 224                         delegate.setVisible(false);
 225                         delegateContainer = new DelegateContainer();
 226                         delegateContainer.add(delegate);
 227                         delegateContainer.addNotify();
 228                         delegate.addNotify();
 229                         resetColorsAndFont(delegate);
 230                         delegate.setOpaque(true);
 231                     } else {
 232                         return;
 233                     }
 234                 }
 235 
 236             } finally {
 237                 setToolkitAWTEventListener(toolkitListener);
 238             }
 239 
 240             // todo swing: later on we will probably have one global RM
 241             SwingUtilities3.setDelegateRepaintManager(delegate, new RepaintManager() {
 242                 @Override
 243                 public void addDirtyRegion(final JComponent c, final int x, final int y, final int w, final int h) {

 244                     repaintPeer(SwingUtilities.convertRectangle(
 245                             c, new Rectangle(x, y, w, h), getDelegate()));
 246                 }
 247             });
 248         }
 249     }
 250 
 251     /**
 252      * This method must be called under Toolkit.getDefaultToolkit() lock
 253      * and followed by setToolkitAWTEventListener()
 254      */
 255     protected final AWTEventListener getToolkitAWTEventListener() {
 256         return AccessController.doPrivileged(new PrivilegedAction<AWTEventListener>() {
 257             public AWTEventListener run() {
 258                 Toolkit toolkit = Toolkit.getDefaultToolkit();
 259                 try {
 260                     Field field = Toolkit.class.getDeclaredField("eventListener");
 261                     field.setAccessible(true);
 262                     return (AWTEventListener) field.get(toolkit);
 263                 } catch (Exception e) {
 264                     throw new InternalError(e.toString());
 265                 }
 266             }
 267         });
 268     }
 269 
 270     protected final void setToolkitAWTEventListener(final AWTEventListener listener) {
 271         AccessController.doPrivileged(new PrivilegedAction<Void>() {
 272             public Void run() {
 273                 Toolkit toolkit = Toolkit.getDefaultToolkit();
 274                 try {
 275                     Field field = Toolkit.class.getDeclaredField("eventListener");
 276                     field.setAccessible(true);
 277                     field.set(toolkit, listener);
 278                 } catch (Exception e) {
 279                     throw new InternalError(e.toString());
 280                 }
 281                 return null;
 282             }
 283         });
 284     }
 285 
 286     /**
 287      * This method is called under getDelegateLock().
 288      * Overridden in subclasses.
 289      */
 290     D createDelegate() {
 291         return null;
 292     }
 293 
 294     final D getDelegate() {
 295         return delegate;
 296     }
 297 
 298     /**
 299      * This method should be called under getDelegateLock().
 300      */
 301     Component getDelegateFocusOwner() {


   1 /*
   2  * Copyright (c) 2011, 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


  25 
  26 
  27 package sun.lwawt;
  28 
  29 import java.awt.*;
  30 
  31 import java.awt.dnd.DropTarget;
  32 import java.awt.dnd.peer.DropTargetPeer;
  33 import java.awt.event.*;
  34 
  35 import java.awt.image.ColorModel;
  36 import java.awt.image.ImageObserver;
  37 import java.awt.image.ImageProducer;
  38 import java.awt.image.VolatileImage;
  39 
  40 import java.awt.peer.ComponentPeer;
  41 import java.awt.peer.ContainerPeer;
  42 
  43 import java.awt.peer.KeyboardFocusManagerPeer;
  44 import java.util.concurrent.atomic.AtomicBoolean;



  45 
  46 import sun.awt.*;
  47 
  48 import sun.awt.event.IgnorePaintEvent;
  49 
  50 import sun.awt.image.SunVolatileImage;
  51 import sun.awt.image.ToolkitImage;
  52 
  53 import sun.java2d.SunGraphics2D;
  54 import sun.java2d.opengl.OGLRenderQueue;
  55 import sun.java2d.pipe.Region;
  56 
  57 import sun.util.logging.PlatformLogger;
  58 
  59 import javax.swing.JComponent;
  60 import javax.swing.SwingUtilities;
  61 import javax.swing.RepaintManager;
  62 
  63 import sun.lwawt.macosx.CDropTarget;
  64 


 112     private Region region;
 113 
 114     // Component state. Should be accessed under the state lock
 115     private boolean visible = false;
 116     private boolean enabled = true;
 117 
 118     private Color background;
 119     private Color foreground;
 120     private Font font;
 121 
 122     /**
 123      * Paint area to coalesce all the paint events and store the target dirty
 124      * area.
 125      */
 126     private final RepaintArea targetPaintArea;
 127 
 128     //   private volatile boolean paintPending;
 129     private volatile boolean isLayouting;
 130 
 131     private final D delegate;
 132     private final Container delegateContainer;
 133     private Component delegateDropTarget;
 134     private final Object dropTargetLock = new Object();
 135 
 136     private int fNumDropTargets = 0;
 137     private CDropTarget fDropTarget = null;
 138 
 139     private final PlatformComponent platformComponent;
 140 
 141     /**
 142      * Character with reasonable value between the minimum width and maximum.
 143      */
 144     static final char WIDE_CHAR = '0';
 145 
 146     /**
 147      * The back buffer provide user with a BufferStrategy.
 148      */
 149     private Image backBuffer;
 150 
 151     /**
 152      * All Swing delegates use delegateContainer as a parent. This container
 153      * intentionally do not use parent of the peer.
 154      */
 155     @SuppressWarnings("serial")// Safe: outer class is non-serializable.
 156     private final class DelegateContainer extends Container {



 157 
 158         // Empty non private constructor was added because access to this
 159         // class shouldn't be emulated by a synthetic accessor method.
 160         DelegateContainer() {
 161             super();
 162         }
 163 
 164         @Override
 165         public boolean isLightweight() {
 166             return false;
 167         }
 168 
 169         @Override
 170         public Point getLocation() {
 171             return getLocationOnScreen();
 172         }
 173 
 174         @Override
 175         public Point getLocationOnScreen() {
 176             return LWComponentPeer.this.getLocationOnScreen();


 189 
 190     LWComponentPeer(final T target, final PlatformComponent platformComponent) {
 191         targetPaintArea = new LWRepaintArea();
 192         this.target = target;
 193         this.platformComponent = platformComponent;
 194 
 195         // Container peer is always null for LWWindowPeers, so
 196         // windowPeer is always null for them as well. On the other
 197         // hand, LWWindowPeer shouldn't use windowPeer at all
 198         final Container container = SunToolkit.getNativeContainer(target);
 199         containerPeer = (LWContainerPeer) LWToolkit.targetToPeer(container);
 200         windowPeer = containerPeer != null ? containerPeer.getWindowPeerOrSelf()
 201                                            : null;
 202         // don't bother about z-order here as updateZOrder()
 203         // will be called from addNotify() later anyway
 204         if (containerPeer != null) {
 205             containerPeer.addChildPeer(this);
 206         }
 207 
 208         // the delegate must be created after the target is set






 209         synchronized (getDelegateLock()) {
 210             delegate = createDelegate();
 211             if (delegate == null) {
 212                 delegateContainer = null;
 213                 return;
 214             }
 215             delegate.setVisible(false);
 216             delegateContainer = new DelegateContainer();
 217             delegateContainer.add(delegate);
 218             delegateContainer.addNotify();

 219             resetColorsAndFont(delegate);
 220             delegate.setOpaque(true);







 221         }
 222 
 223         // todo swing: later on we will probably have one global RM
 224         SwingUtilities3.setDelegateRepaintManager(delegate, new RepaintManager() {
 225             @Override
 226             public void addDirtyRegion(final JComponent c, final int x,
 227                                        final int y, final int w, final int h) {
 228                 repaintPeer(SwingUtilities.convertRectangle(
 229                         c, new Rectangle(x, y, w, h), getDelegate()));




































 230             }
 231         });
 232     }
 233 
 234     /**
 235      * This method is called under getDelegateLock().
 236      * Overridden in subclasses.
 237      */
 238     D createDelegate() {
 239         return null;
 240     }
 241 
 242     final D getDelegate() {
 243         return delegate;
 244     }
 245 
 246     /**
 247      * This method should be called under getDelegateLock().
 248      */
 249     Component getDelegateFocusOwner() {