1 /*
   2  * Copyright (c) 2007, 2011, 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  */
  30 
  31 import com.sun.java.swing.plaf.motif.MotifBorders;
  32 import com.sun.java.swing.plaf.windows.WindowsBorders;
  33 
  34 import java.awt.Color;
  35 import java.awt.Font;
  36 import java.awt.Insets;
  37 
  38 import javax.swing.ActionMap;
  39 import javax.swing.JComponent;
  40 import javax.swing.JFileChooser;
  41 import javax.swing.JLabel;
  42 import javax.swing.JPopupMenu;
  43 import javax.swing.JToggleButton;
  44 import javax.swing.JToolBar;
  45 import javax.swing.border.AbstractBorder;
  46 import javax.swing.border.BevelBorder;
  47 import javax.swing.border.Border;
  48 import javax.swing.border.CompoundBorder;
  49 import javax.swing.border.EmptyBorder;
  50 import javax.swing.border.EtchedBorder;
  51 import javax.swing.border.LineBorder;
  52 import javax.swing.border.MatteBorder;
  53 import javax.swing.border.SoftBevelBorder;
  54 import javax.swing.border.TitledBorder;
  55 import javax.swing.plaf.ActionMapUIResource;
  56 import javax.swing.plaf.BorderUIResource;
  57 import javax.swing.plaf.synth.SynthLookAndFeel;
  58 import javax.swing.plaf.basic.BasicBorders;
  59 import javax.swing.plaf.basic.BasicToolBarUI;
  60 import javax.swing.plaf.metal.MetalBorders;
  61 import javax.swing.plaf.metal.MetalComboBoxEditor;
  62 import javax.swing.plaf.nimbus.NimbusLookAndFeel;
  63 
  64 import sun.swing.plaf.synth.SynthFileChooserUI;
  65 
  66 public class Test4856008 {
  67     private static final JLabel LABEL = new JLabel();
  68     private static final JPopupMenu POPUP = new JPopupMenu();
  69     private static final JToolBar TOOLBAR = new JToolBar();
  70 
  71     private static final Border[] BORDERS = {
  72             new MotifBorders.BevelBorder(true, Color.BLACK, Color.WHITE),
  73             new MotifBorders.ButtonBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
  74             new MotifBorders.FocusBorder(Color.BLACK, Color.WHITE),
  75             new MotifBorders.FrameBorder(LABEL),
  76             new MotifBorders.MenuBarBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
  77             new MotifBorders.MotifPopupMenuBorder(new Font(null, Font.PLAIN, 10), Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
  78             new MotifBorders.ToggleButtonBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
  79 
  80             new WindowsBorders.ProgressBarBorder(Color.BLACK, Color.WHITE),
  81             new WindowsBorders.ToolBarBorder(Color.BLACK, Color.WHITE),
  82             //- WindowsInternalFrameUI.XPBorder is not accessible: check it visually
  83             //? WindowsTableHeaderUI.IconBorder is not accessible: check it visually
  84             //- XPStyle.XPEmptyBorder is not accessible: check it visually
  85             //- XPStyle.XPFillBorder is not accessible: check it visually
  86             //- XPStyle.XPImageBorder is not accessible: check it visually
  87 
  88             new BevelBorder(BevelBorder.RAISED),
  89             new CompoundBorder(),
  90             new EmptyBorder(1, 2, 3, 4),
  91             new EtchedBorder(),
  92             new LineBorder(Color.BLACK, 2, true),
  93             new MatteBorder(1, 2, 3, 4, Color.BLACK),
  94             new SoftBevelBorder(BevelBorder.LOWERED),
  95             new TitledBorder("4856008"),
  96 
  97             new BorderUIResource(new EmptyBorder(1, 2, 3, 4)),
  98 
  99             new BasicBorders.ButtonBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
 100             new BasicBorders.FieldBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
 101             new BasicBorders.MarginBorder(),
 102             new BasicBorders.MenuBarBorder(Color.BLACK, Color.WHITE),
 103             new BasicBorders.RadioButtonBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
 104             //+ BasicBorders.RolloverMarginBorder:
 105             new ToolBar().getRolloverMarginBorder(),
 106             new BasicBorders.SplitPaneBorder(Color.BLACK, Color.WHITE),
 107             //+ BasicBorders.SplitPaneDividerBorder:
 108             BasicBorders.getSplitPaneDividerBorder(),
 109             new BasicBorders.ToggleButtonBorder(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK),
 110 
 111             new MetalBorders.ButtonBorder(),
 112             //- MetalBorders.DialogBorder is not accessible: check it visually
 113             new MetalBorders.Flush3DBorder(),
 114             //- MetalBorders.FrameBorder is not accessible: check it visually
 115             new MetalBorders.InternalFrameBorder(),
 116             new MetalBorders.MenuBarBorder(),
 117             new MetalBorders.MenuItemBorder(),
 118             new MetalBorders.OptionDialogBorder(),
 119             new MetalBorders.PaletteBorder(),
 120             new MetalBorders.PopupMenuBorder(),
 121             //- MetalBorders.RolloverMarginBorder is not accessible: check it visually
 122             new MetalBorders.ScrollPaneBorder(),
 123             new MetalBorders.TableHeaderBorder(),
 124             new MetalBorders.ToolBarBorder(),
 125             //+ MetalComboBoxEditor.EditorBorder:
 126             new MetalEditor().getEditorBorder(),
 127 
 128             //- SynthBorder is not accessible: check it visually
 129             //- SynthScrollPaneUI.ViewportBorder is not accessible: check it visually
 130 
 131             //? CSSBorder is not accessible: check it visually
 132             //? CommentView.CommentBorder is not accessible: check it visually
 133             //- HiddenTagView.EndTagBorder is not accessible: check it visually
 134             //- HiddenTagView.StartTagBorder is not accessible: check it visually
 135 
 136             //+ SynthFileChooserUI.UIBorder:
 137             new SynthFileChooser().getUIBorder(),
 138 
 139             //+ LoweredBorder:
 140             new NimbusLookAndFeel().getDefaults().getBorder("TitledBorder.border"),
 141     };
 142 
 143     public static void main(String[] args) {
 144         for (Border border : BORDERS) {
 145             System.out.println(border.getClass());
 146             test(border, border.getBorderInsets(getComponent(border)));
 147             if (border instanceof AbstractBorder) {
 148                 test((AbstractBorder) border);
 149             }
 150         }
 151     }
 152 
 153     private static void test(AbstractBorder border) {
 154         Insets insets = new Insets(0, 0, 0, 0);
 155         if (insets != border.getBorderInsets(getComponent(border), insets)) {
 156             throw new Error("both instances are differ for " + border.getClass());
 157         }
 158         test(border, insets);
 159     }
 160 
 161     private static void test(Border border, Insets insets) {
 162         Insets result = border.getBorderInsets(getComponent(border));
 163         if (insets == result) {
 164             throw new Error("both instances are the same for " + border.getClass());
 165         }
 166         if (!insets.equals(result)) {
 167             throw new Error("both insets are not equal for " + border.getClass());
 168         }
 169     }
 170 
 171     private static JComponent getComponent(Border border) {
 172         Class type = border.getClass();
 173         if (type.equals(MotifBorders.MotifPopupMenuBorder.class)) {
 174             return POPUP;
 175         }
 176         if (type.equals(WindowsBorders.ToolBarBorder.class)) {
 177             return TOOLBAR;
 178         }
 179         if (type.equals(MetalBorders.ToolBarBorder.class)) {
 180             return TOOLBAR;
 181         }
 182         return LABEL;
 183     }
 184 
 185     // This class is used to get the instance of BasicBorders.RolloverMarginBorder
 186     private static class ToolBar extends BasicToolBarUI {
 187         private Border getRolloverMarginBorder() {
 188             JToggleButton button = new JToggleButton();
 189             CompoundBorder border = (CompoundBorder) getNonRolloverBorder(button);
 190             return border.getInsideBorder();
 191         }
 192     }
 193 
 194     // This class is used to get the instance of MetalComboBoxEditor.EditorBorder
 195     private static class MetalEditor extends MetalComboBoxEditor {
 196         private Border getEditorBorder() {
 197             return editor.getBorder();
 198         }
 199     }
 200 
 201     // This class is used to get the instance of SynthFileChooserUI.UIBorder
 202     private static class SynthFileChooser extends SynthFileChooserUI {
 203         private static final JFileChooser CHOOSER = new JFileChooser();
 204         private String name;
 205 
 206         private SynthFileChooser() {
 207             super(CHOOSER);
 208         }
 209 
 210         private Border getUIBorder() {
 211             new SynthLookAndFeel().initialize();
 212             CHOOSER.setBorder(null);
 213             installDefaults(CHOOSER);
 214             return CHOOSER.getBorder();
 215         }
 216 
 217         @Override
 218         protected ActionMap createActionMap() {
 219             return new ActionMapUIResource();
 220         }
 221 
 222         @Override
 223         public String getFileName() {
 224             return this.name;
 225         }
 226 
 227         @Override
 228         public void setFileName(String name) {
 229             this.name = name;
 230         }
 231     }
 232 }