2 *******************************************************************************
3 * Copyright (C) 1997-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.demo.holiday;
10 import java.awt.Dimension;
12 import java.awt.FontMetrics;
13 import java.awt.Graphics;
14 import java.awt.Insets;
15 import java.awt.Panel;
18 * Various graphical borders. The border itself is a Panel so that it can
19 * contain other Components (i.e. it borders something). You use the
20 * HolidayBorderPanel like any other Panel: you set the layout that you prefer and
21 * add Components to it. Beware that a null layout does not obey the insets
22 * of the panel so if you use null layouts, adjust your measurements to
23 * handle the border by calling insets().
25 * @author Andy Clark, Taligent Inc.
28 public class HolidayBorderPanel extends Panel {
32 private static final long serialVersionUID = 4669213306492461159L;
36 public final static int SOLID = 0;
37 /** A raised border. */
38 public final static int RAISED = 1;
39 /** A lowered border. */
40 public final static int LOWERED = 2;
41 /** An etched in border. */
42 public final static int IN = 3;
43 /** An etched out border. */
44 public final static int OUT = 4;
46 /** Left alignment. */
47 public final static int LEFT = 0;
48 /** Center alignment. */
49 public final static int CENTER = 1;
50 /** Right alignment. */
51 public final static int RIGHT = 2;
53 /** Default style (IN). */
54 public final static int DEFAULT_STYLE = IN;
55 /** Default thickness (10). */
56 public final static int DEFAULT_THICKNESS = 10;
57 /** Default thickness for solid borders (4). */
58 public final static int DEFAULT_SOLID_THICKNESS = 4;
59 /** Default thickness for raised borders (2). */
60 public final static int DEFAULT_RAISED_THICKNESS = 2;
61 /** Default thickness for lowered borders (2). */
62 public final static int DEFAULT_LOWERED_THICKNESS = 2;
63 /** Default thickness for etched-in borders (10). */
64 public final static int DEFAULT_IN_THICKNESS = 10;
65 /** Default thickness for etched-out borders (10). */
66 public final static int DEFAULT_OUT_THICKNESS = 10;
67 /** Default gap between border and contained component (5). */
68 public final static int DEFAULT_GAP = 5;
69 /** Default color (black). Applies to SOLID and etched borders. */
70 public final static Color DEFAULT_COLOR = Color.black;
72 /** Default font (TimesRoman,PLAIN,14). Only applies to etched borders. */
73 public final static Font DEFAULT_FONT = new Font("TimesRoman", Font.PLAIN, 14);
74 /** Default alignment (LEFT). Only applies to etched borders. */
75 public final static int DEFAULT_ALIGNMENT = LEFT;
79 private int thickness;
85 private int alignment;
88 * Constructor. Makes default border.
90 public HolidayBorderPanel() {
93 style = DEFAULT_STYLE;
94 thickness = DEFAULT_THICKNESS;
96 color = DEFAULT_COLOR;
100 alignment = DEFAULT_ALIGNMENT;
105 * Constructor. Makes an etched IN border with given text caption.
107 * @param text Text caption
109 public HolidayBorderPanel(String text) {
117 * Constructor. Makes SOLID border with color and thickness given.
119 * @param color The color for the border.
120 * @param thickness The thickness of the border.
122 public HolidayBorderPanel(Color color, int thickness) {
127 this.thickness = thickness;
131 * Constructor. Makes a border of the given style with the default
132 * thickness for that style.
134 * @param style The style for this border.
136 public HolidayBorderPanel(int style) {
139 // set thickness appropriate to this style
141 case SOLID: thickness = DEFAULT_SOLID_THICKNESS; break;
142 case RAISED: thickness = DEFAULT_RAISED_THICKNESS; break;
143 case LOWERED: thickness = DEFAULT_LOWERED_THICKNESS; break;
144 case IN: thickness = DEFAULT_IN_THICKNESS; break;
145 case OUT: thickness = DEFAULT_OUT_THICKNESS; break;
147 thickness = DEFAULT_THICKNESS;
154 * Constructor. Makes border with given style and thickness.
156 * @param style The style for this border.
157 * @param thickness The thickness for this border.
159 public HolidayBorderPanel(int style, int thickness) {
163 this.thickness = thickness;
167 * Returns the insets of this panel..
169 public Insets getInsets() {
172 // adjust for text string
173 if (style == IN || style == OUT) {
174 if (text != null && text.length() > 0) {
176 // set font and get info
177 int height = getGraphics().getFontMetrics(font).getHeight();
178 if (height > thickness)
179 adjustment = height - thickness;
181 catch (Exception e) {
182 // nothing: just in case there is no graphics context
184 System.out.print("");
189 // return appropriate insets
190 int dist = thickness + gap;
191 return new Insets(dist + adjustment, dist, dist, dist);
195 * Sets the style of the border
197 * @param style The new style.
199 public HolidayBorderPanel setStyle(int style) {
201 // set the style and re-layout the panel
210 * Gets the style of the border
212 public int getStyle() {
218 * Sets the thickness of the border.
220 * @param thickness The new thickness
222 public HolidayBorderPanel setThickness(int thickness) {
225 this.thickness = thickness;
234 * Gets the thickness of the border.
236 public int getThickness() {
242 * Sets the gap between the border and the contained Component.
244 * @param gap The new gap, in pixels.
246 public HolidayBorderPanel setGap(int gap) {
258 * Gets the gap between the border and the contained Component.
260 public int getGap() {
266 * Sets the current color for SOLID borders and the caption text
267 * color for etched borders.
269 * @param color The new color.
271 public HolidayBorderPanel setColor(Color color) {
274 if (style == SOLID || style == IN || style == OUT)
281 * Gets the current color for SOLID borders and the caption
282 * text color for etched borders.
284 public Color getColor() {
290 * Sets the font. Only applies to etched borders.
292 public HolidayBorderPanel setTextFont(Font font) {
297 if (style == IN || style == OUT) {
307 * Gets the font of the text. Only applies to etched borders.
309 public Font getTextFont() {
315 * Sets the text. Only applies to etched borders.
317 * @param text The new text.
319 public HolidayBorderPanel setText(String text) {
322 if (style == IN || style == OUT) {
331 * Gets the text. Only applies to etched borders.
333 public String getText() {
339 * Sets the text alignment. Only applies to etched borders.
341 * @param alignment The new alignment.
343 public HolidayBorderPanel setAlignment(int alignment) {
345 this.alignment = alignment;
346 if (style == IN || style == OUT) {
355 * Gets the text alignment.
357 public int getAlignment() {
363 * Repaints the border.
365 * @param g The graphics context.
367 public void paint(Graphics g) {
369 // get current dimensions
370 Dimension size = getSize();
371 int width = size.width;
372 int height = size.height;
375 Color light = getBackground().brighter().brighter().brighter();
376 Color dark = getBackground().darker().darker().darker();
380 case RAISED: // 3D Border (in or out)
382 Color topleft = null;
383 Color bottomright = null;
386 if (style == RAISED) {
397 for (int i = 0; i < thickness; i++) {
398 g.drawLine(i, i, width - i - 2, i);
399 g.drawLine(i, i + 1, i, height - i - 1);
401 g.setColor(bottomright);
402 for (int i = 0; i < thickness; i++) {
403 g.drawLine(i + 1, height - i - 1, width - i - 1, height - i - 1);
404 g.drawLine(width - i - 1, i, width - i - 1, height - i - 2);
408 case IN: // Etched Border (in or out)
413 // set font and get info
414 Font oldfont = g.getFont();
416 FontMetrics fm = g.getFontMetrics();
417 int ascent = fm.getAscent();
425 // Calculate adjustment for text
427 if (text != null && text.length() > 0) {
428 if (ascent > thickness)
429 adjustment = (ascent - thickness) / 2;
432 // The adjustment is there so that we always draw the
433 // light rectangle first. Otherwise, your eye picks up
434 // the discrepancy where the light rect. passes over
436 int x = thickness / 2;
437 int y = thickness / 2 + adjustment;
438 int w = width - thickness - 1;
439 int h = height - thickness - 1 - adjustment;
443 g.drawRect(x + adjust1, y + adjust1, w, h);
445 g.drawRect(x + adjust2, y + adjust2, w, h);
447 // draw text, if applicable
448 if (text != null && text.length() > 0) {
449 // calculate drawing area
450 int fontheight = fm.getHeight();
451 int strwidth = fm.stringWidth(text);
453 int textwidth = width - 2 * (thickness + 5);
454 if (strwidth > textwidth)
455 strwidth = textwidth;
457 // calculate offset for alignment
461 offset = (width - strwidth) / 2;
464 offset = width - strwidth - thickness - 5;
467 default: // assume left alignment if invalid
468 offset = thickness + 5;
472 // clear drawing area and set clipping region
473 g.clearRect(offset - 5, 0, strwidth + 10, fontheight);
474 g.clipRect(offset, 0, strwidth, fontheight);
478 g.drawString(text, offset, ascent);
480 // restore old clipping area
481 g.clipRect(0, 0, width, height);
488 default: // assume SOLID
490 for (int i = 0; i < thickness; i++)
491 g.drawRect(i, i, width - 2 * i - 1, height - 2 * i - 1);
497 * Returns the settings of this HolidayBorderPanel instance as a string.
499 public String toString() {
500 StringBuffer str = new StringBuffer("HolidayBorderPanel[");
503 str.append("style=");
505 case SOLID: str.append("SOLID"); break;
506 case RAISED: str.append("RAISED"); break;
507 case LOWERED: str.append("LOWERED"); break;
508 case IN: str.append("IN"); break;
509 case OUT: str.append("OUT"); break;
510 default: str.append("unknown");
515 str.append("thickness=");
516 str.append(thickness);
538 str.append("alignment=");
540 case LEFT: str.append("LEFT"); break;
541 case CENTER: str.append("CENTER"); break;
542 case RIGHT: str.append("RIGHT"); break;
543 default: str.append("unknown");
548 return str.toString();