978 lines · 34.2 KB · Java
RawDownload
/* * 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 * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * 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. */ package javax.swing.plaf.metal; import javax.swing.*;import javax.swing.border.*;import javax.swing.plaf.*;import javax.swing.plaf.basic.BasicBorders;import javax.swing.text.JTextComponent; import java.awt.Component;import java.awt.Insets;import java.awt.Color;import java.awt.Dialog;import java.awt.Frame;import java.awt.Graphics;import java.awt.Window; import sun.swing.StringUIClientPropertyKey;  /** * Factory object that can vend Borders appropriate for the metal L & F. * @author Steve Wilson */ public class MetalBorders {     /**     * Client property indicating the button shouldn't provide a rollover     * indicator. Only used with the Ocean theme.     */    static Object NO_BUTTON_ROLLOVER =        new StringUIClientPropertyKey("NoButtonRollover");      public static class Flush3DBorder extends AbstractBorder implements UIResource{        public void paintBorder(Component c, Graphics g, int x, int y,                          int w, int h) {            if (c.isEnabled()) {                MetalUtils.drawFlush3DBorder(g, x, y, w, h);            } else {                MetalUtils.drawDisabledBorder(g, x, y, w, h);            }        }         public Insets getBorderInsets(Component c, Insets newInsets) {            newInsets.set(2, 2, 2, 2);            return newInsets;        }    }     public static class ButtonBorder extends AbstractBorder implements UIResource {         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;            }            AbstractButton button = (AbstractButton)c;            ButtonModel model = button.getModel();             if ( model.isEnabled() ) {                boolean isPressed = model.isPressed() && model.isArmed();                boolean isDefault = (button instanceof JButton && ((JButton)button).isDefaultButton());                 if (isPressed && isDefault) {                    MetalUtils.drawDefaultButtonPressedBorder(g, x, y, w, h);                } else if (isPressed) {                    MetalUtils.drawPressed3DBorder( g, x, y, w, h );                } else if (isDefault) {                    MetalUtils.drawDefaultButtonBorder( g, x, y, w, h, false);                } else {                    MetalUtils.drawButtonBorder( g, x, y, w, h, false);                }            } else { // disabled state                MetalUtils.drawDisabledBorder( g, x, y, w-1, h-1 );            }        }         private void paintOceanBorder(Component c, Graphics g, int x, int y,                                      int w, int h) {            AbstractButton button = (AbstractButton)c;            ButtonModel model = ((AbstractButton)c).getModel();             g.translate(x, y);            if (MetalUtils.isToolBarButton(button)) {                if (model.isEnabled()) {                    if (model.isPressed()) {                        g.setColor(MetalLookAndFeel.getWhite());                        g.fillRect(1, h - 1, w - 1, 1);                        g.fillRect(w - 1, 1, 1, h - 1);                        g.setColor(MetalLookAndFeel.getControlDarkShadow());                        g.drawRect(0, 0, w - 2, h - 2);                        g.fillRect(1, 1, w - 3, 1);                    }                    else if (model.isSelected() || model.isRollover()) {                        g.setColor(MetalLookAndFeel.getWhite());                        g.fillRect(1, h - 1, w - 1, 1);                        g.fillRect(w - 1, 1, 1, h - 1);                        g.setColor(MetalLookAndFeel.getControlDarkShadow());                        g.drawRect(0, 0, w - 2, h - 2);                    }                    else {                        g.setColor(MetalLookAndFeel.getWhite());                        g.drawRect(1, 1, w - 2, h - 2);                        g.setColor(UIManager.getColor(                                "Button.toolBarBorderBackground"));                        g.drawRect(0, 0, w - 2, h - 2);                    }                }                else {                   g.setColor(UIManager.getColor(                           "Button.disabledToolBarBorderBackground"));                   g.drawRect(0, 0, w - 2, h - 2);                }            }            else if (model.isEnabled()) {                boolean pressed = model.isPressed();                boolean armed = model.isArmed();                 if ((c instanceof JButton) && ((JButton)c).isDefaultButton()) {                    g.setColor(MetalLookAndFeel.getControlDarkShadow());                    g.drawRect(0, 0, w - 1, h - 1);                    g.drawRect(1, 1, w - 3, h - 3);                }                else if (pressed) {                    g.setColor(MetalLookAndFeel.getControlDarkShadow());                    g.fillRect(0, 0, w, 2);                    g.fillRect(0, 2, 2, h - 2);                    g.fillRect(w - 1, 1, 1, h - 1);                    g.fillRect(1, h - 1, w - 2, 1);                }                else if (model.isRollover() && button.getClientProperty(                               NO_BUTTON_ROLLOVER) == null) {                    g.setColor(MetalLookAndFeel.getPrimaryControl());                    g.drawRect(0, 0, w - 1, h - 1);                    g.drawRect(2, 2, w - 5, h - 5);                    g.setColor(MetalLookAndFeel.getControlDarkShadow());                    g.drawRect(1, 1, w - 3, h - 3);                }                else {                    g.setColor(MetalLookAndFeel.getControlDarkShadow());                    g.drawRect(0, 0, w - 1, h - 1);                }            }            else {                g.setColor(MetalLookAndFeel.getInactiveControlTextColor());                g.drawRect(0, 0, w - 1, h - 1);                if ((c instanceof JButton) && ((JButton)c).isDefaultButton()) {                    g.drawRect(1, 1, w - 3, h - 3);                }            }        }         public Insets getBorderInsets(Component c, Insets newInsets) {            newInsets.set(3, 3, 3, 3);            return newInsets;        }    }     public static class InternalFrameBorder extends AbstractBorder implements UIResource {        private static final int corner = 14;         public void paintBorder(Component c, Graphics g, int x, int y,                          int w, int h) {             Color background;            Color highlight;            Color shadow;             if (c instanceof JInternalFrame && ((JInternalFrame)c).isSelected()) {                background = MetalLookAndFeel.getPrimaryControlDarkShadow();                highlight = MetalLookAndFeel.getPrimaryControlShadow();                shadow = MetalLookAndFeel.getPrimaryControlInfo();            } else {                background = MetalLookAndFeel.getControlDarkShadow();                highlight = MetalLookAndFeel.getControlShadow();                shadow = MetalLookAndFeel.getControlInfo();            }               g.setColor(background);              // Draw outermost lines              g.drawLine( 1, 0, w-2, 0);              g.drawLine( 0, 1, 0, h-2);              g.drawLine( w-1, 1, w-1, h-2);              g.drawLine( 1, h-1, w-2, h-1);               // Draw the bulk of the border              for (int i = 1; i < 5; i++) {                  g.drawRect(x+i,y+i,w-(i*2)-1, h-(i*2)-1);              }               if (c instanceof JInternalFrame &&                               ((JInternalFrame)c).isResizable()) {                  g.setColor(highlight);                  // Draw the Long highlight lines                  g.drawLine( corner+1, 3, w-corner, 3);                  g.drawLine( 3, corner+1, 3, h-corner);                  g.drawLine( w-2, corner+1, w-2, h-corner);                  g.drawLine( corner+1, h-2, w-corner, h-2);                   g.setColor(shadow);                  // Draw the Long shadow lines                  g.drawLine( corner, 2, w-corner-1, 2);                  g.drawLine( 2, corner, 2, h-corner-1);                  g.drawLine( w-3, corner, w-3, h-corner-1);                  g.drawLine( corner, h-3, w-corner-1, h-3);              }           }           public Insets getBorderInsets(Component c, Insets newInsets) {              newInsets.set(5, 5, 5, 5);              return newInsets;          }    }     /**     * Border for a Frame.     * @since 1.4     */    static class FrameBorder extends AbstractBorder implements UIResource {        private static final int corner = 14;         public void paintBorder(Component c, Graphics g, int x, int y,            int w, int h) {             Color background;            Color highlight;            Color shadow;             Window window = SwingUtilities.getWindowAncestor(c);            if (window != null && window.isActive()) {                background = MetalLookAndFeel.getPrimaryControlDarkShadow();                highlight = MetalLookAndFeel.getPrimaryControlShadow();                shadow = MetalLookAndFeel.getPrimaryControlInfo();            } else {                background = MetalLookAndFeel.getControlDarkShadow();                highlight = MetalLookAndFeel.getControlShadow();                shadow = MetalLookAndFeel.getControlInfo();            }             g.setColor(background);            // Draw outermost lines            g.drawLine( x+1, y+0, x+w-2, y+0);            g.drawLine( x+0, y+1, x+0, y +h-2);            g.drawLine( x+w-1, y+1, x+w-1, y+h-2);            g.drawLine( x+1, y+h-1, x+w-2, y+h-1);             // Draw the bulk of the border            for (int i = 1; i < 5; i++) {                g.drawRect(x+i,y+i,w-(i*2)-1, h-(i*2)-1);            }             if ((window instanceof Frame) && ((Frame) window).isResizable()) {                g.setColor(highlight);                // Draw the Long highlight lines                g.drawLine( corner+1, 3, w-corner, 3);                g.drawLine( 3, corner+1, 3, h-corner);                g.drawLine( w-2, corner+1, w-2, h-corner);                g.drawLine( corner+1, h-2, w-corner, h-2);                 g.setColor(shadow);                // Draw the Long shadow lines                g.drawLine( corner, 2, w-corner-1, 2);                g.drawLine( 2, corner, 2, h-corner-1);                g.drawLine( w-3, corner, w-3, h-corner-1);                g.drawLine( corner, h-3, w-corner-1, h-3);            }         }         public Insets getBorderInsets(Component c, Insets newInsets)        {            newInsets.set(5, 5, 5, 5);            return newInsets;        }    }     /**     * Border for a Frame.     * @since 1.4     */    static class DialogBorder extends AbstractBorder implements UIResource    {        private static final int corner = 14;         protected Color getActiveBackground()        {            return MetalLookAndFeel.getPrimaryControlDarkShadow();        }         protected Color getActiveHighlight()        {            return MetalLookAndFeel.getPrimaryControlShadow();        }         protected Color getActiveShadow()        {            return MetalLookAndFeel.getPrimaryControlInfo();        }         protected Color getInactiveBackground()        {            return MetalLookAndFeel.getControlDarkShadow();        }         protected Color getInactiveHighlight()        {            return MetalLookAndFeel.getControlShadow();        }         protected Color getInactiveShadow()        {            return MetalLookAndFeel.getControlInfo();        }         public void paintBorder(Component c, Graphics g, int x, int y, int w, int h)        {            Color background;            Color highlight;            Color shadow;             Window window = SwingUtilities.getWindowAncestor(c);            if (window != null && window.isActive()) {                background = getActiveBackground();                highlight = getActiveHighlight();                shadow = getActiveShadow();            } else {                background = getInactiveBackground();                highlight = getInactiveHighlight();                shadow = getInactiveShadow();            }             g.setColor(background);            // Draw outermost lines            g.drawLine( x + 1, y + 0, x + w-2, y + 0);            g.drawLine( x + 0, y + 1, x + 0, y + h - 2);            g.drawLine( x + w - 1, y + 1, x + w - 1, y + h - 2);            g.drawLine( x + 1, y + h - 1, x + w - 2, y + h - 1);             // Draw the bulk of the border            for (int i = 1; i < 5; i++) {                g.drawRect(x+i,y+i,w-(i*2)-1, h-(i*2)-1);            }              if ((window instanceof Dialog) && ((Dialog) window).isResizable()) {                g.setColor(highlight);                // Draw the Long highlight lines                g.drawLine( corner+1, 3, w-corner, 3);                g.drawLine( 3, corner+1, 3, h-corner);                g.drawLine( w-2, corner+1, w-2, h-corner);                g.drawLine( corner+1, h-2, w-corner, h-2);                 g.setColor(shadow);                // Draw the Long shadow lines                g.drawLine( corner, 2, w-corner-1, 2);                g.drawLine( 2, corner, 2, h-corner-1);                g.drawLine( w-3, corner, w-3, h-corner-1);                g.drawLine( corner, h-3, w-corner-1, h-3);            }         }         public Insets getBorderInsets(Component c, Insets newInsets)        {            newInsets.set(5, 5, 5, 5);            return newInsets;        }    }     /**     * Border for an Error Dialog.     * @since 1.4     */    static class ErrorDialogBorder extends DialogBorder implements UIResource    {        protected Color getActiveBackground() {            return UIManager.getColor("OptionPane.errorDialog.border.background");        }    }      /**     * Border for a QuestionDialog.  Also used for a JFileChooser and a     * JColorChooser..     * @since 1.4     */    static class QuestionDialogBorder extends DialogBorder implements UIResource    {        protected Color getActiveBackground() {            return UIManager.getColor("OptionPane.questionDialog.border.background");        }    }      /**     * Border for a Warning Dialog.     * @since 1.4     */    static class WarningDialogBorder extends DialogBorder implements UIResource    {        protected Color getActiveBackground() {            return UIManager.getColor("OptionPane.warningDialog.border.background");        }    }      /**     * Border for a Palette.     * @since 1.3     */    public static class PaletteBorder extends AbstractBorder implements UIResource {        int titleHeight = 0;         public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {             g.translate(x,y);            g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());            g.drawLine(0, 1, 0, h-2);            g.drawLine(1, h-1, w-2, h-1);            g.drawLine(w-1,  1, w-1, h-2);            g.drawLine( 1, 0, w-2, 0);            g.drawRect(1,1, w-3, h-3);            g.translate(-x,-y);         }         public Insets getBorderInsets(Component c, Insets newInsets) {            newInsets.set(1, 1, 1, 1);            return newInsets;        }    }     public static class OptionDialogBorder extends AbstractBorder implements UIResource {        int titleHeight = 0;         public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {             g.translate(x,y);             int messageType = JOptionPane.PLAIN_MESSAGE;            if (c instanceof JInternalFrame) {                Object obj = ((JInternalFrame) c).getClientProperty(                              "JInternalFrame.messageType");                if (obj instanceof Integer) {                    messageType = (Integer) obj;                }            }             Color borderColor;             switch (messageType) {            case(JOptionPane.ERROR_MESSAGE):                borderColor = UIManager.getColor(                    "OptionPane.errorDialog.border.background");                break;            case(JOptionPane.QUESTION_MESSAGE):                borderColor = UIManager.getColor(                    "OptionPane.questionDialog.border.background");                break;            case(JOptionPane.WARNING_MESSAGE):                borderColor = UIManager.getColor(                    "OptionPane.warningDialog.border.background");                break;            case(JOptionPane.INFORMATION_MESSAGE):            case(JOptionPane.PLAIN_MESSAGE):            default:                borderColor = MetalLookAndFeel.getPrimaryControlDarkShadow();                break;

Lines 1–500 of 978.