Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MessageStyleFactory |
|
| 1.5;1.5 |
1 | package us.daveread.basicquery.gui; | |
2 | ||
3 | import javax.swing.text.AttributeSet; | |
4 | import java.awt.Color; | |
5 | import javax.swing.text.SimpleAttributeSet; | |
6 | import javax.swing.text.StyleConstants; | |
7 | ||
8 | /** | |
9 | * <p> | |
10 | * Title: | |
11 | * </p> | |
12 | * | |
13 | * <p> | |
14 | * Description: | |
15 | * </p> | |
16 | * | |
17 | * <p> | |
18 | * Copyright: Copyright (c) 2004 | |
19 | * </p> | |
20 | * | |
21 | * <p> | |
22 | * Company: | |
23 | * </p> | |
24 | * | |
25 | * @author not attributable | |
26 | * @version 1.0 | |
27 | */ | |
28 | public class MessageStyleFactory { | |
29 | ||
30 | /** | |
31 | * The factory instance - Singleton | |
32 | */ | |
33 | private static MessageStyleFactory factory; | |
34 | ||
35 | /** | |
36 | * Attribute: bold font | |
37 | */ | |
38 | public static final int BOLD = 1; | |
39 | ||
40 | /** | |
41 | * Attribute: italic font | |
42 | */ | |
43 | public static final int ITALIC = 2; | |
44 | ||
45 | /** | |
46 | * Attribute: underline font | |
47 | */ | |
48 | public static final int UNDERLINE = 4; | |
49 | ||
50 | /** | |
51 | * private constructor for Singleton | |
52 | */ | |
53 | 1 | private MessageStyleFactory() { |
54 | ||
55 | 1 | } |
56 | ||
57 | /** | |
58 | * Get the instance of the factory | |
59 | * | |
60 | * @return The factory instance | |
61 | */ | |
62 | public static synchronized MessageStyleFactory instance() { | |
63 | 1 | if (factory == null) { |
64 | 1 | factory = new MessageStyleFactory(); |
65 | } | |
66 | ||
67 | 1 | return factory; |
68 | } | |
69 | ||
70 | /** | |
71 | * Create the attribute set with the supplied color | |
72 | * | |
73 | * @param textColor | |
74 | * The font foreground color | |
75 | * | |
76 | * @return The created attribute set | |
77 | */ | |
78 | public AttributeSet createStyle(Color textColor) { | |
79 | 1 | return createStyle(textColor, 0); |
80 | } | |
81 | ||
82 | /** | |
83 | * Create the attribute set with the supplied style | |
84 | * | |
85 | * @param styleId | |
86 | * The style identifier | |
87 | * | |
88 | * @return The created attribute set | |
89 | */ | |
90 | public AttributeSet createStyle(int styleId) { | |
91 | 1 | return createStyle(Color.black, styleId); |
92 | } | |
93 | ||
94 | /** | |
95 | * Create the attribute set with the supplied color and style | |
96 | * | |
97 | * @param textColor | |
98 | * The font foreground color | |
99 | * @param style | |
100 | * The style identifier | |
101 | * | |
102 | * @return The created attribute set | |
103 | */ | |
104 | public AttributeSet createStyle(Color textColor, int style) { | |
105 | 3 | return createStyle(textColor, Color.white, style); |
106 | } | |
107 | ||
108 | /** | |
109 | * Create the attribute set with the supplied forground color, background | |
110 | * color and style | |
111 | * | |
112 | * @param textColor | |
113 | * The font foreground color | |
114 | * @param backgroundColor | |
115 | * The background color | |
116 | * @param style | |
117 | * The style identifier | |
118 | * | |
119 | * @return The created attribute set | |
120 | */ | |
121 | public AttributeSet createStyle(Color textColor, Color backgroundColor, | |
122 | int style) { | |
123 | 11 | return createStyle(textColor, backgroundColor, isTrue(style, BOLD), |
124 | isTrue(style, ITALIC), isTrue(style, UNDERLINE)); | |
125 | } | |
126 | ||
127 | /** | |
128 | * | |
129 | * Create the attribute set with the supplied forground color, background | |
130 | * color and style features | |
131 | * | |
132 | * @param textColor | |
133 | * The font foreground color | |
134 | * @param backgroundColor | |
135 | * The background color | |
136 | * @param bold | |
137 | * Bold style on/off | |
138 | * @param italic | |
139 | * Italic setting on/off | |
140 | * @param underline | |
141 | * Underlined setting on/off | |
142 | * | |
143 | * @return The created attribute set | |
144 | */ | |
145 | private AttributeSet createStyle(Color textColor, Color backgroundColor, | |
146 | boolean bold, boolean italic, | |
147 | boolean underline) { | |
148 | AttributeSet attributeSet; | |
149 | ||
150 | 11 | attributeSet = new SimpleAttributeSet(); |
151 | 11 | StyleConstants.setForeground((SimpleAttributeSet) attributeSet, textColor); |
152 | 11 | StyleConstants.setBackground((SimpleAttributeSet) attributeSet, |
153 | backgroundColor); | |
154 | ||
155 | 11 | if (bold) { |
156 | 5 | StyleConstants.setBold((SimpleAttributeSet) attributeSet, true); |
157 | } | |
158 | ||
159 | 11 | if (italic) { |
160 | 6 | StyleConstants.setItalic((SimpleAttributeSet) attributeSet, true); |
161 | } | |
162 | ||
163 | 11 | if (underline) { |
164 | 4 | StyleConstants.setUnderline((SimpleAttributeSet) attributeSet, true); |
165 | } | |
166 | ||
167 | // StyleConstants.setFontSize((SimpleAttributeSet)attributeSet, fontSize); | |
168 | ||
169 | 11 | return attributeSet; |
170 | } | |
171 | ||
172 | /** | |
173 | * Check whether a given style is part of the attributes | |
174 | * | |
175 | * @param style The style id | |
176 | * @param attribute The attribute setting | |
177 | * | |
178 | * @return True if the style is included in the attribute | |
179 | */ | |
180 | private boolean isTrue(int style, int attribute) { | |
181 | 33 | return (style & attribute) > 0; |
182 | } | |
183 | } |