src/java.desktop/share/classes/com/sun/java/swing/plaf/windows/WindowsToggleButtonUI.java
branchhttp-client-branch
changeset 55982 b6ff245c0db6
parent 55981 907bddce488c
parent 48332 651a95f30dfb
child 55983 e4a1f0c9d4c6
equal deleted inserted replaced
55981:907bddce488c 55982:b6ff245c0db6
     1 /*
       
     2  * Copyright (c) 1997, 2006, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package com.sun.java.swing.plaf.windows;
       
    27 
       
    28 import sun.awt.AppContext;
       
    29 
       
    30 import javax.swing.plaf.basic.*;
       
    31 import javax.swing.border.*;
       
    32 import javax.swing.plaf.*;
       
    33 import javax.swing.*;
       
    34 
       
    35 import java.awt.*;
       
    36 import java.beans.PropertyChangeEvent;
       
    37 
       
    38 
       
    39 
       
    40 /**
       
    41  * A Windows toggle button.
       
    42  * <p>
       
    43  * <strong>Warning:</strong>
       
    44  * Serialized objects of this class will not be compatible with
       
    45  * future Swing releases.  The current serialization support is appropriate
       
    46  * for short term storage or RMI between applications running the same
       
    47  * version of Swing.  A future release of Swing will provide support for
       
    48  * long term persistence.
       
    49  *
       
    50  * @author Jeff Dinkins
       
    51  */
       
    52 public class WindowsToggleButtonUI extends BasicToggleButtonUI
       
    53 {
       
    54     protected int dashedRectGapX;
       
    55     protected int dashedRectGapY;
       
    56     protected int dashedRectGapWidth;
       
    57     protected int dashedRectGapHeight;
       
    58 
       
    59     protected Color focusColor;
       
    60 
       
    61     private static final Object WINDOWS_TOGGLE_BUTTON_UI_KEY = new Object();
       
    62 
       
    63     private boolean defaults_initialized = false;
       
    64 
       
    65     public static ComponentUI createUI(JComponent b) {
       
    66         AppContext appContext = AppContext.getAppContext();
       
    67         WindowsToggleButtonUI windowsToggleButtonUI =
       
    68                 (WindowsToggleButtonUI) appContext.get(WINDOWS_TOGGLE_BUTTON_UI_KEY);
       
    69         if (windowsToggleButtonUI == null) {
       
    70             windowsToggleButtonUI = new WindowsToggleButtonUI();
       
    71             appContext.put(WINDOWS_TOGGLE_BUTTON_UI_KEY, windowsToggleButtonUI);
       
    72         }
       
    73         return windowsToggleButtonUI;
       
    74     }
       
    75 
       
    76 
       
    77     // ********************************
       
    78     //            Defaults
       
    79     // ********************************
       
    80     protected void installDefaults(AbstractButton b) {
       
    81         super.installDefaults(b);
       
    82         if(!defaults_initialized) {
       
    83             String pp = getPropertyPrefix();
       
    84             dashedRectGapX = ((Integer)UIManager.get("Button.dashedRectGapX")).intValue();
       
    85             dashedRectGapY = ((Integer)UIManager.get("Button.dashedRectGapY")).intValue();
       
    86             dashedRectGapWidth = ((Integer)UIManager.get("Button.dashedRectGapWidth")).intValue();
       
    87             dashedRectGapHeight = ((Integer)UIManager.get("Button.dashedRectGapHeight")).intValue();
       
    88             focusColor = UIManager.getColor(pp + "focus");
       
    89             defaults_initialized = true;
       
    90         }
       
    91 
       
    92         XPStyle xp = XPStyle.getXP();
       
    93         if (xp != null) {
       
    94             b.setBorder(xp.getBorder(b, WindowsButtonUI.getXPButtonType(b)));
       
    95             LookAndFeel.installProperty(b, "opaque", Boolean.FALSE);
       
    96             LookAndFeel.installProperty(b, "rolloverEnabled", Boolean.TRUE);
       
    97         }
       
    98     }
       
    99 
       
   100     protected void uninstallDefaults(AbstractButton b) {
       
   101         super.uninstallDefaults(b);
       
   102         defaults_initialized = false;
       
   103     }
       
   104 
       
   105 
       
   106     protected Color getFocusColor() {
       
   107         return focusColor;
       
   108     }
       
   109 
       
   110 
       
   111     // ********************************
       
   112     //         Paint Methods
       
   113     // ********************************
       
   114 
       
   115     private transient Color cachedSelectedColor = null;
       
   116     private transient Color cachedBackgroundColor = null;
       
   117     private transient Color cachedHighlightColor = null;
       
   118 
       
   119     protected void paintButtonPressed(Graphics g, AbstractButton b) {
       
   120         if (XPStyle.getXP() == null && b.isContentAreaFilled()) {
       
   121             Color oldColor = g.getColor();
       
   122             Color c1 = b.getBackground();
       
   123             Color c2 = UIManager.getColor("ToggleButton.highlight");
       
   124             if (c1 != cachedBackgroundColor || c2 != cachedHighlightColor) {
       
   125                 int r1 = c1.getRed(), r2 = c2.getRed();
       
   126                 int g1 = c1.getGreen(), g2 = c2.getGreen();
       
   127                 int b1 = c1.getBlue(), b2 = c2.getBlue();
       
   128                 cachedSelectedColor = new Color(
       
   129                         Math.min(r1, r2) + Math.abs(r1 - r2) / 2,
       
   130                         Math.min(g1, g2) + Math.abs(g1 - g2) / 2,
       
   131                         Math.min(b1, b2) + Math.abs(b1 - b2) / 2
       
   132                 );
       
   133                 cachedBackgroundColor = c1;
       
   134                 cachedHighlightColor = c2;
       
   135             }
       
   136             g.setColor(cachedSelectedColor);
       
   137             g.fillRect(0, 0, b.getWidth(), b.getHeight());
       
   138             g.setColor(oldColor);
       
   139         }
       
   140     }
       
   141 
       
   142     public void paint(Graphics g, JComponent c) {
       
   143         if (XPStyle.getXP() != null) {
       
   144             WindowsButtonUI.paintXPButtonBackground(g, c);
       
   145         }
       
   146         super.paint(g, c);
       
   147     }
       
   148 
       
   149 
       
   150     /**
       
   151      * Overridden method to render the text without the mnemonic
       
   152      */
       
   153     protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) {
       
   154         WindowsGraphicsUtils.paintText(g, b, textRect, text, getTextShiftOffset());
       
   155     }
       
   156 
       
   157     protected void paintFocus(Graphics g, AbstractButton b,
       
   158                               Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
       
   159         g.setColor(getFocusColor());
       
   160         BasicGraphicsUtils.drawDashedRect(g, dashedRectGapX, dashedRectGapY,
       
   161                                           b.getWidth() - dashedRectGapWidth,
       
   162                                           b.getHeight() - dashedRectGapHeight);
       
   163     }
       
   164 
       
   165     // ********************************
       
   166     //          Layout Methods
       
   167     // ********************************
       
   168     public Dimension getPreferredSize(JComponent c) {
       
   169         Dimension d = super.getPreferredSize(c);
       
   170 
       
   171         /* Ensure that the width and height of the button is odd,
       
   172          * to allow for the focus line if focus is painted
       
   173          */
       
   174         AbstractButton b = (AbstractButton)c;
       
   175         if (d != null && b.isFocusPainted()) {
       
   176             if(d.width % 2 == 0) { d.width += 1; }
       
   177             if(d.height % 2 == 0) { d.height += 1; }
       
   178         }
       
   179         return d;
       
   180     }
       
   181 }