changeset 2866:7fc59f27318c

6978482: MetalBorders.ToolBarBorder should specify that its getBorderInsets impl accepts only JToolBar inst Reviewed-by: alexp
author malenkov
date Tue, 14 Sep 2010 19:12:28 +0400
parents bb733c150f94
children 2ffd71748740
files src/share/classes/com/sun/java/swing/plaf/motif/MotifBorders.java src/share/classes/com/sun/java/swing/plaf/windows/WindowsBorders.java src/share/classes/javax/swing/plaf/basic/BasicBorders.java src/share/classes/javax/swing/plaf/metal/MetalBorders.java src/share/classes/sun/swing/plaf/synth/SynthFileChooserUI.java test/javax/swing/border/Test6978482.java
diffstat 6 files changed, 268 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/java/swing/plaf/motif/MotifBorders.java	Tue Sep 14 10:45:38 2010 -0400
+++ b/src/share/classes/com/sun/java/swing/plaf/motif/MotifBorders.java	Tue Sep 14 19:12:28 2010 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -40,8 +40,6 @@
 import java.awt.Point;
 import java.awt.Rectangle;
 
-import java.io.Serializable;
-
 /**
  * Factory object that can vend Icons appropriate for the basic L & F.
  * <p>
@@ -99,7 +97,7 @@
         }
 
         public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
-            if (((JComponent)c).hasFocus()) {
+            if (c.hasFocus()) {
                 g.setColor(focus);
                 g.drawRect(x, y, w-1, h-1);
             } else {
@@ -233,6 +231,9 @@
         }
 
         public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
+            if (!(c instanceof JMenuBar)) {
+                return;
+            }
             JMenuBar menuBar = (JMenuBar)c;
             if (menuBar.isBorderPainted() == true) {
                 // this draws the MenuBar border
@@ -658,6 +659,9 @@
          * @param height the height of the painted border
          */
         public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
+            if (!(c instanceof JPopupMenu)) {
+                return;
+            }
 
             Font origFont = g.getFont();
             Color origColor = g.getColor();
