1 30 31 package com.jgoodies.looks.windows; 32 33 import java.awt.Color ; 34 import java.awt.Component ; 35 import java.awt.Graphics ; 36 import java.awt.Insets ; 37 38 import javax.swing.AbstractButton ; 39 import javax.swing.ButtonModel ; 40 import javax.swing.JButton ; 41 import javax.swing.JToggleButton ; 42 import javax.swing.UIDefaults ; 43 import javax.swing.UIManager ; 44 import javax.swing.border.AbstractBorder ; 45 import javax.swing.border.Border ; 46 import javax.swing.border.EmptyBorder ; 47 import javax.swing.plaf.BorderUIResource ; 48 import javax.swing.plaf.UIResource ; 49 import javax.swing.plaf.basic.BasicBorders ; 50 import javax.swing.plaf.basic.BasicGraphicsUtils ; 51 52 59 final class WindowsBorders { 60 61 63 private static Border menuBorder; 64 private static Border menuItemBorder; 65 private static Border popupMenuBorder; 66 private static Border separatorBorder; 67 private static Border etchedBorder; 68 private static Border menuBarHeaderBorder; 69 private static Border toolBarHeaderBorder; 70 private static Border rolloverButtonBorder; 71 72 73 76 public static Border getButtonBorder() { 77 UIDefaults table = UIManager.getLookAndFeelDefaults(); 78 Border outerBorder = new ButtonBorder(table.getColor("Button.shadow"), 79 table.getColor("Button.darkShadow"), table 80 .getColor("Button.light"), table 81 .getColor("Button.highlight"), table 82 .getColor("controlText")); 83 84 Border buttonBorder = new BorderUIResource.CompoundBorderUIResource ( 85 outerBorder, new BasicBorders.MarginBorder ()); 86 return buttonBorder; 87 } 88 89 90 93 static Border getMenuBorder() { 94 if (menuBorder == null) { 95 menuBorder = new BorderUIResource.CompoundBorderUIResource ( 96 new MenuBorder(), 97 new BasicBorders.MarginBorder ()); 98 } 99 return menuBorder; 100 } 101 102 105 static Border getMenuItemBorder() { 106 if (menuItemBorder == null) { 107 menuItemBorder = new BorderUIResource (new BasicBorders.MarginBorder ()); 108 } 109 return menuItemBorder; 110 } 111 112 115 static Border getSeparatorBorder() { 116 if (separatorBorder == null) { 117 separatorBorder = new BorderUIResource.CompoundBorderUIResource ( 118 new SeparatorBorder(), 119 new BasicBorders.MarginBorder ()); 120 } 121 return separatorBorder; 122 } 123 124 127 static Border getEtchedBorder() { 128 if (etchedBorder == null) { 129 etchedBorder = new BorderUIResource.CompoundBorderUIResource ( 130 new EtchedBorder(), 131 new BasicBorders.MarginBorder ()); 132 } 133 return etchedBorder; 134 } 135 136 140 static Border getMenuBarHeaderBorder() { 141 if (menuBarHeaderBorder == null) { 142 menuBarHeaderBorder = new BorderUIResource.CompoundBorderUIResource ( 143 new MenuBarHeaderBorder(), 144 new BasicBorders.MarginBorder ()); 145 } 146 return menuBarHeaderBorder; 147 } 148 149 154 static Border getPopupMenuBorder() { 155 if (popupMenuBorder == null) { 156 popupMenuBorder = new PopupMenuBorder(); 157 } 158 return popupMenuBorder; 159 } 160 161 165 static Border getToolBarHeaderBorder() { 166 if (toolBarHeaderBorder == null) { 167 toolBarHeaderBorder = new BorderUIResource.CompoundBorderUIResource ( 168 new ToolBarHeaderBorder(), 169 new BasicBorders.MarginBorder ()); 170 } 171 return toolBarHeaderBorder; 172 } 173 174 177 static Border getRolloverButtonBorder() { 178 if (rolloverButtonBorder == null) { 179 rolloverButtonBorder = new BorderUIResource.CompoundBorderUIResource ( 180 new RolloverButtonBorder(), 181 new RolloverMarginBorder()); 182 } 183 return rolloverButtonBorder; 184 } 185 186 187 189 190 private static class ButtonBorder extends AbstractBorder implements UIResource { 192 193 private static final Insets EMPTY_INSETS = new Insets (0, 0, 0, 0); 194 195 private final Color shadow; 196 private final Color darkShadow; 197 private final Color highlight; 198 private final Color lightHighlight; 199 private final Color defaultColor; 200 201 public ButtonBorder(Color shadow, Color darkShadow, 202 Color highlight, Color lightHighlight, Color defaultColor) { 203 this.shadow = shadow; 204 this.darkShadow = darkShadow; 205 this.highlight = highlight; 206 this.lightHighlight = lightHighlight; 207 this.defaultColor = defaultColor; 208 } 209 210 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 211 boolean isPressed = false; 212 boolean isDefault = false; 213 214 if (c instanceof AbstractButton ) { 215 AbstractButton b = (AbstractButton )c; 216 ButtonModel model = b.getModel(); 217 218 isPressed = model.isPressed() && model.isArmed(); 219 if (c instanceof JButton ) { 220 isDefault = ((JButton )c).isDefaultButton(); 221 } 222 } 223 drawBezel(g, x, y, width, height, isPressed, isDefault, shadow, 224 darkShadow, highlight, lightHighlight, defaultColor); 225 } 226 227 public Insets getBorderInsets(Component c) { 228 return getBorderInsets(c, EMPTY_INSETS); 229 } 230 231 public Insets getBorderInsets(Component c, Insets insets) { 232 insets.top = 2; 234 insets.left = insets.bottom = insets.right = 3; 235 return insets; 236 } 237 238 } 239 240 243 private static abstract class AbstractButtonBorder extends AbstractBorder implements UIResource { 244 245 private static final Insets INSETS = new Insets (2, 2, 2, 2); 246 247 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 248 AbstractButton button = (AbstractButton ) c; 249 ButtonModel model = button.getModel(); 250 251 255 if (model.isPressed()) 256 WindowsUtils.drawPressed3DBorder(g, x, y, w, h); 257 else 258 WindowsUtils.drawFlush3DBorder(g, x, y, w, h); 259 } 260 261 public Insets getBorderInsets(Component c) { return INSETS; } 262 } 263 264 265 268 private static class RolloverButtonBorder extends AbstractButtonBorder { 269 270 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 271 AbstractButton b = (AbstractButton ) c; 272 ButtonModel model = b.getModel(); 273 274 if (!model.isEnabled()) 275 return; 276 277 if (!(c instanceof JToggleButton )) { 278 if (model.isRollover()) super.paintBorder(c, g, x, y, w, h); 280 return; 281 } 282 283 if (model.isSelected()) 284 WindowsUtils.drawPressed3DBorder(g, x, y, w, h); 285 else if (model.isRollover()) { 286 super.paintBorder(c, g, x, y, w, h); 287 293 } 294 } 295 } 296 297 298 302 private static class RolloverMarginBorder extends EmptyBorder { 303 304 private RolloverMarginBorder() { 305 super(1, 1, 1, 1); 306 } 307 308 309 public Insets getBorderInsets(Component c) { 310 return getBorderInsets(c, new Insets (0, 0, 0, 0)); 311 } 312 313 314 public Insets getBorderInsets(Component c, Insets insets) { 315 Insets margin = null; 316 317 if (c instanceof AbstractButton ) { 318 margin = ((AbstractButton ) c).getMargin(); 319 } 320 if (margin == null || margin instanceof UIResource ) { 321 insets.left = left; 323 insets.top = top; 324 insets.right = right; 325 insets.bottom = bottom; 326 } else { 327 insets.left = margin.left; 329 insets.top = margin.top; 330 insets.right = margin.right; 331 insets.bottom = margin.bottom; 332 } 333 return insets; 334 } 335 } 336 337 340 private static class SeparatorBorder extends AbstractBorder implements UIResource { 341 342 private static final Insets INSETS = new Insets (0, 3, 2, 1); 343 344 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 345 g.translate(x, y); 346 g.setColor( UIManager.getColor("Separator.foreground")); 347 g.drawLine( 0, h - 2, w - 1, h - 2 ); 348 349 g.setColor( UIManager.getColor("Separator.background")); 350 g.drawLine( 0, h - 1, w - 1, h - 1 ); 351 g.translate(-x, -y); 352 } 353 354 public Insets getBorderInsets(Component c) { return INSETS; } 355 } 356 357 358 361 static class ThinRaisedBorder extends AbstractBorder implements UIResource { 362 363 private static final Insets INSETS = new Insets (1, 1, 1, 1); 364 365 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 366 WindowsUtils.drawFlush3DBorder(g, x, y, w, h); 367 } 368 369 public Insets getBorderInsets(Component c) { return INSETS; } 370 } 371 372 373 376 static class ThinLoweredBorder extends AbstractBorder implements UIResource { 377 378 private static final Insets INSETS = new Insets (1, 1, 1, 1); 379 380 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 381 WindowsUtils.drawPressed3DBorder(g, x, y, w, h); 382 } 383 384 public Insets getBorderInsets(Component c) { return INSETS; } 385 } 386 387 388 393 private static class EtchedBorder extends AbstractBorder implements UIResource { 394 395 private static final Insets INSETS = new Insets (2, 2, 2, 2); 396 397 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 398 WindowsUtils.drawPressed3DBorder(g, x, y, w, h); 399 WindowsUtils.drawFlush3DBorder (g, x + 1, y + 1, w - 2, h - 2); 400 } 401 402 public Insets getBorderInsets(Component c) { return INSETS; } 403 } 404 405 406 411 private static class MenuBarHeaderBorder extends AbstractBorder implements UIResource { 412 413 private static final Insets INSETS = new Insets (2, 2, 1, 2); 414 415 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 416 WindowsUtils.drawPressed3DBorder(g, x, y, w, h + 1); 417 WindowsUtils.drawFlush3DBorder (g, x + 1, y + 1, w - 2, h - 1); 418 } 419 420 public Insets getBorderInsets(Component c) { return INSETS; } 421 } 422 423 424 private static class PopupMenuBorder extends AbstractBorder implements UIResource { 425 private static final Insets INSETS = new Insets (3, 3, 3, 3); 426 427 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 428 g.translate(x, y); 429 g.setColor(UIManager.getColor("controlShadow")); 430 g.drawRect(0, 0, w-1, h-1); 431 g.setColor(UIManager.getColor("MenuItem.background")); 432 g.drawRect(1, 1, w-3, h-3); 433 g.drawRect(2, 2, w-5, h-5); 434 g.translate(-x, -y); 435 } 436 437 public Insets getBorderInsets(Component c) { return INSETS; } 438 } 439 440 441 446 private static class ToolBarHeaderBorder extends AbstractBorder implements UIResource { 447 448 private static final Insets INSETS = new Insets (1, 2, 2, 2); 449 450 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 451 WindowsUtils.drawPressed3DBorder(g, x, y - 1, w, h + 1); 452 WindowsUtils.drawFlush3DBorder (g, x + 1, y, w - 2, h - 1); 453 } 454 455 public Insets getBorderInsets(Component c) { return INSETS; } 456 } 457 458 459 462 private static class MenuBorder extends AbstractBorder implements UIResource { 463 464 private static final Insets INSETS = new Insets (1, 1, 1, 1); 465 466 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 467 AbstractButton b = (AbstractButton ) c; 468 ButtonModel model = b.getModel(); 469 470 if (model.isSelected()) 473 WindowsUtils.drawPressed3DBorder(g, x, y, w, h); 474 else if (model.isRollover()) 475 WindowsUtils.drawFlush3DBorder(g, x, y, w, h); 476 } 478 479 public Insets getBorderInsets(Component c) { return INSETS; } 480 481 } 482 483 484 486 private static void drawBezel(Graphics g, int x, int y, int w, int h, 488 boolean isPressed, boolean isDefault, 489 Color shadow, Color darkShadow, 490 Color highlight, Color lightHighlight, Color defaultColor) 491 { 492 Color oldColor = g.getColor(); g.translate(x, y); 494 495 if (isPressed && isDefault) { 496 g.setColor(darkShadow); 497 g.drawRect(0, 0, w - 1, h - 1); 498 g.setColor(shadow); 499 g.drawRect(1, 1, w - 3, h - 3); 500 } else if (isPressed) { 501 BasicGraphicsUtils.drawLoweredBezel(g, x, y, w, h, 502 shadow, darkShadow, highlight, lightHighlight); 503 } else if (isDefault) { 504 g.setColor(defaultColor); 505 g.drawRect(0, 0, w-1, h-1); 506 507 g.setColor(lightHighlight); 508 g.drawLine(1, 1, 1, h-3); 509 g.drawLine(2, 1, w-3, 1); 510 511 g.setColor(highlight); 512 g.drawLine(2, 2, 2, h-4); 513 g.drawLine(3, 2, w-4, 2); 514 515 g.setColor(shadow); 516 g.drawLine(2, h-3, w-3, h-3); 517 g.drawLine(w-3, 2, w-3, h-4); 518 519 g.setColor(darkShadow); 520 g.drawLine(1, h-2, w-2, h-2); 521 g.drawLine(w-2, h-2, w-2, 1); 522 } else { 523 g.setColor(lightHighlight); 524 g.drawLine(0, 0, 0, h-1); 525 g.drawLine(1, 0, w-2, 0); 526 527 g.setColor(highlight); 528 g.drawLine(1, 1, 1, h-3); 529 g.drawLine(2, 1, w-3, 1); 530 531 g.setColor(shadow); 532 g.drawLine(1, h-2, w-2, h-2); 533 g.drawLine(w-2, 1, w-2, h-3); 534 535 g.setColor(darkShadow); 536 g.drawLine(0, h-1, w-1, h-1); 537 g.drawLine(w-1, h-1, w-1, 0); 538 } 539 g.translate(-x, -y); 540 g.setColor(oldColor); 541 } 542 543 } | Popular Tags |