KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > motif > MotifBorders


1 /*
2  * @(#)MotifBorders.java 1.36 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.motif;
9
10 import com.sun.java.swing.SwingUtilities2;
11 import javax.swing.*;
12 import javax.swing.border.*;
13 import javax.swing.plaf.*;
14
15 import java.awt.Color JavaDoc;
16 import java.awt.Component JavaDoc;
17 import java.awt.Dimension JavaDoc;
18 import java.awt.Font JavaDoc;
19 import java.awt.FontMetrics JavaDoc;
20 import java.awt.Graphics JavaDoc;
21 import java.awt.Insets JavaDoc;
22 import java.awt.Point JavaDoc;
23 import java.awt.Rectangle JavaDoc;
24
25 import java.io.Serializable JavaDoc;
26
27 /**
28  * Factory object that can vend Icons appropriate for the basic L & F.
29  * <p>
30  * <strong>Warning:</strong>
31  * Serialized objects of this class will not be compatible with
32  * future Swing releases. The current serialization support is appropriate
33  * for short term storage or RMI between applications running the same
34  * version of Swing. A future release of Swing will provide support for
35  * long term persistence.
36  *
37  * @version 1.36 12/19/03
38  * @author Amy Fowler
39  */

40 public class MotifBorders {
41
42     public static class BevelBorder extends AbstractBorder implements UIResource {
43     private Color JavaDoc darkShadow = UIManager.getColor("controlShadow");
44     private Color JavaDoc lightShadow = UIManager.getColor("controlLtHighlight");
45     private boolean isRaised;
46
47     public BevelBorder(boolean isRaised, Color JavaDoc darkShadow, Color JavaDoc lightShadow) {
48         this.isRaised = isRaised;
49             this.darkShadow = darkShadow;
50             this.lightShadow = lightShadow;
51     }
52
53     public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int w, int h) {
54         g.setColor((isRaised) ? lightShadow : darkShadow);
55         g.drawLine(x, y, x+w-1, y); // top
56
g.drawLine(x, y+h-1, x, y+1); // left
57

58         g.setColor((isRaised) ? darkShadow : lightShadow);
59         g.drawLine(x+1, y+h-1, x+w-1, y+h-1); // bottom
60
g.drawLine(x+w-1, y+h-1, x+w-1, y+1); // right
61
}
62
63     public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
64         return getBorderInsets(c, new Insets JavaDoc(0,0,0,0));
65     }
66
67         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
68         insets.top = insets.left = insets.bottom = insets.right = 1;
69         return insets;
70     }
71
72     public boolean isOpaque(Component JavaDoc c) {
73         return true;
74     }
75
76     }
77
78
79     public static class FocusBorder extends AbstractBorder implements UIResource {
80         private Color JavaDoc focus;
81     private Color JavaDoc control;
82
83     public FocusBorder(Color JavaDoc control, Color JavaDoc focus) {
84             this.control = control;
85             this.focus = focus;
86     }
87
88     public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int w, int h) {
89         if (((JComponent)c).hasFocus()) {
90             g.setColor(focus);
91         g.drawRect(x, y, w-1, h-1);
92         } else {
93         g.setColor(control);
94         g.drawRect(x, y, w-1, h-1);
95         }
96     }
97
98     public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
99         return getBorderInsets(c, new Insets JavaDoc(0,0,0,0));
100     }
101
102         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
103         insets.top = insets.left = insets.bottom = insets.right = 1;
104         return insets;
105     }
106     }
107
108
109     public static class ButtonBorder extends AbstractBorder implements UIResource {
110         protected Color JavaDoc focus = UIManager.getColor("activeCaptionBorder");
111         protected Color JavaDoc shadow = UIManager.getColor("Button.shadow");
112         protected Color JavaDoc highlight = UIManager.getColor("Button.light");
113         protected Color JavaDoc darkShadow;
114
115         public ButtonBorder(Color JavaDoc shadow, Color JavaDoc highlight, Color JavaDoc darkShadow, Color JavaDoc focus) {
116             this.shadow = shadow;
117             this.highlight = highlight;
118             this.darkShadow = darkShadow;
119             this.focus = focus;
120         }
121       
122         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int w, int h) {
123             boolean isPressed = false;
124             boolean hasFocus = false;
125             boolean canBeDefault = false;
126             boolean isDefault = false;
127
128             if (c instanceof AbstractButton) {
129             AbstractButton b = (AbstractButton)c;
130             ButtonModel model = b.getModel();
131
132             isPressed = (model.isArmed() && model.isPressed());
133             hasFocus = (model.isArmed() && isPressed) ||
134                        (b.isFocusPainted() && b.hasFocus());
135                 if (b instanceof JButton) {
136                     canBeDefault = ((JButton)b).isDefaultCapable();
137                     isDefault = ((JButton)b).isDefaultButton();
138                 }
139             }
140             int bx1 = x+1;
141             int by1 = y+1;
142             int bx2 = x+w-2;
143             int by2 = y+h-2;
144
145             if (canBeDefault) {
146                 if (isDefault) {
147                     g.setColor(shadow);
148                     g.drawLine(x+3, y+3, x+3, y+h-4);
149                     g.drawLine(x+3, y+3, x+w-4, y+3);
150       
151                     g.setColor(highlight);
152                     g.drawLine(x+4, y+h-4, x+w-4, y+h-4);
153                     g.drawLine(x+w-4, y+3, x+w-4, y+h-4);
154                 }
155                 bx1 +=6;
156                 by1 += 6;
157                 bx2 -= 6;
158                 by2 -= 6;
159             }
160
161             if (hasFocus) {
162                 g.setColor(focus);
163                 if (isDefault) {
164                     g.drawRect(x, y, w-1, h-1);
165                 } else {
166                     g.drawRect(bx1-1, by1-1, bx2-bx1+2, by2-by1+2);
167                 }
168             }
169
170             g.setColor(isPressed? shadow : highlight);
171             g.drawLine(bx1, by1, bx2, by1);
172             g.drawLine(bx1, by1, bx1, by2);
173
174             g.setColor(isPressed? highlight : shadow);
175             g.drawLine(bx2, by1+1, bx2, by2);
176             g.drawLine(bx1+1, by2, bx2, by2);
177         }
178     
179         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
180         return getBorderInsets(c, new Insets JavaDoc(0,0,0,0));
181     }
182
183     public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
184         int thickness = (c instanceof JButton && ((JButton)c).isDefaultCapable())? 8 : 2;
185         insets.top = insets.left = insets.bottom = insets.right = thickness;
186         return insets;
187         }
188
189     }
190
191     public static class ToggleButtonBorder extends ButtonBorder {
192
193         public ToggleButtonBorder(Color JavaDoc shadow, Color JavaDoc highlight, Color JavaDoc darkShadow, Color JavaDoc focus) {
194              super(shadow, highlight, darkShadow, focus);
195         }
196
197         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
198                 int width, int height) {
199         if (c instanceof AbstractButton) {
200             AbstractButton b = (AbstractButton)c;
201             ButtonModel model = b.getModel();
202
203             if (model.isArmed() && model.isPressed() || model.isSelected()) {
204             drawBezel(g, x, y, width, height,
205                   (model.isPressed() || model.isSelected()),
206                   b.isFocusPainted() && b.hasFocus(), shadow, highlight, darkShadow, focus);
207             } else {
208             drawBezel(g, x, y, width, height,
209                   false, b.isFocusPainted() && b.hasFocus(),
210                               shadow, highlight, darkShadow, focus);
211                 }
212         } else {
213             drawBezel(g, x, y, width, height, false, false,
214                           shadow, highlight, darkShadow, focus);
215             }
216         }
217
218         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
219         return new Insets JavaDoc(2, 2, 3, 3);
220         }
221
222         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
223         insets.top = insets.left = 2;
224         insets.bottom = insets.right = 3;
225         return insets;
226     }
227     }
228
229     public static class MenuBarBorder extends ButtonBorder {
230
231         public MenuBarBorder(Color JavaDoc shadow, Color JavaDoc highlight, Color JavaDoc darkShadow, Color JavaDoc focus) {
232             super(shadow, highlight, darkShadow, focus);
233         }
234
235         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
236             JMenuBar menuBar = (JMenuBar)c;
237             if (menuBar.isBorderPainted() == true) {
238             // this draws the MenuBar border
239
Dimension JavaDoc size = menuBar.getSize();
240             drawBezel(g,x,y,size.width,size.height,false,false,
241                           shadow, highlight, darkShadow, focus);
242         }
243         }
244
245     public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
246         return getBorderInsets(c, new Insets JavaDoc(0,0,0,0));
247     }
248
249         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
250         insets.top = insets.left = insets.bottom = insets.right = 6;
251         return insets;
252     }
253     }
254
255     public static class FrameBorder extends AbstractBorder implements UIResource {
256
257         JComponent jcomp;
258         Color JavaDoc frameHighlight;
259         Color JavaDoc frameColor;
260         Color JavaDoc frameShadow;
261
262         // The width of the border
263
public final static int BORDER_SIZE = 5;
264
265         /** Constructs an FrameBorder for the JComponent <b>comp</b>.
266         */

267         public FrameBorder(JComponent comp) {
268             jcomp = comp;
269         }
270
271         /** Sets the FrameBorder's JComponent.
272       */

273         public void setComponent(JComponent comp) {
274             jcomp = comp;
275         }
276
277         /** Returns the FrameBorder's JComponent.
278           * @see #setComponent
279           */

280         public JComponent component() {
281             return jcomp;
282         }
283
284         protected Color JavaDoc getFrameHighlight() {
285             return frameHighlight;
286         }
287
288         protected Color JavaDoc getFrameColor() {
289             return frameColor;
290         }
291     
292         protected Color JavaDoc getFrameShadow() {
293             return frameShadow;
294         }
295
296         static Insets JavaDoc insets = new Insets JavaDoc(BORDER_SIZE, BORDER_SIZE,
297                                       BORDER_SIZE, BORDER_SIZE);
298
299         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
300             return insets;
301         }
302
303         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc newInsets) {
304         newInsets.top = insets.top;
305         newInsets.left = insets.left;
306         newInsets.bottom = insets.bottom;
307         newInsets.right = insets.right;
308         return newInsets;
309     }
310
311        /** Draws the FrameBorder's top border.
312          */

