1 /*
   2  * Copyright (c) 2007, 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4856008 7025987
  27  * @summary Tests border insets
  28  * @author Sergey Malenkov
  29  * @modules java.desktop/com.sun.java.swing.plaf.motif
  30  *          java.desktop/com.sun.java.swing.plaf.windows
  31  *          java.desktop/sun.swing.plaf.synth
  32  */
  33 
  34 import com.sun.java.swing.plaf.motif.MotifBorders;
  35 import com.sun.java.swing.plaf.windows.WindowsBorders;
  36 
  37 import java.awt.Color;
  38 import java.awt.Font;
  39 import java.awt.Insets;
  40 
  41 import javax.swing.ActionMap;
  42 import javax.swing.JComponent;
  43 import javax.swing.JFileChooser;
  44 import javax.swing.JLabel;
  45 import javax.swing.JPopupMenu;
  46 import javax.swing.JToggleButton;
  47 import javax.swing.JToolBar;
  48 import javax.swing.border.AbstractBorder;
  49 import javax.swing.border.BevelBorder;
  50 import javax.swing.border.Border;
  51 import javax.swing.border.CompoundBorder;
  52 import javax.swing.border.EmptyBorder;
  53 import javax.swing.border.EtchedBorder;
  54 import javax.swing.border.LineBorder;
  55 import javax.swing.border.MatteBorder;
  56 import javax.swing.border.SoftBevelBorder;
  57 import javax.swing.border.TitledBorder;
  58 import javax.swing.plaf.ActionMapUIResource;
  59 import javax.swing.plaf.BorderUIResource;
  60 import javax.swing.plaf.synth.SynthLookAndFeel;
  61 import javax.swing.plaf.basic.BasicBorders;
  62 import javax.swing.plaf.basic.BasicToolBarUI;
  63 import javax.swing.plaf.metal.MetalBorders;
  64 import javax.swing.plaf.metal.MetalComboBoxEditor;
  65 import javax.swing.plaf.nimbus.NimbusLookAndFeel;
  66 
  67 import sun.swing.plaf.synth.SynthFileChooserUI;
  68 
  69 public class Test4856008 {
  70     private static final JLabel LABEL = new JLabel();
  71     private static final JPopupMenu POPUP = new JPopupMenu();
  72     private static final JToolBar TOOLBAR = new JToolBar();
  73 
  74     private static final Border[] BORDERS = {
  75             new MotifBorders.BevelBorder(true, Color.BLACK, Color.WHITE),
  76             new MotifBorders.ButtonBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
  77             new MotifBorders.FocusBorder(Color.BLACK, Color.WHITE),
  78             new MotifBorders.FrameBorder(LABEL),
  79             new MotifBorders.MenuBarBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
  80             new MotifBorders.MotifPopupMenuBorder(new Font(null, Font.PLAIN, 10), Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
  81             new MotifBorders.ToggleButtonBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
  82 
  83             new WindowsBorders.ProgressBarBorder(Color.BLACK, Color.WHITE),
  84             new WindowsBorders.ToolBarBorder(Color.BLACK, Color.WHITE),
  85             //- WindowsInternalFrameUI.XPBorder is not accessible: check it visually
  86             //? WindowsTableHeaderUI.IconBorder is not accessible: check it visually
  87             //- XPStyle.XPEmptyBorder is not accessible: check it visually
  88             //- XPStyle.XPFillBorder is not accessible: check it visually
  89             //- XPStyle.XPImageBorder is not accessible: check it visually
  90 
  91             new BevelBorder(BevelBorder.RAISED),
  92             new CompoundBorder(),
  93             new EmptyBorder(1, 2, 3, 4),
  94             new EtchedBorder(),
  95             new LineBorder(Color.BLACK, 2, true),
  96             new MatteBorder(1, 2, 3, 4, Color.BLACK),
  97             new SoftBevelBorder(BevelBorder.LOWERED),
  98             new TitledBorder("4856008"),
  99 
 100             new BorderUIResource(new EmptyBorder(1, 2, 3, 4)),
 101 
 102             new BasicBorders.ButtonBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
 103             new BasicBorders.FieldBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
 104             new BasicBorders.MarginBorder(),
 105             new BasicBorders.MenuBarBorder(Color.BLACK, Color.WHITE),
 106             new BasicBorders.RadioButtonBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
 107             //+ BasicBorders.RolloverMarginBorder:
 108             new ToolBar().getRolloverMarginBorder(),
 109             new BasicBorders.SplitPaneBorder(Color.BLACK, Color.WHITE),
 110             //+ BasicBorders.SplitPaneDividerBorder:
 111             BasicBorders.getSplitPaneDividerBorder(),
 112             new BasicBorders.ToggleButtonBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
 113 
 114             new MetalBorders.ButtonBorder(),
 115             //- MetalBorders.DialogBorder is not accessible: check it visually
 116             new MetalBorders.Flush3DBorder(),
 117             //- MetalBorders.FrameBorder is not accessible: check it visually
 118             new MetalBorders.InternalFrameBorder(),
 119             new MetalBorders.MenuBarBorder(),
 120             new MetalBorders.MenuItemBorder(),
 121             new MetalBorders.OptionDialogBorder(),
 122             new MetalBorders.PaletteBorder(),
 123             new MetalBorders.PopupMenuBorder(),
 124             //- MetalBorders.RolloverMarginBorder is not accessible: check it visually
 125             new MetalBorders.ScrollPaneBorder(),
 126             new MetalBorders.TableHeaderBorder(),
 127             new MetalBorders.ToolBarBorder(),
 128             //+ MetalComboBoxEditor.EditorBorder:
 129             new MetalEditor().getEditorBorder(),
 130 
 131             //- SynthBorder is not accessible: check it visually
 132             //- SynthScrollPaneUI.ViewportBorder is not accessible: check it visually
 133 
 134             //? CSSBorder is not accessible: check it visually
 135             //? CommentView.CommentBorder is not accessible: check it visually
 136             //- HiddenTagView.EndTagBorder is not accessible: check it visually
 137             //- HiddenTagView.StartTagBorder is not accessible: check it visually
 138 
 139             //+ SynthFileChooserUI.UIBorder:
 140             new SynthFileChooser().getUIBorder(),
 141 
 142             //+ LoweredBorder:
 143             new NimbusLookAndFeel().getDefaults().getBorder("TitledBorder.border"),
 144     };
 145 
 146     public static void main(String[] args) {
 147         for (Border border : BORDERS) {
 148             System.out.println(border.getClass());
 149             test(border, border.getBorderInsets(getComponent(border)));
 150             if (border instanceof AbstractBorder) {
 151                 test((AbstractBorder) border);
 152             }
 153         }
 154     }
 155 
 156     private static void test(AbstractBorder border) {
 157         Insets insets = new Insets(0, 0, 0, 0);
 158         if (insets != border.getBorderInsets(getComponent(border), insets)) {
 159             throw new Error("both instances are differ for " + border.getClass());
 160         }
 161         test(border, insets);
 162     }
 163 
 164     private static void test(Border border, Insets insets) {
 165         Insets result = border.getBorderInsets(getComponent(border));
 166         if (insets == result) {
 167             throw new Error("both instances are the same for " + border.getClass());
 168         }
 169         if (!insets.equals(result)) {
 170             throw new Error("both insets are not equal for " + border.getClass());
 171         }
 172     }
 173 
 174     private static JComponent getComponent(Border border) {
 175         Class type = border.getClass();
 176         if (type.equals(MotifBorders.MotifPopupMenuBorder.class)) {
 177             return POPUP;
 178         }
 179         if (type.equals(WindowsBorders.ToolBarBorder.class)) {
 180             return TOOLBAR;
 181         }
 182         if (type.equals(MetalBorders.ToolBarBorder.class)) {
 183             return TOOLBAR;
 184         }
 185         return LABEL;
 186     }
 187 
 188     // This class is used to get the instance of BasicBorders.RolloverMarginBorder
 189     private static class ToolBar extends BasicToolBarUI {
 190         private Border getRolloverMarginBorder() {
 191             JToggleButton button = new JToggleButton();
 192             CompoundBorder border = (CompoundBorder) getNonRolloverBorder(button);
 193             return border.getInsideBorder();
 194         }
 195     }
 196 
 197     // This class is used to get the instance of MetalComboBoxEditor.EditorBorder
 198     private static class MetalEditor extends MetalComboBoxEditor {
 199         private Border getEditorBorder() {
 200             return editor.getBorder();
 201         }
 202     }
 203 
 204     // This class is used to get the instance of SynthFileChooserUI.UIBorder
 205     private static class SynthFileChooser extends SynthFileChooserUI {
 206         private static final JFileChooser CHOOSER = new JFileChooser();
 207         private String name;
 208 
 209         private SynthFileChooser() {
 210             super(CHOOSER);
 211         }
 212 
 213         private Border getUIBorder() {
 214             new SynthLookAndFeel().initialize();
 215             CHOOSER.setBorder(null);
 216             installDefaults(CHOOSER);
 217             return CHOOSER.getBorder();
 218         }
 219 
 220         @Override
 221         protected ActionMap createActionMap() {
 222             return new ActionMapUIResource();
 223         }
 224 
 225         @Override
 226         public String getFileName() {
 227             return this.name;
 228         }
 229 
 230         @Override
 231         public void setFileName(String name) {
 232             this.name = name;
 233         }
 234     }
 235 }