Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | package be.nikiroo.jvcard.tui; |
2 | ||
3 | import java.util.HashMap; | |
4 | import java.util.Map; | |
176a8327 | 5 | import java.util.MissingResourceException; |
2a96e7b2 NR |
6 | import java.util.ResourceBundle; |
7 | ||
8 | import be.nikiroo.jvcard.resources.Bundles; | |
a3b510ab NR |
9 | |
10 | import com.googlecode.lanterna.TextColor; | |
11 | import com.googlecode.lanterna.gui2.Label; | |
12 | ||
13 | /** | |
14 | * All colour information must come from here. | |
15 | * | |
16 | * @author niki | |
17 | * | |
18 | */ | |
19 | public class UiColors { | |
20 | static private Object lock = new Object(); | |
21 | static private UiColors instance = null; | |
22 | ||
2a96e7b2 NR |
23 | private ResourceBundle bundle = null; |
24 | private Map<String, TextColor> colorMap = null; | |
296a0b75 | 25 | private boolean utf = true; |
a3b510ab | 26 | |
2a96e7b2 NR |
27 | private UiColors() { |
28 | colorMap = new HashMap<String, TextColor>(); | |
29 | bundle = Bundles.getBundle("colors"); | |
30 | } | |
31 | ||
a3b510ab | 32 | /** |
2a96e7b2 NR |
33 | * Represent an element that can be coloured (foreground/background |
34 | * colours). | |
a3b510ab | 35 | * |
2a96e7b2 NR |
36 | * @author niki |
37 | * | |
a3b510ab | 38 | */ |
a3b510ab | 39 | public enum Element { |
296a0b75 | 40 | DEFAULT, // |
0b0b2b0f NR |
41 | TITLE_MAIN, TITLE_VARIABLE, TITLE_COUNT, // |
42 | ACTION_KEY, ACTION_DESC, // | |
296a0b75 | 43 | LINE_MESSAGE, LINE_MESSAGE_ERR, LINE_MESSAGE_QUESTION, LINE_MESSAGE_ANS, // |
f82bad11 NR |
44 | CONTACT_LINE, CONTACT_LINE_SEPARATOR, CONTACT_LINE_SELECTED, CONTACT_LINE_SEPARATOR_SELECTED, CONTACT_LINE_DIRTY, CONTACT_LINE_DIRTY_SELECTED, // |
45 | VIEW_CONTACT_NAME, VIEW_CONTACT_NORMAL, VIEW_CONTACT_NOTES_TITLE, // | |
46 | ; | |
a3b510ab NR |
47 | |
48 | /** | |
49 | * Get the foreground colour of this element. | |
50 | * | |
51 | * @return the colour | |
52 | */ | |
53 | public TextColor getForegroundColor() { | |
54 | return UiColors.getInstance().getForegroundColor(this); | |
55 | } | |
56 | ||
57 | /** | |
58 | * Get the background colour of this element. | |
59 | * | |
60 | * @return the colour | |
61 | */ | |
62 | public TextColor getBackgroundColor() { | |
63 | return UiColors.getInstance().getBackgroundColor(this); | |
64 | } | |
65 | ||
2a96e7b2 NR |
66 | /** |
67 | * Create a new {@link Label} with the colours of this {@link Element}. | |
68 | * | |
69 | * @param text | |
70 | * the text of the {@link Label} | |
71 | * | |
72 | * @return the new {@link Label} | |
73 | */ | |
a3b510ab NR |
74 | public Label createLabel(String text) { |
75 | return UiColors.getInstance().createLabel(this, text); | |
76 | } | |
77 | ||
2a96e7b2 NR |
78 | /** |
79 | * Theme a {@link Label} with the colours of this {@link Element}. | |
80 | * | |
81 | * @param lbl | |
82 | * the {@link Label} | |
83 | */ | |
a3b510ab NR |
84 | public void themeLabel(Label lbl) { |
85 | UiColors.getInstance().themeLabel(this, lbl); | |
86 | } | |
87 | } | |
88 | ||
296a0b75 NR |
89 | /** |
90 | * Check if unicode characters should be used. | |
91 | * | |
92 | * @return TRUE to allow unicode | |
93 | */ | |
94 | public boolean isUnicode() { | |
95 | return utf; | |
96 | } | |
97 | ||
98 | /** | |
99 | * Allow or disallow unicode characters in the program. | |
100 | * | |
101 | * @param utf | |
102 | * TRUE to allow unuciode, FALSE to only allow ASCII characters | |
103 | */ | |
104 | public void setUnicode(boolean utf) { | |
105 | this.utf = utf; | |
106 | } | |
107 | ||
2a96e7b2 NR |
108 | /** |
109 | * Create a new {@link Label} with the colours of the given {@link Element}. | |
110 | * | |
111 | * @param el | |
112 | * the {@link Element} | |
113 | * @param text | |
114 | * the text of the {@link Label} | |
115 | * | |
116 | * @return the new {@link Label} | |
117 | */ | |
a3b510ab | 118 | private Label createLabel(Element el, String text) { |
f82bad11 NR |
119 | if (text == null) |
120 | text = ""; | |
121 | ||
a3b510ab NR |
122 | Label lbl = new Label(text); |
123 | themeLabel(el, lbl); | |
124 | return lbl; | |
125 | } | |
126 | ||
2a96e7b2 NR |
127 | /** |
128 | * Theme a {@link Label} with the colours of the given {@link Element}. | |
129 | * | |
130 | * @param el | |
131 | * the {@link Element} | |
132 | * @param lbl | |
133 | * the {@link Label} | |
134 | */ | |
a3b510ab NR |
135 | private void themeLabel(Element el, Label lbl) { |
136 | lbl.setForegroundColor(el.getForegroundColor()); | |
137 | lbl.setBackgroundColor(el.getBackgroundColor()); | |
138 | } | |
139 | ||
2a96e7b2 NR |
140 | /** |
141 | * Return the background colour of the given element. | |
142 | * | |
143 | * @param el | |
144 | * the {@link Element} | |
145 | * | |
146 | * @return its background colour | |
147 | */ | |
148 | private TextColor getBackgroundColor(Element el) { | |
149 | if (!colorMap.containsKey(el.name() + "_BG")) { | |
176a8327 NR |
150 | String value = null; |
151 | try { | |
152 | value = bundle.getString(el.name() + "_BG"); | |
153 | } catch (MissingResourceException mre) { | |
154 | value = null; | |
155 | } | |
2a96e7b2 NR |
156 | colorMap.put(el.name() + "_BG", |
157 | convertToColor(value, TextColor.ANSI.BLACK)); | |
a3b510ab NR |
158 | } |
159 | ||
2a96e7b2 | 160 | return colorMap.get(el.name() + "_BG"); |
a3b510ab NR |
161 | } |
162 | ||
2a96e7b2 NR |
163 | /** |
164 | * Return the foreground colour of the given element. | |
165 | * | |
166 | * @param el | |
167 | * the {@link Element} | |
168 | * | |
169 | * @return its foreground colour | |
170 | */ | |
171 | private TextColor getForegroundColor(Element el) { | |
172 | if (!colorMap.containsKey(el.name() + "_FG")) { | |
176a8327 NR |
173 | String value = null; |
174 | try { | |
175 | value = bundle.getString(el.name() + "_FG"); | |
176 | } catch (MissingResourceException mre) { | |
177 | value = null; | |
178 | } | |
2a96e7b2 NR |
179 | colorMap.put(el.name() + "_FG", |
180 | convertToColor(value, TextColor.ANSI.WHITE)); | |
a3b510ab NR |
181 | } |
182 | ||
2a96e7b2 | 183 | return colorMap.get(el.name() + "_FG"); |
a3b510ab NR |
184 | } |
185 | ||
2a96e7b2 NR |
186 | /** |
187 | * Get the (unique) instance of this class. | |
188 | * | |
189 | * @return the (unique) instance | |
190 | */ | |
191 | static public UiColors getInstance() { | |
192 | synchronized (lock) { | |
193 | if (instance == null) | |
194 | instance = new UiColors(); | |
195 | } | |
196 | ||
197 | return instance; | |
a3b510ab NR |
198 | } |
199 | ||
2a96e7b2 NR |
200 | /** |
201 | * Convert the given {@link String} value to a {@link TextColor}. | |
202 | * | |
203 | * @param value | |
204 | * the {@link String} to convert | |
205 | * @param defaultColor | |
206 | * the default {@link TextColor} to return if the conversion | |
207 | * failed | |
208 | * | |
209 | * @return the converted colour | |
210 | */ | |
211 | static private TextColor convertToColor(String value, TextColor defaultColor) { | |
212 | try { | |
213 | if (value.startsWith("@")) { | |
214 | int r = Integer.parseInt(value.substring(1, 3), 16); | |
215 | int g = Integer.parseInt(value.substring(3, 5), 16); | |
216 | int b = Integer.parseInt(value.substring(5, 7), 16); | |
217 | return TextColor.Indexed.fromRGB(r, g, b); | |
218 | } else if (value.startsWith("#")) { | |
219 | int r = Integer.parseInt(value.substring(1, 3), 16); | |
220 | int g = Integer.parseInt(value.substring(3, 5), 16); | |
221 | int b = Integer.parseInt(value.substring(5, 7), 16); | |
222 | return new TextColor.RGB(r, g, b); | |
223 | } else { | |
224 | return TextColor.ANSI.valueOf(value); | |
225 | } | |
226 | } catch (Exception e) { | |
227 | new Exception("Cannot convert value to colour: " + value, e) | |
228 | .printStackTrace(); | |
229 | } | |
230 | ||
231 | return defaultColor; | |
a3b510ab | 232 | } |
2a96e7b2 | 233 | |
a3b510ab | 234 | } |