1 |
772 |
jeremybenn |
/* CSSBorder.java -- A border for rendering CSS border styles
|
2 |
|
|
Copyright (C) 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
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.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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.
|
20 |
|
|
|
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.
|
25 |
|
|
|
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. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package javax.swing.text.html;
|
40 |
|
|
|
41 |
|
|
import gnu.javax.swing.text.html.css.BorderWidth;
|
42 |
|
|
import gnu.javax.swing.text.html.css.CSSColor;
|
43 |
|
|
|
44 |
|
|
import java.awt.Color;
|
45 |
|
|
import java.awt.Component;
|
46 |
|
|
import java.awt.Graphics;
|
47 |
|
|
import java.awt.Insets;
|
48 |
|
|
|
49 |
|
|
import javax.swing.border.Border;
|
50 |
|
|
import javax.swing.text.AttributeSet;
|
51 |
|
|
|
52 |
|
|
/**
|
53 |
|
|
* A border implementation to render CSS border styles.
|
54 |
|
|
*/
|
55 |
|
|
class CSSBorder
|
56 |
|
|
implements Border
|
57 |
|
|
{
|
58 |
|
|
|
59 |
|
|
/**
|
60 |
|
|
* The CSS border styles.
|
61 |
|
|
*/
|
62 |
|
|
|
63 |
|
|
private static final int STYLE_NOT_SET = -1;
|
64 |
|
|
private static final int STYLE_NONE = 0;
|
65 |
|
|
private static final int STYLE_HIDDEN = 1;
|
66 |
|
|
private static final int STYLE_DOTTED = 2;
|
67 |
|
|
private static final int STYLE_DASHED = 3;
|
68 |
|
|
private static final int STYLE_SOLID = 4;
|
69 |
|
|
private static final int STYLE_DOUBLE = 5;
|
70 |
|
|
private static final int STYLE_GROOVE = 6;
|
71 |
|
|
private static final int STYLE_RIDGE = 7;
|
72 |
|
|
private static final int STYLE_INSET = 8;
|
73 |
|
|
private static final int STYLE_OUTSET = 9;
|
74 |
|
|
|
75 |
|
|
/**
|
76 |
|
|
* The left insets.
|
77 |
|
|
*/
|
78 |
|
|
private int left;
|
79 |
|
|
|
80 |
|
|
/**
|
81 |
|
|
* The right insets.
|
82 |
|
|
*/
|
83 |
|
|
private int right;
|
84 |
|
|
|
85 |
|
|
/**
|
86 |
|
|
* The top insets.
|
87 |
|
|
*/
|
88 |
|
|
private int top;
|
89 |
|
|
|
90 |
|
|
/**
|
91 |
|
|
* The bottom insets.
|
92 |
|
|
*/
|
93 |
|
|
private int bottom;
|
94 |
|
|
|
95 |
|
|
/**
|
96 |
|
|
* The border style on the left.
|
97 |
|
|
*/
|
98 |
|
|
private int leftStyle;
|
99 |
|
|
|
100 |
|
|
/**
|
101 |
|
|
* The border style on the right.
|
102 |
|
|
*/
|
103 |
|
|
private int rightStyle;
|
104 |
|
|
|
105 |
|
|
/**
|
106 |
|
|
* The border style on the top.
|
107 |
|
|
*/
|
108 |
|
|
private int topStyle;
|
109 |
|
|
|
110 |
|
|
/**
|
111 |
|
|
* The color for the top border.
|
112 |
|
|
*/
|
113 |
|
|
private Color topColor;
|
114 |
|
|
|
115 |
|
|
/**
|
116 |
|
|
* The color for the bottom border.
|
117 |
|
|
*/
|
118 |
|
|
private Color bottomColor;
|
119 |
|
|
|
120 |
|
|
/**
|
121 |
|
|
* The color for the left border.
|
122 |
|
|
*/
|
123 |
|
|
private Color leftColor;
|
124 |
|
|
|
125 |
|
|
/**
|
126 |
|
|
* The color for the right border.
|
127 |
|
|
*/
|
128 |
|
|
private Color rightColor;
|
129 |
|
|
|
130 |
|
|
/**
|
131 |
|
|
* The border style on the bottom.
|
132 |
|
|
*/
|
133 |
|
|
private int bottomStyle;
|
134 |
|
|
|
135 |
|
|
/**
|
136 |
|
|
* Creates a new CSS border and fetches its attributes from the specified
|
137 |
|
|
* attribute set.
|
138 |
|
|
*
|
139 |
|
|
* @param atts the attribute set that contains the border spec
|
140 |
|
|
*/
|
141 |
|
|
CSSBorder(AttributeSet atts, StyleSheet ss)
|
142 |
|
|
{
|
143 |
|
|
// Determine the border styles.
|
144 |
|
|
int style = getBorderStyle(atts, CSS.Attribute.BORDER_STYLE);
|
145 |
|
|
if (style == STYLE_NOT_SET)
|
146 |
|
|
style = STYLE_NONE; // Default to none.
|
147 |
|
|
topStyle = bottomStyle = leftStyle = rightStyle = style;
|
148 |
|
|
style = getBorderStyle(atts, CSS.Attribute.BORDER_TOP_STYLE);
|
149 |
|
|
if (style != STYLE_NOT_SET)
|
150 |
|
|
topStyle = style;
|
151 |
|
|
style = getBorderStyle(atts, CSS.Attribute.BORDER_BOTTOM_STYLE);
|
152 |
|
|
if (style != STYLE_NOT_SET)
|
153 |
|
|
bottomStyle = style;
|
154 |
|
|
style = getBorderStyle(atts, CSS.Attribute.BORDER_LEFT_STYLE);
|
155 |
|
|
if (style != STYLE_NOT_SET)
|
156 |
|
|
leftStyle = style;
|
157 |
|
|
style = getBorderStyle(atts, CSS.Attribute.BORDER_RIGHT_STYLE);
|
158 |
|
|
if (style != STYLE_NOT_SET)
|
159 |
|
|
rightStyle = style;
|
160 |
|
|
|
161 |
|
|
// Determine the border colors.
|
162 |
|
|
Color color = getBorderColor(atts, CSS.Attribute.BORDER_COLOR);
|
163 |
|
|
if (color == null)
|
164 |
|
|
color = Color.BLACK;
|
165 |
|
|
topColor = bottomColor = leftColor = rightColor = color;
|
166 |
|
|
color = getBorderColor(atts, CSS.Attribute.BORDER_TOP_COLOR);
|
167 |
|
|
if (color != null)
|
168 |
|
|
topColor = color;
|
169 |
|
|
color = getBorderColor(atts, CSS.Attribute.BORDER_BOTTOM_COLOR);
|
170 |
|
|
if (color != null)
|
171 |
|
|
bottomColor = color;
|
172 |
|
|
color = getBorderColor(atts, CSS.Attribute.BORDER_LEFT_COLOR);
|
173 |
|
|
if (color != null)
|
174 |
|
|
leftColor = color;
|
175 |
|
|
color = getBorderColor(atts, CSS.Attribute.BORDER_RIGHT_COLOR);
|
176 |
|
|
if (color != null)
|
177 |
|
|
rightColor = color;
|
178 |
|
|
|
179 |
|
|
// Determine the border widths.
|
180 |
|
|
int width = getBorderWidth(atts, CSS.Attribute.BORDER_WIDTH, ss);
|
181 |
|
|
if (width == -1)
|
182 |
|
|
width = 0;
|
183 |
|
|
top = bottom = left = right = width;
|
184 |
|
|
width = getBorderWidth(atts, CSS.Attribute.BORDER_TOP_WIDTH, ss);
|
185 |
|
|
if (width >= 0)
|
186 |
|
|
top = width;
|
187 |
|
|
width = getBorderWidth(atts, CSS.Attribute.BORDER_BOTTOM_WIDTH, ss);
|
188 |
|
|
if (width >= 0)
|
189 |
|
|
bottom = width;
|
190 |
|
|
width = getBorderWidth(atts, CSS.Attribute.BORDER_LEFT_WIDTH, ss);
|
191 |
|
|
if (width >= 0)
|
192 |
|
|
left = width;
|
193 |
|
|
width = getBorderWidth(atts, CSS.Attribute.BORDER_RIGHT_WIDTH, ss);
|
194 |
|
|
if (width >= 0)
|
195 |
|
|
right = width;
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
/**
|
199 |
|
|
* Determines the border style for a given CSS attribute.
|
200 |
|
|
*
|
201 |
|
|
* @param atts the attribute set
|
202 |
|
|
* @param key the CSS key
|
203 |
|
|
*
|
204 |
|
|
* @return the border style according to the constants defined in this class
|
205 |
|
|
*/
|
206 |
|
|
private int getBorderStyle(AttributeSet atts, CSS.Attribute key)
|
207 |
|
|
{
|
208 |
|
|
int style = STYLE_NOT_SET;
|
209 |
|
|
Object o = atts.getAttribute(key);
|
210 |
|
|
if (o != null)
|
211 |
|
|
{
|
212 |
|
|
String cssStyle = o.toString();
|
213 |
|
|
if (cssStyle.equals("none"))
|
214 |
|
|
style = STYLE_NONE;
|
215 |
|
|
else if (cssStyle.equals("hidden"))
|
216 |
|
|
style = STYLE_HIDDEN;
|
217 |
|
|
else if (cssStyle.equals("dotted"))
|
218 |
|
|
style = STYLE_DOTTED;
|
219 |
|
|
else if (cssStyle.equals("dashed"))
|
220 |
|
|
style = STYLE_DASHED;
|
221 |
|
|
else if (cssStyle.equals("solid"))
|
222 |
|
|
style = STYLE_SOLID;
|
223 |
|
|
else if (cssStyle.equals("double"))
|
224 |
|
|
style = STYLE_DOUBLE;
|
225 |
|
|
else if (cssStyle.equals("groove"))
|
226 |
|
|
style = STYLE_GROOVE;
|
227 |
|
|
else if (cssStyle.equals("ridge"))
|
228 |
|
|
style = STYLE_RIDGE;
|
229 |
|
|
else if (cssStyle.equals("inset"))
|
230 |
|
|
style = STYLE_INSET;
|
231 |
|
|
else if (cssStyle.equals("outset"))
|
232 |
|
|
style = STYLE_OUTSET;
|
233 |
|
|
}
|
234 |
|
|
return style;
|
235 |
|
|
}
|
236 |
|
|
|
237 |
|
|
/**
|
238 |
|
|
* Determines the border color for the specified key.
|
239 |
|
|
*
|
240 |
|
|
* @param atts the attribute set from which to fetch the color
|
241 |
|
|
* @param key the CSS key
|
242 |
|
|
*
|
243 |
|
|
* @return the border color
|
244 |
|
|
*/
|
245 |
|
|
private Color getBorderColor(AttributeSet atts, CSS.Attribute key)
|
246 |
|
|
{
|
247 |
|
|
Object o = atts.getAttribute(key);
|
248 |
|
|
Color color = null;
|
249 |
|
|
if (o instanceof CSSColor)
|
250 |
|
|
{
|
251 |
|
|
CSSColor cssColor = (CSSColor) o;
|
252 |
|
|
color = cssColor.getValue();
|
253 |
|
|
}
|
254 |
|
|
return color;
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
/**
|
258 |
|
|
* Returns the width for the specified key.
|
259 |
|
|
*
|
260 |
|
|
* @param atts the attributes to fetch the width from
|
261 |
|
|
* @param key the CSS key
|
262 |
|
|
*
|
263 |
|
|
* @return the width, or -1 of none has been set
|
264 |
|
|
*/
|
265 |
|
|
private int getBorderWidth(AttributeSet atts, CSS.Attribute key,
|
266 |
|
|
StyleSheet ss)
|
267 |
|
|
{
|
268 |
|
|
int width = -1;
|
269 |
|
|
Object o = atts.getAttribute(key);
|
270 |
|
|
if (o instanceof BorderWidth)
|
271 |
|
|
{
|
272 |
|
|
BorderWidth w = (BorderWidth) o;
|
273 |
|
|
w.setFontBases(ss.getEMBase(atts), ss.getEXBase(atts));
|
274 |
|
|
width = (int) ((BorderWidth) o).getValue();
|
275 |
|
|
}
|
276 |
|
|
return width;
|
277 |
|
|
}
|
278 |
|
|
|
279 |
|
|
/**
|
280 |
|
|
* Returns the border insets.
|
281 |
|
|
*/
|
282 |
|
|
public Insets getBorderInsets(Component c)
|
283 |
|
|
{
|
284 |
|
|
return new Insets(top, left, bottom, right);
|
285 |
|
|
}
|
286 |
|
|
|
287 |
|
|
/**
|
288 |
|
|
* CSS borders are generally opaque so return true here.
|
289 |
|
|
*/
|
290 |
|
|
public boolean isBorderOpaque()
|
291 |
|
|
{
|
292 |
|
|
return true;
|
293 |
|
|
}
|
294 |
|
|
|
295 |
|
|
public void paintBorder(Component c, Graphics g, int x, int y, int width,
|
296 |
|
|
int height)
|
297 |
|
|
{
|
298 |
|
|
// Top border.
|
299 |
|
|
paintBorderLine(g, x, y + top / 2, x + width, y + top / 2, topStyle, top,
|
300 |
|
|
topColor, false);
|
301 |
|
|
// Left border.
|
302 |
|
|
paintBorderLine(g, x + left / 2, y, x + left / 2, y + height, leftStyle,
|
303 |
|
|
left, leftColor, true);
|
304 |
|
|
// Bottom border.
|
305 |
|
|
paintBorderLine(g, x, y + height - bottom / 2, x + width,
|
306 |
|
|
y + height - bottom / 2, topStyle, bottom, bottomColor,
|
307 |
|
|
false);
|
308 |
|
|
// Right border.
|
309 |
|
|
paintBorderLine(g, x + width - right / 2, y, x + width - right / 2,
|
310 |
|
|
y + height, topStyle, right, rightColor, true);
|
311 |
|
|
|
312 |
|
|
}
|
313 |
|
|
|
314 |
|
|
private void paintBorderLine(Graphics g, int x1, int y1, int x2, int y2,
|
315 |
|
|
int style, int width, Color color,
|
316 |
|
|
boolean vertical)
|
317 |
|
|
{
|
318 |
|
|
switch (style)
|
319 |
|
|
{
|
320 |
|
|
case STYLE_DOTTED:
|
321 |
|
|
paintDottedLine(g, x1, y1, x2, y2, width, color, vertical);
|
322 |
|
|
break;
|
323 |
|
|
case STYLE_DASHED:
|
324 |
|
|
paintDashedLine(g, x1, y1, x2, y2, width, color, vertical);
|
325 |
|
|
break;
|
326 |
|
|
case STYLE_SOLID:
|
327 |
|
|
paintSolidLine(g, x1, y1, x2, y2, width, color, vertical);
|
328 |
|
|
break;
|
329 |
|
|
case STYLE_DOUBLE:
|
330 |
|
|
paintDoubleLine(g, x1, y1, x2, y2, width, color, vertical);
|
331 |
|
|
break;
|
332 |
|
|
case STYLE_GROOVE:
|
333 |
|
|
paintGrooveLine(g, x1, y1, x2, y2, width, color, vertical);
|
334 |
|
|
break;
|
335 |
|
|
case STYLE_RIDGE:
|
336 |
|
|
paintRidgeLine(g, x1, y1, x2, y2, width, color, vertical);
|
337 |
|
|
break;
|
338 |
|
|
case STYLE_OUTSET:
|
339 |
|
|
paintOutsetLine(g, x1, y1, x2, y2, width, color, vertical);
|
340 |
|
|
break;
|
341 |
|
|
case STYLE_INSET:
|
342 |
|
|
paintInsetLine(g, x1, y1, x2, y2, width, color, vertical);
|
343 |
|
|
break;
|
344 |
|
|
case STYLE_NONE:
|
345 |
|
|
case STYLE_HIDDEN:
|
346 |
|
|
default:
|
347 |
|
|
// Nothing to do in these cases.
|
348 |
|
|
}
|
349 |
|
|
}
|
350 |
|
|
|
351 |
|
|
private void paintDottedLine(Graphics g, int x1, int y1, int x2, int y2,
|
352 |
|
|
int width, Color color, boolean vertical)
|
353 |
|
|
{
|
354 |
|
|
// FIXME: Implement this.
|
355 |
|
|
paintSolidLine(g, x1, y1, x2, y2, width, color, vertical);
|
356 |
|
|
}
|
357 |
|
|
|
358 |
|
|
private void paintDashedLine(Graphics g, int x1, int y1, int x2, int y2,
|
359 |
|
|
int width, Color color, boolean vertical)
|
360 |
|
|
{
|
361 |
|
|
// FIXME: Implement this.
|
362 |
|
|
paintSolidLine(g, x1, y1, x2, y2, width, color, vertical);
|
363 |
|
|
}
|
364 |
|
|
|
365 |
|
|
private void paintSolidLine(Graphics g, int x1, int y1, int x2, int y2,
|
366 |
|
|
int width, Color color, boolean vertical)
|
367 |
|
|
{
|
368 |
|
|
int x = Math.min(x1, x2);
|
369 |
|
|
int y = Math.min(y1, y1);
|
370 |
|
|
int w = Math.abs(x2 - x1);
|
371 |
|
|
int h = Math.abs(y2 - y1);
|
372 |
|
|
if (vertical)
|
373 |
|
|
{
|
374 |
|
|
w = width;
|
375 |
|
|
x -= width / 2;
|
376 |
|
|
}
|
377 |
|
|
else
|
378 |
|
|
{
|
379 |
|
|
h = width;
|
380 |
|
|
y -= width / 2;
|
381 |
|
|
}
|
382 |
|
|
g.setColor(color);
|
383 |
|
|
g.fillRect(x, y, w, h);
|
384 |
|
|
}
|
385 |
|
|
|
386 |
|
|
private void paintDoubleLine(Graphics g, int x1, int y1, int x2, int y2,
|
387 |
|
|
int width, Color color, boolean vertical)
|
388 |
|
|
{
|
389 |
|
|
// FIXME: Implement this.
|
390 |
|
|
paintSolidLine(g, x1, y1, x2, y2, width, color, vertical);
|
391 |
|
|
}
|
392 |
|
|
|
393 |
|
|
private void paintGrooveLine(Graphics g, int x1, int y1, int x2, int y2,
|
394 |
|
|
int width, Color color, boolean vertical)
|
395 |
|
|
{
|
396 |
|
|
// FIXME: Implement this.
|
397 |
|
|
paintSolidLine(g, x1, y1, x2, y2, width, color, vertical);
|
398 |
|
|
}
|
399 |
|
|
|
400 |
|
|
private void paintRidgeLine(Graphics g, int x1, int y1, int x2, int y2,
|
401 |
|
|
int width, Color color, boolean vertical)
|
402 |
|
|
{
|
403 |
|
|
// FIXME: Implement this.
|
404 |
|
|
paintSolidLine(g, x1, y1, x2, y2, width, color, vertical);
|
405 |
|
|
}
|
406 |
|
|
|
407 |
|
|
private void paintOutsetLine(Graphics g, int x1, int y1, int x2, int y2,
|
408 |
|
|
int width, Color color, boolean vertical)
|
409 |
|
|
{
|
410 |
|
|
// FIXME: Implement this.
|
411 |
|
|
paintSolidLine(g, x1, y1, x2, y2, width, color, vertical);
|
412 |
|
|
}
|
413 |
|
|
|
414 |
|
|
private void paintInsetLine(Graphics g, int x1, int y1, int x2, int y2,
|
415 |
|
|
int width, Color color, boolean vertical)
|
416 |
|
|
{
|
417 |
|
|
// FIXME: Implement this.
|
418 |
|
|
paintSolidLine(g, x1, y1, x2, y2, width, color, vertical);
|
419 |
|
|
}
|
420 |
|
|
|
421 |
|
|
}
|