New launcher class to start all 3 modes:
[jvcard.git] / src / be / nikiroo / jvcard / tui / UiColors.java
CommitLineData
a3b510ab
NR
1package be.nikiroo.jvcard.tui;
2
3import java.util.HashMap;
4import java.util.Map;
176a8327 5import java.util.MissingResourceException;
2a96e7b2
NR
6import java.util.ResourceBundle;
7
8import be.nikiroo.jvcard.resources.Bundles;
a3b510ab
NR
9
10import com.googlecode.lanterna.TextColor;
11import com.googlecode.lanterna.gui2.Label;
12
13/**
14 * All colour information must come from here.
15 *
16 * @author niki
17 *
18 */
19public 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;
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
2a96e7b2
NR
88 /**
89 * Create a new {@link Label} with the colours of the given {@link Element}.
90 *
91 * @param el
92 * the {@link Element}
93 * @param text
94 * the text of the {@link Label}
95 *
96 * @return the new {@link Label}
97 */
a3b510ab 98 private Label createLabel(Element el, String text) {
f82bad11
NR
99 if (text == null)
100 text = "";
101
a3b510ab
NR
102 Label lbl = new Label(text);
103 themeLabel(el, lbl);
104 return lbl;
105 }
106
2a96e7b2
NR
107 /**
108 * Theme a {@link Label} with the colours of the given {@link Element}.
109 *
110 * @param el
111 * the {@link Element}
112 * @param lbl
113 * the {@link Label}
114 */
a3b510ab
NR
115 private void themeLabel(Element el, Label lbl) {
116 lbl.setForegroundColor(el.getForegroundColor());
117 lbl.setBackgroundColor(el.getBackgroundColor());
118 }
119
2a96e7b2
NR
120 /**
121 * Return the background colour of the given element.
122 *
123 * @param el
124 * the {@link Element}
125 *
126 * @return its background colour
127 */
128 private TextColor getBackgroundColor(Element el) {
129 if (!colorMap.containsKey(el.name() + "_BG")) {
176a8327
NR
130 String value = null;
131 try {
132 value = bundle.getString(el.name() + "_BG");
133 } catch (MissingResourceException mre) {
134 value = null;
135 }
2a96e7b2
NR
136 colorMap.put(el.name() + "_BG",
137 convertToColor(value, TextColor.ANSI.BLACK));
a3b510ab
NR
138 }
139
2a96e7b2 140 return colorMap.get(el.name() + "_BG");
a3b510ab
NR
141 }
142
2a96e7b2
NR
143 /**
144 * Return the foreground colour of the given element.
145 *
146 * @param el
147 * the {@link Element}
148 *
149 * @return its foreground colour
150 */
151 private TextColor getForegroundColor(Element el) {
152 if (!colorMap.containsKey(el.name() + "_FG")) {
176a8327
NR
153 String value = null;
154 try {
155 value = bundle.getString(el.name() + "_FG");
156 } catch (MissingResourceException mre) {
157 value = null;
158 }
2a96e7b2
NR
159 colorMap.put(el.name() + "_FG",
160 convertToColor(value, TextColor.ANSI.WHITE));
a3b510ab
NR
161 }
162
2a96e7b2 163 return colorMap.get(el.name() + "_FG");
a3b510ab
NR
164 }
165
2a96e7b2
NR
166 /**
167 * Get the (unique) instance of this class.
168 *
169 * @return the (unique) instance
170 */
171 static public UiColors getInstance() {
172 synchronized (lock) {
173 if (instance == null)
174 instance = new UiColors();
175 }
176
177 return instance;
a3b510ab
NR
178 }
179
2a96e7b2
NR
180 /**
181 * Convert the given {@link String} value to a {@link TextColor}.
182 *
183 * @param value
184 * the {@link String} to convert
185 * @param defaultColor
186 * the default {@link TextColor} to return if the conversion
187 * failed
188 *
189 * @return the converted colour
190 */
191 static private TextColor convertToColor(String value, TextColor defaultColor) {
192 try {
193 if (value.startsWith("@")) {
194 int r = Integer.parseInt(value.substring(1, 3), 16);
195 int g = Integer.parseInt(value.substring(3, 5), 16);
196 int b = Integer.parseInt(value.substring(5, 7), 16);
197 return TextColor.Indexed.fromRGB(r, g, b);
198 } else if (value.startsWith("#")) {
199 int r = Integer.parseInt(value.substring(1, 3), 16);
200 int g = Integer.parseInt(value.substring(3, 5), 16);
201 int b = Integer.parseInt(value.substring(5, 7), 16);
202 return new TextColor.RGB(r, g, b);
1c03abaf
NR
203 } else if (value.replaceAll("[0-9]*", "").length() == 0) {
204 return new TextColor.Indexed(Integer.parseInt(value));
2a96e7b2
NR
205 } else {
206 return TextColor.ANSI.valueOf(value);
207 }
208 } catch (Exception e) {
209 new Exception("Cannot convert value to colour: " + value, e)
210 .printStackTrace();
211 }
212
213 return defaultColor;
a3b510ab 214 }
2a96e7b2 215
a3b510ab 216}