1 7 8 package com.sun.java.swing.plaf.windows; 9 10 import javax.swing.plaf.basic.*; 11 import javax.swing.border.*; 12 import javax.swing.plaf.*; 13 import javax.swing.*; 14 15 import java.awt.*; 16 17 import com.sun.java.swing.plaf.windows.TMSchema.*; 18 import com.sun.java.swing.plaf.windows.XPStyle.Skin; 19 20 34 public class WindowsButtonUI extends BasicButtonUI 35 { 36 private final static WindowsButtonUI windowsButtonUI = new WindowsButtonUI(); 37 38 protected int dashedRectGapX; 39 protected int dashedRectGapY; 40 protected int dashedRectGapWidth; 41 protected int dashedRectGapHeight; 42 43 protected Color focusColor; 44 45 private boolean defaults_initialized = false; 46 47 48 public static ComponentUI createUI(JComponent c){ 52 return windowsButtonUI; 53 } 54 55 56 protected void installDefaults(AbstractButton b) { 60 super.installDefaults(b); 61 if(!defaults_initialized) { 62 String pp = getPropertyPrefix(); 63 dashedRectGapX = UIManager.getInt(pp + "dashedRectGapX"); 64 dashedRectGapY = UIManager.getInt(pp + "dashedRectGapY"); 65 dashedRectGapWidth = UIManager.getInt(pp + "dashedRectGapWidth"); 66 dashedRectGapHeight = UIManager.getInt(pp + "dashedRectGapHeight"); 67 focusColor = UIManager.getColor(pp + "focus"); 68 defaults_initialized = true; 69 } 70 71 XPStyle xp = XPStyle.getXP(); 72 if (xp != null) { 73 b.setBorder(xp.getBorder(b, getXPButtonType(b))); 74 LookAndFeel.installProperty(b, "rolloverEnabled", Boolean.TRUE); 75 } 76 } 77 78 protected void uninstallDefaults(AbstractButton b) { 79 super.uninstallDefaults(b); 80 defaults_initialized = false; 81 } 82 83 protected Color getFocusColor() { 84 return focusColor; 85 } 86 87 91 94 protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) { 95 WindowsGraphicsUtils.paintText(g, b, textRect, text, getTextShiftOffset()); 96 } 97 98 protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect){ 99 if (b.getParent() instanceof JToolBar) { 100 return; 102 } 103 104 if (XPStyle.getXP() != null) { 105 return; 106 } 107 108 int width = b.getWidth(); 110 int height = b.getHeight(); 111 g.setColor(getFocusColor()); 112 BasicGraphicsUtils.drawDashedRect(g, dashedRectGapX, dashedRectGapY, 113 width - dashedRectGapWidth, height - dashedRectGapHeight); 114 } 115 116 protected void paintButtonPressed(Graphics g, AbstractButton b){ 117 setTextShiftOffset(); 118 } 119 120 public Dimension getPreferredSize(JComponent c) { 124 Dimension d = super.getPreferredSize(c); 125 126 129 AbstractButton b = (AbstractButton)c; 130 if (d != null && b.isFocusPainted()) { 131 if(d.width % 2 == 0) { d.width += 1; } 132 if(d.height % 2 == 0) { d.height += 1; } 133 } 134 return d; 135 } 136 137 138 143 private static Rectangle viewRect = new Rectangle(); 144 145 public void paint(Graphics g, JComponent c) { 146 if (XPStyle.getXP() != null) { 147 WindowsButtonUI.paintXPButtonBackground(g, c); 148 } 149 super.paint(g, c); 150 } 151 152 static Part getXPButtonType(AbstractButton b) { 153 boolean toolbar = (b.getParent() instanceof JToolBar); 154 return toolbar ? Part.TP_BUTTON : Part.BP_PUSHBUTTON; 155 } 156 157 static void paintXPButtonBackground(Graphics g, JComponent c) { 158 AbstractButton b = (AbstractButton)c; 159 160 XPStyle xp = XPStyle.getXP(); 161 162 boolean toolbar = (b.getParent() instanceof JToolBar); 163 Part part = getXPButtonType(b); 164 165 if (b.isContentAreaFilled() && xp != null) { 166 167 ButtonModel model = b.getModel(); 168 Skin skin = xp.getSkin(b, part); 169 170 State state = State.NORMAL; 172 if (toolbar) { 173 if (model.isArmed() && model.isPressed()) { 174 state = State.PRESSED; 175 } else if (!model.isEnabled()) { 176 state = State.DISABLED; 177 } else if (model.isSelected() && model.isRollover()) { 178 state = State.HOTCHECKED; 179 } else if (model.isSelected()) { 180 state = State.CHECKED; 181 } else if (model.isRollover()) { 182 state = State.HOT; 183 } 184 } else { 185 if (model.isArmed() && model.isPressed() || model.isSelected()) { 186 state = State.PRESSED; 187 } else if (!model.isEnabled()) { 188 state = State.DISABLED; 189 } else if (model.isRollover() || model.isPressed()) { 190 state = State.HOT; 191 } else if (b instanceof JButton && ((JButton)b).isDefaultButton()) { 192 state = State.DEFAULTED; 193 } else if (c.hasFocus()) { 194 state = State.HOT; 195 } 196 } 197 Dimension d = c.getSize(); 198 int dx = 0; 199 int dy = 0; 200 int dw = d.width; 201 int dh = d.height; 202 203 Border border = c.getBorder(); 204 Insets insets; 205 if (border != null) { 206 insets = WindowsButtonUI.getOpaqueInsets(border, c); 212 } else { 213 insets = c.getInsets(); 214 } 215 if (insets != null) { 216 dx += insets.left; 217 dy += insets.top; 218 dw -= (insets.left + insets.right); 219 dh -= (insets.top + insets.bottom); 220 } 221 skin.paintSkin(g, dx, dy, dw, dh, state); 222 } 223 } 224 225 231 private static Insets getOpaqueInsets(Border b, Component c) { 232 if (b == null) { 233 return null; 234 } 235 if (b.isBorderOpaque()) { 236 return b.getBorderInsets(c); 237 } else if (b instanceof CompoundBorder) { 238 CompoundBorder cb = (CompoundBorder)b; 239 Insets iOut = getOpaqueInsets(cb.getOutsideBorder(), c); 240 if (iOut != null && iOut.equals(cb.getOutsideBorder().getBorderInsets(c))) { 241 Insets iIn = getOpaqueInsets(cb.getInsideBorder(), c); 243 if (iIn == null) { 244 return iOut; 246 } else { 247 return new Insets(iOut.top + iIn.top, iOut.left + iIn.left, 250 iOut.bottom + iIn.bottom, iOut.right + iIn.right); 251 } 252 } else { 253 return iOut; 256 } 257 } else { 258 return null; 259 } 260 } 261 } 262 263 | Popular Tags |