@@ -701,6 +705,9 @@
          * @param insets the object to be reinitialized
          */
         public Insets getBorderInsets(Component c, Insets insets) {
+            if (!(c instanceof JPopupMenu)) {
+                return insets;
+            }
             FontMetrics fm;
             int         descent = 0;
             int         ascent = 16;
--- a/src/share/classes/com/sun/java/swing/plaf/windows/WindowsBorders.java	Tue Sep 14 10:45:38 2010 -0400
+++ b/src/share/classes/com/sun/java/swing/plaf/windows/WindowsBorders.java	Tue Sep 14 19:12:28 2010 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -32,12 +32,8 @@
 
 import java.awt.Component;
 import java.awt.Insets;
-import java.awt.Dimension;
-import java.awt.Image;
-import java.awt.Rectangle;
 import java.awt.Color;
 import java.awt.Graphics;
-import java.io.Serializable;
 
 import static com.sun.java.swing.plaf.windows.TMSchema.*;
 import static com.sun.java.swing.plaf.windows.XPStyle.Skin;
@@ -159,6 +155,9 @@
 
         public void paintBorder(Component c, Graphics g, int x, int y,
                                 int width, int height) {
+            if (!(c instanceof JToolBar)) {
+                return;
+            }
             g.translate(x, y);
 
             XPStyle xp = XPStyle.getXP();
@@ -190,33 +189,33 @@
 
                 } else {
 
-                if (!vertical) {
-                    if (c.getComponentOrientation().isLeftToRight()) {
+                    if (!vertical) {
+                        if (c.getComponentOrientation().isLeftToRight()) {
+                            g.setColor(shadow);
+                            g.drawLine(4, 3, 4, height - 4);
+                            g.drawLine(4, height - 4, 2, height - 4);
+
+                            g.setColor(highlight);
+                            g.drawLine(2, 3, 3, 3);
+                            g.drawLine(2, 3, 2, height - 5);
+                        } else {
+                            g.setColor(shadow);
+                            g.drawLine(width - 3, 3, width - 3, height - 4);
+                            g.drawLine(width - 4, height - 4, width - 4, height - 4);
+
+                            g.setColor(highlight);
+                            g.drawLine(width - 5, 3, width - 4, 3);
+                            g.drawLine(width - 5, 3, width - 5, height - 5);
+                        }
+                    } else { // Vertical
                         g.setColor(shadow);
-                        g.drawLine(4, 3, 4, height - 4);
-                        g.drawLine(4, height - 4, 2, height - 4);
+                        g.drawLine(3, 4, width - 4, 4);
+                        g.drawLine(width - 4, 2, width - 4, 4);
 
                         g.setColor(highlight);
-                        g.drawLine(2, 3, 3, 3);
-                        g.drawLine(2, 3, 2, height - 5);
-                    } else {
-                        g.setColor(shadow);
-                        g.drawLine(width - 3, 3, width - 3, height - 4);
-                        g.drawLine(width - 4, height - 4, width - 4, height - 4);
-
-                        g.setColor(highlight);
-                        g.drawLine(width - 5, 3, width - 4, 3);
-                        g.drawLine(width - 5, 3, width - 5, height - 5);
+                        g.drawLine(3, 2, width - 4, 2);
+                        g.drawLine(3, 2, 3, 3);
                     }
-                } else { // Vertical
-                    g.setColor(shadow);
-                    g.drawLine(3, 4, width - 4, 4);
-                    g.drawLine(width - 4, 2, width - 4, 4);
-
-                    g.setColor(highlight);
-                    g.drawLine(3, 2, width - 4, 2);
-                    g.drawLine(3, 2, 3, 3);
-                }
                 }
             }
 
@@ -225,6 +224,9 @@
 
         public Insets getBorderInsets(Component c, Insets insets) {
             insets.set(1,1,1,1);
+            if (!(c instanceof JToolBar)) {
+                return insets;
+            }
             if (((JToolBar)c).isFloatable()) {
                 int gripInset = (XPStyle.getXP() != null) ? 12 : 9;
                 if (((JToolBar)c).getOrientation() == HORIZONTAL) {
--- a/src/share/classes/javax/swing/plaf/basic/BasicBorders.java	Tue Sep 14 10:45:38 2010 -0400
+++ b/src/share/classes/javax/swing/plaf/basic/BasicBorders.java	Tue Sep 14 19:12:28 2010 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -36,8 +36,6 @@
 import java.awt.Rectangle;
 import java.awt.Color;
 import java.awt.Graphics;
-import java.io.Serializable;
-
 
 /**
  * Factory object that can vend Borders appropriate for the basic L & F.
@@ -431,6 +429,9 @@
 
         public void paintBorder(Component c, Graphics g, int x, int y,
                                 int width, int height) {
+            if (!(c instanceof BasicSplitPaneDivider)) {
+                return;
+            }
             Component          child;
             Rectangle          cBounds;
             JSplitPane         splitPane = ((BasicSplitPaneDivider)c).
@@ -510,6 +511,9 @@
 
         public void paintBorder(Component c, Graphics g, int x, int y,
                                 int width, int height) {
+            if (!(c instanceof JSplitPane)) {
+                return;
+            }
             // The only tricky part with this border is that the divider is
             // not positioned at the top (for horizontal) or left (for vert),
             // so this border draws to where the divider is:
--- a/src/share/classes/javax/swing/plaf/metal/MetalBorders.java	Tue Sep 14 10:45:38 2010 -0400
+++ b/src/share/classes/javax/swing/plaf/metal/MetalBorders.java	Tue Sep 14 19:12:28 2010 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -33,14 +33,11 @@
 
 import java.awt.Component;
 import java.awt.Insets;
-import java.awt.Dimension;
-import java.awt.Rectangle;
 import java.awt.Color;
 import java.awt.Dialog;
 import java.awt.Frame;
 import java.awt.Graphics;
 import java.awt.Window;
-import java.io.Serializable;
 
 import sun.swing.StringUIClientPropertyKey;
 
@@ -81,6 +78,9 @@
         protected static Insets borderInsets = new Insets( 3, 3, 3, 3 );
 
         public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
+            if (!(c instanceof AbstractButton)) {
+                return;
+            }
             if (MetalLookAndFeel.usingOcean()) {
                 paintOceanBorder(c, g, x, y, w, h);
                 return;
@@ -473,8 +473,8 @@
             if (c instanceof JInternalFrame) {
                 Object obj = ((JInternalFrame) c).getClientProperty(
                               "JInternalFrame.messageType");
-                if (obj != null && (obj instanceof Integer)) {
-                    messageType = ((Integer) obj).intValue();
+                if (obj instanceof Integer) {
+                    messageType = (Integer) obj;
                 }
             }
 
@@ -533,7 +533,7 @@
             if (MetalLookAndFeel.usingOcean()) {
                 // Only paint a border if we're not next to a horizontal
                 // toolbar
-                if (!MetalToolBarUI.doesMenuBarBorderToolBar((JMenuBar)c)) {
+                if ((c instanceof JMenuBar) && !MetalToolBarUI.doesMenuBarBorderToolBar((JMenuBar)c)) {
                     g.setColor(MetalLookAndFeel.getControl());
                     g.drawLine(0, h - 2, w, h - 2);
                     g.setColor(UIManager.getColor("MenuBar.borderColor"));
@@ -564,6 +564,9 @@
         protected static Insets borderInsets = new Insets( 2, 2, 2, 2 );
 
         public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
+            if (!(c instanceof JMenuItem)) {
+                return;
+            }
             JMenuItem b = (JMenuItem) c;
             ButtonModel model = b.getModel();
 
@@ -687,6 +690,9 @@
 
         public void paintBorder( Component c, Graphics g, int x, int y, int w, int h )
         {
+            if (!(c instanceof JToolBar)) {
+                return;
+            }
             g.translate( x, y );
 
             if ( ((JToolBar) c).isFloatable() )
@@ -729,6 +735,9 @@
                 newInsets.top = newInsets.left = newInsets.bottom = newInsets.right = 2;
             }
 
+            if (!(c instanceof JToolBar)) {
+                return newInsets;
+            }
             if ( ((JToolBar) c).isFloatable() ) {
                 if ( ((JToolBar) c).getOrientation() == HORIZONTAL ) {
                     if (c.getComponentOrientation().isLeftToRight()) {
@@ -827,6 +836,9 @@
         public void paintBorder(Component c, Graphics g, int x, int y,
                           int w, int h) {
 
+            if (!(c instanceof JScrollPane)) {
+                return;
+            }
             JScrollPane scroll = (JScrollPane)c;
             JComponent colHeader = scroll.getColumnHeader();
             int colHeaderHeight = 0;
--- a/src/share/classes/sun/swing/plaf/synth/SynthFileChooserUI.java	Tue Sep 14 10:45:38 2010 -0400
+++ b/src/share/classes/sun/swing/plaf/synth/SynthFileChooserUI.java	Tue Sep 14 19:12:28 2010 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -551,6 +551,9 @@
 
         public void paintBorder(Component c, Graphics g, int x, int y,
                                 int width, int height) {
+            if (!(c instanceof JComponent)) {
+                return;
+            }
             JComponent jc = (JComponent)c;
             SynthContext context = getContext(jc);
             SynthStyle style = context.getStyle();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/javax/swing/border/Test6978482.java	Tue Sep 14 19:12:28 2010 +0400
@@ -0,0 +1,197 @@
+/*
+ * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 6978482
+ * @summary Tests unchecked casts
+ * @author Sergey Malenkov
+ */
+
+import com.sun.java.swing.plaf.motif.MotifBorders;
+import com.sun.java.swing.plaf.windows.WindowsBorders;
+
+import java.awt.Color;
+import java.awt.Component;
+import java.awt.Font;
+import java.awt.Graphics;
+import java.awt.image.BufferedImage;
+
+import javax.swing.ActionMap;
+import javax.swing.JFileChooser;
+import javax.swing.JLabel;
+import javax.swing.JToggleButton;
+import javax.swing.JToolBar;
+import javax.swing.border.BevelBorder;
+import javax.swing.border.Border;
+import javax.swing.border.CompoundBorder;
+import javax.swing.border.EmptyBorder;
+import javax.swing.border.EtchedBorder;
+import javax.swing.border.LineBorder;
+import javax.swing.border.MatteBorder;
+import javax.swing.border.SoftBevelBorder;
+import javax.swing.border.TitledBorder;
+import javax.swing.plaf.ActionMapUIResource;
+import javax.swing.plaf.BorderUIResource;
+import javax.swing.plaf.synth.SynthLookAndFeel;
+import javax.swing.plaf.basic.BasicBorders;
+import javax.swing.plaf.basic.BasicToolBarUI;
+import javax.swing.plaf.metal.MetalBorders;
+import javax.swing.plaf.metal.MetalComboBoxEditor;
+
+import sun.swing.plaf.synth.SynthFileChooserUI;
+
+public class Test6978482 {
+    private static final JLabel LABEL = new JLabel();
+    private static final JToolBar TOOLBAR = new JToolBar(); // init non-rollover border
+
+    private static final Border[] BORDERS = {
+            new MotifBorders.BevelBorder(true, Color.BLACK, Color.WHITE),
+            new MotifBorders.ButtonBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
+            new MotifBorders.FocusBorder(Color.BLACK, Color.WHITE),
+            new MotifBorders.FrameBorder(LABEL),
+            new MotifBorders.MenuBarBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
+            new MotifBorders.MotifPopupMenuBorder(new Font(null, Font.PLAIN, 10), Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
+            new MotifBorders.ToggleButtonBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
+
+            new WindowsBorders.ProgressBarBorder(Color.BLACK, Color.WHITE),
+            new WindowsBorders.ToolBarBorder(Color.BLACK, Color.WHITE),
+            //- WindowsInternalFrameUI.XPBorder is not accessible: check it visually
+            //? WindowsTableHeaderUI.IconBorder is not accessible: check it visually
+            //- XPStyle.XPEmptyBorder is not accessible: check it visually
+            //- XPStyle.XPFillBorder is not accessible: check it visually
+            //- XPStyle.XPImageBorder is not accessible: check it visually
+
+            new BevelBorder(BevelBorder.RAISED),
+            new CompoundBorder(),
+            new EmptyBorder(1, 2, 3, 4),
+            new EtchedBorder(),
+            new LineBorder(Color.BLACK, 2, true),
+            new MatteBorder(1, 2, 3, 4, Color.BLACK),
+            new SoftBevelBorder(BevelBorder.LOWERED),
+            new TitledBorder("4856008"),
+
+            new BorderUIResource(new EmptyBorder(1, 2, 3, 4)),
+
+            new BasicBorders.ButtonBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
+            new BasicBorders.FieldBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
+            new BasicBorders.MarginBorder(),
+            new BasicBorders.MenuBarBorder(Color.BLACK, Color.WHITE),
+            new BasicBorders.RadioButtonBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
+            //+ BasicBorders.RolloverMarginBorder:
+            new ToolBar().getRolloverMarginBorder(),
+            new BasicBorders.SplitPaneBorder(Color.BLACK, Color.WHITE),
+            //+ BasicBorders.SplitPaneDividerBorder:
+            BasicBorders.getSplitPaneDividerBorder(),
+            new BasicBorders.ToggleButtonBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
+
+            new MetalBorders.ButtonBorder(),
+            //- MetalBorders.DialogBorder is not accessible: check it visually
+            new MetalBorders.Flush3DBorder(),
+            //- MetalBorders.FrameBorder is not accessible: check it visually
+            new MetalBorders.InternalFrameBorder(),
+            new MetalBorders.MenuBarBorder(),
+            new MetalBorders.MenuItemBorder(),
+            new MetalBorders.OptionDialogBorder(),
+            new MetalBorders.PaletteBorder(),
+            new MetalBorders.PopupMenuBorder(),
+            //- MetalBorders.RolloverMarginBorder is not accessible: check it visually
+            new MetalBorders.ScrollPaneBorder(),
+            new MetalBorders.TableHeaderBorder(),
+            new MetalBorders.ToolBarBorder(),
+            //+ MetalComboBoxEditor.EditorBorder:
+            new MetalEditor().getEditorBorder(),
+
+            //- SynthBorder is not accessible: check it visually
+            //- SynthScrollPaneUI.ViewportBorder is not accessible: check it visually
+
+            //? CSSBorder is not accessible: check it visually
+            //? CommentView.CommentBorder is not accessible: check it visually
+            //- HiddenTagView.EndTagBorder is not accessible: check it visually
+            //- HiddenTagView.StartTagBorder is not accessible: check it visually
+
+            //+ SynthFileChooserUI.UIBorder:
+            new SynthFileChooser().getUIBorder(),
+    };
+
+    public static void main(String[] args) {
+        Component c = new Component() {};
+        c.setBackground(Color.WHITE);
+        c.setForeground(Color.BLACK);
+        Graphics g = new BufferedImage(1024, 768, BufferedImage.TYPE_INT_RGB).getGraphics();
+        g.setClip(0, 0, 1024, 768);
+        for (Border border : BORDERS) {
+            System.out.println(border.getClass());
+            border.getBorderInsets(c);
+            border.paintBorder(c, g, 0, 0, 1024, 768);
+        }
+    }
+
+    // This class is used to get the instance of BasicBorders.RolloverMarginBorder
+    private static class ToolBar extends BasicToolBarUI {
+        private Border getRolloverMarginBorder() {
+            JToggleButton button = new JToggleButton();
+            CompoundBorder border = (CompoundBorder) getNonRolloverBorder(button);
+            return border.getInsideBorder();
+        }
+    }
+
+    // This class is used to get the instance of MetalComboBoxEditor.EditorBorder
+    private static class MetalEditor extends MetalComboBoxEditor {
+        private Border getEditorBorder() {
+            return editor.getBorder();
+        }
+    }
+
+    // This class is used to get the instance of SynthFileChooserUI.UIBorder
+    private static class SynthFileChooser extends SynthFileChooserUI {
+        private static final JFileChooser CHOOSER = new JFileChooser();
+        private String name;
+
+        private SynthFileChooser() {
+            super(CHOOSER);
+        }
+
+        private Border getUIBorder() {
+            new SynthLookAndFeel().initialize();
+            CHOOSER.setBorder(null);
+            installDefaults(CHOOSER);
+            return CHOOSER.getBorder();
+        }
+
+        @Override
+        protected ActionMap createActionMap() {
+            return new ActionMapUIResource();
+        }
+
+        @Override
+        public String getFileName() {
+            return this.name;
+        }
+
+        @Override
+        public void setFileName(String name) {
+            this.name = name;
+        }
+    }
+}