313         protected boolean drawTopBorder(Component JavaDoc c, Graphics JavaDoc g,
314                                     int x, int y, int width, int height) {
315             Rectangle JavaDoc titleBarRect = new Rectangle JavaDoc(x, y, width, BORDER_SIZE);
316             if (!g.getClipBounds().intersects(titleBarRect)) {
317                 return false;
318             }
319
320             int maxX = width - 1;
321             int maxY = BORDER_SIZE - 1;
322
323             // Draw frame
324
g.setColor(frameColor);
325             g.drawLine(x, y + 2, maxX - 2, y + 2);
326             g.drawLine(x, y + 3, maxX - 2, y + 3);
327             g.drawLine(x, y + 4, maxX - 2, y + 4);
328
329             // Draw highlights
330
g.setColor(frameHighlight);
331             g.drawLine(x, y, maxX, y);
332             g.drawLine(x, y + 1, maxX, y + 1);
333             g.drawLine(x, y + 2, x, y + 4);
334             g.drawLine(x + 1, y + 2, x + 1, y + 4);
335
336             // Draw shadows
337
g.setColor(frameShadow);
338             g.drawLine(x + 4, y + 4, maxX - 4, y + 4);
339             g.drawLine(maxX, y + 1, maxX, maxY);
340             g.drawLine(maxX - 1, y + 2, maxX - 1, maxY);
341
342             return true;
343         }
344
345         /** Draws the FrameBorder's left border.
346           */

