49 |
import javax.swing.border.AbstractBorder; |
import javax.swing.border.AbstractBorder; |
50 |
import javax.swing.border.Border; |
import javax.swing.border.Border; |
51 |
import javax.swing.plaf.BorderUIResource; |
import javax.swing.plaf.BorderUIResource; |
52 |
|
import javax.swing.plaf.UIResource; |
53 |
import javax.swing.plaf.basic.BasicGraphicsUtils; |
import javax.swing.plaf.basic.BasicGraphicsUtils; |
54 |
import javax.swing.plaf.basic.BasicBorders; |
import javax.swing.plaf.basic.BasicBorders; |
55 |
|
|
62 |
public class MetalBorders |
public class MetalBorders |
63 |
{ |
{ |
64 |
|
|
65 |
|
/** The shared instance for getButtonBorder(). */ |
66 |
|
private static Border buttonBorder; |
67 |
|
|
68 |
|
/** The shared instance for getRolloverButtonBorder(). */ |
69 |
|
private static Border toolbarButtonBorder; |
70 |
|
|
71 |
/** |
/** |
72 |
* A MarginBorder that gets shared by multiple components. |
* A MarginBorder that gets shared by multiple components. |
73 |
* Created on demand by the private helper function {@link |
* Created on demand by the private helper function {@link |
74 |
* #getMarginBorder()}. |
* #getMarginBorder()}. |
75 |
*/ |
*/ |
76 |
private static BasicBorders.MarginBorder sharedMarginBorder; |
private static BasicBorders.MarginBorder marginBorder; |
77 |
|
|
78 |
/** |
/** |
79 |
* The border that is drawn around Swing buttons. |
* The border that is drawn around Swing buttons. |
80 |
*/ |
*/ |
81 |
public static class MetalButtonBorder |
public static class MetalButtonBorder |
82 |
extends AbstractBorder |
extends AbstractBorder |
83 |
|
implements UIResource |
84 |
{ |
{ |
85 |
/** The borders insets. */ |
/** The borders insets. */ |
86 |
protected static Insets borderInsets = new Insets(2, 2, 2, 2); |
protected static Insets borderInsets = new Insets(3, 3, 3, 3); |
87 |
|
|
88 |
/** |
/** |
89 |
* Creates a new instance of ButtonBorder. |
* Creates a new instance of ButtonBorder. |
174 |
if (newInsets == null) |
if (newInsets == null) |
175 |
newInsets = new Insets(0, 0, 0, 0); |
newInsets = new Insets(0, 0, 0, 0); |
176 |
|
|
177 |
|
AbstractButton b = (AbstractButton) c; |
178 |
|
newInsets.bottom = borderInsets.bottom; |
179 |
|
newInsets.left = borderInsets.left; |
180 |
|
newInsets.right = borderInsets.right; |
181 |
|
newInsets.top = borderInsets.top; |
182 |
|
return newInsets; |
183 |
|
} |
184 |
|
} |
185 |
|
|
186 |
|
/** |
187 |
|
* A border for JScrollPanes. |
188 |
|
*/ |
189 |
|
public static class ScrollPaneBorder |
190 |
|
extends AbstractBorder |
191 |
|
implements UIResource |
192 |
|
{ |
193 |
|
/** The border insets. */ |
194 |
|
private static Insets insets = new Insets(1, 1, 2, 2); |
195 |
|
|
196 |
|
/** |
197 |
|
* Constructs a new ScrollPaneBorder. |
198 |
|
*/ |
199 |
|
public ScrollPaneBorder() |
200 |
|
{ |
201 |
|
} |
202 |
|
|
203 |
|
/** |
204 |
|
* Returns the insets of the border for the Component <code>c</code>. |
205 |
|
* |
206 |
|
* @param c the Component for which we return the border insets |
207 |
|
*/ |
208 |
|
public Insets getBorderInsets(Component c) |
209 |
|
{ |
210 |
|
return insets; |
211 |
|
} |
212 |
|
|
213 |
|
/** |
214 |
|
* Paints the border. |
215 |
|
* |
216 |
|
* @param c the Component for which the border is painted |
217 |
|
* @param g the Graphics context |
218 |
|
* @param x the X coordinate of the upper left corner of the border |
219 |
|
* @param y the Y coordinate of the upper left corner of the border |
220 |
|
* @param w the width of the border |
221 |
|
* @param h the height of the border |
222 |
|
*/ |
223 |
|
public void paintBorder(Component c, Graphics g, int x, int y, |
224 |
|
int w, int h) |
225 |
|
{ |
226 |
|
Color darkShadow = MetalLookAndFeel.getControlDarkShadow(); |
227 |
|
Color shadow = MetalLookAndFeel.getControlShadow(); |
228 |
|
Color light = MetalLookAndFeel.getWhite(); |
229 |
|
Color middle = MetalLookAndFeel.getControl(); |
230 |
|
|
231 |
|
// paint top border line |
232 |
|
g.setColor(darkShadow); |
233 |
|
g.drawLine(x, y, x + w - 2, y); |
234 |
|
|
235 |
|
// paint left border line |
236 |
|
g.drawLine(x, y, x, y + h - 2); |
237 |
|
|
238 |
|
// paint right inner border line |
239 |
|
g.drawLine(x + w - 2, y, x + w - 2, y + h + 1); |
240 |
|
|
241 |
|
// paint bottom inner border line |
242 |
|
g.drawLine(x + 2, y + h - 2, x + w - 2, y + h - 2); |
243 |
|
|
244 |
|
// draw right outer border line |
245 |
|
g.setColor(light); |
246 |
|
g.drawLine(x + w - 1, y, x + w - 1, y + h - 1); |
247 |
|
|
248 |
|
// draw bottom outer border line |
249 |
|
g.drawLine(x, y + h - 1, x + w - 1, y + h - 1); |
250 |
|
|
251 |
|
// paint the lighter points |
252 |
|
g.setColor(middle); |
253 |
|
g.drawLine(x + w - 1, y, x + w - 1, y); |
254 |
|
g.drawLine(x + w - 2, y + 2, x + w - 2, y + 2); |
255 |
|
g.drawLine(x, y + h - 1, x, y + h - 1); |
256 |
|
g.drawLine(x + 1, y + h - 2, x + 1, y + h - 2); |
257 |
|
|
258 |
|
} |
259 |
|
|
260 |
|
} |
261 |
|
|
262 |
|
/** |
263 |
|
* This border is used in Toolbar buttons as inner border. |
264 |
|
*/ |
265 |
|
static class RolloverMarginBorder extends AbstractBorder |
266 |
|
{ |
267 |
|
/** The borders insets. */ |
268 |
|
protected static Insets borderInsets = new Insets(3, 3, 3, 3); |
269 |
|
|
270 |
|
/** |
271 |
|
* Creates a new instance of RolloverBorder. |
272 |
|
*/ |
273 |
|
public RolloverMarginBorder() |
274 |
|
{ |
275 |
|
} |
276 |
|
|
277 |
|
/** |
278 |
|
* Returns the insets of the RolloverBorder. |
279 |
|
* |
280 |
|
* @param c the component for which the border is used |
281 |
|
* |
282 |
|
* @return the insets of the RolloverBorder |
283 |
|
*/ |
284 |
|
public Insets getBorderInsets(Component c) |
285 |
|
{ |
286 |
|
return getBorderInsets(c, null); |
287 |
|
} |
288 |
|
|
289 |
|
/** |
290 |
|
* Returns the insets of the RolloverMarginBorder in the specified |
291 |
|
* Insets object. |
292 |
|
* |
293 |
|
* @param c the component for which the border is used |
294 |
|
* @param newInsets the insets object where to put the values |
295 |
|
* |
296 |
|
* @return the insets of the RolloverMarginBorder |
297 |
|
*/ |
298 |
|
public Insets getBorderInsets(Component c, Insets newInsets) |
299 |
|
{ |
300 |
|
if (newInsets == null) |
301 |
|
newInsets = new Insets(0, 0, 0, 0); |
302 |
|
|
303 |
|
AbstractButton b = (AbstractButton) c; |
304 |
|
Insets margin = b.getMargin(); |
305 |
newInsets.bottom = borderInsets.bottom; |
newInsets.bottom = borderInsets.bottom; |
306 |
newInsets.left = borderInsets.left; |
newInsets.left = borderInsets.left; |
307 |
newInsets.right = borderInsets.right; |
newInsets.right = borderInsets.right; |
310 |
} |
} |
311 |
} |
} |
312 |
|
|
313 |
|
/** |
314 |
|
* A border implementation for popup menus. |
315 |
|
*/ |
316 |
|
public static class PopupMenuBorder |
317 |
|
extends AbstractBorder |
318 |
|
implements UIResource |
319 |
|
{ |
320 |
|
|
321 |
|
/** The border's insets. */ |
322 |
|
protected static Insets borderInsets = new Insets(2, 2, 1, 1); |
323 |
|
|
324 |
|
/** |
325 |
|
* Constructs a new PopupMenuBorder. |
326 |
|
*/ |
327 |
|
public PopupMenuBorder() |
328 |
|
{ |
329 |
|
} |
330 |
|
|
331 |
|
/** |
332 |
|
* Returns the insets of the border, creating a new Insets instance |
333 |
|
* with each call. |
334 |
|
* |
335 |
|
* @param c the component for which we return the border insets |
336 |
|
* (not used here) |
337 |
|
*/ |
338 |
|
public Insets getBorderInsets(Component c) |
339 |
|
{ |
340 |
|
return getBorderInsets(c, null); |
341 |
|
} |
342 |
|
|
343 |
|
/** |
344 |
|
* Returns the insets of the border, using the supplied Insets instance. |
345 |
|
* |
346 |
|
* @param c the component for which we return the border insets |
347 |
|
* (not used here) |
348 |
|
* @param i the Insets instance to fill with the Insets values |
349 |
|
*/ |
350 |
|
public Insets getBorderInsets(Component c, Insets i) |
351 |
|
{ |
352 |
|
Insets insets; |
353 |
|
if (i == null) |
354 |
|
insets = new Insets(borderInsets.top, borderInsets.left, |
355 |
|
borderInsets.bottom, borderInsets.right); |
356 |
|
else |
357 |
|
{ |
358 |
|
insets = i; |
359 |
|
insets.top = borderInsets.top; |
360 |
|
insets.left = borderInsets.left; |
361 |
|
insets.bottom = borderInsets.bottom; |
362 |
|
insets.right = borderInsets.right; |
363 |
|
} |
364 |
|
|
365 |
|
return insets; |
366 |
|
} |
367 |
|
|
368 |
|
/** |
369 |
|
* Paints the border for component <code>c</code> using the |
370 |
|
* Graphics context <code>g</code> with the dimension |
371 |
|
* <code>x, y, w, h</code>. |
372 |
|
* |
373 |
|
* @param c the component for which we paint the border |
374 |
|
* @param g the Graphics context to use |
375 |
|
* @param x the X coordinate of the upper left corner of c |
376 |
|
* @param y the Y coordinate of the upper left corner of c |
377 |
|
* @param w the width of c |
378 |
|
* @param h the height of c |
379 |
|
*/ |
380 |
|
public void paintBorder(Component c, Graphics g, int x, int y, int w, |
381 |
|
int h) |
382 |
|
{ |
383 |
|
Color darkShadow = MetalLookAndFeel.getPrimaryControlDarkShadow(); |
384 |
|
Color light = MetalLookAndFeel.getPrimaryControlHighlight(); |
385 |
|
|
386 |
|
// draw dark outer border |
387 |
|
g.setColor(darkShadow); |
388 |
|
g.drawRect(x, y, w - 1, h - 1); |
389 |
|
|
390 |
|
// draw highlighted inner border (only top and left) |
391 |
|
g.setColor(light); |
392 |
|
g.drawLine(x + 1, y + 1, x + 1, y + h - 2); |
393 |
|
g.drawLine(x + 1, y + 1, x + w - 2, y + 1); |
394 |
|
} |
395 |
|
|
396 |
|
} |
397 |
|
|
398 |
/** |
/** |
399 |
* Returns a border for Swing buttons in the Metal Look & Feel. |
* Returns a border for Swing buttons in the Metal Look & Feel. |
402 |
*/ |
*/ |
403 |
public static Border getButtonBorder() |
public static Border getButtonBorder() |
404 |
{ |
{ |
405 |
Border outer = new MetalButtonBorder(); |
if (buttonBorder == null) |
406 |
Border inner = getMarginBorder(); |
{ |
407 |
|
Border outer = new MetalButtonBorder(); |
408 |
return new BorderUIResource.CompoundBorderUIResource(outer, inner); |
Border inner = getMarginBorder(); |
409 |
|
buttonBorder = new BorderUIResource.CompoundBorderUIResource |
410 |
|
(outer, inner); |
411 |
|
} |
412 |
|
return buttonBorder; |
413 |
} |
} |
414 |
|
|
415 |
/** |
/** |
416 |
* Returns a shared MarginBorder. |
* Returns a border for Toolbar buttons in the Metal Look & Feel. |
417 |
|
* |
418 |
|
* @return a border for Toolbar buttons in the Metal Look & Feel |
419 |
*/ |
*/ |
420 |
static Border getMarginBorder() // intentionally not public |
static Border getToolbarButtonBorder() |
421 |
{ |
{ |
422 |
/* Swing is not designed to be thread-safe, so there is no |
if (toolbarButtonBorder == null) |
423 |
* need to synchronize the access to the global variable. |
{ |
424 |
*/ |
Border outer = new MetalButtonBorder(); |
425 |
if (sharedMarginBorder == null) |
Border inner = new RolloverMarginBorder(); |
426 |
sharedMarginBorder = new BasicBorders.MarginBorder(); |
toolbarButtonBorder = new BorderUIResource.CompoundBorderUIResource |
427 |
|
(outer, inner); |
428 |
return sharedMarginBorder; |
} |
429 |
|
return toolbarButtonBorder; |
430 |
} |
} |
431 |
|
|
432 |
|
/** |
433 |
|
* Returns a shared instance of {@link BasicBorders.MarginBorder}. |
434 |
|
* |
435 |
|
* @return a shared instance of {@link BasicBorders.MarginBorder} |
436 |
|
*/ |
437 |
|
static Border getMarginBorder() |
438 |
|
{ |
439 |
|
if (marginBorder == null) |
440 |
|
marginBorder = new BasicBorders.MarginBorder(); |
441 |
|
return marginBorder; |
442 |
|
} |
443 |
} |
} |