| MetalPanelBorder.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * Copyright: 2005
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: MetalPanelBorder.java 2050 2010-12-09 15:31:45Z dmsmith $
21 */
22 package org.crosswire.common.swing.plaf;
23
24 import java.awt.Component;
25 import java.awt.Graphics;
26 import java.awt.Insets;
27 import javax.swing.border.AbstractBorder;
28 import javax.swing.plaf.UIResource;
29 import javax.swing.plaf.metal.MetalLookAndFeel;
30
31 /**
32 * A class that provides a border that matches MetalBorders.ScrollPaneBorder.
33 *
34 * @see gnu.lgpl.License for license details.<br>
35 * The copyright to this program is held by it's authors.
36 * @author Willie Thean [williethean at yahoo dot com]
37 */
38 public final class MetalPanelBorder extends AbstractBorder implements UIResource {
39 public static final int TOP = 1;
40 public static final int LEFT = 2;
41 public static final int BOTTOM = 4;
42 public static final int RIGHT = 8;
43
44 /**
45 * Default constructor.
46 */
47 public MetalPanelBorder() {
48 insets = new Insets(insetTop, insetLeft, insetBottom, insetRight);
49 }
50
51 /**
52 * Create a MetalPanelBorder instance where the border visbility (top, left,
53 * bottom and right border) is controlled by the bit mask
54 * <CODE>borderFlags</CODE>.
55 *
56 * @param borderFlags
57 * Match flags, a bit mask that may include TOP, LEFT, BOTTOM,
58 * and RIGHT
59 */
60 public MetalPanelBorder(int borderFlags) {
61 flags = 0 | borderFlags;
62
63 if ((flags & TOP) != TOP) {
64 insetTop = 0;
65 }
66
67 if ((flags & LEFT) != LEFT) {
68 insetLeft = 0;
69 }
70
71 if ((flags & BOTTOM) != BOTTOM) {
72 insetBottom = 0;
73 }
74
75 if ((flags & RIGHT) != RIGHT) {
76 insetRight = 0;
77 }
78
79 insets = new Insets(insetTop, insetLeft, insetBottom, insetRight);
80 }
81
82 /*
83 * (non-Javadoc)
84 *
85 * @see javax.swing.border.Border#paintBorder(java.awt.Component,
86 * java.awt.Graphics, int, int, int, int)
87 */
88 @Override
89 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
90 g.translate(x, y);
91
92 if ((flags & TOP) == TOP) {
93 g.setColor(MetalLookAndFeel.getControlDarkShadow());
94 g.drawLine(0, 0, w - 2, 0);
95 }
96
97 if ((flags & LEFT) == LEFT) {
98 g.drawLine(0, 0, 0, h - 2);
99 }
100
101 if ((flags & BOTTOM) == BOTTOM) {
102 g.drawLine(0, h - 2, w - 2, h - 2);
103 g.setColor(MetalLookAndFeel.getControlHighlight());
104 g.drawLine(1, h - 1, w - 1, h - 1);
105 }
106
107 if ((flags & RIGHT) == RIGHT) {
108 g.setColor(MetalLookAndFeel.getControlDarkShadow());
109 g.drawLine(w - 2, h - 2, w - 2, 0);
110 g.setColor(MetalLookAndFeel.getControlHighlight());
111 g.drawLine(w - 1, h - 1, w - 1, 1);
112 }
113
114 g.translate(-x, -y);
115 }
116
117 /*
118 * (non-Javadoc)
119 *
120 * @see javax.swing.border.Border#getBorderInsets(java.awt.Component)
121 */
122 @Override
123 public Insets getBorderInsets(Component c) {
124 return insets;
125 }
126
127 /**
128 * Serialization ID
129 */
130 private static final long serialVersionUID = 7929433986066846750L;
131
132 private int insetTop = 1;
133 private int insetLeft = 1;
134 private int insetBottom = 2;
135 private int insetRight = 2;
136
137 private int flags;
138
139 private Insets insets;
140 }
141