J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.java.swing.plaf.windows; |
| 27 | |
| 28 | import javax.swing.plaf.basic.*; |
| 29 | import javax.swing.border.*; |
| 30 | import javax.swing.plaf.*; |
| 31 | import javax.swing.*; |
| 32 | |
| 33 | import java.awt.*; |
| 34 | |
| 35 | import static com.sun.java.swing.plaf.windows.TMSchema.*; |
| 36 | import static com.sun.java.swing.plaf.windows.TMSchema.Part.*; |
| 37 | import static com.sun.java.swing.plaf.windows.XPStyle.Skin; |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * Windows 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 | */ |
| 53 | public class WindowsButtonUI extends BasicButtonUI |
| 54 | { |
| 55 | private final static WindowsButtonUI windowsButtonUI = new WindowsButtonUI(); |
| 56 | |
| 57 | protected int dashedRectGapX; |
| 58 | protected int dashedRectGapY; |
| 59 | protected int dashedRectGapWidth; |
| 60 | protected int dashedRectGapHeight; |
| 61 | |
| 62 | protected Color focusColor; |
| 63 | |
| 64 | private boolean defaults_initialized = false; |
| 65 | |
| 66 | |
| 67 | // ******************************** |
| 68 | // Create PLAF |
| 69 | // ******************************** |
| 70 | public static ComponentUI createUI(JComponent c){ |
| 71 | return windowsButtonUI; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | // ******************************** |
| 76 | // Defaults |
| 77 | // ******************************** |
| 78 | protected void installDefaults(AbstractButton b) { |
| 79 | super.installDefaults(b); |
| 80 | if(!defaults_initialized) { |
| 81 | String pp = getPropertyPrefix(); |
| 82 | dashedRectGapX = UIManager.getInt(pp + "dashedRectGapX"); |
| 83 | dashedRectGapY = UIManager.getInt(pp + "dashedRectGapY"); |
| 84 | dashedRectGapWidth = UIManager.getInt(pp + "dashedRectGapWidth"); |
| 85 | dashedRectGapHeight = UIManager.getInt(pp + "dashedRectGapHeight"); |
| 86 | focusColor = UIManager.getColor(pp + "focus"); |
| 87 | defaults_initialized = true; |
| 88 | } |
| 89 | |
| 90 | XPStyle xp = XPStyle.getXP(); |
| 91 | if (xp != null) { |
| 92 | b.setBorder(xp.getBorder(b, getXPButtonType(b))); |
| 93 | LookAndFeel.installProperty(b, "rolloverEnabled", Boolean.TRUE); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | protected void uninstallDefaults(AbstractButton b) { |
| 98 | super.uninstallDefaults(b); |
| 99 | defaults_initialized = false; |
| 100 | } |
| 101 | |
| 102 | protected Color getFocusColor() { |
| 103 | return focusColor; |
| 104 | } |
| 105 | |
| 106 | // ******************************** |
| 107 | // Paint Methods |
| 108 | // ******************************** |
| 109 | |
| 110 | /** |
| 111 | * Overridden method to render the text without the mnemonic |
| 112 | */ |
| 113 | protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) { |
| 114 | WindowsGraphicsUtils.paintText(g, b, textRect, text, getTextShiftOffset()); |
| 115 | } |
| 116 | |
| 117 | protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect){ |
| 118 | |
| 119 | // focus painted same color as text on Basic?? |
| 120 | int width = b.getWidth(); |
| 121 | int height = b.getHeight(); |
| 122 | g.setColor(getFocusColor()); |
| 123 | BasicGraphicsUtils.drawDashedRect(g, dashedRectGapX, dashedRectGapY, |
| 124 | width - dashedRectGapWidth, height - dashedRectGapHeight); |
| 125 | } |
| 126 | |
| 127 | protected void paintButtonPressed(Graphics g, AbstractButton b){ |
| 128 | setTextShiftOffset(); |
| 129 | } |
| 130 | |
| 131 | // ******************************** |
| 132 | // Layout Methods |
| 133 | // ******************************** |
| 134 | public Dimension getPreferredSize(JComponent c) { |
| 135 | Dimension d = super.getPreferredSize(c); |
| 136 | |
| 137 | /* Ensure that the width and height of the button is odd, |
| 138 | * to allow for the focus line if focus is painted |
| 139 | */ |
| 140 | AbstractButton b = (AbstractButton)c; |
| 141 | if (d != null && b.isFocusPainted()) { |
| 142 | if(d.width % 2 == 0) { d.width += 1; } |
| 143 | if(d.height % 2 == 0) { d.height += 1; } |
| 144 | } |
| 145 | return d; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /* These rectangles/insets are allocated once for all |
| 150 | * ButtonUI.paint() calls. Re-using rectangles rather than |
| 151 | * allocating them in each paint call substantially reduced the time |
| 152 | * it took paint to run. Obviously, this method can't be re-entered. |
| 153 | */ |
| 154 | private static Rectangle viewRect = new Rectangle(); |
| 155 | |
| 156 | public void paint(Graphics g, JComponent c) { |
| 157 | if (XPStyle.getXP() != null) { |
| 158 | WindowsButtonUI.paintXPButtonBackground(g, c); |
| 159 | } |
| 160 | super.paint(g, c); |
| 161 | } |
| 162 | |
| 163 | static Part getXPButtonType(AbstractButton b) { |
| 164 | if(b instanceof JCheckBox) { |
| 165 | return Part.BP_CHECKBOX; |
| 166 | } |
| 167 | if(b instanceof JRadioButton) { |
| 168 | return Part.BP_RADIOBUTTON; |
| 169 | } |
| 170 | boolean toolbar = (b.getParent() instanceof JToolBar); |
| 171 | return toolbar ? Part.TP_BUTTON : Part.BP_PUSHBUTTON; |
| 172 | } |
| 173 | |
| 174 | static State getXPButtonState(AbstractButton b) { |
| 175 | Part part = getXPButtonType(b); |
| 176 | ButtonModel model = b.getModel(); |
| 177 | State state = State.NORMAL; |
| 178 | switch (part) { |
| 179 | case BP_RADIOBUTTON: |
| 180 | /* falls through */ |
| 181 | case BP_CHECKBOX: |
| 182 | if (! model.isEnabled()) { |
| 183 | state = (model.isSelected()) ? State.CHECKEDDISABLED |
| 184 | : State.UNCHECKEDDISABLED; |
| 185 | } else if (model.isPressed() && model.isArmed()) { |
| 186 | state = (model.isSelected()) ? State.CHECKEDPRESSED |
| 187 | : State.UNCHECKEDPRESSED; |
| 188 | } else if (model.isRollover()) { |
| 189 | state = (model.isSelected()) ? State.CHECKEDHOT |
| 190 | : State.UNCHECKEDHOT; |
| 191 | } else { |
| 192 | state = (model.isSelected()) ? State.CHECKEDNORMAL |
| 193 | : State.UNCHECKEDNORMAL; |
| 194 | } |
| 195 | break; |
| 196 | case BP_PUSHBUTTON: |
| 197 | /* falls through */ |
| 198 | case TP_BUTTON: |
| 199 | boolean toolbar = (b.getParent() instanceof JToolBar); |
| 200 | if (toolbar) { |
| 201 | if (model.isArmed() && model.isPressed()) { |
| 202 | state = State.PRESSED; |
| 203 | } else if (!model.isEnabled()) { |
| 204 | state = State.DISABLED; |
| 205 | } else if (model.isSelected() && model.isRollover()) { |
| 206 | state = State.HOTCHECKED; |
| 207 | } else if (model.isSelected()) { |
| 208 | state = State.CHECKED; |
| 209 | } else if (model.isRollover()) { |
| 210 | state = State.HOT; |
| 211 | } else if (b.hasFocus()) { |
| 212 | state = State.HOT; |
| 213 | } |
| 214 | } else { |
| 215 | if ((model.isArmed() && model.isPressed()) |
| 216 | || model.isSelected()) { |
| 217 | state = State.PRESSED; |
| 218 | } else if (!model.isEnabled()) { |
| 219 | state = State.DISABLED; |
| 220 | } else if (model.isRollover() || model.isPressed()) { |
| 221 | state = State.HOT; |
| 222 | } else if (b instanceof JButton |
| 223 | && ((JButton)b).isDefaultButton()) { |
| 224 | state = State.DEFAULTED; |
| 225 | } else if (b.hasFocus()) { |
| 226 | state = State.HOT; |
| 227 | } |
| 228 | } |
| 229 | break; |
| 230 | default : |
| 231 | state = State.NORMAL; |
| 232 | } |
| 233 | |
| 234 | return state; |
| 235 | } |
| 236 | |
| 237 | static void paintXPButtonBackground(Graphics g, JComponent c) { |
| 238 | AbstractButton b = (AbstractButton)c; |
| 239 | |
| 240 | XPStyle xp = XPStyle.getXP(); |
| 241 | |
| 242 | Part part = getXPButtonType(b); |
| 243 | |
| 244 | if (b.isContentAreaFilled() && xp != null) { |
| 245 | |
| 246 | Skin skin = xp.getSkin(b, part); |
| 247 | |
| 248 | State state = getXPButtonState(b); |
| 249 | Dimension d = c.getSize(); |
| 250 | int dx = 0; |
| 251 | int dy = 0; |
| 252 | int dw = d.width; |
| 253 | int dh = d.height; |
| 254 | |
| 255 | Border border = c.getBorder(); |
| 256 | Insets insets; |
| 257 | if (border != null) { |
| 258 | // Note: The border may be compound, containing an outer |
| 259 | // opaque border (supplied by the application), plus an |
| 260 | // inner transparent margin border. We want to size the |
| 261 | // background to fill the transparent part, but stay |
| 262 | // inside the opaque part. |
| 263 | insets = WindowsButtonUI.getOpaqueInsets(border, c); |
| 264 | } else { |
| 265 | insets = c.getInsets(); |
| 266 | } |
| 267 | if (insets != null) { |
| 268 | dx += insets.left; |
| 269 | dy += insets.top; |
| 270 | dw -= (insets.left + insets.right); |
| 271 | dh -= (insets.top + insets.bottom); |
| 272 | } |
| 273 | skin.paintSkin(g, dx, dy, dw, dh, state); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * returns - b.getBorderInsets(c) if border is opaque |
| 279 | * - null if border is completely non-opaque |
| 280 | * - somewhere inbetween if border is compound and |
| 281 | * outside border is opaque and inside isn't |
| 282 | */ |
| 283 | private static Insets getOpaqueInsets(Border b, Component c) { |
| 284 | if (b == null) { |
| 285 | return null; |
| 286 | } |
| 287 | if (b.isBorderOpaque()) { |
| 288 | return b.getBorderInsets(c); |
| 289 | } else if (b instanceof CompoundBorder) { |
| 290 | CompoundBorder cb = (CompoundBorder)b; |
| 291 | Insets iOut = getOpaqueInsets(cb.getOutsideBorder(), c); |
| 292 | if (iOut != null && iOut.equals(cb.getOutsideBorder().getBorderInsets(c))) { |
| 293 | // Outside border is opaque, keep looking |
| 294 | Insets iIn = getOpaqueInsets(cb.getInsideBorder(), c); |
| 295 | if (iIn == null) { |
| 296 | // Inside is non-opaque, use outside insets |
| 297 | return iOut; |
| 298 | } else { |
| 299 | // Found non-opaque somewhere in the inside (which is |
| 300 | // also compound). |
| 301 | return new Insets(iOut.top + iIn.top, iOut.left + iIn.left, |
| 302 | iOut.bottom + iIn.bottom, iOut.right + iIn.right); |
| 303 | } |
| 304 | } else { |
| 305 | // Outside is either all non-opaque or has non-opaque |
| 306 | // border inside another compound border |
| 307 | return iOut; |
| 308 | } |
| 309 | } else { |
| 310 | return null; |
| 311 | } |
| 312 | } |
| 313 | } |