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