0N/A/*
2362N/A * Copyright (c) 1997, 2006, 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 com.sun.java.swing.plaf.windows;
0N/A
0N/Aimport javax.swing.plaf.basic.*;
0N/Aimport javax.swing.border.*;
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.swing.*;
0N/A
0N/Aimport java.awt.*;
0N/A
0N/Aimport static com.sun.java.swing.plaf.windows.TMSchema.*;
0N/Aimport static com.sun.java.swing.plaf.windows.TMSchema.Part.*;
0N/Aimport static com.sun.java.swing.plaf.windows.XPStyle.Skin;
1859N/Aimport sun.awt.AppContext;
0N/A
0N/A
0N/A/**
0N/A * Windows button.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is appropriate
0N/A * for short term storage or RMI between applications running the same
0N/A * version of Swing. A future release of Swing will provide support for
0N/A * long term persistence.
0N/A *
0N/A * @author Jeff Dinkins
0N/A *
0N/A */
0N/Apublic class WindowsButtonUI extends BasicButtonUI
0N/A{
0N/A protected int dashedRectGapX;
0N/A protected int dashedRectGapY;
0N/A protected int dashedRectGapWidth;
0N/A protected int dashedRectGapHeight;
0N/A
0N/A protected Color focusColor;
0N/A
0N/A private boolean defaults_initialized = false;
0N/A
1859N/A private static final Object WINDOWS_BUTTON_UI_KEY = new Object();
0N/A
0N/A // ********************************
0N/A // Create PLAF
0N/A // ********************************
1859N/A public static ComponentUI createUI(JComponent c) {
1859N/A AppContext appContext = AppContext.getAppContext();
1859N/A WindowsButtonUI windowsButtonUI =
1859N/A (WindowsButtonUI) appContext.get(WINDOWS_BUTTON_UI_KEY);
1859N/A if (windowsButtonUI == null) {
1859N/A windowsButtonUI = new WindowsButtonUI();
1859N/A appContext.put(WINDOWS_BUTTON_UI_KEY, windowsButtonUI);
1859N/A }
0N/A return windowsButtonUI;
0N/A }
0N/A
0N/A
0N/A // ********************************
0N/A // Defaults
0N/A // ********************************
0N/A protected void installDefaults(AbstractButton b) {
0N/A super.installDefaults(b);
0N/A if(!defaults_initialized) {
0N/A String pp = getPropertyPrefix();
0N/A dashedRectGapX = UIManager.getInt(pp + "dashedRectGapX");
0N/A dashedRectGapY = UIManager.getInt(pp + "dashedRectGapY");
0N/A dashedRectGapWidth = UIManager.getInt(pp + "dashedRectGapWidth");
0N/A dashedRectGapHeight = UIManager.getInt(pp + "dashedRectGapHeight");
0N/A focusColor = UIManager.getColor(pp + "focus");
0N/A defaults_initialized = true;
0N/A }
0N/A
0N/A XPStyle xp = XPStyle.getXP();
0N/A if (xp != null) {
0N/A b.setBorder(xp.getBorder(b, getXPButtonType(b)));
0N/A LookAndFeel.installProperty(b, "rolloverEnabled", Boolean.TRUE);
0N/A }
0N/A }
0N/A
0N/A protected void uninstallDefaults(AbstractButton b) {
0N/A super.uninstallDefaults(b);
0N/A defaults_initialized = false;
0N/A }
0N/A
0N/A protected Color getFocusColor() {
0N/A return focusColor;
0N/A }
0N/A
0N/A // ********************************
0N/A // Paint Methods
0N/A // ********************************
0N/A
0N/A /**
0N/A * Overridden method to render the text without the mnemonic
0N/A */
0N/A protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) {
0N/A WindowsGraphicsUtils.paintText(g, b, textRect, text, getTextShiftOffset());
0N/A }
0N/A
0N/A protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
0N/A
0N/A // focus painted same color as text on Basic??
0N/A int width = b.getWidth();
0N/A int height = b.getHeight();
0N/A g.setColor(getFocusColor());
0N/A BasicGraphicsUtils.drawDashedRect(g, dashedRectGapX, dashedRectGapY,
0N/A width - dashedRectGapWidth, height - dashedRectGapHeight);
0N/A }
0N/A
0N/A protected void paintButtonPressed(Graphics g, AbstractButton b){
0N/A setTextShiftOffset();
0N/A }
0N/A
0N/A // ********************************
0N/A // Layout Methods
0N/A // ********************************
0N/A public Dimension getPreferredSize(JComponent c) {
0N/A Dimension d = super.getPreferredSize(c);
0N/A
0N/A /* Ensure that the width and height of the button is odd,
0N/A * to allow for the focus line if focus is painted
0N/A */
0N/A AbstractButton b = (AbstractButton)c;
0N/A if (d != null && b.isFocusPainted()) {
0N/A if(d.width % 2 == 0) { d.width += 1; }
0N/A if(d.height % 2 == 0) { d.height += 1; }
0N/A }
0N/A return d;
0N/A }
0N/A
0N/A
0N/A /* These rectangles/insets are allocated once for all
0N/A * ButtonUI.paint() calls. Re-using rectangles rather than
0N/A * allocating them in each paint call substantially reduced the time
0N/A * it took paint to run. Obviously, this method can't be re-entered.
0N/A */
1859N/A private Rectangle viewRect = new Rectangle();
0N/A
0N/A public void paint(Graphics g, JComponent c) {
0N/A if (XPStyle.getXP() != null) {
0N/A WindowsButtonUI.paintXPButtonBackground(g, c);
0N/A }
0N/A super.paint(g, c);
0N/A }
0N/A
0N/A static Part getXPButtonType(AbstractButton b) {
0N/A if(b instanceof JCheckBox) {
0N/A return Part.BP_CHECKBOX;
0N/A }
0N/A if(b instanceof JRadioButton) {
0N/A return Part.BP_RADIOBUTTON;
0N/A }
0N/A boolean toolbar = (b.getParent() instanceof JToolBar);
0N/A return toolbar ? Part.TP_BUTTON : Part.BP_PUSHBUTTON;
0N/A }
0N/A
0N/A static State getXPButtonState(AbstractButton b) {
0N/A Part part = getXPButtonType(b);
0N/A ButtonModel model = b.getModel();
0N/A State state = State.NORMAL;
0N/A switch (part) {
0N/A case BP_RADIOBUTTON:
0N/A /* falls through */
0N/A case BP_CHECKBOX:
0N/A if (! model.isEnabled()) {
0N/A state = (model.isSelected()) ? State.CHECKEDDISABLED
0N/A : State.UNCHECKEDDISABLED;
0N/A } else if (model.isPressed() && model.isArmed()) {
0N/A state = (model.isSelected()) ? State.CHECKEDPRESSED
0N/A : State.UNCHECKEDPRESSED;
0N/A } else if (model.isRollover()) {
0N/A state = (model.isSelected()) ? State.CHECKEDHOT
0N/A : State.UNCHECKEDHOT;
0N/A } else {
0N/A state = (model.isSelected()) ? State.CHECKEDNORMAL
0N/A : State.UNCHECKEDNORMAL;
0N/A }
0N/A break;
0N/A case BP_PUSHBUTTON:
0N/A /* falls through */
0N/A case TP_BUTTON:
0N/A boolean toolbar = (b.getParent() instanceof JToolBar);
0N/A if (toolbar) {
0N/A if (model.isArmed() && model.isPressed()) {
0N/A state = State.PRESSED;
0N/A } else if (!model.isEnabled()) {
0N/A state = State.DISABLED;
0N/A } else if (model.isSelected() && model.isRollover()) {
0N/A state = State.HOTCHECKED;
0N/A } else if (model.isSelected()) {
0N/A state = State.CHECKED;
0N/A } else if (model.isRollover()) {
0N/A state = State.HOT;
0N/A } else if (b.hasFocus()) {
0N/A state = State.HOT;
0N/A }
0N/A } else {
0N/A if ((model.isArmed() && model.isPressed())
0N/A || model.isSelected()) {
0N/A state = State.PRESSED;
0N/A } else if (!model.isEnabled()) {
0N/A state = State.DISABLED;
0N/A } else if (model.isRollover() || model.isPressed()) {
0N/A state = State.HOT;
0N/A } else if (b instanceof JButton
0N/A && ((JButton)b).isDefaultButton()) {
0N/A state = State.DEFAULTED;
0N/A } else if (b.hasFocus()) {
0N/A state = State.HOT;
0N/A }
0N/A }
0N/A break;
0N/A default :
0N/A state = State.NORMAL;
0N/A }
0N/A
0N/A return state;
0N/A }
0N/A
0N/A static void paintXPButtonBackground(Graphics g, JComponent c) {
0N/A AbstractButton b = (AbstractButton)c;
0N/A
0N/A XPStyle xp = XPStyle.getXP();
0N/A
0N/A Part part = getXPButtonType(b);
0N/A
0N/A if (b.isContentAreaFilled() && xp != null) {
0N/A
0N/A Skin skin = xp.getSkin(b, part);
0N/A
0N/A State state = getXPButtonState(b);
0N/A Dimension d = c.getSize();
0N/A int dx = 0;
0N/A int dy = 0;
0N/A int dw = d.width;
0N/A int dh = d.height;
0N/A
0N/A Border border = c.getBorder();
0N/A Insets insets;
0N/A if (border != null) {
0N/A // Note: The border may be compound, containing an outer
0N/A // opaque border (supplied by the application), plus an
0N/A // inner transparent margin border. We want to size the
0N/A // background to fill the transparent part, but stay
0N/A // inside the opaque part.
0N/A insets = WindowsButtonUI.getOpaqueInsets(border, c);
0N/A } else {
0N/A insets = c.getInsets();
0N/A }
0N/A if (insets != null) {
0N/A dx += insets.left;
0N/A dy += insets.top;
0N/A dw -= (insets.left + insets.right);
0N/A dh -= (insets.top + insets.bottom);
0N/A }
0N/A skin.paintSkin(g, dx, dy, dw, dh, state);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * returns - b.getBorderInsets(c) if border is opaque
0N/A * - null if border is completely non-opaque
0N/A * - somewhere inbetween if border is compound and
0N/A * outside border is opaque and inside isn't
0N/A */
0N/A private static Insets getOpaqueInsets(Border b, Component c) {
0N/A if (b == null) {
0N/A return null;
0N/A }
0N/A if (b.isBorderOpaque()) {
0N/A return b.getBorderInsets(c);
0N/A } else if (b instanceof CompoundBorder) {
0N/A CompoundBorder cb = (CompoundBorder)b;
0N/A Insets iOut = getOpaqueInsets(cb.getOutsideBorder(), c);
0N/A if (iOut != null && iOut.equals(cb.getOutsideBorder().getBorderInsets(c))) {
0N/A // Outside border is opaque, keep looking
0N/A Insets iIn = getOpaqueInsets(cb.getInsideBorder(), c);
0N/A if (iIn == null) {
0N/A // Inside is non-opaque, use outside insets
0N/A return iOut;
0N/A } else {
0N/A // Found non-opaque somewhere in the inside (which is
0N/A // also compound).
0N/A return new Insets(iOut.top + iIn.top, iOut.left + iIn.left,
0N/A iOut.bottom + iIn.bottom, iOut.right + iIn.right);
0N/A }
0N/A } else {
0N/A // Outside is either all non-opaque or has non-opaque
0N/A // border inside another compound border
0N/A return iOut;
0N/A }
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A}