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