347         protected boolean drawLeftBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
348                                int width, int height) {
349             Rectangle JavaDoc borderRect =
350                 new Rectangle JavaDoc(0, 0, getBorderInsets(c).left, height);
351             if (!g.getClipBounds().intersects(borderRect)) {
352                 return false;
353             }
354
355             int startY = BORDER_SIZE;
356
357             g.setColor(frameHighlight);
358             g.drawLine(x, startY, x, height - 1);
359             g.drawLine(x + 1, startY, x + 1, height - 2);
360
361             g.setColor(frameColor);
362             g.fillRect(x + 2, startY, x + 2, height - 3);
363
364             g.setColor(frameShadow);
365             g.drawLine(x + 4, startY, x + 4, height - 5);
366
367             return true;
368         }
369
370         /** Draws the FrameBorder's right border.
371           */

372         protected boolean drawRightBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
373                                 int width, int height) {
374             Rectangle JavaDoc borderRect = new Rectangle JavaDoc(
375                 width - getBorderInsets(c).right, 0,
376                 getBorderInsets(c).right, height);
377             if (!g.getClipBounds().intersects(borderRect)) {
378                 return false;
379             }
380
381             int startX = width - getBorderInsets(c).right;
382             int startY = BORDER_SIZE;
383
384             g.setColor(frameColor);
385             g.fillRect(startX + 1, startY, 2, height - 1);
386
387             g.setColor(frameShadow);
388             g.fillRect(startX + 3, startY, 2, height - 1);
389
390             g.setColor(frameHighlight);
391             g.drawLine(startX, startY, startX, height - 1);
392
393             return true;
394         }
395
396         /** Draws the FrameBorder's bottom border.
397           */

