Imported GNU Classpath 0.19 + gcj-import-20051115.
[official-gcc.git] / libjava / classpath / javax / swing / plaf / metal / MetalBorders.java
blob4fa3b3640564ac998c836e2fde0b7d8fa52b40e7
1 /* MetalBorders.java
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.plaf.metal;
41 import java.awt.Color;
42 import java.awt.Component;
43 import java.awt.Graphics;
44 import java.awt.Insets;
46 import javax.swing.AbstractButton;
47 import javax.swing.ButtonModel;
48 import javax.swing.JButton;
49 import javax.swing.JInternalFrame;
50 import javax.swing.JMenu;
51 import javax.swing.JMenuBar;
52 import javax.swing.JMenuItem;
53 import javax.swing.JOptionPane;
54 import javax.swing.JScrollPane;
55 import javax.swing.JTextField;
56 import javax.swing.JToggleButton;
57 import javax.swing.JToolBar;
58 import javax.swing.SwingConstants;
59 import javax.swing.UIDefaults;
60 import javax.swing.UIManager;
61 import javax.swing.border.AbstractBorder;
62 import javax.swing.border.Border;
63 import javax.swing.plaf.BorderUIResource;
64 import javax.swing.plaf.UIResource;
65 import javax.swing.plaf.basic.BasicBorders;
66 import javax.swing.text.JTextComponent;
69 /**
70 * A factory class that creates borders for the different Swing components.
72 * @author Roman Kennke (roman@kennke.org)
74 public class MetalBorders
77 /** The shared instance for getButtonBorder(). */
78 private static Border buttonBorder;
80 /** The shared instance for getToggleButtonBorder(). */
81 private static Border toggleButtonBorder;
83 /** The shared instance for getDesktopIconBorder(). */
84 private static Border desktopIconBorder;
86 /** The shared instance for getRolloverButtonBorder(). */
87 private static Border toolbarButtonBorder;
89 /** The shared instance for getTextFieldBorder(). */
90 private static Border textFieldBorder;
92 /** The shared instance for getTextBorder(). */
93 private static Border textBorder;
95 /** The shared instance for getRolloverBorder(). */
96 private static Border rolloverBorder;
98 /**
99 * A MarginBorder that gets shared by multiple components.
100 * Created on demand by the private helper function {@link
101 * #getMarginBorder()}.
103 private static BasicBorders.MarginBorder marginBorder;
106 * A border used for {@link JButton} components.
108 public static class ButtonBorder extends AbstractBorder implements UIResource
110 /** The borders insets. */
111 protected static Insets borderInsets = new Insets(3, 3, 3, 3);
114 * Creates a new instance of <code>ButtonBorder</code>.
116 public ButtonBorder()
118 // Nothing to do here.
122 * Paints the button border.
124 * @param c the component for which we paint the border
125 * @param g the Graphics context to use
126 * @param x the X coordinate of the upper left corner of c
127 * @param y the Y coordinate of the upper left corner of c
128 * @param w the width of c
129 * @param h the height of c
131 public void paintBorder(Component c, Graphics g, int x, int y, int w,
132 int h)
134 ButtonModel bmodel = null;
136 if (c instanceof AbstractButton)
137 bmodel = ((AbstractButton) c).getModel();
139 Color darkShadow = MetalLookAndFeel.getControlDarkShadow();
140 Color shadow = MetalLookAndFeel.getControlShadow();
141 Color light = MetalLookAndFeel.getWhite();
142 Color middle = MetalLookAndFeel.getControl();
144 if (c.isEnabled())
146 // draw dark border
147 g.setColor(darkShadow);
148 g.drawRect(x, y, w - 2, h - 2);
150 if (!bmodel.isPressed())
152 // draw light border
153 g.setColor(light);
154 g.drawRect(x + 1, y + 1, w - 2, h - 2);
156 // draw crossing pixels of both borders
157 g.setColor(middle);
158 g.drawRect(x + 1, y + h - 2, 0, 0);
159 g.drawRect(x + w - 2, y + 1, 0, 0);
161 else
163 // draw light border
164 g.setColor(light);
165 g.drawLine(x + w - 1, y + 1, x + w - 1, y + h - 1);
166 g.drawLine(x + 1, y + h - 1, x + w - 1, y + h - 1);
168 // draw shadow border
169 g.setColor(middle);
170 g.drawLine(x + 1, y + 1, x + w - 2, y + 1);
171 g.drawLine(x + 1, y + 1, x + 1, y + h - 2);
173 // draw crossing pixels of both borders
174 g.setColor(shadow);
175 g.drawRect(x + 1, y + h - 2, 0, 0);
176 g.drawRect(x + w - 2, y + 1, 0, 0);
179 else
181 // draw disabled border
182 g.setColor(MetalLookAndFeel.getInactiveControlTextColor());
183 g.drawRect(x, y, w - 2, h - 2);
188 * Returns the insets of the <code>ButtonBorder</code>.
190 * @param c the component for which the border is used
192 * @return The insets of the ButtonBorder
194 public Insets getBorderInsets(Component c)
196 return getBorderInsets(c, null);
200 * Returns the insets of the <code>ButtonBorder</code> in the specified
201 * <code>newInsets</code> object.
203 * @param c the component for which the border is used
204 * @param newInsets the insets object where to put the values (if
205 * <code>null</code>, a new instance is created).
207 * @return The insets.
209 public Insets getBorderInsets(Component c, Insets newInsets)
211 if (newInsets == null)
212 newInsets = new Insets(0, 0, 0, 0);
214 newInsets.bottom = borderInsets.bottom;
215 newInsets.left = borderInsets.left;
216 newInsets.right = borderInsets.right;
217 newInsets.top = borderInsets.top;
218 return newInsets;
223 * A border used when painting {@link JInternalFrame} instances.
225 static class DesktopIconBorder extends AbstractBorder
226 implements UIResource
229 * Creates a new border instance.
231 public DesktopIconBorder()
233 // Nothing to do here.
237 * Returns the border insets.
239 * @param c the component (ignored).
241 * @return The border insets.
243 public Insets getBorderInsets(Component c)
245 return getBorderInsets(c, null);
249 * Returns the border insets.
251 * @param c the component (ignored).
252 * @return The border insets.
254 public Insets getBorderInsets(Component c, Insets newInsets)
256 if (newInsets == null)
257 newInsets = new Insets(3, 3, 2, 3);
258 else
260 newInsets.top = 3;
261 newInsets.left = 3;
262 newInsets.bottom = 2;
263 newInsets.right = 3;
265 return newInsets;
269 * Paints the border for the specified component.
271 * @param c the component.
272 * @param g the graphics device.
273 * @param x the x-coordinate.
274 * @param y the y-coordinate.
275 * @param w the width.
276 * @param h the height.
278 public void paintBorder(Component c, Graphics g, int x, int y, int w,
279 int h)
281 g.setColor(MetalLookAndFeel.getControlDarkShadow());
282 g.drawRect(x, y, w - 1, h - 1);
288 * A simple 3D border.
290 public static class Flush3DBorder extends AbstractBorder
291 implements UIResource
294 * Creates a new border instance.
296 public Flush3DBorder()
298 // Nothing to do here.
302 * Returns the border insets.
304 * @param c the component (ignored).
306 * @return The border insets.
308 public Insets getBorderInsets(Component c)
310 return getBorderInsets(c, null);
314 * Returns the border insets.
316 * @param c the component (ignored).
317 * @return The border insets.
319 public Insets getBorderInsets(Component c, Insets newInsets)
321 if (newInsets == null)
322 newInsets = new Insets(2, 2, 2, 2);
323 else
325 newInsets.top = 2;
326 newInsets.left = 2;
327 newInsets.bottom = 2;
328 newInsets.right = 2;
330 return newInsets;
334 * Paints the border for the specified component.
336 * @param c the component (ignored).
337 * @param g the graphics device.
338 * @param x the x-coordinate.
339 * @param y the y-coordinate.
340 * @param w the width.
341 * @param h the height.
343 public void paintBorder(Component c, Graphics g, int x, int y, int w,
344 int h)
346 Color savedColor = g.getColor();
347 g.setColor(MetalLookAndFeel.getControlDarkShadow());
348 g.drawRect(x, y, w - 2, h - 2);
349 g.setColor(MetalLookAndFeel.getControlHighlight());
350 g.drawRect(x + 1, y + 1, w - 2, h - 2);
351 g.setColor(MetalLookAndFeel.getControl());
352 g.drawLine(x + 1, y + h - 2, x + 1, y + h - 2);
353 g.drawLine(x + w - 2, y + 1, x + w - 2, y + 1);
354 g.setColor(savedColor);
360 * A border used for a {@link JInternalFrame} when it is being used as a
361 * palette.
363 * @since 1.3
365 public static class PaletteBorder extends AbstractBorder
366 implements UIResource
369 * Creates a new <code>PaletteBorder</code>.
371 public PaletteBorder()
373 // Nothing to do here.
377 * Returns the border insets.
379 * @param c the component (ignored).
381 * @return The border insets.
383 public Insets getBorderInsets(Component c)
385 return getBorderInsets(c, null);
389 * Returns the border insets.
391 * @param c the component (ignored).
392 * @param newInsets the insets object that, if non-<code>null</code>, will
393 * be populated with the result from this method.
395 * @return The border insets.
397 public Insets getBorderInsets(Component c, Insets newInsets)
399 if (newInsets == null)
400 newInsets = new Insets(1, 1, 1, 1);
401 else
403 newInsets.top = 1;
404 newInsets.left = 1;
405 newInsets.bottom = 1;
406 newInsets.right = 1;
408 return newInsets;
412 * Paints the border for the specified component.
414 * @param c the component (ignored).
415 * @param g the graphics device.
416 * @param x the x-coordinate.
417 * @param y the y-coordinate.
418 * @param w the width.
419 * @param h the height.
421 public void paintBorder(Component c, Graphics g, int x, int y, int w,
422 int h)
424 Color savedColor = g.getColor();
426 // draw the outline
427 g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
428 g.drawRect(x, y, w - 1, h - 1);
430 // put a dot in each corner
431 g.setColor(MetalLookAndFeel.getControl());
432 g.fillRect(x, y, 1, 1);
433 g.fillRect(x + w - 1, y, 1, 1);
434 g.fillRect(x + w - 1, y + h - 1, 1, 1);
435 g.fillRect(x, y + h - 1, 1, 1);
436 g.setColor(savedColor);
442 * A border used for the {@link JTextField} component.
444 public static class TextFieldBorder extends Flush3DBorder
445 implements UIResource
448 * Creates a new border instance.
450 public TextFieldBorder()
452 // Nothing to do here.
456 * Paints the border for the specified component.
458 * @param c the component (ignored).
459 * @param g the graphics device.
460 * @param x the x-coordinate.
461 * @param y the y-coordinate.
462 * @param w the width.
463 * @param h the height.
465 public void paintBorder(Component c, Graphics g, int x, int y, int w,
466 int h)
468 boolean enabledTextBorder;
469 if (c instanceof JTextComponent)
471 JTextComponent tc = (JTextComponent) c;
472 enabledTextBorder = tc.isEnabled() && tc.isEditable();
474 else
475 enabledTextBorder = false;
477 if (enabledTextBorder)
478 super.paintBorder(c, g, x, y, w, h);
479 else
481 Color savedColor = g.getColor();
482 g.setColor(MetalLookAndFeel.getControlShadow());
483 g.drawRect(x, y, w - 1, h - 1);
484 g.setColor(savedColor);
491 * A border used for the {@link JInternalFrame} component.
493 public static class InternalFrameBorder extends AbstractBorder
494 implements UIResource
497 * Creates a new border instance.
499 public InternalFrameBorder()
501 // Nothing to do here.
505 * Returns the border insets.
507 * @param c the component (ignored).
509 * @return The border insets.
511 public Insets getBorderInsets(Component c)
513 return getBorderInsets(c, null);
517 * Returns the border insets.
519 * @param c the component (ignored).
520 * @return The border insets.
522 public Insets getBorderInsets(Component c, Insets newInsets)
524 if (newInsets == null)
525 newInsets = new Insets(5, 5, 5, 5);
526 else
528 newInsets.top = 5;
529 newInsets.left = 5;
530 newInsets.bottom = 5;
531 newInsets.right = 5;
533 return newInsets;
537 * Paints the border for the specified component.
539 * @param c the component.
540 * @param g the graphics device.
541 * @param x the x-coordinate.
542 * @param y the y-coordinate.
543 * @param w the width.
544 * @param h the height.
546 public void paintBorder(Component c, Graphics g, int x, int y, int w,
547 int h)
550 JInternalFrame f = (JInternalFrame) c;
551 if (f.isSelected())
552 g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
553 else
554 g.setColor(MetalLookAndFeel.getControlDarkShadow());
556 // fill the border background
557 g.fillRect(x, y, w, 5);
558 g.fillRect(x, y, 5, h);
559 g.fillRect(x + w - 5, y, 5, h);
560 g.fillRect(x, y + h - 5, w, 5);
562 // draw a dot in each corner
563 g.setColor(MetalLookAndFeel.getControl());
564 g.fillRect(x, y, 1, 1);
565 g.fillRect(x + w - 1, y, 1, 1);
566 g.fillRect(x + w - 1, y + h - 1, 1, 1);
567 g.fillRect(x, y + h - 1, 1, 1);
569 // draw the lines
570 g.setColor(MetalLookAndFeel.getBlack());
571 g.drawLine(x + 14, y + 2, x + w - 15, y + 2);
572 g.drawLine(x + 14, y + h - 3, x + w - 15, y + h - 3);
573 g.drawLine(x + 2, y + 14, x + 2, y + h - 15);
574 g.drawLine(x + w - 3, y + 14, x + w - 3, y + h - 15);
576 // draw the line highlights
577 if (f.isSelected())
578 g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
579 else
580 g.setColor(MetalLookAndFeel.getControlShadow());
581 g.drawLine(x + 15, y + 3, x + w - 14, y + 3);
582 g.drawLine(x + 15, y + h - 2, x + w - 14, y + h - 2);
583 g.drawLine(x + 3, y + 15, x + 3, y + h - 14);
584 g.drawLine(x + w - 2, y + 15, x + w - 2, y + h - 14);
590 * A border used for {@link JInternalFrame} components that are
591 * presented as dialogs (by the {@link JOptionPane} class).
593 public static class OptionDialogBorder extends AbstractBorder
594 implements UIResource
598 * Creates a new border instance.
600 public OptionDialogBorder()
602 // Nothing to do here.
606 * Returns the border insets.
608 * @param c the component (ignored).
610 * @return The border insets.
612 public Insets getBorderInsets(Component c)
614 return getBorderInsets(c, null);
618 * Returns the border insets.
620 * @param c the component (ignored).
621 * @return The border insets.
623 public Insets getBorderInsets(Component c, Insets newInsets)
625 if (newInsets == null)
626 newInsets = new Insets(3, 3, 3, 3);
627 else
629 newInsets.top = 3;
630 newInsets.left = 3;
631 newInsets.bottom = 3;
632 newInsets.right = 3;
634 return newInsets;
638 * Paints the border for the specified component.
640 * @param c the component.
641 * @param g the graphics device.
642 * @param x the x-coordinate.
643 * @param y the y-coordinate.
644 * @param w the width.
645 * @param h the height.
647 public void paintBorder(Component c, Graphics g, int x, int y, int w,
648 int h)
651 JInternalFrame f = (JInternalFrame) c;
652 g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
653 if (f.getContentPane() instanceof JOptionPane)
655 JOptionPane pane = (JOptionPane) f.getContentPane();
656 int type = pane.getMessageType();
657 UIDefaults defaults = UIManager.getLookAndFeelDefaults();
658 if (type == JOptionPane.QUESTION_MESSAGE)
660 Color bc = defaults.getColor(
661 "OptionPane.questionDialog.border.background");
662 if (bc != null)
663 g.setColor(bc);
665 if (type == JOptionPane.WARNING_MESSAGE)
667 Color bc = defaults.getColor(
668 "OptionPane.warningDialog.border.background");
669 if (bc != null)
670 g.setColor(bc);
672 else if (type == JOptionPane.ERROR_MESSAGE)
674 Color bc = defaults.getColor(
675 "OptionPane.errorDialog.border.background");
676 if (bc != null)
677 g.setColor(bc);
681 // fill the border background
682 g.fillRect(x, y, w, 3);
683 g.fillRect(x, y, 3, h);
684 g.fillRect(x + w - 3, y, 3, h);
685 g.fillRect(x, y + h - 3, w, 3);
687 // draw a dot in each corner
688 g.setColor(MetalLookAndFeel.getControl());
689 g.fillRect(x, y, 1, 1);
690 g.fillRect(x + w - 1, y, 1, 1);
691 g.fillRect(x + w - 1, y + h - 1, 1, 1);
692 g.fillRect(x, y + h - 1, 1, 1);
699 * A border used for {@link JMenu} and {@link JMenuItem} components.
701 public static class MenuItemBorder extends AbstractBorder
702 implements UIResource
704 /** The border insets. */
705 protected static Insets borderInsets = new Insets(1, 1, 1, 1);
708 * Creates a new border instance.
710 public MenuItemBorder()
712 // Nothing to do here.
716 * Paints the border for the component. A border is painted only if the
717 * component is a selected {@link JMenu} or an armed {@link JMenuItem}.
719 * @param c the component.
720 * @param g the graphics device.
721 * @param x the x-coordinate of the border area.
722 * @param y the y-coordinate of the border area.
723 * @param w the width of the border area.
724 * @param h the height of the border area.
726 public void paintBorder(Component c, Graphics g, int x, int y, int w,
727 int h)
729 Color dark = MetalLookAndFeel.getPrimaryControlDarkShadow();
730 Color light = MetalLookAndFeel.getPrimaryControlHighlight();
731 if (c instanceof JMenu) {
732 JMenu menu = (JMenu) c;
733 if (menu.isSelected())
735 g.setColor(dark);
736 g.drawLine(x, y, x, y + h);
737 g.drawLine(x, y, x + w, y);
738 g.drawLine(x + w - 2, y + 1, x + w - 2, y + h);
739 g.setColor(light);
740 g.drawLine(x + w - 1, y + 1, x + w - 1, y + h);
743 else if (c instanceof JMenuItem)
745 JMenuItem item = (JMenuItem) c;
746 if (item.isArmed())
748 g.setColor(dark);
749 g.drawLine(x, y, x + w, y);
750 g.setColor(light);
751 g.drawLine(x, y + h - 1, x + w, y + h - 1);
753 else
755 // Normally we draw a light line on the left.
756 g.setColor(light);
757 g.drawLine(x, y, x, y + h);
763 * Returns the border insets.
765 * @param c the component (ignored).
767 * @return The border insets.
769 public Insets getBorderInsets(Component c)
771 return borderInsets;
775 * Populates <code>insets</code> with the border insets, then returns it.
777 * @param c the component (ignored).
778 * @param insets the object to populate with the border insets.
780 * @return The border insets.
782 * @throws NullPointerException if <code>insets</code> is <code>null</code>.
784 public Insets getBorderInsets(Component c, Insets insets)
786 insets.left = borderInsets.left;
787 insets.top = borderInsets.top;
788 insets.bottom = borderInsets.bottom;
789 insets.right = borderInsets.right;
790 return insets;
795 * A border used for {@link JMenuBar} components.
797 public static class MenuBarBorder
798 extends AbstractBorder
799 implements UIResource
801 /** The border insets. */
802 protected static Insets borderInsets = new Insets(1, 0, 1, 0);
804 // TODO: find where this color really comes from
805 private static Color borderColor = new Color(153, 153, 153);
808 * Creates a new border instance.
810 public MenuBarBorder()
812 // Nothing to do here.
816 * Paints the border for the component. A border is painted only if the
817 * component is a selected {@link JMenu} or an armed {@link JMenuItem}.
819 * @param c the component.
820 * @param g the graphics device.
821 * @param x the x-coordinate of the border area.
822 * @param y the y-coordinate of the border area.
823 * @param w the width of the border area.
824 * @param h the height of the border area.
826 public void paintBorder(Component c, Graphics g, int x, int y, int w,
827 int h)
829 g.setColor(borderColor);
830 g.drawLine(x, y + h - 1, x + w, y + h - 1);
834 * Returns the border insets.
836 * @param c the component (ignored).
838 * @return The border insets.
840 public Insets getBorderInsets(Component c)
842 return borderInsets;
846 * Populates <code>insets</code> with the border insets, then returns it.
848 * @param c the component (ignored).
849 * @param insets the object to populate with the border insets.
851 * @return The border insets.
853 * @throws NullPointerException if <code>insets</code> is <code>null</code>.
855 public Insets getBorderInsets(Component c, Insets insets)
857 insets.left = borderInsets.left;
858 insets.top = borderInsets.top;
859 insets.bottom = borderInsets.bottom;
860 insets.right = borderInsets.right;
861 return insets;
866 * A border for {@link JScrollPane} components.
868 public static class ScrollPaneBorder
869 extends AbstractBorder
870 implements UIResource
872 /** The border insets. */
873 private static Insets insets = new Insets(1, 1, 2, 2);
876 * Constructs a new ScrollPaneBorder.
878 public ScrollPaneBorder()
880 // Nothing to do here.
884 * Returns the insets of the border for the Component <code>c</code>.
886 * @param c the Component for which we return the border insets
888 public Insets getBorderInsets(Component c)
890 return insets;
894 * Paints the border.
896 * @param c the Component for which the border is painted
897 * @param g the Graphics context
898 * @param x the X coordinate of the upper left corner of the border
899 * @param y the Y coordinate of the upper left corner of the border
900 * @param w the width of the border
901 * @param h the height of the border
903 public void paintBorder(Component c, Graphics g, int x, int y,
904 int w, int h)
906 Color darkShadow = MetalLookAndFeel.getControlDarkShadow();
907 Color shadow = MetalLookAndFeel.getControlShadow();
908 Color light = MetalLookAndFeel.getWhite();
909 Color middle = MetalLookAndFeel.getControl();
911 // paint top border line
912 g.setColor(darkShadow);
913 g.drawLine(x, y, x + w - 2, y);
915 // paint left border line
916 g.drawLine(x, y, x, y + h - 2);
918 // paint right inner border line
919 g.drawLine(x + w - 2, y, x + w - 2, y + h + 1);
921 // paint bottom inner border line
922 g.drawLine(x + 2, y + h - 2, x + w - 2, y + h - 2);
924 // draw right outer border line
925 g.setColor(light);
926 g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);
928 // draw bottom outer border line
929 g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);
931 // paint the lighter points
932 g.setColor(middle);
933 g.drawLine(x + w - 1, y, x + w - 1, y);
934 g.drawLine(x + w - 2, y + 2, x + w - 2, y + 2);
935 g.drawLine(x, y + h - 1, x, y + h - 1);
936 g.drawLine(x + 1, y + h - 2, x + 1, y + h - 2);
943 * A button border that is only visible when the mouse pointer is within
944 * the button's bounds.
946 public static class RolloverButtonBorder
947 extends MetalBorders.ButtonBorder
950 * Creates a new border instance.
952 public RolloverButtonBorder()
954 // Nothing to do here.
958 * Paints the border.
960 * @param c the component.
961 * @param g the graphics device.
962 * @param x the x-coordinate.
963 * @param y the y-coordinate.
964 * @param w the width.
965 * @param h the height.
967 public void paintBorder(Component c, Graphics g, int x, int y, int w,
968 int h)
970 boolean mouseIsOver = false;
971 if (c instanceof AbstractButton)
973 ButtonModel bmodel = ((AbstractButton) c).getModel();
974 mouseIsOver = bmodel.isRollover();
976 if (mouseIsOver)
977 super.paintBorder(c, g, x, y, w, h);
982 * This border is used in Toolbar buttons as inner border.
984 static class RolloverMarginBorder extends AbstractBorder
986 /** The borders insets. */
987 protected static Insets borderInsets = new Insets(3, 3, 3, 3);
990 * Creates a new instance of RolloverBorder.
992 public RolloverMarginBorder()
994 // Nothing to do here.
998 * Returns the insets of the RolloverBorder.
1000 * @param c the component for which the border is used
1002 * @return the insets of the RolloverBorder
1004 public Insets getBorderInsets(Component c)
1006 return getBorderInsets(c, null);
1010 * Returns the insets of the RolloverMarginBorder in the specified
1011 * Insets object.
1013 * @param c the component for which the border is used
1014 * @param newInsets the insets object where to put the values
1016 * @return the insets of the RolloverMarginBorder
1018 public Insets getBorderInsets(Component c, Insets newInsets)
1020 if (newInsets == null)
1021 newInsets = new Insets(0, 0, 0, 0);
1023 AbstractButton b = (AbstractButton) c;
1024 Insets margin = b.getMargin();
1025 newInsets.bottom = borderInsets.bottom;
1026 newInsets.left = borderInsets.left;
1027 newInsets.right = borderInsets.right;
1028 newInsets.top = borderInsets.top;
1029 return newInsets;
1034 * A border implementation for popup menus.
1036 public static class PopupMenuBorder
1037 extends AbstractBorder
1038 implements UIResource
1041 /** The border's insets. */
1042 protected static Insets borderInsets = new Insets(3, 1, 2, 1);
1045 * Constructs a new PopupMenuBorder.
1047 public PopupMenuBorder()
1049 // Nothing to do here.
1053 * Returns the insets of the border, creating a new Insets instance
1054 * with each call.
1056 * @param c the component for which we return the border insets
1057 * (not used here)
1059 public Insets getBorderInsets(Component c)
1061 return getBorderInsets(c, null);
1065 * Returns the insets of the border, using the supplied Insets instance.
1067 * @param c the component for which we return the border insets
1068 * (not used here)
1069 * @param i the Insets instance to fill with the Insets values
1071 public Insets getBorderInsets(Component c, Insets i)
1073 Insets insets;
1074 if (i == null)
1075 insets = new Insets(borderInsets.top, borderInsets.left,
1076 borderInsets.bottom, borderInsets.right);
1077 else
1079 insets = i;
1080 insets.top = borderInsets.top;
1081 insets.left = borderInsets.left;
1082 insets.bottom = borderInsets.bottom;
1083 insets.right = borderInsets.right;
1086 return insets;
1090 * Paints the border for component <code>c</code> using the
1091 * Graphics context <code>g</code> with the dimension
1092 * <code>x, y, w, h</code>.
1094 * @param c the component for which we paint the border
1095 * @param g the Graphics context to use
1096 * @param x the X coordinate of the upper left corner of c
1097 * @param y the Y coordinate of the upper left corner of c
1098 * @param w the width of c
1099 * @param h the height of c
1101 public void paintBorder(Component c, Graphics g, int x, int y, int w,
1102 int h)
1104 Color darkShadow = MetalLookAndFeel.getPrimaryControlDarkShadow();
1105 Color light = MetalLookAndFeel.getPrimaryControlHighlight();
1107 // draw dark outer border
1108 g.setColor(darkShadow);
1109 g.drawRect(x, y, w - 1, h - 1);
1111 // draw highlighted inner border (only top and left)
1112 g.setColor(light);
1113 g.drawLine(x + 1, y + 1, x + w - 2, y + 1);
1119 * A border used for the {@link JToggleButton} component.
1121 * @since 1.3
1123 public static class ToggleButtonBorder
1124 extends ButtonBorder
1127 * Creates a new border instance.
1129 public ToggleButtonBorder()
1131 // Nothing to do here.
1135 * Paints the toggle button border.
1137 * @param c the component for which we paint the border
1138 * @param g the Graphics context to use
1139 * @param x the X coordinate of the upper left corner of c
1140 * @param y the Y coordinate of the upper left corner of c
1141 * @param w the width of c
1142 * @param h the height of c
1144 public void paintBorder(Component c, Graphics g, int x, int y, int w,
1145 int h)
1147 ButtonModel bmodel = null;
1149 if (c instanceof AbstractButton)
1150 bmodel = ((AbstractButton) c).getModel();
1152 Color darkShadow = MetalLookAndFeel.getControlDarkShadow();
1153 Color shadow = MetalLookAndFeel.getControlShadow();
1154 Color light = MetalLookAndFeel.getWhite();
1155 Color middle = MetalLookAndFeel.getControl();
1157 if (c.isEnabled())
1159 // draw dark border
1160 g.setColor(darkShadow);
1161 g.drawRect(x, y, w - 2, h - 2);
1163 if (!bmodel.isArmed())
1165 // draw light border
1166 g.setColor(light);
1167 g.drawLine(x + 1, y + h - 1, x + w - 1, y + h - 1);
1168 g.drawLine(x + w - 1, y + 1, x + w - 1, y + h - 1);
1169 if (bmodel.isSelected())
1170 g.setColor(middle);
1171 g.drawLine(x + 1, y + 1, x + w - 3, y + 1);
1172 g.drawLine(x + 1, y + 1, x + 1, y + h - 3);
1174 // draw crossing pixels of both borders
1175 g.setColor(shadow);
1176 g.drawLine(x + 1, y + h - 2, x + 1, y + h - 2);
1177 g.drawLine(x + w - 2, y + 1, x + w - 2, y + 1);
1179 else
1181 // draw light border
1182 g.setColor(light);
1183 g.drawLine(x + w - 1, y + 1, x + w - 1, y + h - 1);
1184 g.drawLine(x + 1, y + h - 1, x + w - 1, y + h - 1);
1186 // draw shadow border
1187 g.setColor(shadow);
1188 g.drawLine(x + 1, y + 1, x + w - 2, y + 1);
1189 g.drawLine(x + 1, y + 1, x + 1, y + h - 2);
1191 // draw crossing pixels of both borders
1192 g.setColor(shadow);
1193 g.drawLine(x + 1, y + h - 2, x + 1, y + h - 2);
1194 g.drawLine(x + w - 2, y + 1, x + w - 2, y + 1);
1197 // draw corners
1198 g.setColor(middle);
1199 g.drawLine(x, y + h - 1, x, y + h - 1);
1200 g.drawLine(x + w - 1, y, x + w - 1, y);
1202 else
1204 // draw disabled border
1205 g.setColor(MetalLookAndFeel.getControlDisabled());
1206 g.drawRect(x, y, w - 2, h - 2);
1212 * A border used for the {@link JToolBar} component.
1214 public static class ToolBarBorder extends AbstractBorder
1215 implements UIResource, SwingConstants
1218 * Creates a new border instance.
1220 public ToolBarBorder()
1222 // Nothing to do here.
1226 * Returns the border insets.
1228 * @param c the component (ignored).
1230 * @return The border insets.
1232 public Insets getBorderInsets(Component c)
1234 return getBorderInsets(c, null);
1238 * Returns the border insets.
1240 * @param c the component (ignored).
1241 * @return The border insets.
1243 public Insets getBorderInsets(Component c, Insets newInsets)
1245 JToolBar tb = (JToolBar) c;
1246 if (tb.getOrientation() == JToolBar.HORIZONTAL)
1248 if (newInsets == null)
1249 newInsets = new Insets(2, 16, 2, 2);
1250 else
1252 newInsets.top = 2;
1253 newInsets.left = 16;
1254 newInsets.bottom = 2;
1255 newInsets.right = 2;
1257 return newInsets;
1259 else // assume JToolBar.VERTICAL
1261 if (newInsets == null)
1262 newInsets = new Insets(16, 2, 2, 2);
1263 else
1265 newInsets.top = 16;
1266 newInsets.left = 2;
1267 newInsets.bottom = 2;
1268 newInsets.right = 2;
1270 return newInsets;
1276 * Paints the border for the specified component.
1278 * @param c the component.
1279 * @param g the graphics device.
1280 * @param x the x-coordinate.
1281 * @param y the y-coordinate.
1282 * @param w the width.
1283 * @param h the height.
1285 public void paintBorder(Component c, Graphics g, int x, int y, int w,
1286 int h)
1289 JToolBar tb = (JToolBar) c;
1290 if (tb.getOrientation() == JToolBar.HORIZONTAL)
1292 MetalUtils.fillMetalPattern(tb, g, x + 2, y + 2, x + 11, y + h - 5,
1293 MetalLookAndFeel.getControlHighlight(),
1294 MetalLookAndFeel.getControlDarkShadow());
1296 else
1298 MetalUtils.fillMetalPattern(tb, g, x + 2, y + 2, x + w - 5, y + 11,
1299 MetalLookAndFeel.getControlHighlight(),
1300 MetalLookAndFeel.getControlDarkShadow());
1307 * A border for table header cells.
1309 * @since 1.3
1311 public static class TableHeaderBorder extends AbstractBorder
1314 * The insets of this border.
1316 // TODO: According to tests that I have done, this is really the border
1317 // that should be returned by getBorderInsets(). However, the name
1318 // is very distracting. Is there any deeper meaning in it?
1319 protected Insets editorBorderInsets;
1322 * Creates a new instance of <code>TableHeaderBorder</code>.
1324 public TableHeaderBorder()
1326 editorBorderInsets = new Insets(1, 1, 1, 1);
1330 * Return the insets of this border.
1332 * @return the insets of this border
1334 public Insets getBorderInsets(Component c)
1336 return editorBorderInsets;
1340 * Paints the border.
1342 * @param c the component for which to paint the border
1343 * @param g the graphics context to use
1344 * @param x the x cooridinate of the border rectangle
1345 * @param y the y cooridinate of the border rectangle
1346 * @param w the width of the border rectangle
1347 * @param h the height of the border rectangle
1349 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h)
1351 Color dark = MetalLookAndFeel.getControlDarkShadow();
1352 Color light = MetalLookAndFeel.getWhite();
1353 Color old = g.getColor();
1354 g.setColor(light);
1355 g.drawLine(x, y, x + w - 2, y);
1356 g.drawLine(x, y, x, y + h - 2);
1357 g.setColor(dark);
1358 g.drawLine(x + w - 1, y, x + w - 1, y + h - 1);
1359 g.drawLine(x + 1, y + h - 1, x + w - 1, y + h - 1);
1360 g.setColor(old);
1365 * Returns a border for Swing buttons in the Metal Look &amp; Feel.
1367 * @return a border for Swing buttons in the Metal Look &amp; Feel
1369 public static Border getButtonBorder()
1371 if (buttonBorder == null)
1373 Border outer = new ButtonBorder();
1374 Border inner = getMarginBorder();
1375 buttonBorder = new BorderUIResource.CompoundBorderUIResource
1376 (outer, inner);
1378 return buttonBorder;
1382 * Returns a border for use with {@link JToggleButton} components.
1384 * @return A border.
1386 * @since 1.3
1388 public static Border getToggleButtonBorder()
1390 if (toggleButtonBorder == null)
1392 Border outer = new ToggleButtonBorder();
1393 Border inner = getMarginBorder();
1394 toggleButtonBorder = new BorderUIResource.CompoundBorderUIResource
1395 (outer, inner);
1397 return toggleButtonBorder;
1401 * Returns a border instance that is used with a {@link JInternalFrame} when
1402 * it is in the iconified state.
1404 * @return A border.
1406 * @since 1.3
1408 public static Border getDesktopIconBorder()
1410 if (desktopIconBorder == null)
1411 desktopIconBorder = new DesktopIconBorder();
1412 return desktopIconBorder;
1416 * Returns a border for use by the {@link JTextField} component.
1418 * @return A border.
1420 * @since 1.3
1422 public static Border getTextFieldBorder()
1424 if (textFieldBorder == null)
1426 Border inner = getMarginBorder();
1427 Border outer = new TextFieldBorder();
1428 textFieldBorder =
1429 new BorderUIResource.CompoundBorderUIResource(outer, inner);
1431 return textFieldBorder;
1435 * Returns the border that is used for text components (except text fields,
1436 * which use {@link #getTextFieldBorder}.
1438 * @return the border that is used for text components
1440 * @since 1.3
1442 public static Border getTextBorder()
1444 if (textBorder == null)
1446 Border inner = getMarginBorder();
1447 Border outer = new Flush3DBorder();
1448 textBorder =
1449 new BorderUIResource.CompoundBorderUIResource(outer, inner);
1451 return textBorder;
1455 * Returns a border for Toolbar buttons in the Metal Look &amp; Feel.
1457 * @return a border for Toolbar buttons in the Metal Look &amp; Feel
1459 static Border getToolbarButtonBorder()
1461 if (toolbarButtonBorder == null)
1463 Border outer = new ButtonBorder();
1464 Border inner = new RolloverMarginBorder();
1465 toolbarButtonBorder = new BorderUIResource.CompoundBorderUIResource
1466 (outer, inner);
1468 return toolbarButtonBorder;
1472 * Returns a shared instance of {@link BasicBorders.MarginBorder}.
1474 * @return a shared instance of {@link BasicBorders.MarginBorder}
1476 static Border getMarginBorder()
1478 if (marginBorder == null)
1479 marginBorder = new BasicBorders.MarginBorder();
1480 return marginBorder;
1484 * Returns a shared instance of a compound border for rollover buttons.
1486 * @return A shared border instance.
1488 static Border getRolloverBorder()
1490 if (rolloverBorder == null)
1492 Border outer = new MetalBorders.RolloverButtonBorder();
1493 Border inner = MetalBorders.getMarginBorder();
1494 rolloverBorder = new BorderUIResource.CompoundBorderUIResource(outer,
1495 inner);
1497 return rolloverBorder;