]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/demo/holiday/HolidayBorderPanel.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / demo / holiday / HolidayBorderPanel.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1997-2008, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.demo.holiday;\r
8 \r
9 import java.awt.*;\r
10 \r
11 /**\r
12  * Various graphical borders. The border itself is a Panel so that it can\r
13  * contain other Components (i.e. it borders something). You use the\r
14  * HolidayBorderPanel like any other Panel: you set the layout that you prefer and\r
15  * add Components to it. Beware that a null layout does not obey the insets\r
16  * of the panel so if you use null layouts, adjust your measurements to\r
17  * handle the border by calling insets().\r
18  *\r
19  * @author  Andy Clark, Taligent Inc.\r
20  * @version 1.0\r
21  */\r
22 public class HolidayBorderPanel extends Panel {\r
23     /**\r
24      * For serialization\r
25      */\r
26     private static final long serialVersionUID = 4669213306492461159L;\r
27     // Constants\r
28 \r
29     /** Solid border. */\r
30     public final static int SOLID = 0;\r
31     /** A raised border. */\r
32     public final static int RAISED = 1;\r
33     /** A lowered border. */\r
34     public final static int LOWERED = 2;\r
35     /** An etched in border. */\r
36     public final static int IN = 3;\r
37     /** An etched out border. */\r
38     public final static int OUT = 4;\r
39 \r
40     /** Left alignment. */\r
41     public final static int LEFT = 0;\r
42     /** Center alignment. */\r
43     public final static int CENTER = 1;\r
44     /** Right alignment. */\r
45     public final static int RIGHT = 2;\r
46 \r
47     /** Default style (IN). */\r
48     public final static int DEFAULT_STYLE = IN;\r
49     /** Default thickness (10). */\r
50     public final static int DEFAULT_THICKNESS = 10;\r
51     /** Default thickness for solid borders (4). */\r
52     public final static int DEFAULT_SOLID_THICKNESS = 4;\r
53     /** Default thickness for raised borders (2). */\r
54     public final static int DEFAULT_RAISED_THICKNESS = 2;\r
55     /** Default thickness for lowered borders (2). */\r
56     public final static int DEFAULT_LOWERED_THICKNESS = 2;\r
57     /** Default thickness for etched-in borders (10). */\r
58     public final static int DEFAULT_IN_THICKNESS = 10;\r
59     /** Default thickness for etched-out borders (10). */\r
60     public final static int DEFAULT_OUT_THICKNESS = 10;\r
61     /** Default gap between border and contained component (5). */\r
62     public final static int DEFAULT_GAP = 5;\r
63     /** Default color (black). Applies to SOLID and etched borders. */\r
64     public final static Color DEFAULT_COLOR = Color.black;\r
65 \r
66     /** Default font (TimesRoman,PLAIN,14). Only applies to etched borders. */\r
67     public final static Font DEFAULT_FONT = new Font("TimesRoman", Font.PLAIN, 14);\r
68     /** Default alignment (LEFT). Only applies to etched borders. */\r
69     public final static int DEFAULT_ALIGNMENT = LEFT;\r
70 \r
71     // Data\r
72     private int style;\r
73     private int thickness;\r
74     private int gap;\r
75     private Color color;\r
76 \r
77     private Font font;\r
78     private String text;\r
79     private int alignment;\r
80 \r
81     /**\r
82      * Constructor. Makes default border.\r
83      */\r
84     public HolidayBorderPanel() {\r
85 \r
86         // initialize data\r
87         style       = DEFAULT_STYLE;\r
88         thickness   = DEFAULT_THICKNESS;\r
89         gap         = DEFAULT_GAP;\r
90         color       = DEFAULT_COLOR;\r
91 \r
92         text        = null;\r
93         font        = DEFAULT_FONT;\r
94         alignment   = DEFAULT_ALIGNMENT;\r
95 \r
96         }\r
97 \r
98     /**\r
99      * Constructor. Makes an etched IN border with given text caption.\r
100      *\r
101      * @param text  Text caption\r
102      */\r
103     public HolidayBorderPanel(String text) {\r
104         this();\r
105 \r
106         style = IN;\r
107         this.text = text;\r
108         }\r
109 \r
110     /**\r
111      * Constructor. Makes SOLID border with color and thickness given.\r
112      *\r
113      * @param color     The color for the border.\r
114      * @param thickness The thickness of the border.\r
115      */\r
116     public HolidayBorderPanel(Color color, int thickness) {\r
117         this();\r
118 \r
119         style = SOLID;\r
120         this.color = color;\r
121         this.thickness = thickness;\r
122         }\r
123 \r
124     /**\r
125      * Constructor. Makes a border of the given style with the default\r
126      * thickness for that style.\r
127      *\r
128      * @param style The style for this border.\r
129      */\r
130     public HolidayBorderPanel(int style) {\r
131         this();\r
132 \r
133         // set thickness appropriate to this style\r
134         switch (style) {\r
135             case SOLID: thickness = DEFAULT_SOLID_THICKNESS; break;\r
136             case RAISED: thickness = DEFAULT_RAISED_THICKNESS; break;\r
137             case LOWERED: thickness = DEFAULT_LOWERED_THICKNESS; break;\r
138             case IN: thickness = DEFAULT_IN_THICKNESS; break;\r
139             case OUT: thickness = DEFAULT_OUT_THICKNESS; break;\r
140             default:\r
141                 thickness = DEFAULT_THICKNESS;\r
142             }\r
143 \r
144         this.style = style;\r
145         }\r
146 \r
147     /**\r
148      * Constructor. Makes border with given style and thickness.\r
149      *\r
150      * @param style     The style for this border.\r
151      * @param thickness The thickness for this border.\r
152      */\r
153     public HolidayBorderPanel(int style, int thickness) {\r
154         this();\r
155 \r
156         this.style = style;\r
157         this.thickness = thickness;\r
158         }\r
159 \r
160     /**\r
161      * Returns the insets of this panel..\r
162      */\r
163     public Insets getInsets() {\r
164         int adjustment = 0;\r
165 \r
166         // adjust for text string\r
167         if (style == IN || style == OUT) {\r
168             if (text != null && text.length() > 0) {\r
169                 try {\r
170                     // set font and get info\r
171                     int height = getGraphics().getFontMetrics(font).getHeight();\r
172                     if (height > thickness)\r
173                         adjustment = height - thickness;\r
174                     }\r
175                 catch (Exception e) {\r
176                     // nothing: just in case there is no graphics context\r
177                     //   at the beginning.\r
178                     System.out.print("");\r
179                     }\r
180                 }\r
181             }\r
182 \r
183         // return appropriate insets\r
184         int dist = thickness + gap;\r
185         return new Insets(dist + adjustment, dist, dist, dist);\r
186         }\r
187 \r
188     /**\r
189      * Sets the style of the border\r
190      *\r
191      * @param style The new style.\r
192      */\r
193     public HolidayBorderPanel setStyle(int style) {\r
194 \r
195         // set the style and re-layout the panel\r
196         this.style = style;\r
197         doLayout();\r
198         repaint();\r
199 \r
200         return this;\r
201         }\r
202 \r
203     /**\r
204      * Gets the style of the border\r
205      */\r
206     public int getStyle() {\r
207 \r
208         return style;\r
209         }\r
210 \r
211     /**\r
212      * Sets the thickness of the border.\r
213      *\r
214      * @param thickness The new thickness\r
215      */\r
216     public HolidayBorderPanel setThickness(int thickness) {\r
217 \r
218         if (thickness > 0) {\r
219             this.thickness = thickness;\r
220             doLayout();\r
221             repaint();\r
222             }\r
223 \r
224         return this;\r
225         }\r
226 \r
227     /**\r
228      * Gets the thickness of the border.\r
229      */\r
230     public int getThickness() {\r
231 \r
232         return thickness;\r
233         }\r
234 \r
235     /**\r
236      * Sets the gap between the border and the contained Component.\r
237      *\r
238      * @param gap The new gap, in pixels.\r
239      */\r
240     public HolidayBorderPanel setGap(int gap) {\r
241 \r
242         if (gap > -1) {\r
243             this.gap = gap;\r
244             doLayout();\r
245             repaint();\r
246             }\r
247 \r
248         return this;\r
249         }\r
250 \r
251     /**\r
252      * Gets the gap between the border and the contained Component.\r
253      */\r
254     public int getGap() {\r
255 \r
256         return gap;\r
257         }\r
258 \r
259     /**\r
260      * Sets the current color for SOLID borders and the caption text\r
261      * color for etched borders.\r
262      *\r
263      * @param color The new color.\r
264      */\r
265     public HolidayBorderPanel setColor(Color color) {\r
266 \r
267         this.color = color;\r
268         if (style == SOLID || style == IN || style == OUT)\r
269             repaint();\r
270 \r
271         return this;\r
272         }\r
273 \r
274     /**\r
275      * Gets the current color for SOLID borders and the caption\r
276      * text color for etched borders.\r
277      */\r
278     public Color getColor() {\r
279 \r
280         return color;\r
281         }\r
282 \r
283     /**\r
284      * Sets the font. Only applies to etched borders.\r
285      */\r
286     public HolidayBorderPanel setTextFont(Font font) {\r
287 \r
288         // set font\r
289         if (font != null) {\r
290             this.font = font;\r
291             if (style == IN || style == OUT) {\r
292                 doLayout();\r
293                 repaint();\r
294                 }\r
295             }\r
296 \r
297         return this;\r
298         }\r
299 \r
300     /**\r
301      * Gets the font of the text. Only applies to etched borders.\r
302      */\r
303     public Font getTextFont() {\r
304 \r
305         return font;\r
306         }\r
307 \r
308     /**\r
309      * Sets the text. Only applies to etched borders.\r
310      *\r
311      * @param text  The new text.\r
312      */\r
313     public HolidayBorderPanel setText(String text) {\r
314 \r
315         this.text = text;\r
316         if (style == IN || style == OUT) {\r
317             doLayout();\r
318             repaint();\r
319             }\r
320 \r
321         return this;\r
322         }\r
323 \r
324     /**\r
325      * Gets the text. Only applies to etched borders.\r
326      */\r
327     public String getText() {\r
328 \r
329         return text;\r
330         }\r
331 \r
332     /**\r
333      * Sets the text alignment. Only applies to etched borders.\r
334      *\r
335      * @param alignment The new alignment.\r
336      */\r
337     public HolidayBorderPanel setAlignment(int alignment) {\r
338 \r
339         this.alignment = alignment;\r
340         if (style == IN || style == OUT) {\r
341             doLayout();\r
342             repaint();\r
343             }\r
344 \r
345         return this;\r
346         }\r
347 \r
348     /**\r
349      * Gets the text alignment.\r
350      */\r
351     public int getAlignment() {\r
352 \r
353         return alignment;\r
354         }\r
355 \r
356     /**\r
357      * Repaints the border.\r
358      *\r
359      * @param g The graphics context.\r
360      */\r
361     public void paint(Graphics g) {\r
362 \r
363         // get current dimensions\r
364         Dimension size = getSize();\r
365         int width = size.width;\r
366         int height = size.height;\r
367 \r
368         // set colors\r
369         Color light = getBackground().brighter().brighter().brighter();\r
370         Color dark = getBackground().darker().darker().darker();\r
371 \r
372         // Draw border\r
373         switch (style) {\r
374             case RAISED:    // 3D Border (in or out)\r
375             case LOWERED:\r
376                 Color topleft = null;\r
377                 Color bottomright = null;\r
378 \r
379                 // set colors\r
380                 if (style == RAISED) {\r
381                     topleft = light;\r
382                     bottomright = dark;\r
383                     }\r
384                 else {\r
385                     topleft = dark;\r
386                     bottomright = light;\r
387                     }\r
388 \r
389                 // draw border\r
390                 g.setColor(topleft);\r
391                 for (int i = 0; i < thickness; i++) {\r
392                     g.drawLine(i, i, width - i - 2, i);\r
393                     g.drawLine(i, i + 1, i, height - i - 1);\r
394                     }\r
395                 g.setColor(bottomright);\r
396                 for (int i = 0; i < thickness; i++) {\r
397                     g.drawLine(i + 1, height - i - 1, width - i - 1, height - i - 1);\r
398                     g.drawLine(width - i - 1, i, width - i - 1, height - i - 2);\r
399                     }\r
400                 break;\r
401 \r
402             case IN:    // Etched Border (in or out)\r
403             case OUT:\r
404                 int adjust1 = 0;\r
405                 int adjust2 = 0;\r
406 \r
407                 // set font and get info\r
408                 Font oldfont = g.getFont();\r
409                 g.setFont(font);\r
410                 FontMetrics fm = g.getFontMetrics();\r
411                 int ascent = fm.getAscent();\r
412 \r
413                 // set adjustment\r
414                 if (style == IN)\r
415                     adjust1 = 1;\r
416                 else\r
417                     adjust2 = 1;\r
418 \r
419                 // Calculate adjustment for text\r
420                 int adjustment = 0;\r
421                 if (text != null && text.length() > 0) {\r
422                     if (ascent > thickness)\r
423                         adjustment = (ascent - thickness) / 2;\r
424                     }\r
425 \r
426                 // The adjustment is there so that we always draw the\r
427                 // light rectangle first. Otherwise, your eye picks up\r
428                 // the discrepancy where the light rect. passes over\r
429                 // the darker rect.\r
430                 int x = thickness / 2;\r
431                 int y = thickness / 2 + adjustment;\r
432                 int w = width - thickness - 1;\r
433                 int h = height - thickness - 1 - adjustment;\r
434 \r
435                 // draw rectangles\r
436                 g.setColor(light);\r
437                 g.drawRect(x + adjust1, y + adjust1, w, h);\r
438                 g.setColor(dark);\r
439                 g.drawRect(x + adjust2, y + adjust2, w, h);\r
440 \r
441                 // draw text, if applicable\r
442                 if (text != null && text.length() > 0) {\r
443                     // calculate drawing area\r
444                     int fontheight = fm.getHeight();\r
445                     int strwidth = fm.stringWidth(text);\r
446 \r
447                     int textwidth = width - 2 * (thickness + 5);\r
448                     if (strwidth > textwidth)\r
449                         strwidth = textwidth;\r
450 \r
451                     // calculate offset for alignment\r
452                     int offset;\r
453                     switch (alignment) {\r
454                         case CENTER:\r
455                             offset = (width - strwidth) / 2;\r
456                             break;\r
457                         case RIGHT:\r
458                             offset = width - strwidth - thickness - 5;\r
459                             break;\r
460                         case LEFT:\r
461                         default: // assume left alignment if invalid\r
462                             offset = thickness + 5;\r
463                             break;\r
464                         }\r
465 \r
466                     // clear drawing area and set clipping region\r
467                     g.clearRect(offset - 5, 0, strwidth  + 10, fontheight);\r
468                     g.clipRect(offset, 0, strwidth, fontheight);\r
469 \r
470                     // draw text\r
471                     g.setColor(color);\r
472                     g.drawString(text, offset, ascent);\r
473 \r
474                     // restore old clipping area\r
475                     g.clipRect(0, 0, width, height);\r
476                     }\r
477 \r
478                 g.setFont(oldfont);\r
479                 break;\r
480 \r
481             case SOLID:\r
482             default: // assume SOLID\r
483                 g.setColor(color);\r
484                 for (int i = 0; i < thickness; i++)\r
485                     g.drawRect(i, i, width - 2 * i - 1, height - 2 * i - 1);\r
486             }\r
487 \r
488         }\r
489 \r
490     /**\r
491      * Returns the settings of this HolidayBorderPanel instance as a string.\r
492      */\r
493     public String toString() {\r
494         StringBuffer str = new StringBuffer("HolidayBorderPanel[");\r
495 \r
496         // style\r
497         str.append("style=");\r
498         switch (style) {\r
499             case SOLID: str.append("SOLID"); break;\r
500             case RAISED: str.append("RAISED"); break;\r
501             case LOWERED: str.append("LOWERED"); break;\r
502             case IN: str.append("IN"); break;\r
503             case OUT: str.append("OUT"); break;\r
504             default: str.append("unknown");\r
505             }\r
506         str.append(",");\r
507 \r
508         // thickness\r
509         str.append("thickness=");\r
510         str.append(thickness);\r
511         str.append(",");\r
512 \r
513         // gap\r
514         str.append("gap=");\r
515         str.append(gap);\r
516         str.append(",");\r
517 \r
518         // color\r
519         str.append(color);\r
520         str.append(",");\r
521 \r
522         // font\r
523         str.append(font);\r
524         str.append(",");\r
525 \r
526         // text\r
527         str.append("text=");\r
528         str.append(text);\r
529         str.append(",");\r
530 \r
531         // alignment\r
532         str.append("alignment=");\r
533         switch (alignment) {\r
534             case LEFT: str.append("LEFT"); break;\r
535             case CENTER: str.append("CENTER"); break;\r
536             case RIGHT: str.append("RIGHT"); break;\r
537             default: str.append("unknown");\r
538             }\r
539 \r
540         str.append("]");\r
541 \r
542         return str.toString();\r
543         }\r
544 \r
545     }\r
546 \r