226 |
|
|
227 |
|
|
228 |
/** |
/** |
229 |
|
* Returns a border for drawing a two-pixel thick separator line |
230 |
|
* below menu bars. |
231 |
|
* |
232 |
|
* <p>The colors of the border are retrieved from the |
233 |
|
* <code>UIDefaults</code> of the currently active look and feel |
234 |
|
* using the keys <code>“MenuBar.shadow”</code> and |
235 |
|
* <code>“MenuBar.highlight”</code>. |
236 |
|
* |
237 |
|
* <p><img src="BasicBorders.MenuBarBorder-1.png" width="500" |
238 |
|
* height="140" alt="[A screen shot of a JMenuBar with this border]" /> |
239 |
|
* |
240 |
|
* @return a {@link #MenuBarBorder}. |
241 |
|
* |
242 |
|
* @see javax.swing.JMenuBar |
243 |
|
*/ |
244 |
|
public static Border getMenuBarBorder() |
245 |
|
{ |
246 |
|
UIDefaults defaults; |
247 |
|
|
248 |
|
defaults = UIManager.getLookAndFeelDefaults(); |
249 |
|
return new MenuBarBorder(defaults.getColor("MenuBar.shadow"), |
250 |
|
defaults.getColor("MenuBar.highlight")); |
251 |
|
} |
252 |
|
|
253 |
|
|
254 |
|
/** |
255 |
* Returns a shared MarginBorder. |
* Returns a shared MarginBorder. |
256 |
*/ |
*/ |
257 |
static Border getMarginBorder() // intentionally not public |
static Border getMarginBorder() // intentionally not public |
551 |
} |
} |
552 |
} |
} |
553 |
|
|
554 |
|
|
555 |
|
/** |
556 |
|
* A border for drawing a separator line below JMenuBar. |
557 |
|
* |
558 |
|
* <p><img src="BasicBorders.MenuBarBorder-1.png" width="500" |
559 |
|
* height="140" alt="[A screen shot of a JMenuBar with this border]" /> |
560 |
|
* |
561 |
|
* @author Sascha Brawer (brawer@dandelis.ch) |
562 |
|
*/ |
563 |
public static class MenuBarBorder |
public static class MenuBarBorder |
564 |
|
extends AbstractBorder |
565 |
|
implements UIResource |
566 |
{ |
{ |
567 |
|
/** |
568 |
|
* Determined using the <code>serialver</code> tool |
569 |
|
* of Apple/Sun JDK 1.3.1 on MacOS X 10.1.5. |
570 |
|
*/ |
571 |
|
static final long serialVersionUID = -6909056571935227506L; |
572 |
|
|
573 |
|
|
574 |
|
/** |
575 |
|
* The shadow color, which is used for the upper line of the |
576 |
|
* two-pixel thick bottom edge. |
577 |
|
*/ |
578 |
|
private Color shadow; |
579 |
|
|
580 |
|
|
581 |
|
/** |
582 |
|
* The highlight color, which is used for the lower line of the |
583 |
|
* two-pixel thick bottom edge. |
584 |
|
*/ |
585 |
|
private Color highlight; |
586 |
|
|
587 |
|
|
588 |
|
/** |
589 |
|
* Constructs a new MenuBarBorder for drawing a JMenuBar in |
590 |
|
* the Basic look and feel. |
591 |
|
* |
592 |
|
* <p><img src="BasicBorders.MenuBarBorder-1.png" width="500" |
593 |
|
* height="140" alt="[A screen shot of a JMenuBar with this |
594 |
|
* border]" /> |
595 |
|
* |
596 |
|
* @param shadow the shadow color, which is used for the upper |
597 |
|
* line of the two-pixel thick bottom edge. |
598 |
|
* |
599 |
|
* @param highlight the shadow color, which is used for the lower |
600 |
|
* line of the two-pixel thick bottom edge. |
601 |
|
*/ |
602 |
public MenuBarBorder(Color shadow, Color highlight) |
public MenuBarBorder(Color shadow, Color highlight) |
603 |
{ |
{ |
604 |
|
/* These colors usually come from the UIDefaults of the current |
605 |
|
* look and feel. Use fallback values if the colors are not |
606 |
|
* supplied. The API specification is silent about what |
607 |
|
* behavior is expected for null colors, so users should not |
608 |
|
* rely on this fallback (which is why it is not documented in |
609 |
|
* the above Javadoc). |
610 |
|
*/ |
611 |
|
this.shadow = (shadow != null) ? shadow : Color.gray; |
612 |
|
this.highlight = (highlight != null) ? highlight : Color.white; |
613 |
|
} |
614 |
|
|
615 |
|
|
616 |
|
/** |
617 |
|
* Paints the MenuBarBorder around a given component. |
618 |
|
* |
619 |
|
* @param c the component whose border is to be painted, usually |
620 |
|
* an instance of {@link javax.swing.JMenuBar}. |
621 |
|
* |
622 |
|
* @param g the graphics for painting. |
623 |
|
* @param x the horizontal position for painting the border. |
624 |
|
* @param y the vertical position for painting the border. |
625 |
|
* @param width the width of the available area for painting the border. |
626 |
|
* @param height the height of the available area for painting the border. |
627 |
|
*/ |
628 |
|
public void paintBorder(Component c, Graphics g, |
629 |
|
int x, int y, int width, int height) |
630 |
|
{ |
631 |
|
Color oldColor; |
632 |
|
|
633 |
|
/* To understand this code, it might be helpful to look at the |
634 |
|
* image "BasicBorders.MenuBarBorder-1.png" that is included |
635 |
|
* with the JavaDoc. It is located in the "doc-files" |
636 |
|
* subdirectory. |
637 |
|
*/ |
638 |
|
oldColor = g.getColor(); |
639 |
|
y = y + height - 2; |
640 |
|
try |
641 |
|
{ |
642 |
|
g.setColor(shadow); |
643 |
|
g.drawLine(x, y, x + width - 2, y); |
644 |
|
g.drawLine(x, y + 1, x, y + 1); |
645 |
|
g.drawLine(x + width - 2, y + 1, x + width - 2, y + 1); |
646 |
|
|
647 |
|
g.setColor(highlight); |
648 |
|
g.drawLine(x + 1, y + 1, x + width - 3, y + 1); |
649 |
|
g.drawLine(x + width - 1, y, x + width - 1, y + 1); |
650 |
|
} |
651 |
|
finally |
652 |
|
{ |
653 |
|
g.setColor(oldColor); |
654 |
|
} |
655 |
} |
} |
656 |
} // class MenuBarBorder |
|
657 |
|
|
658 |
|
/** |
659 |
|
* Measures the width of this border. |
660 |
|
* |
661 |
|
* @param c the component whose border is to be measured. |
662 |
|
* |
663 |
|
* @return an Insets object whose <code>left</code>, |
664 |
|
* <code>right</code>, <code>top</code> and |
665 |
|
* <code>bottom</code> fields indicate the width of the |
666 |
|
* border at the respective edge. |
667 |
|
* |
668 |
|
* @see #getBorderInsets(java.awt.Component, java.awt.Insets) |
669 |
|
*/ |
670 |
|
public Insets getBorderInsets(Component c) |
671 |
|
{ |
672 |
|
/* There is no obvious reason for overriding this method, but we |
673 |
|
* try to have exactly the same API as the Sun reference |
674 |
|
* implementation. |
675 |
|
*/ |
676 |
|
return getBorderInsets(c, null); |
677 |
|
} |
678 |
|
|
679 |
|
|
680 |
|
/** |
681 |
|
* Measures the width of this border, storing the results into a |
682 |
|
* pre-existing Insets object. |
683 |
|
* |
684 |
|
* @param insets an Insets object for holding the result values. |
685 |
|
* After invoking this method, the <code>left</code>, |
686 |
|
* <code>right</code>, <code>top</code> and |
687 |
|
* <code>bottom</code> fields indicate the width of the |
688 |
|
* border at the respective edge. |
689 |
|
* |
690 |
|
* @return the same object that was passed for <code>insets</code>. |
691 |
|
* |
692 |
|
* @see #getBorderInsets() |
693 |
|
*/ |
694 |
|
public Insets getBorderInsets(Component c, Insets insets) |
695 |
|
{ |
696 |
|
/* The exact amount has been determined using a test program |
697 |
|
* that was run on the Apple/Sun JDK 1.3.1 on MacOS X, and the |
698 |
|
* Sun JDK 1.4.1_01 on GNU/Linux for x86. Both gave [0,0,2,0], |
699 |
|
* which was expected from looking at the screen shot. |
700 |
|
*/ |
701 |
|
if (insets == null) |
702 |
|
return new Insets(0, 0, 2, 0); |
703 |
|
|
704 |
|
insets.left = insets.right = insets.top = 0; |
705 |
|
insets.bottom = 2; |
706 |
|
return insets; |
707 |
|
} |
708 |
|
} |
709 |
|
|
710 |
|
|
711 |
/** |
/** |