398         protected boolean drawBottomBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
399                                  int width, int height) {
400             Rectangle JavaDoc borderRect;
401             int marginHeight, startY;
402
403             borderRect = new Rectangle JavaDoc(0, height - getBorderInsets(c).bottom,
404                                   width, getBorderInsets(c).bottom);
405             if (!g.getClipBounds().intersects(borderRect)) {
406                 return false;
407             }
408
409             startY = height - getBorderInsets(c).bottom;
410
411             g.setColor(frameShadow);
412             g.drawLine(x + 1, height - 1, width - 1, height - 1);
413             g.drawLine(x + 2, height - 2, width - 2, height - 2);
414
415             g.setColor(frameColor);
416             g.fillRect(x + 2, startY + 1, width - 4, 2);
417
418             g.setColor(frameHighlight);
419             g.drawLine(x + 5, startY, width - 5, startY);
420
421             return true;
422         }
423
424         // Returns true if the associated component has focus.
425
protected boolean isActiveFrame() {
426             return jcomp.hasFocus();
427         }
428
429         /** Draws the FrameBorder in the given Rect. Calls
430           * <b>drawTitleBar</b>, <b>drawLeftBorder</b>, <b>drawRightBorder</b> and
431           * <b>drawBottomBorder</b>.
432           */

433         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g,
434                             int x, int y, int width, int height) {
435             if (isActiveFrame()) {
436                 frameColor = UIManager.getColor("activeCaptionBorder");
437             } else {
438                 frameColor = UIManager.getColor("inactiveCaptionBorder");
439             }
440             frameHighlight = frameColor.brighter();
441             frameShadow = frameColor.darker().darker();
442
443             drawTopBorder(c, g, x, y, width, height);
444             drawLeftBorder(c, g, x, y, width, height);
445             drawRightBorder(c, g, x, y, width, height);
446             drawBottomBorder(c, g, x, y, width, height);
447         }
448     }
449
450     public static class InternalFrameBorder extends FrameBorder {
451
452         JInternalFrame frame;
453
454         // The size of the bounding box for Motif frame corners.
455
public final static int CORNER_SIZE = 24;
456
457         /** Constructs an InternalFrameBorder for the InternalFrame
458           * <b>aFrame</b>.
459           */

460         public InternalFrameBorder(JInternalFrame aFrame) {
461             super(aFrame);
462             frame = aFrame;
463         }
464
465         /** Sets the InternalFrameBorder's InternalFrame.
466           */

467         public void setFrame(JInternalFrame aFrame) {
468             frame = aFrame;
469         }
470
471         /** Returns the InternalFrameBorder's InternalFrame.
472           * @see #setFrame
473           */

474         public JInternalFrame frame() {
475             return frame;
476         }
477
478         /** Returns the width of the InternalFrameBorder's resize controls,
479           * appearing along the InternalFrameBorder's bottom border. Clicking
480           * and dragging within these controls lets the user change both the
481           * InternalFrame's width and height, while dragging between the controls
482           * constrains resizing to just the vertical dimension. Override this
483           * method if you implement your own bottom border painting and use a
484           * resize control with a different size.
485           */

486         public int resizePartWidth() {
487             if (!frame.isResizable()) {
488                 return 0;
489             }
490             return FrameBorder.BORDER_SIZE;
491         }
492
493         /** Draws the InternalFrameBorder's top border.
494          */

495         protected boolean drawTopBorder(Component JavaDoc c, Graphics JavaDoc g,
496                                     int x, int y, int width, int height) {
497             if (super.drawTopBorder(c, g, x, y, width, height) &&
498                 frame.isResizable()) {
499                 g.setColor(getFrameShadow());
500                 g.drawLine(CORNER_SIZE - 1, y + 1, CORNER_SIZE - 1, y + 4);
501                 g.drawLine(width - CORNER_SIZE - 1, y + 1,
502                        width - CORNER_SIZE - 1, y + 4);
503
504                 g.setColor(getFrameHighlight());
505                 g.drawLine(CORNER_SIZE, y, CORNER_SIZE, y + 4);
506                 g.drawLine(width - CORNER_SIZE, y, width - CORNER_SIZE, y + 4);
507                 return true;
508             }
509             return false;
510         }
511
512         /** Draws the InternalFrameBorder's left border.
513           */

514         protected boolean drawLeftBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
515                                      int width, int height) {
516             if (super.drawLeftBorder(c, g, x, y, width, height) &&
517                 frame.isResizable()) {
518                 g.setColor(getFrameHighlight());
519                 int topY = y + CORNER_SIZE;
520                 g.drawLine(x, topY, x + 4, topY);
521                 int bottomY = height - CORNER_SIZE;
522                 g.drawLine(x + 1, bottomY, x + 5, bottomY);
523                 g.setColor(getFrameShadow());
524                 g.drawLine(x + 1, topY - 1, x + 5, topY - 1);
525                 g.drawLine(x + 1, bottomY - 1, x + 5, bottomY - 1);
526                 return true;
527             }
528             return false;
529         }
530
531         /** Draws the InternalFrameBorder's right border.
532           */

