1 30 31 package com.jgoodies.looks.plastic; 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.JInternalFrame ; 42 import javax.swing.JMenuItem ; 43 import javax.swing.JToggleButton ; 44 import javax.swing.UIManager ; 45 import javax.swing.border.AbstractBorder ; 46 import javax.swing.border.Border ; 47 import javax.swing.border.CompoundBorder ; 48 import javax.swing.border.EmptyBorder ; 49 import javax.swing.plaf.BorderUIResource ; 50 import javax.swing.plaf.UIResource ; 51 import javax.swing.plaf.basic.BasicBorders ; 52 import javax.swing.plaf.metal.MetalBorders ; 53 import javax.swing.text.JTextComponent ; 54 55 import com.jgoodies.looks.LookUtils; 56 57 58 65 66 final class PlasticBorders { 67 68 69 71 private static Border buttonBorder; 72 private static Border comboBoxEditorBorder; 73 private static Border comboBoxArrowButtonBorder; 74 private static Border etchedBorder; 75 private static Border flush3DBorder; 76 private static Border menuBarHeaderBorder; 77 private static Border menuBorder; 78 private static Border menuItemBorder; 79 private static Border popupMenuBorder; 80 private static Border rolloverButtonBorder; 81 private static Border scrollPaneBorder; 82 private static Border separatorBorder; 83 private static Border textFieldBorder; 84 private static Border thinLoweredBorder; 85 private static Border thinRaisedBorder; 86 private static Border toggleButtonBorder; 87 private static Border toolBarHeaderBorder; 88 89 90 95 static Border getButtonBorder() { 96 if (buttonBorder == null) { 97 buttonBorder = new BorderUIResource.CompoundBorderUIResource ( 98 new ButtonBorder(), 99 new BasicBorders.MarginBorder ()); 100 } 101 return buttonBorder; 102 } 103 104 109 static Border getComboBoxArrowButtonBorder() { 110 if (comboBoxArrowButtonBorder == null) { 111 comboBoxArrowButtonBorder = new CompoundBorder ( new ComboBoxArrowButtonBorder(), 113 new BasicBorders.MarginBorder ()); 114 } 115 return comboBoxArrowButtonBorder; 116 } 117 118 123 static Border getComboBoxEditorBorder() { 124 if (comboBoxEditorBorder == null) { 125 comboBoxEditorBorder = new CompoundBorder ( new ComboBoxEditorBorder(), 127 new BasicBorders.MarginBorder ()); 128 } 129 return comboBoxEditorBorder; 130 } 131 132 138 static Border getEtchedBorder() { 139 if (etchedBorder == null) { 140 etchedBorder = new BorderUIResource.CompoundBorderUIResource ( 141 new EtchedBorder(), 142 new BasicBorders.MarginBorder ()); 143 } 144 return etchedBorder; 145 } 146 147 152 static Border getFlush3DBorder() { 153 if (flush3DBorder == null) { 154 flush3DBorder = new Flush3DBorder(); 155 } 156 return flush3DBorder; 157 } 158 159 164 static Border getInternalFrameBorder() { 165 return new InternalFrameBorder(); 166 } 167 168 174 static Border getMenuBarHeaderBorder() { 175 if (menuBarHeaderBorder == null) { 176 menuBarHeaderBorder = new BorderUIResource.CompoundBorderUIResource ( 177 new MenuBarHeaderBorder(), 178 new BasicBorders.MarginBorder ()); 179 } 180 return menuBarHeaderBorder; 181 } 182 183 188 static Border getMenuBorder() { 189 if (menuBorder == null) { 190 menuBorder = new BorderUIResource.CompoundBorderUIResource ( 191 new MenuBorder(), 192 new BasicBorders.MarginBorder ()); 193 } 194 return menuBorder; 195 } 196 197 202 static Border getMenuItemBorder() { 203 if (menuItemBorder == null) { 204 menuItemBorder = 205 new BorderUIResource (new BasicBorders.MarginBorder ()); 206 } 207 return menuItemBorder; 208 } 209 210 215 static Border getPopupMenuBorder() { 216 if (popupMenuBorder == null) { 217 popupMenuBorder = new PopupMenuBorder(); 218 } 219 return popupMenuBorder; 220 } 221 222 227 static Border getPaletteBorder() { 228 return new PaletteBorder(); 229 } 230 231 236 static Border getRolloverButtonBorder() { 237 if (rolloverButtonBorder == null) { 238 rolloverButtonBorder = new BorderUIResource.CompoundBorderUIResource ( 239 new RolloverButtonBorder(), 240 new RolloverMarginBorder()); 241 } 242 return rolloverButtonBorder; 243 } 244 245 250 static Border getScrollPaneBorder() { 251 if (scrollPaneBorder == null) { 252 scrollPaneBorder = new ScrollPaneBorder(); 253 } 254 return scrollPaneBorder; 255 } 256 257 263 static Border getSeparatorBorder() { 264 if (separatorBorder == null) { 265 separatorBorder = new BorderUIResource.CompoundBorderUIResource ( 266 new SeparatorBorder(), 267 new BasicBorders.MarginBorder ()); 268 } 269 return separatorBorder; 270 } 271 272 277 static Border getTextFieldBorder() { 278 if (textFieldBorder == null) { 279 textFieldBorder = new BorderUIResource.CompoundBorderUIResource ( 280 new TextFieldBorder(), 281 new BasicBorders.MarginBorder ()); 282 } 283 return textFieldBorder; 284 } 285 286 291 static Border getThinLoweredBorder() { 292 if (thinLoweredBorder == null) { 293 thinLoweredBorder = new ThinLoweredBorder(); 294 } 295 return thinLoweredBorder; 296 } 297 298 303 static Border getThinRaisedBorder() { 304 if (thinRaisedBorder == null) { 305 thinRaisedBorder = new ThinRaisedBorder(); 306 } 307 return thinRaisedBorder; 308 } 309 310 315 static Border getToggleButtonBorder() { 316 if (toggleButtonBorder == null) { 317 toggleButtonBorder = new BorderUIResource.CompoundBorderUIResource ( 318 new ToggleButtonBorder(), 319 new BasicBorders.MarginBorder ()); 320 } 321 return toggleButtonBorder; 322 } 323 324 330 static Border getToolBarHeaderBorder() { 331 if (toolBarHeaderBorder == null) { 332 toolBarHeaderBorder = new BorderUIResource.CompoundBorderUIResource ( 333 new ToolBarHeaderBorder(), 334 new BasicBorders.MarginBorder ()); 335 } 336 return toolBarHeaderBorder; 337 } 338 339 private static class Flush3DBorder extends AbstractBorder implements UIResource { 340 341 private static final Insets INSETS = new Insets (2, 2, 2, 2); 342 343 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 344 if (c.isEnabled()) 345 PlasticUtils.drawFlush3DBorder(g, x, y, w, h); 346 else 347 PlasticUtils.drawDisabledBorder(g, x, y, w, h); 348 } 349 350 public Insets getBorderInsets(Component c) { return INSETS; } 351 352 public Insets getBorderInsets(Component c, Insets newInsets) { 353 newInsets.top = INSETS.top; 354 newInsets.left = INSETS.left; 355 newInsets.bottom = INSETS.bottom; 356 newInsets.right = INSETS.right; 357 return newInsets; 358 } 359 } 360 361 362 private static class ButtonBorder extends AbstractBorder implements UIResource { 363 364 protected static final Insets INSETS = LookUtils.IS_LOW_RESOLUTION 365 ? new Insets (2, 3, 3, 3) 366 : new Insets (1, 3, 1, 3); 367 368 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 369 AbstractButton button = (AbstractButton ) c; 370 ButtonModel model = button.getModel(); 371 372 if (model.isEnabled()) { 373 boolean isPressed = model.isPressed() && model.isArmed(); 374 boolean isDefault = button instanceof JButton 375 && ((JButton ) button).isDefaultButton(); 376 377 if (isPressed && isDefault) 378 PlasticUtils.drawDefaultButtonPressedBorder(g, x, y, w, h); 379 else if (isPressed) 380 PlasticUtils.drawPressed3DBorder(g, x, y, w, h); 381 else if (isDefault) 382 PlasticUtils.drawDefaultButtonBorder(g, x, y, w, h, false); 383 else 384 PlasticUtils.drawButtonBorder(g, x, y, w, h, false); 385 } else { PlasticUtils.drawDisabledBorder(g, x, y, w - 1, h - 1); 387 } 388 } 389 390 public Insets getBorderInsets(Component c) { return INSETS; } 391 392 public Insets getBorderInsets(Component c, Insets newInsets) { 393 newInsets.top = INSETS.top; 394 newInsets.left = INSETS.left; 395 newInsets.bottom = INSETS.bottom; 396 newInsets.right = INSETS.right; 397 return newInsets; 398 } 399 } 400 401 402 private static class ComboBoxArrowButtonBorder extends AbstractBorder implements UIResource { 403 404 protected static final Insets INSETS = new Insets (1, 1, 1, 1); 405 406 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 407 AbstractButton button = (AbstractButton ) c; 408 ButtonModel model = button.getModel(); 409 410 if (model.isEnabled()) { 411 boolean isPressed = model.isPressed() && model.isArmed(); 412 413 if (isPressed) 414 PlasticUtils.drawPressed3DBorder(g, x, y, w, h); 415 else 416 PlasticUtils.drawButtonBorder(g, x, y, w, h, false); 417 } else { 418 PlasticUtils.drawDisabledBorder(g, x, y, w - 1, h - 1); 419 } 420 } 421 422 public Insets getBorderInsets(Component c) { return INSETS; } 423 } 424 425 426 private static class ComboBoxEditorBorder extends AbstractBorder { 427 428 private static final Insets INSETS = new Insets (2, 2, 2, 0); 429 430 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 431 if (c.isEnabled()) 432 PlasticUtils.drawFlush3DBorder(g, x, y, w + 2, h); 433 else { 434 PlasticUtils.drawDisabledBorder(g, x, y, w + 2, h-1); 435 g.setColor(UIManager.getColor("control")); 436 g.drawLine(x, y + h-1, x + w, y + h-1); 437 } 438 } 439 440 public Insets getBorderInsets(Component c) { return INSETS; } 441 } 442 443 444 447 private static class InternalFrameBorder extends AbstractBorder implements UIResource { 448 449 private static final Insets NORMAL_INSETS = new Insets (2, 2, 3, 3); 450 private static final Insets MAXIMIZED_INSETS = new Insets (2, 2, 2, 2); 451 static final int ALPHA1 = 150; 452 static final int ALPHA2 = 50; 453 454 455 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 456 JInternalFrame frame = (JInternalFrame ) c; 457 if (frame.isMaximum()) 458 PlasticUtils.drawFlush3DBorder(g, x, y, w, h); 459 else 460 paintShadowedBorder(g, x, y, w, h); 461 } 462 463 private void paintShadowedBorder(Graphics g, int x, int y, int w, int h) { 464 Color background = UIManager.getColor("desktop"); 465 Color highlight = UIManager.getColor("controlLtHighlight"); 466 Color darkShadow = UIManager.getColor("controlDkShadow"); 467 Color lightShadow = new Color (darkShadow.getRed(), 468 darkShadow.getGreen(), 469 darkShadow.getBlue(), 470 ALPHA1); 471 Color lighterShadow = new Color (darkShadow.getRed(), 472 darkShadow.getGreen(), 473 darkShadow.getBlue(), 474 ALPHA2); 475 g.translate(x, y); 476 g.setColor(darkShadow); 478 g.drawRect(0, 0, w-3, h-3); 479 g.setColor(highlight); 481 g.drawLine(1, 1, w - 4, 1); 482 g.drawLine(1, 1, 1, h - 4); 483 g.setColor(background); 485 g.fillRect(w - 2, 0, 2, h); 486 g.fillRect(0, h-2, w, 2); 487 g.setColor(lightShadow); 489 g.drawLine(w - 2, 1, w - 2, h - 2); 490 g.drawLine(1, h - 2, w - 3, h - 2); 491 g.setColor(lighterShadow); 493 g.drawLine(w - 1, 2, w - 1, h - 2); 494 g.drawLine(2, h - 1, w - 2, h - 1); 495 g.translate(-x, -y); 496 } 497 498 public Insets getBorderInsets(Component c) { 499 return ((JInternalFrame ) c).isMaximum() ? MAXIMIZED_INSETS : NORMAL_INSETS; 500 } 501 } 502 503 504 507 private static class PaletteBorder extends AbstractBorder implements UIResource { 508 509 private static final Insets INSETS = new Insets (1, 1, 1, 1); 510 511 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h ) { 512 g.translate(x,y); 513 g.setColor(PlasticLookAndFeel.getControlDarkShadow()); 514 g.drawRect(0, 0, w-1, h-1); 515 g.translate(-x,-y); 516 } 517 518 public Insets getBorderInsets(Component c) { return INSETS; } 519 } 520 521 522 526 private static class SeparatorBorder extends AbstractBorder implements UIResource { 527 528 private static final Insets INSETS = new Insets (0, 0, 2, 1); 529 530 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 531 g.translate(x, y); 532 g.setColor( UIManager.getColor("Separator.foreground")); 533 g.drawLine( 0, h - 2, w - 1, h - 2 ); 534 535 g.setColor( UIManager.getColor("Separator.background")); 536 g.drawLine( 0, h - 1, w - 1, h - 1 ); 537 g.translate(-x, -y); 538 } 539 public Insets getBorderInsets(Component c) { return INSETS; } 540 } 541 542 543 private static class ThinRaisedBorder extends AbstractBorder implements UIResource { 544 private static final Insets INSETS = new Insets (2, 2, 2, 2); 545 546 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 547 PlasticUtils.drawThinFlush3DBorder(g, x, y, w, h); 548 } 549 550 public Insets getBorderInsets(Component c) { return INSETS; } 551 } 552 553 554 private static class ThinLoweredBorder extends AbstractBorder implements UIResource { 555 private static final Insets INSETS = new Insets (2, 2, 2, 2); 556 557 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 558 PlasticUtils.drawThinPressed3DBorder(g, x, y, w, h); 559 } 560 561 public Insets getBorderInsets(Component c) { return INSETS; } 562 } 563 564 565 571 private static class EtchedBorder extends AbstractBorder implements UIResource { 572 573 private static final Insets INSETS = new Insets (2, 2, 2, 2); 574 575 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 576 PlasticUtils.drawThinPressed3DBorder(g, x, y, w, h); 577 PlasticUtils.drawThinFlush3DBorder (g, x + 1, y + 1, w - 2, h - 2); 578 } 579 580 public Insets getBorderInsets(Component c) { return INSETS; } 581 } 582 583 584 589 private static class MenuBarHeaderBorder extends AbstractBorder implements UIResource { 590 591 private static final Insets INSETS = new Insets (2, 2, 1, 2); 592 593 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 594 PlasticUtils.drawThinPressed3DBorder(g, x, y, w, h + 1); 595 PlasticUtils.drawThinFlush3DBorder (g, x + 1, y + 1, w - 2, h - 1); 596 } 597 598 public Insets getBorderInsets(Component c) { return INSETS; } 599 } 600 601 602 607 private static class ToolBarHeaderBorder extends AbstractBorder implements UIResource { 608 609 private static final Insets INSETS = new Insets (1, 2, 2, 2); 610 611 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 612 PlasticUtils.drawThinPressed3DBorder(g, x, y - 1, w, h + 1); 613 PlasticUtils.drawThinFlush3DBorder (g, x + 1, y, w - 2, h - 1); 614 } 615 616 public Insets getBorderInsets(Component c) { return INSETS; } 617 } 618 619 620 private static class MenuBorder extends AbstractBorder implements UIResource { 621 private static final Insets INSETS = new Insets ( 2, 2, 2, 2 ); 622 623 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 624 JMenuItem b = (JMenuItem ) c; 625 ButtonModel model = b.getModel(); 626 627 if (model.isArmed() || model.isSelected()) { 628 g.setColor(PlasticLookAndFeel.getControlDarkShadow()); 629 g.drawLine(0, 0, w - 2, 0 ); 630 g.drawLine(0, 0, 0, h - 1 ); 631 633 g.setColor(PlasticLookAndFeel.getPrimaryControlHighlight()); 634 g.drawLine(w - 1, 0, w - 1, h - 1 ); 635 } else if (model.isRollover()) { 636 g.translate(x, y); 637 PlasticUtils.drawFlush3DBorder(g, x, y, w, h); 638 g.translate(-x, -y); 639 } 640 } 641 642 public Insets getBorderInsets(Component c) { return INSETS; } 643 644 public Insets getBorderInsets(Component c, Insets newInsets) { 645 newInsets.top = INSETS.top; 646 newInsets.left = INSETS.left; 647 newInsets.bottom = INSETS.bottom; 648 newInsets.right = INSETS.right; 649 return newInsets; 650 } 651 } 652 653 654 private static class PopupMenuBorder extends AbstractBorder implements UIResource { 655 private static final Insets INSETS = new Insets (3, 3, 3, 3); 656 657 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 658 g.translate(x, y); 659 g.setColor(PlasticLookAndFeel.getControlDarkShadow()); 660 g.drawRect(0, 0, w-1, h-1); 661 g.setColor(PlasticLookAndFeel.getMenuItemBackground()); 662 g.drawRect(1, 1, w-3, h-3); 663 g.drawRect(2, 2, w-5, h-5); 664 g.translate(-x, -y); 665 } 666 667 public Insets getBorderInsets(Component c) { return INSETS; } 668 } 669 670 671 private static class RolloverButtonBorder extends ButtonBorder { 672 private static final Insets INSETS_3 = new Insets (3, 3, 3, 3); 673 674 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 675 AbstractButton b = (AbstractButton ) c; 676 ButtonModel model = b.getModel(); 677 678 if (!model.isEnabled()) 679 return; 680 681 if (!(c instanceof JToggleButton )) { 682 if ( model.isRollover() && !( model.isPressed() && !model.isArmed() ) ) { 683 super.paintBorder( c, g, x, y, w, h ); 684 } 685 return; 686 } 687 688 692 if (model.isRollover()) { 693 if (model.isPressed() && model.isArmed()) { 694 PlasticUtils.drawPressed3DBorder(g, x, y, w, h); 695 } else { 696 PlasticUtils.drawFlush3DBorder(g, x, y, w, h); 697 } 698 } else if (model.isSelected()) 699 PlasticUtils.drawDark3DBorder(g, x, y, w, h); 700 } 701 public Insets getBorderInsets(Component c) { return INSETS_3; } 702 } 703 704 705 709 private static class RolloverMarginBorder extends EmptyBorder { 710 711 private RolloverMarginBorder() { 712 super(1, 1, 1, 1); 713 } 714 715 716 public Insets getBorderInsets(Component c) { 717 return getBorderInsets(c, new Insets (0, 0, 0, 0)); 718 } 719 720 721 public Insets getBorderInsets(Component c, Insets insets) { 722 Insets margin = null; 723 724 if (c instanceof AbstractButton ) { 725 margin = ((AbstractButton ) c).getMargin(); 726 } 727 if (margin == null || margin instanceof UIResource ) { 728 insets.left = left; 730 insets.top = top; 731 insets.right = right; 732 insets.bottom = bottom; 733 } else { 734 insets.left = margin.left; 736 insets.top = margin.top; 737 insets.right = margin.right; 738 insets.bottom = margin.bottom; 739 } 740 return insets; 741 } 742 } 743 744 749 private static class ScrollPaneBorder extends MetalBorders.ScrollPaneBorder { 750 751 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 752 g.translate(x, y); 753 754 g.setColor(PlasticLookAndFeel.getControlDarkShadow()); 755 g.drawRect(0, 0, w - 2, h - 2); 756 g.setColor(PlasticLookAndFeel.getControlHighlight()); 757 g.drawLine(w - 1, 0, w - 1, h - 1); 758 g.drawLine(0, h - 1, w - 1, h - 1); 759 760 g.translate(-x, -y); 761 } 762 } 763 764 765 private static class TextFieldBorder extends Flush3DBorder { 766 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 767 768 if (!(c instanceof JTextComponent )) { 769 if (c.isEnabled()) { 771 PlasticUtils.drawFlush3DBorder(g, x, y, w, h); 772 } else { 773 PlasticUtils.drawDisabledBorder(g, x, y, w, h); 774 } 775 return; 776 } 777 778 if (c.isEnabled() && ((JTextComponent ) c).isEditable()) 779 PlasticUtils.drawFlush3DBorder(g, x, y, w, h); 780 else 781 PlasticUtils.drawDisabledBorder(g, x, y, w, h); 782 } 783 } 784 785 786 private static class ToggleButtonBorder extends ButtonBorder { 787 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 788 if (!c.isEnabled()) { 789 PlasticUtils.drawDisabledBorder(g, x, y, w - 1, h - 1); 790 } else { 791 AbstractButton button = (AbstractButton ) c; 792 ButtonModel model = button.getModel(); 793 if (model.isPressed() && model.isArmed()) 794 PlasticUtils.drawPressed3DBorder(g, x, y, w, h); 795 else if (model.isSelected()) 796 PlasticUtils.drawDark3DBorder(g, x, y, w, h); 797 else 798 PlasticUtils.drawFlush3DBorder(g, x, y, w, h); 799 } 800 } 801 } 802 803 804 } | Popular Tags |