0N/A/*
2721N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing.plaf.basic;
0N/A
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.border.*;
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.swing.text.JTextComponent;
0N/A
0N/Aimport java.awt.Component;
0N/Aimport java.awt.Insets;
0N/Aimport java.awt.Dimension;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.awt.Color;
0N/Aimport java.awt.Graphics;
0N/A
0N/A/**
0N/A * Factory object that can vend Borders appropriate for the basic L & F.
0N/A * @author Georges Saab
0N/A * @author Amy Fowler
0N/A */
0N/A
0N/Apublic class BasicBorders {
0N/A
0N/A public static Border getButtonBorder() {
0N/A UIDefaults table = UIManager.getLookAndFeelDefaults();
0N/A Border buttonBorder = new BorderUIResource.CompoundBorderUIResource(
0N/A new BasicBorders.ButtonBorder(
0N/A table.getColor("Button.shadow"),
0N/A table.getColor("Button.darkShadow"),
0N/A table.getColor("Button.light"),
0N/A table.getColor("Button.highlight")),
0N/A new MarginBorder());
0N/A return buttonBorder;
0N/A }
0N/A
0N/A public static Border getRadioButtonBorder() {
0N/A UIDefaults table = UIManager.getLookAndFeelDefaults();
0N/A Border radioButtonBorder = new BorderUIResource.CompoundBorderUIResource(
0N/A new BasicBorders.RadioButtonBorder(
0N/A table.getColor("RadioButton.shadow"),
0N/A table.getColor("RadioButton.darkShadow"),
0N/A table.getColor("RadioButton.light"),
0N/A table.getColor("RadioButton.highlight")),
0N/A new MarginBorder());
0N/A return radioButtonBorder;
0N/A }
0N/A
0N/A public static Border getToggleButtonBorder() {
0N/A UIDefaults table = UIManager.getLookAndFeelDefaults();
0N/A Border toggleButtonBorder = new BorderUIResource.CompoundBorderUIResource(
0N/A new BasicBorders.ToggleButtonBorder(
0N/A table.getColor("ToggleButton.shadow"),
0N/A table.getColor("ToggleButton.darkShadow"),
0N/A table.getColor("ToggleButton.light"),
0N/A table.getColor("ToggleButton.highlight")),
0N/A new MarginBorder());
0N/A return toggleButtonBorder;
0N/A }
0N/A
0N/A public static Border getMenuBarBorder() {
0N/A UIDefaults table = UIManager.getLookAndFeelDefaults();
0N/A Border menuBarBorder = new BasicBorders.MenuBarBorder(
0N/A table.getColor("MenuBar.shadow"),
0N/A table.getColor("MenuBar.highlight")
0N/A );
0N/A return menuBarBorder;
0N/A }
0N/A
0N/A public static Border getSplitPaneBorder() {
0N/A UIDefaults table = UIManager.getLookAndFeelDefaults();
0N/A Border splitPaneBorder = new BasicBorders.SplitPaneBorder(
0N/A table.getColor("SplitPane.highlight"),
0N/A table.getColor("SplitPane.darkShadow"));
0N/A return splitPaneBorder;
0N/A }
0N/A
0N/A /**
0N/A * Returns a border instance for a JSplitPane divider
0N/A * @since 1.3
0N/A */
0N/A public static Border getSplitPaneDividerBorder() {
0N/A UIDefaults table = UIManager.getLookAndFeelDefaults();
0N/A Border splitPaneBorder = new BasicBorders.SplitPaneDividerBorder(
0N/A table.getColor("SplitPane.highlight"),
0N/A table.getColor("SplitPane.darkShadow"));
0N/A return splitPaneBorder;
0N/A }
0N/A
0N/A public static Border getTextFieldBorder() {
0N/A UIDefaults table = UIManager.getLookAndFeelDefaults();
0N/A Border textFieldBorder = new BasicBorders.FieldBorder(
0N/A table.getColor("TextField.shadow"),
0N/A table.getColor("TextField.darkShadow"),
0N/A table.getColor("TextField.light"),
0N/A table.getColor("TextField.highlight"));
0N/A return textFieldBorder;
0N/A }
0N/A
0N/A public static Border getProgressBarBorder() {
0N/A UIDefaults table = UIManager.getLookAndFeelDefaults();
0N/A Border progressBarBorder = new BorderUIResource.LineBorderUIResource(Color.green, 2);
0N/A return progressBarBorder;
0N/A }
0N/A
0N/A public static Border getInternalFrameBorder() {
0N/A UIDefaults table = UIManager.getLookAndFeelDefaults();
0N/A Border internalFrameBorder = new BorderUIResource.CompoundBorderUIResource(
0N/A new BevelBorder(BevelBorder.RAISED,
0N/A table.getColor("InternalFrame.borderLight"),
0N/A table.getColor("InternalFrame.borderHighlight"),
0N/A table.getColor("InternalFrame.borderDarkShadow"),
0N/A table.getColor("InternalFrame.borderShadow")),
0N/A BorderFactory.createLineBorder(
0N/A table.getColor("InternalFrame.borderColor"), 1));
0N/A
0N/A return internalFrameBorder;
0N/A }
0N/A
0N/A /**
0N/A * Special thin border for rollover toolbar buttons.
0N/A * @since 1.4
0N/A */
0N/A public static class RolloverButtonBorder extends ButtonBorder {
0N/A
0N/A public RolloverButtonBorder(Color shadow, Color darkShadow,
0N/A Color highlight, Color lightHighlight) {
0N/A super(shadow, darkShadow, highlight, lightHighlight);
0N/A }
0N/A
0N/A public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
0N/A AbstractButton b = (AbstractButton) c;
0N/A ButtonModel model = b.getModel();
0N/A
0N/A Color shade = shadow;
0N/A Component p = b.getParent();
0N/A if (p != null && p.getBackground().equals(shadow)) {
0N/A shade = darkShadow;
0N/A }
0N/A
0N/A if ((model.isRollover() && !(model.isPressed() && !model.isArmed())) ||
0N/A model.isSelected()) {
0N/A
0N/A Color oldColor = g.getColor();
0N/A g.translate(x, y);
0N/A
0N/A if (model.isPressed() && model.isArmed() || model.isSelected()) {
0N/A // Draw the pressd button
0N/A g.setColor(shade);
0N/A g.drawRect(0, 0, w-1, h-1);
0N/A g.setColor(lightHighlight);
0N/A g.drawLine(w-1, 0, w-1, h-1);
0N/A g.drawLine(0, h-1, w-1, h-1);
0N/A } else {
0N/A // Draw a rollover button
0N/A g.setColor(lightHighlight);
0N/A g.drawRect(0, 0, w-1, h-1);
0N/A g.setColor(shade);
0N/A g.drawLine(w-1, 0, w-1, h-1);
0N/A g.drawLine(0, h-1, w-1, h-1);
0N/A }
0N/A g.translate(-x, -y);
0N/A g.setColor(oldColor);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * A border which is like a Margin border but it will only honor the margin
0N/A * if the margin has been explicitly set by the developer.
0N/A *
0N/A * Note: This is identical to the package private class
0N/A * MetalBorders.RolloverMarginBorder and should probably be consolidated.
0N/A */
0N/A static class RolloverMarginBorder extends EmptyBorder {
0N/A
0N/A public RolloverMarginBorder() {
0N/A super(3,3,3,3); // hardcoded margin for JLF requirements.
0N/A }
0N/A
0N/A public Insets getBorderInsets(Component c, Insets insets) {
0N/A Insets margin = null;
0N/A
0N/A if (c instanceof AbstractButton) {
0N/A margin = ((AbstractButton)c).getMargin();
0N/A }
0N/A if (margin == null || margin instanceof UIResource) {
0N/A // default margin so replace
0N/A insets.left = left;
0N/A insets.top = top;
0N/A insets.right = right;
0N/A insets.bottom = bottom;
0N/A } else {
0N/A // Margin which has been explicitly set by the user.
0N/A insets.left = margin.left;
0N/A insets.top = margin.top;
0N/A insets.right = margin.right;
0N/A insets.bottom = margin.bottom;
0N/A }
0N/A return insets;
0N/A }
0N/A }
0N/A
0N/A public static class ButtonBorder extends AbstractBorder implements UIResource {
0N/A protected Color shadow;
0N/A protected Color darkShadow;
0N/A protected Color highlight;
0N/A protected Color lightHighlight;
0N/A
0N/A public ButtonBorder(Color shadow, Color darkShadow,
0N/A Color highlight, Color lightHighlight) {
0N/A this.shadow = shadow;
0N/A this.darkShadow = darkShadow;
0N/A this.highlight = highlight;
0N/A this.lightHighlight = lightHighlight;
0N/A }
0N/A
0N/A public void paintBorder(Component c, Graphics g, int x, int y,
0N/A int width, int height) {
0N/A boolean isPressed = false;
0N/A boolean isDefault = false;
0N/A
0N/A if (c instanceof AbstractButton) {
0N/A AbstractButton b = (AbstractButton)c;
0N/A ButtonModel model = b.getModel();
0N/A
0N/A isPressed = model.isPressed() && model.isArmed();
0N/A
0N/A if (c instanceof JButton) {
0N/A isDefault = ((JButton)c).isDefaultButton();
0N/A }
0N/A }
0N/A BasicGraphicsUtils.drawBezel(g, x, y, width, height,
0N/A isPressed, isDefault, shadow,
0N/A darkShadow, highlight, lightHighlight);
0N/A }
0N/A
0N/A public Insets getBorderInsets(Component c, Insets insets) {
0N/A // leave room for default visual
0N/A insets.set(2, 3, 3, 3);
0N/A return insets;
0N/A }
0N/A
0N/A }
0N/A
0N/A public static class ToggleButtonBorder extends ButtonBorder {
0N/A
0N/A public ToggleButtonBorder(Color shadow, Color darkShadow,
0N/A Color highlight, Color lightHighlight) {
0N/A super(shadow, darkShadow, highlight, lightHighlight);
0N/A }
0N/A
0N/A public void paintBorder(Component c, Graphics g, int x, int y,
0N/A int width, int height) {
0N/A BasicGraphicsUtils.drawBezel(g, x, y, width, height,
0N/A false, false,
0N/A shadow, darkShadow,
0N/A highlight, lightHighlight);
0N/A }
0N/A
0N/A public Insets getBorderInsets(Component c, Insets insets) {
0N/A insets.set(2, 2, 2, 2);
0N/A return insets;
0N/A }
0N/A }
0N/A
0N/A public static class RadioButtonBorder extends ButtonBorder {
0N/A
0N/A public RadioButtonBorder(Color shadow, Color darkShadow,
0N/A Color highlight, Color lightHighlight) {
0N/A super(shadow, darkShadow, highlight, lightHighlight);
0N/A }
0N/A
0N/A
0N/A public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
0N/A
0N/A if (c instanceof AbstractButton) {
0N/A AbstractButton b = (AbstractButton)c;
0N/A ButtonModel model = b.getModel();
0N/A
0N/A if (model.isArmed() && model.isPressed() || model.isSelected()) {
0N/A BasicGraphicsUtils.drawLoweredBezel(g, x, y, width, height,
0N/A shadow, darkShadow,
0N/A highlight, lightHighlight);
0N/A } else {
0N/A BasicGraphicsUtils.drawBezel(g, x, y, width, height,
0N/A false, b.isFocusPainted() && b.hasFocus(),
0N/A shadow, darkShadow,
0N/A highlight, lightHighlight);
0N/A }
0N/A } else {
0N/A BasicGraphicsUtils.drawBezel(g, x, y, width, height, false, false,
0N/A shadow, darkShadow, highlight, lightHighlight);
0N/A }
0N/A }
0N/A
0N/A public Insets getBorderInsets(Component c, Insets insets) {
0N/A insets.set(2, 2, 2, 2);
0N/A return insets;
0N/A }
0N/A }
0N/A
0N/A public static class MenuBarBorder extends AbstractBorder implements UIResource {
0N/A private Color shadow;
0N/A private Color highlight;
0N/A
0N/A public MenuBarBorder(Color shadow, Color highlight) {
0N/A this.shadow = shadow;
0N/A this.highlight = highlight;
0N/A }
0N/A
0N/A public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
0N/A Color oldColor = g.getColor();
0N/A g.translate(x, y);
0N/A g.setColor(shadow);
0N/A g.drawLine(0, height-2, width, height-2);
0N/A g.setColor(highlight);
0N/A g.drawLine(0, height-1, width, height-1);
0N/A g.translate(-x,-y);
0N/A g.setColor(oldColor);
0N/A }
0N/A
0N/A public Insets getBorderInsets(Component c, Insets insets) {
0N/A insets.set(0, 0, 2, 0);
0N/A return insets;
0N/A }
0N/A }
0N/A
0N/A public static class MarginBorder extends AbstractBorder implements UIResource {
0N/A public Insets getBorderInsets(Component c, Insets insets) {
0N/A Insets margin = null;
0N/A //
0N/A // Ideally we'd have an interface defined for classes which
0N/A // support margins (to avoid this hackery), but we've
0N/A // decided against it for simplicity
0N/A //
0N/A if (c instanceof AbstractButton) {
0N/A AbstractButton b = (AbstractButton)c;
0N/A margin = b.getMargin();
0N/A } else if (c instanceof JToolBar) {
0N/A JToolBar t = (JToolBar)c;
0N/A margin = t.getMargin();
0N/A } else if (c instanceof JTextComponent) {
0N/A JTextComponent t = (JTextComponent)c;
0N/A margin = t.getMargin();
0N/A }
0N/A insets.top = margin != null? margin.top : 0;
0N/A insets.left = margin != null? margin.left : 0;
0N/A insets.bottom = margin != null? margin.bottom : 0;
0N/A insets.right = margin != null? margin.right : 0;
0N/A
0N/A return insets;
0N/A }
0N/A }
0N/A
0N/A public static class FieldBorder extends AbstractBorder implements UIResource {
0N/A protected Color shadow;
0N/A protected Color darkShadow;
0N/A protected Color highlight;
0N/A protected Color lightHighlight;
0N/A
0N/A public FieldBorder(Color shadow, Color darkShadow,
0N/A Color highlight, Color lightHighlight) {
0N/A this.shadow = shadow;
0N/A this.highlight = highlight;
0N/A this.darkShadow = darkShadow;
0N/A this.lightHighlight = lightHighlight;
0N/A }
0N/A
0N/A public void paintBorder(Component c, Graphics g, int x, int y,
0N/A int width, int height) {
0N/A BasicGraphicsUtils.drawEtchedRect(g, x, y, width, height,
0N/A shadow, darkShadow,
0N/A highlight, lightHighlight);
0N/A }
0N/A
0N/A public Insets getBorderInsets(Component c, Insets insets) {
0N/A Insets margin = null;
0N/A if (c instanceof JTextComponent) {
0N/A margin = ((JTextComponent)c).getMargin();
0N/A }
0N/A insets.top = margin != null? 2+margin.top : 2;
0N/A insets.left = margin != null? 2+margin.left : 2;
0N/A insets.bottom = margin != null? 2+margin.bottom : 2;
0N/A insets.right = margin != null? 2+margin.right : 2;
0N/A
0N/A return insets;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Draws the border around the divider in a splitpane
0N/A * (when BasicSplitPaneUI is used). To get the appropriate effect, this
0N/A * needs to be used with a SplitPaneBorder.
0N/A */
0N/A static class SplitPaneDividerBorder implements Border, UIResource {
0N/A Color highlight;
0N/A Color shadow;
0N/A
0N/A SplitPaneDividerBorder(Color highlight, Color shadow) {
0N/A this.highlight = highlight;
0N/A this.shadow = shadow;
0N/A }
0N/A
0N/A public void paintBorder(Component c, Graphics g, int x, int y,
0N/A int width, int height) {
2721N/A if (!(c instanceof BasicSplitPaneDivider)) {
2721N/A return;
2721N/A }
0N/A Component child;
0N/A Rectangle cBounds;
0N/A JSplitPane splitPane = ((BasicSplitPaneDivider)c).
0N/A getBasicSplitPaneUI().getSplitPane();
0N/A Dimension size = c.getSize();
0N/A
0N/A child = splitPane.getLeftComponent();
0N/A // This is needed for the space between the divider and end of
0N/A // splitpane.
0N/A g.setColor(c.getBackground());
0N/A g.drawRect(x, y, width - 1, height - 1);
0N/A if(splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
0N/A if(child != null) {
0N/A g.setColor(highlight);
0N/A g.drawLine(0, 0, 0, size.height);
0N/A }
0N/A child = splitPane.getRightComponent();
0N/A if(child != null) {
0N/A g.setColor(shadow);
0N/A g.drawLine(size.width - 1, 0, size.width - 1, size.height);
0N/A }
0N/A } else {
0N/A if(child != null) {
0N/A g.setColor(highlight);
0N/A g.drawLine(0, 0, size.width, 0);
0N/A }
0N/A child = splitPane.getRightComponent();
0N/A if(child != null) {
0N/A g.setColor(shadow);
0N/A g.drawLine(0, size.height - 1, size.width,
0N/A size.height - 1);
0N/A }
0N/A }
0N/A }
0N/A public Insets getBorderInsets(Component c) {
0N/A Insets insets = new Insets(0,0,0,0);
0N/A if (c instanceof BasicSplitPaneDivider) {
0N/A BasicSplitPaneUI bspui = ((BasicSplitPaneDivider)c).
0N/A getBasicSplitPaneUI();
0N/A
0N/A if (bspui != null) {
0N/A JSplitPane splitPane = bspui.getSplitPane();
0N/A
0N/A if (splitPane != null) {
0N/A if (splitPane.getOrientation() ==
0N/A JSplitPane.HORIZONTAL_SPLIT) {
0N/A insets.top = insets.bottom = 0;
0N/A insets.left = insets.right = 1;
0N/A return insets;
0N/A }
0N/A // VERTICAL_SPLIT
0N/A insets.top = insets.bottom = 1;
0N/A insets.left = insets.right = 0;
0N/A return insets;
0N/A }
0N/A }
0N/A }
0N/A insets.top = insets.bottom = insets.left = insets.right = 1;
0N/A return insets;
0N/A }
0N/A public boolean isBorderOpaque() { return true; }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Draws the border around the splitpane. To work correctly you shoudl
0N/A * also install a border on the divider (property SplitPaneDivider.border).
0N/A */
0N/A public static class SplitPaneBorder implements Border, UIResource {
0N/A protected Color highlight;
0N/A protected Color shadow;
0N/A
0N/A public SplitPaneBorder(Color highlight, Color shadow) {
0N/A this.highlight = highlight;
0N/A this.shadow = shadow;
0N/A }
0N/A
0N/A public void paintBorder(Component c, Graphics g, int x, int y,
0N/A int width, int height) {
2721N/A if (!(c instanceof JSplitPane)) {
2721N/A return;
2721N/A }
0N/A // The only tricky part with this border is that the divider is
0N/A // not positioned at the top (for horizontal) or left (for vert),
0N/A // so this border draws to where the divider is:
0N/A // -----------------
0N/A // |xxxxxxx xxxxxxx|
0N/A // |x --- x|
0N/A // |x | | x|
0N/A // |x |D| x|
0N/A // |x | | x|
0N/A // |x --- x|
0N/A // |xxxxxxx xxxxxxx|
0N/A // -----------------
0N/A // The above shows (rather excessively) what this looks like for
0N/A // a horizontal orientation. This border then draws the x's, with
0N/A // the SplitPaneDividerBorder drawing its own border.
0N/A
0N/A Component child;
0N/A Rectangle cBounds;
0N/A
0N/A JSplitPane splitPane = (JSplitPane)c;
0N/A
0N/A child = splitPane.getLeftComponent();
0N/A // This is needed for the space between the divider and end of
0N/A // splitpane.
0N/A g.setColor(c.getBackground());
0N/A g.drawRect(x, y, width - 1, height - 1);
0N/A if(splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
0N/A if(child != null) {
0N/A cBounds = child.getBounds();
0N/A g.setColor(shadow);
0N/A g.drawLine(0, 0, cBounds.width + 1, 0);
0N/A g.drawLine(0, 1, 0, cBounds.height + 1);
0N/A
0N/A g.setColor(highlight);
0N/A g.drawLine(0, cBounds.height + 1, cBounds.width + 1,
0N/A cBounds.height + 1);
0N/A }
0N/A child = splitPane.getRightComponent();
0N/A if(child != null) {
0N/A cBounds = child.getBounds();
0N/A
0N/A int maxX = cBounds.x + cBounds.width;
0N/A int maxY = cBounds.y + cBounds.height;
0N/A
0N/A g.setColor(shadow);
0N/A g.drawLine(cBounds.x - 1, 0, maxX, 0);
0N/A g.setColor(highlight);
0N/A g.drawLine(cBounds.x - 1, maxY, maxX, maxY);
0N/A g.drawLine(maxX, 0, maxX, maxY + 1);
0N/A }
0N/A } else {
0N/A if(child != null) {
0N/A cBounds = child.getBounds();
0N/A g.setColor(shadow);
0N/A g.drawLine(0, 0, cBounds.width + 1, 0);
0N/A g.drawLine(0, 1, 0, cBounds.height);
0N/A g.setColor(highlight);
0N/A g.drawLine(1 + cBounds.width, 0, 1 + cBounds.width,
0N/A cBounds.height + 1);
0N/A g.drawLine(0, cBounds.height + 1, 0, cBounds.height + 1);
0N/A }
0N/A child = splitPane.getRightComponent();
0N/A if(child != null) {
0N/A cBounds = child.getBounds();
0N/A
0N/A int maxX = cBounds.x + cBounds.width;
0N/A int maxY = cBounds.y + cBounds.height;
0N/A
0N/A g.setColor(shadow);
0N/A g.drawLine(0, cBounds.y - 1, 0, maxY);
0N/A g.drawLine(maxX, cBounds.y - 1, maxX, cBounds.y - 1);
0N/A g.setColor(highlight);
0N/A g.drawLine(0, maxY, cBounds.width + 1, maxY);
0N/A g.drawLine(maxX, cBounds.y, maxX, maxY);
0N/A }
0N/A }
0N/A }
0N/A public Insets getBorderInsets(Component c) {
0N/A return new Insets(1, 1, 1, 1);
0N/A }
0N/A public boolean isBorderOpaque() { return true; }
0N/A }
0N/A
0N/A}