533         protected boolean drawRightBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
534                                       int width, int height) {
535             if (super.drawRightBorder(c, g, x, y, width, height) &&
536                 frame.isResizable()) {
537                 int startX = width - getBorderInsets(c).right;
538                 g.setColor(getFrameHighlight());
539                 int topY = y + CORNER_SIZE;
540                 g.drawLine(startX, topY, width - 2, topY);
541                 int bottomY = height - CORNER_SIZE;
542                 g.drawLine(startX + 1, bottomY, startX + 3, bottomY);
543                 g.setColor(getFrameShadow());
544                 g.drawLine(startX + 1, topY - 1, width - 2, topY - 1);
545                 g.drawLine(startX + 1, bottomY - 1, startX + 3, bottomY - 1);
546                 return true;
547             }
548             return false;
549         }
550
551         /** Draws the InternalFrameBorder's bottom border.
552           */

553         protected boolean drawBottomBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
554                                        int width, int height) {
555             if (super.drawBottomBorder(c, g, x, y, width, height) &&
556                 frame.isResizable()) {
557                 int startY = height - getBorderInsets(c).bottom;
558
559                 g.setColor(getFrameShadow());
560                 g.drawLine(CORNER_SIZE - 1, startY + 1,
561                        CORNER_SIZE - 1, height - 1);
562                 g.drawLine(width - CORNER_SIZE, startY + 1,
563                        width - CORNER_SIZE, height - 1);
564         
565                 g.setColor(getFrameHighlight());
566                 g.drawLine(CORNER_SIZE, startY, CORNER_SIZE, height - 2);
567                 g.drawLine(width - CORNER_SIZE + 1, startY,
568                        width - CORNER_SIZE + 1, height - 2);
569                 return true;
570             }
571             return false;
572         }
573
574         // Returns true if the associated internal frame has focus.
575
protected boolean isActiveFrame() {
576             return frame.isSelected();
577         }
578     }
579
580     public static void drawBezel(Graphics JavaDoc g, int x, int y, int w, int h,
581                    boolean isPressed, boolean hasFocus,
582                                Color JavaDoc shadow, Color JavaDoc highlight,
583                                Color JavaDoc darkShadow, Color JavaDoc focus) {
584
585         Color JavaDoc oldColor = g.getColor();
586         g.translate(x, y);
587
588         if (isPressed) {
589             if (hasFocus){
590             g.setColor(focus);
591             g.drawRect(0, 0, w-1, h-1);
592             }
593             g.setColor(shadow); // inner border
594
g.drawRect(1, 1, w-3, h-3);
595       
596             g.setColor(highlight); // inner 3D border
597
g.drawLine(2, h-3, w-3, h-3);
598             g.drawLine(w-3, 2, w-3, h-4);
599       
600         } else {
601             if (hasFocus) {
602             g.setColor(focus);
603             g.drawRect(0, 0, w-1, h-1);
604     
605             g.setColor(highlight); // inner 3D border
606
g.drawLine(1, 1, 1, h-3);
607             g.drawLine(2, 1, w-4, 1);
608     
609             g.setColor(shadow);
610             g.drawLine(2, h-3, w-3, h-3);
611             g.drawLine(w-3, 1, w-3, h-4);
612     
613             g.setColor(darkShadow); // black drop shadow __|
614
g.drawLine(1, h-2, w-2, h-2);
615             g.drawLine(w-2, h-2, w-2, 1);
616             } else {
617             g.setColor(highlight); // inner 3D border
618
g.drawLine(1,1,1,h-3);
619             g.drawLine(2,1,w-4,1);
620             g.setColor(shadow);
621                 g.drawLine(2,h-3,w-3,h-3);
622             g.drawLine(w-3,1,w-3,h-4);
623     
624             g.setColor(darkShadow); // black drop shadow __|
625
g.drawLine(1,h-2,w-2,h-2);
626             g.drawLine(w-2,h-2,w-2,0);
627     
628             }
629             g.translate(-x, -y);
630         }
631         g.setColor(oldColor);
632     }
633
634     public static class MotifPopupMenuBorder extends AbstractBorder implements UIResource {
635     protected Font JavaDoc font;
636     protected Color JavaDoc background;
637     protected Color JavaDoc foreground;
638     protected Color JavaDoc shadowColor;
639     protected Color JavaDoc highlightColor;
640
641     // Space between the border and text
642
static protected final int TEXT_SPACING = 2;
643
644     // Space for the separator under the title
645
static protected final int GROOVE_HEIGHT = 2;
646
647     /**
648      * Creates a MotifPopupMenuBorder instance
649      *
650      */

651     public MotifPopupMenuBorder(
652                     Font JavaDoc titleFont,
653                     Color JavaDoc bgColor,
654                     Color JavaDoc fgColor,
655                     Color JavaDoc shadow,
656                     Color JavaDoc highlight) {
657         this.font = titleFont;
658         this.background = bgColor;
659         this.foreground = fgColor;
660         this.shadowColor = shadow;
661         this.highlightColor = highlight;
662     }
663     
664     /**
665      * Paints the border for the specified component with the
666      * specified position and size.
667      * @param c the component for which this border is being painted
668      * @param g the paint graphics
669      * @param x the x position of the painted border
670      * @param y the y position of the painted border
671      * @param width the width of the painted border
672      * @param height the height of the painted border
673      */

674     public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
675         
676         Font JavaDoc origFont = g.getFont();
677         Color JavaDoc origColor = g.getColor();
678             JPopupMenu popup = (JPopupMenu)c;
679
680         String JavaDoc title = popup.getLabel();
681         if (title == null) {
682         return;
683         }
684
685         g.setFont(font);
686         
687         FontMetrics JavaDoc fm = SwingUtilities2.getFontMetrics(popup, g, font);
688         int fontHeight = fm.getHeight();
689         int descent = fm.getDescent();
690         int ascent = fm.getAscent();
691         Point JavaDoc textLoc = new Point JavaDoc();
692         int stringWidth = SwingUtilities2.stringWidth(popup, fm,
693                                                                   title);
694         
695         textLoc.y = y + ascent + TEXT_SPACING;
696         textLoc.x = x + ((width - stringWidth) / 2);
697         
698         g.setColor(background);
699         g.fillRect(textLoc.x - TEXT_SPACING, textLoc.y - (fontHeight-descent),
700                stringWidth + (2 * TEXT_SPACING), fontHeight - descent);
701         g.setColor(foreground);
702         SwingUtilities2.drawString(popup, g, title, textLoc.x, textLoc.y);
703         
704         MotifGraphicsUtils.drawGroove(g, x, textLoc.y + TEXT_SPACING,
705                       width, GROOVE_HEIGHT,
706                                           shadowColor, highlightColor);
707
708         g.setFont(origFont);
709         g.setColor(origColor);
710     }
711     
712     /**
713      * Returns the insets of the border.
714      * @param c the component for which this border insets value applies
715      */

716     public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
717         return getBorderInsets(c, new Insets JavaDoc(0, 0, 0, 0));
718     }
719     
720     /**
721      * Reinitialize the insets parameter with this Border's current Insets.
722      * @param c the component for which this border insets value applies
723      * @param insets the object to be reinitialized
724      */

725     public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
726         FontMetrics JavaDoc fm;
727         int descent = 0;
728         int ascent = 16;
729
730         String JavaDoc title = ((JPopupMenu)c).getLabel();
731         if (title == null) {
732         insets.left = insets.top = insets.right = insets.bottom = 0;
733         return insets;
734         }
735
736         fm = c.getFontMetrics(font);
737         
738         if(fm != null) {
739         descent = fm.getDescent();
740         ascent = fm.getAscent();
741         }
742         
743         insets.top += ascent + descent + TEXT_SPACING + GROOVE_HEIGHT;
744         return insets;
745     }
746             
747     }
748
749 }
750
Popular Tags