changeset 4893:aa30a9aebd68

7124530: What is background color of AWT component? (And foreground, for that matter) Reviewed-by: alexp
author serb
date Thu, 26 Jan 2012 16:13:42 +0400
parents ff2d1f46256a
children 3e0b39f1caa6
files src/macosx/classes/com/apple/laf/AquaImageFactory.java src/macosx/classes/sun/lwawt/LWCanvasPeer.java src/macosx/classes/sun/lwawt/LWComponentPeer.java src/macosx/classes/sun/lwawt/LWContainerPeer.java src/macosx/classes/sun/lwawt/LWListPeer.java src/macosx/classes/sun/lwawt/LWPanelPeer.java src/macosx/classes/sun/lwawt/LWTextComponentPeer.java src/macosx/classes/sun/lwawt/LWWindowPeer.java src/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java src/macosx/native/sun/awt/CSystemColors.m
diffstat 10 files changed, 149 insertions(+), 239 deletions(-) [+]
line wrap: on
line diff
--- a/src/macosx/classes/com/apple/laf/AquaImageFactory.java	Thu Jan 26 08:58:24 2012 +0000
+++ b/src/macosx/classes/com/apple/laf/AquaImageFactory.java	Thu Jan 26 16:13:42 2012 +0400
@@ -453,7 +453,8 @@
     }
 
     public static Color getWindowBackgroundColorUIResource() {
-        return AquaNativeResources.getWindowBackgroundColorUIResource();
+        //return AquaNativeResources.getWindowBackgroundColorUIResource();
+        return new SystemColorProxy(SystemColor.window);
     }
 
     public static Color getTextSelectionBackgroundColorUIResource() {
--- a/src/macosx/classes/sun/lwawt/LWCanvasPeer.java	Thu Jan 26 08:58:24 2012 +0000
+++ b/src/macosx/classes/sun/lwawt/LWCanvasPeer.java	Thu Jan 26 16:13:42 2012 +0400
@@ -23,32 +23,25 @@
  * questions.
  */
 
+
 package sun.lwawt;
 
 import java.awt.BufferCapabilities;
 import java.awt.Canvas;
 import java.awt.Component;
-import java.awt.Dimension;
-import java.awt.Graphics;
 import java.awt.GraphicsConfiguration;
 import java.awt.Image;
 import java.awt.peer.CanvasPeer;
 
 import javax.swing.JComponent;
 
-final class LWCanvasPeer
-    extends LWComponentPeer<Component, JComponent>
-    implements CanvasPeer
-{
+final class LWCanvasPeer extends LWComponentPeer<Component, JComponent>
+        implements CanvasPeer {
+
     LWCanvasPeer(final Canvas target, PlatformComponent platformComponent) {
         super(target, platformComponent);
     }
 
-    @Override
-    public JComponent createDelegate() {
-        return new JCanvasDelegate();
-    }
-
     // ---- PEER METHODS ---- //
 
     @Override
@@ -64,8 +57,7 @@
 
     @Override
     public void flip(int x1, int y1, int x2, int y2,
-                     BufferCapabilities.FlipContents flipAction)
-    {
+                     BufferCapabilities.FlipContents flipAction) {
         // TODO
     }
 
@@ -81,28 +73,4 @@
         // TODO
         return gc;
     }
-
-    class JCanvasDelegate
-        extends JComponent
-    {
-
-        @Override
-        public Dimension getMinimumSize() {
-            return getTarget().getSize();
-        }
-
-        @Override
-        public Dimension getPreferredSize() {
-            return getTarget().getSize();
-        }
-
-        @Override
-        protected void paintComponent(final Graphics g) {
-            if (isOpaque()) {
-                g.setColor(getBackground());
-                g.fillRect(0, 0, getWidth(), getHeight());
-                g.setColor(getForeground());
-            }
-        }
-    }
 }
--- a/src/macosx/classes/sun/lwawt/LWComponentPeer.java	Thu Jan 26 08:58:24 2012 +0000
+++ b/src/macosx/classes/sun/lwawt/LWComponentPeer.java	Thu Jan 26 16:13:42 2012 +0400
@@ -41,7 +41,6 @@
 import java.awt.peer.ContainerPeer;
 
 import java.util.concurrent.atomic.AtomicBoolean;
-import java.beans.Transient;
 import java.lang.reflect.Field;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
@@ -114,6 +113,10 @@
     private boolean visible = false;
     private boolean enabled = true;
 
+    private Color background;
+    private Color foreground;
+    private Font font;
+
     // Paint area to coalesce all the paint events and store
     // the target dirty area
     private RepaintArea targetPaintArea;
@@ -164,24 +167,6 @@
         public int getY() {
             return getLocation().y;
         }
-
-        @Override
-        @Transient
-        public Color getBackground() {
-            return getTarget().getBackground();
-        }
-
-        @Override
-        @Transient
-        public Color getForeground() {
-            return getTarget().getForeground();
-        }
-
-        @Override
-        @Transient
-        public Font getFont() {
-            return getTarget().getFont();
-        }
     }
 
     public LWComponentPeer(T target, PlatformComponent platformComponent) {
@@ -228,10 +213,13 @@
             SwingUtilities3.setDelegateRepaintManager(delegate, new RepaintManager() {
                 @Override
                 public void addDirtyRegion(final JComponent c, final int x, final int y, final int w, final int h) {
-                    // Repainting in Swing is asynchronous, so it is emulated here by using invokeLater()
-                    // to extract the painting call from the event's processing routine
-                    //TODO: so why exactly do we have to emulate this in lwawt? We use a back-buffer anyway,
-                    //why not paint the components synchronously into the buffer?
+                    if (SunToolkit.isDispatchThreadForAppContext(getTarget())) {
+                        synchronized (getDelegateLock()) {
+                            if (getDelegate().isPaintingForPrint()) {
+                                return;
+                            }
+                        }
+                    }
                     Rectangle res = SwingUtilities.convertRectangle(
                             c, new Rectangle(x, y, w, h), getDelegate());
                     repaintPeer(res);
@@ -302,24 +290,15 @@
     public void initialize() {
         platformComponent.initialize(target, this, getPlatformWindow());
         targetPaintArea = new LWRepaintArea();
-        synchronized (getDelegateLock()) {
-            if (getDelegate() != null) {
+        if (getDelegate() != null) {
+            synchronized (getDelegateLock()) {
+                resetColorsAndFont(delegate);
                 getDelegate().setOpaque(true);
-                resetColorsAndFont(delegate);
-                // we must explicitly set the font here
-                // see Component.getFont_NoClientCode() for details
-                delegateContainer.setFont(target.getFont());
             }
         }
-        if (target.isBackgroundSet()) {
-            setBackground(target.getBackground());
-        }
-        if (target.isForegroundSet()) {
-            setForeground(target.getForeground());
-        }
-        if (target.isFontSet()) {
-            setFont(target.getFont());
-        }
+        setBackground(target.getBackground());
+        setForeground(target.getForeground());
+        setFont(target.getFont());
         setBounds(target.getBounds());
         setEnabled(target.isEnabled());
         setVisible(target.isVisible());
@@ -638,71 +617,81 @@
     }
 
     @Override
-    public void setBackground(Color c) {
-        synchronized (getDelegateLock()) {
-            D delegate = getDelegate();
-            if (delegate != null) {
+    public void setBackground(final Color c) {
+        final Color oldBg = getBackground();
+        if (oldBg == c || (oldBg != null && oldBg.equals(c))) {
+            return;
+        }
+        synchronized (getStateLock()) {
+            background = c;
+        }
+        final D delegate = getDelegate();
+        if (delegate != null) {
+            synchronized (getDelegateLock()) {
                 // delegate will repaint the target
                 delegate.setBackground(c);
             }
+        } else {
+            repaintPeer();
         }
     }
 
-    protected Color getBackground() {
-        synchronized (getDelegateLock()) {
-            D delegate = getDelegate();
-            if (delegate != null) {
-                return delegate.getBackground();
-            }
+    protected final Color getBackground() {
+        synchronized (getStateLock()) {
+            return background;
         }
-        return null;
     }
 
     @Override
-    public void setForeground(Color c) {
-        synchronized (getDelegateLock()) {
-            D delegate = getDelegate();
-            if (delegate != null) {
+    public void setForeground(final Color c) {
+        final Color oldFg = getForeground();
+        if (oldFg == c || (oldFg != null && oldFg.equals(c))) {
+            return;
+        }
+        synchronized (getStateLock()) {
+            foreground = c;
+        }
+        final D delegate = getDelegate();
+        if (delegate != null) {
+            synchronized (getDelegateLock()) {
                 // delegate will repaint the target
                 delegate.setForeground(c);
             }
+        } else {
+            repaintPeer();
         }
     }
 
-    protected Color getForeground() {
-        synchronized (getDelegateLock()) {
-            D delegate = getDelegate();
-            if (delegate != null) {
-                return delegate.getForeground();
-            }
+    protected final Color getForeground() {
+        synchronized (getStateLock()) {
+            return foreground;
         }
-        return null;
     }
 
     @Override
-    public void setFont(Font f) {
-        synchronized (getDelegateLock()) {
-            D delegate = getDelegate();
-            if (delegate != null) {
+    public void setFont(final Font f) {
+        final Font oldF = getFont();
+        if (oldF == f || (oldF != null && oldF.equals(f))) {
+            return;
+        }
+        synchronized (getStateLock()) {
+            font = f;
+        }
+        final D delegate = getDelegate();
+        if (delegate != null) {
+            synchronized (getDelegateLock()) {
                 // delegate will repaint the target
                 delegate.setFont(f);
-                if (f == null) {
-                    // we must explicitly set the font here
-                    // see Component.getFont_NoClientCode() for details
-                    delegateContainer.setFont(getTarget().getFont());
-                }
             }
+        } else {
+            repaintPeer();
         }
     }
 
-    protected Font getFont() {
-        synchronized (getDelegateLock()) {
-            D delegate = getDelegate();
-            if (delegate != null) {
-                return delegate.getFont();
-            }
+    protected final Font getFont() {
+        synchronized (getStateLock()) {
+            return font;
         }
-        return null;
     }
 
     @Override
@@ -754,7 +743,7 @@
     }
 
     // Helper method
-    public boolean isEnabled() {
+    public final boolean isEnabled() {
         synchronized (getStateLock()) {
             return enabled;
         }
@@ -784,7 +773,7 @@
     }
 
     // Helper method
-    public boolean isVisible() {
+    public final boolean isVisible() {
         synchronized (getStateLock()) {
             return visible;
         }
@@ -1322,7 +1311,7 @@
      * Paints the peer. Overridden in subclasses to delegate the actual painting
      * to Swing components.
      */
-    protected void paintPeer(final Graphics g) {
+    protected final void paintPeer(final Graphics g) {
         final D delegate = getDelegate();
         if (delegate != null) {
             if (!SwingUtilities.isEventDispatchThread()) {
--- a/src/macosx/classes/sun/lwawt/LWContainerPeer.java	Thu Jan 26 08:58:24 2012 +0000
+++ b/src/macosx/classes/sun/lwawt/LWContainerPeer.java	Thu Jan 26 16:13:42 2012 +0400
@@ -28,7 +28,9 @@
 import sun.awt.SunGraphicsCallback;
 import sun.java2d.pipe.Region;
 
+import java.awt.Color;
 import java.awt.Container;
+import java.awt.Font;
 import java.awt.Graphics;
 import java.awt.Insets;
 import java.awt.Rectangle;
@@ -234,20 +236,50 @@
     }
 
     @Override
+    public void setBackground(final Color c) {
+        for (final LWComponentPeer child : getChildren()) {
+            if (!child.getTarget().isBackgroundSet()) {
+                child.setBackground(c);
+            }
+        }
+        super.setBackground(c);
+    }
+
+    @Override
+    public void setForeground(final Color c) {
+        for (final LWComponentPeer child : getChildren()) {
+            if (!child.getTarget().isForegroundSet()) {
+                child.setForeground(c);
+            }
+        }
+        super.setForeground(c);
+    }
+
+    @Override
+    public void setFont(final Font f) {
+        for (final LWComponentPeer child : getChildren()) {
+            if (!child.getTarget().isFontSet()) {
+                child.setFont(f);
+            }
+        }
+        super.setFont(f);
+    }
+
+    @Override
     public final void paint(final Graphics g) {
         super.paint(g);
-        SunGraphicsCallback.PaintHeavyweightComponentsCallback.getInstance().
-            runComponents(getTarget().getComponents(), g,
-                          SunGraphicsCallback.LIGHTWEIGHTS
-                          | SunGraphicsCallback.HEAVYWEIGHTS);
+        SunGraphicsCallback.PaintHeavyweightComponentsCallback.getInstance()
+                .runComponents(getTarget().getComponents(), g,
+                               SunGraphicsCallback.LIGHTWEIGHTS
+                               | SunGraphicsCallback.HEAVYWEIGHTS);
     }
 
     @Override
     public final void print(final Graphics g) {
         super.print(g);
-        SunGraphicsCallback.PrintHeavyweightComponentsCallback.getInstance().
-            runComponents(getTarget().getComponents(), g,
-                          SunGraphicsCallback.LIGHTWEIGHTS
-                          | SunGraphicsCallback.HEAVYWEIGHTS);
+        SunGraphicsCallback.PrintHeavyweightComponentsCallback.getInstance()
+                .runComponents(getTarget().getComponents(), g,
+                               SunGraphicsCallback.LIGHTWEIGHTS
+                               | SunGraphicsCallback.HEAVYWEIGHTS);
     }
 }
--- a/src/macosx/classes/sun/lwawt/LWListPeer.java	Thu Jan 26 08:58:24 2012 +0000
+++ b/src/macosx/classes/sun/lwawt/LWListPeer.java	Thu Jan 26 16:13:42 2012 +0400
@@ -38,6 +38,9 @@
 
     LWListPeer(final List target, final PlatformComponent platformComponent) {
         super(target, platformComponent);
+        if (!getTarget().isBackgroundSet()) {
+            getTarget().setBackground(SystemColor.text);
+        }
     }
 
     @Override
@@ -45,6 +48,7 @@
         return new ScrollableJList();
     }
 
+    @Override
     public void initialize() {
         super.initialize();
         setMultipleMode(getTarget().isMultipleMode());
@@ -56,6 +60,7 @@
         }
     }
 
+    @Override
     public boolean isFocusable() {
         return true;
     }
@@ -224,30 +229,6 @@
         }
 
         @Override
-        public void setBackground(final Color bg) {
-            super.setBackground(bg);
-            if (getView() != null) {
-                getView().setBackground(bg);
-            }
-        }
-
-        @Override
-        public void setForeground(final Color fg) {
-            super.setForeground(fg);
-            if (getView() != null) {
-                getView().setForeground(fg);
-            }
-        }
-
-        @Override
-        public void setFont(final Font font) {
-            super.setFont(font);
-            if (getView() != null) {
-                getView().setFont(font);
-            }
-        }
-
-        @Override
         public void setOpaque(final boolean isOpaque) {
             super.setOpaque(isOpaque);
             if (getView() != null) {
--- a/src/macosx/classes/sun/lwawt/LWPanelPeer.java	Thu Jan 26 08:58:24 2012 +0000
+++ b/src/macosx/classes/sun/lwawt/LWPanelPeer.java	Thu Jan 26 16:13:42 2012 +0400
@@ -46,6 +46,6 @@
 
     @Override
     public Dimension getMinimumSize() {
-        return getTarget().getSize();
+        return getBounds().getSize();
     }
 }
--- a/src/macosx/classes/sun/lwawt/LWTextComponentPeer.java	Thu Jan 26 08:58:24 2012 +0000
+++ b/src/macosx/classes/sun/lwawt/LWTextComponentPeer.java	Thu Jan 26 16:13:42 2012 +0400
@@ -57,14 +57,14 @@
     LWTextComponentPeer(final T target,
                         final PlatformComponent platformComponent) {
         super(target, platformComponent);
+        if (!getTarget().isBackgroundSet()) {
+            getTarget().setBackground(SystemColor.text);
+        }
     }
 
     @Override
     public void initialize() {
         super.initialize();
-        if (!getTarget().isBackgroundSet()) {
-            getTarget().setBackground(SystemColor.text);
-        }
         synchronized (getDelegateLock()) {
             // This listener should be added before setText().
             getTextComponent().getDocument().addDocumentListener(this);
--- a/src/macosx/classes/sun/lwawt/LWWindowPeer.java	Thu Jan 26 08:58:24 2012 +0000
+++ b/src/macosx/classes/sun/lwawt/LWWindowPeer.java	Thu Jan 26 16:13:42 2012 +0400
@@ -105,13 +105,9 @@
 
     private volatile boolean cachedFocusableWindow;
 
-    private static final Font DEFAULT_FONT = new Font(Font.DIALOG, Font.PLAIN, 12);
+    private volatile boolean isOpaque = true;
 
-    private Color background;
-    private Color foreground;
-    private Font font;
-
-    private volatile boolean isOpaque = true;
+    private static final Font DEFAULT_FONT = new Font("Lucida Grande", Font.PLAIN, 13);
 
     private static LWWindowPeer grabbingWindow;
 
@@ -140,6 +136,27 @@
             this.graphicsConfig = gc;
         }
 
+        if (!target.isFontSet()) {
+            target.setFont(DEFAULT_FONT);
+        }
+
+        if (!target.isBackgroundSet()) {
+            target.setBackground(SystemColor.window);
+        } else {
+            // first we check if user provided alpha for background. This is
+            // similar to what Apple's Java do.
+            // Since JDK7 we should rely on setOpacity() only.
+            // this.opacity = c.getAlpha();
+            // System.out.println("Delegate assigns alpha (we ignore setOpacity()):"
+            // +this.opacity);
+        }
+
+        if (!target.isForegroundSet()) {
+            target.setForeground(SystemColor.windowText);
+            // we should not call setForeground because it will call a repaint
+            // which the peer may not be ready to do yet.
+        }
+
         platformWindow.initialize(target, this, ownerDelegate);
     }
 
@@ -168,60 +185,6 @@
         replaceSurfaceData(1, null);
     }
 
-    public void setBackground(Color c) {
-        final boolean changed;
-        synchronized (getStateLock()) {
-            changed = ((background != null) ? !background.equals(c) :
-                    ((c != null) && !c.equals(background)));
-                background = c;
-        }
-        if (changed) {
-            repaintPeer();
-        }
-    }
-
-    protected Color getBackground() {
-        synchronized (getStateLock()) {
-            return background;
-        }
-    }
-
-    public void setForeground(Color c) {
-        final boolean changed;
-        synchronized (getStateLock()) {
-            changed = ((foreground != null) ? !foreground.equals(c) :
-                    ((c != null) && !c.equals(foreground)));
-            foreground = c;
-        }
-        if (changed) {
-            repaintPeer();
-        }
-    }
-
-    protected Color getForeground() {
-        synchronized (getStateLock()) {
-            return foreground;
-        }
-    }
-
-    public void setFont(Font f) {
-        final boolean changed;
-        synchronized (getStateLock()) {
-            changed = ((font != null) ? !font.equals(f) :
-                    ((f != null) && !f.equals(font)));
-            font = f;
-        }
-        if (changed) {
-            repaintPeer();
-        }
-    }
-
-    protected Font getFont() {
-        synchronized (getStateLock()) {
-            return font == null ? DEFAULT_FONT : font;
-        }
-    }
-
     // Just a helper method
     public PlatformWindow getPlatformWindow() {
         return platformWindow;
--- a/src/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java	Thu Jan 26 08:58:24 2012 +0000
+++ b/src/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java	Thu Jan 26 16:13:42 2012 +0400
@@ -195,8 +195,6 @@
         }
     };
 
-    private static final Font DEFAULT_FONT = new Font("Lucida Grande", Font.PLAIN, 13);
-
     // Bounds of the native widget but in the Java coordinate system.
     // In order to keep it up-to-date we will update them on
     // 1) setting native bounds via nativeSetBounds() call
@@ -225,29 +223,6 @@
         if (_owner instanceof CPlatformWindow) {
             this.owner = (CPlatformWindow)_owner;
         }
-        final Font font = target.getFont();
-        if (font == null) {
-            target.setFont(DEFAULT_FONT);
-        }
-
-        final Color background = target.getBackground();
-        if (background == null) {
-            target.setBackground(SystemColor.window);
-        } else {
-            // first we check if user provided alpha for background. This is
-            // similar to what Apple's Java do.
-            // Since JDK7 we should rely on setOpacity() only.
-            // this.opacity = c.getAlpha();
-            // System.out.println("Delegate assigns alpha (we ignore setOpacity()):"
-            // +this.opacity);
-        }
-
-        Color foreground = target.getForeground();
-        if (foreground == null) {
-            target.setForeground(SystemColor.windowText);
-            // we should not call setForeground because it will call a repaint
-            // which the peer may not be ready to do yet.
-        }
 
         final int styleBits = getInitialStyleBits();
 
--- a/src/macosx/native/sun/awt/CSystemColors.m	Thu Jan 26 08:58:24 2012 +0000
+++ b/src/macosx/native/sun/awt/CSystemColors.m	Thu Jan 26 16:13:42 2012 +0400
@@ -81,7 +81,8 @@
     sColors[java_awt_SystemColor_INACTIVE_CAPTION] =        [NSColor grayColor];
     sColors[java_awt_SystemColor_INACTIVE_CAPTION_TEXT] =    [NSColor grayColor];
     sColors[java_awt_SystemColor_INACTIVE_CAPTION_BORDER] =    [NSColor grayColor];
-    sColors[java_awt_SystemColor_WINDOW] =                    [NSColor grayColor];
+    const CGFloat color = (CGFloat)0xEE/(CGFloat)0xFF;
+    sColors[java_awt_SystemColor_WINDOW] = [NSColor colorWithCalibratedRed:color green:color blue:color alpha:1.0f];
     sColors[java_awt_SystemColor_WINDOW_BORDER] =            [NSColor windowFrameColor];
     sColors[java_awt_SystemColor_WINDOW_TEXT] =                [NSColor windowFrameTextColor];
     sColors[java_awt_SystemColor_MENU] =                    [NSColor controlBackgroundColor];