1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.aequo.gui;
14
15 import java.awt.BasicStroke;
16 import java.awt.Component;
17 import java.awt.Graphics;
18 import java.awt.Graphics2D;
19 import java.awt.Insets;
20 import java.awt.Stroke;
21
22 import javax.swing.border.AbstractBorder;
23
24
25
26
27
28
29 public class RowFocusBorder extends AbstractBorder {
30
31
32 protected static BasicStroke stroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, new float[]{2.0f}, 1.0f);
33
34
35 protected int leftMargin;
36
37
38 protected int rightMargin;
39
40
41 protected int topMargin;
42
43
44 protected int bottomMargin;
45
46
47 protected boolean focused = false;
48
49
50 public static int FIRST_CELL_IN_ROW = 1;
51
52
53 public static int ONLY_CELL_IN_ROW = 2;
54
55
56 public static int LAST_CELL_IN_ROW = 3;
57
58
59 public static int MIDDLE_CELL_IN_ROW = 4;
60
61
62
63
64
65
66
67
68 protected int cellType = ONLY_CELL_IN_ROW;
69
70
71
72
73 public RowFocusBorder() {
74 }
75
76
77
78
79
80 public void setFocused(boolean focused) {
81 this.focused = focused;
82 }
83
84
85
86
87
88 public void setLeftMargin(int pixels) {
89 leftMargin = pixels;
90 }
91
92
93
94
95
96 public void setRightMargin(int pixels) {
97 rightMargin = pixels;
98 }
99
100
101
102
103
104 public void setTopMargin(int pixels) {
105 topMargin = pixels;
106 }
107
108
109
110
111
112 public void setBottomMargin(int pixels) {
113 bottomMargin = pixels;
114 }
115
116
117
118
119
120 public void setCellType(int cellType) {
121 this.cellType = cellType;
122 }
123
124
125
126
127
128
129 public Insets getBorderInsets(Component c) {
130 return new Insets(topMargin + 1, leftMargin + 1, bottomMargin + 1, rightMargin + 1);
131 }
132
133
134
135
136
137
138
139 public Insets getBorderInsets(Component c, Insets insets) {
140 insets.left = leftMargin + 1;
141 insets.top = topMargin + 1;
142 insets.right = rightMargin + 1;
143 insets.bottom = bottomMargin + 1;
144 return insets;
145 }
146
147
148
149
150
151 public boolean isBorderOpaque() { return false; }
152
153
154
155
156
157
158
159
160
161
162
163 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
164 if (focused) {
165 int w = width - 1;
166 int h = height - 1;
167
168 Graphics2D g2 = (Graphics2D)g;
169
170 g.translate(x, y);
171
172 Stroke oldStroke = g2.getStroke();
173 g2.setStroke(stroke);
174
175 g.setColor(c.getBackground().brighter());
176
177 if (cellType == FIRST_CELL_IN_ROW || cellType == ONLY_CELL_IN_ROW) {
178 g.drawLine(0, 0, 0, h);
179 }
180 if (cellType == LAST_CELL_IN_ROW || cellType == ONLY_CELL_IN_ROW) {
181 g.drawLine(w, 0, w, h);
182 }
183 g.drawLine(0, 0, w, 0);
184 g.drawLine(0, h, w, h);
185
186 g2.setStroke(oldStroke);
187 g.translate(-x, -y);
188 }
189 }
190
191 }