1 30 31 package com.jgoodies.looks.plastic; 32 33 import java.awt.Component ; 34 import java.awt.Graphics ; 35 import java.awt.Insets ; 36 37 import javax.swing.AbstractButton ; 38 import javax.swing.ButtonModel ; 39 import javax.swing.JButton ; 40 import javax.swing.JComboBox ; 41 import javax.swing.border.AbstractBorder ; 42 import javax.swing.border.Border ; 43 import javax.swing.border.CompoundBorder ; 44 import javax.swing.plaf.BorderUIResource ; 45 import javax.swing.plaf.UIResource ; 46 import javax.swing.plaf.basic.BasicBorders ; 47 import javax.swing.plaf.metal.MetalBorders ; 48 import javax.swing.plaf.metal.MetalLookAndFeel ; 49 import javax.swing.text.JTextComponent ; 50 51 import com.jgoodies.looks.LookUtils; 52 53 54 61 62 final class PlasticXPBorders { 63 64 65 67 private static Border buttonBorder; 68 private static Border comboBoxArrowButtonBorder; 69 private static Border comboBoxEditorBorder; 70 private static Border scrollPaneBorder; 71 private static Border textFieldBorder; 72 private static Border toggleButtonBorder; 73 74 75 78 static Border getButtonBorder() { 79 if (buttonBorder == null) { 80 buttonBorder = new BorderUIResource.CompoundBorderUIResource ( 81 new XPButtonBorder(), 82 new BasicBorders.MarginBorder ()); 83 } 84 return buttonBorder; 85 } 86 87 90 static Border getComboBoxArrowButtonBorder() { 91 if (comboBoxArrowButtonBorder == null) { 92 comboBoxArrowButtonBorder = new CompoundBorder ( new XPComboBoxArrowButtonBorder(), 94 new BasicBorders.MarginBorder ()); 95 } 96 return comboBoxArrowButtonBorder; 97 } 98 99 102 static Border getComboBoxEditorBorder() { 103 if (comboBoxEditorBorder == null) { 104 comboBoxEditorBorder = new CompoundBorder ( new XPComboBoxEditorBorder(), 106 new BasicBorders.MarginBorder ()); 107 } 108 return comboBoxEditorBorder; 109 } 110 111 114 static Border getScrollPaneBorder() { 115 if (scrollPaneBorder == null) { 116 scrollPaneBorder = new XPScrollPaneBorder(); 117 } 118 return scrollPaneBorder; 119 } 120 121 124 static Border getTextFieldBorder() { 125 if (textFieldBorder == null) { 126 textFieldBorder = new BorderUIResource.CompoundBorderUIResource ( 127 new XPTextFieldBorder(), 128 new BasicBorders.MarginBorder ()); 129 } 130 return textFieldBorder; 131 } 132 133 136 static Border getToggleButtonBorder() { 137 if (toggleButtonBorder == null) { 138 toggleButtonBorder = new BorderUIResource.CompoundBorderUIResource ( 139 new XPButtonBorder(), 140 new BasicBorders.MarginBorder ()); 141 } 142 return toggleButtonBorder; 143 } 144 145 148 private static class XPButtonBorder extends AbstractBorder implements UIResource { 149 150 protected static final Insets INSETS = LookUtils.IS_LOW_RESOLUTION 151 ? new Insets (3, 2, 3, 2) 152 : new Insets (2, 2, 2, 2); 153 154 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 155 AbstractButton button = (AbstractButton ) c; 156 ButtonModel model = button.getModel(); 157 158 if (!model.isEnabled()) { 159 PlasticXPUtils.drawDisabledButtonBorder(g, x, y, w, h); 160 return; 161 } 162 163 boolean isPressed = model.isPressed() && model.isArmed(); 164 boolean isDefault = button instanceof JButton 165 && ((JButton ) button).isDefaultButton(); 166 boolean isFocused = button.isFocusPainted() && button.hasFocus(); 167 168 if (isPressed) 169 PlasticXPUtils.drawPressedButtonBorder(g, x, y, w, h); 170 else if (isFocused) 171 PlasticXPUtils.drawFocusedButtonBorder(g, x, y, w, h); 172 else if (isDefault) 173 PlasticXPUtils.drawDefaultButtonBorder(g, x, y, w, h); 174 else 175 PlasticXPUtils.drawPlainButtonBorder(g, x, y, w, h); 176 } 177 178 public Insets getBorderInsets(Component c) { return INSETS; } 179 180 public Insets getBorderInsets(Component c, Insets newInsets) { 181 newInsets.top = INSETS.top; 182 newInsets.left = INSETS.left; 183 newInsets.bottom = INSETS.bottom; 184 newInsets.right = INSETS.right; 185 return newInsets; 186 } 187 } 188 189 190 193 private static class XPComboBoxArrowButtonBorder extends AbstractBorder implements UIResource { 194 195 protected static final Insets INSETS = new Insets (1, 1, 1, 1); 196 197 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 198 PlasticComboBoxButton button = (PlasticComboBoxButton) c; 199 JComboBox comboBox = button.getComboBox(); 200 ButtonModel model = button.getModel(); 201 202 if (!model.isEnabled()) { 203 PlasticXPUtils.drawDisabledButtonBorder(g, x, y, w, h); 204 } else { 205 boolean isPressed = model.isPressed() && model.isArmed(); 206 boolean isFocused = comboBox.hasFocus(); 207 if (isPressed) 208 PlasticXPUtils.drawPressedButtonBorder(g, x, y, w, h); 209 else if (isFocused) 210 PlasticXPUtils.drawFocusedButtonBorder(g, x, y, w, h); 211 else 212 PlasticXPUtils.drawPlainButtonBorder(g, x, y, w, h); 213 } 214 if (comboBox.isEditable()) { 215 g.setColor(model.isEnabled() 217 ? PlasticLookAndFeel.getControlDarkShadow() 218 : MetalLookAndFeel.getControlShadow()); 219 g.fillRect(x, y, 1, 1); 220 g.fillRect(x, y + h-1, 1, 1); 221 } 222 } 223 224 public Insets getBorderInsets(Component c) { return INSETS; } 225 } 226 227 228 231 private static class XPComboBoxEditorBorder extends AbstractBorder { 232 233 private static final Insets INSETS = new Insets (1, 1, 1, 0); 234 235 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 236 g.setColor(c.isEnabled() 237 ? PlasticLookAndFeel.getControlDarkShadow() 238 : MetalLookAndFeel.getControlShadow()); 239 PlasticXPUtils.drawRect(g, x, y, w+1, h-1); 240 } 241 242 public Insets getBorderInsets(Component c) { return INSETS; } 243 } 244 245 246 249 private static class XPTextFieldBorder extends AbstractBorder { 250 251 private static final Insets INSETS = new Insets (1, 1, 1, 1); 252 253 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 254 255 boolean enabled = ((c instanceof JTextComponent ) 256 && (c.isEnabled() && ((JTextComponent ) c).isEditable())) 257 || 258 c.isEnabled(); 259 260 g.setColor(enabled 261 ? PlasticLookAndFeel.getControlDarkShadow() 262 : MetalLookAndFeel.getControlShadow()); 263 PlasticXPUtils.drawRect(g, x, y, w-1, h-1); 264 } 265 266 public Insets getBorderInsets(Component c) { return INSETS; } 267 268 public Insets getBorderInsets(Component c, Insets newInsets) { 269 newInsets.top = INSETS.top; 270 newInsets.left = INSETS.left; 271 newInsets.bottom = INSETS.bottom; 272 newInsets.right = INSETS.right; 273 return newInsets; 274 } 275 } 276 277 282 private static class XPScrollPaneBorder extends MetalBorders.ScrollPaneBorder { 283 284 private static final Insets INSETS = new Insets (1, 1, 1, 1); 285 286 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 287 g.setColor(c.isEnabled() 288 ? PlasticLookAndFeel.getControlDarkShadow() 289 : MetalLookAndFeel.getControlShadow()); 290 PlasticXPUtils.drawRect(g, x, y, w-1, h-1); 291 } 292 293 public Insets getBorderInsets(Component c) { return INSETS; } 294 295 public Insets getBorderInsets(Component c, Insets newInsets) { 296 newInsets.top = INSETS.top; 297 newInsets.left = INSETS.left; 298 newInsets.bottom = INSETS.bottom; 299 newInsets.right = INSETS.right; 300 return newInsets; 301 } 302 } 303 304 305 } | Popular Tags |