Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | package be.nikiroo.jvcard.tui; |
2 | ||
ed91f27a NR |
3 | import java.io.FileInputStream; |
4 | import java.io.IOException; | |
5 | import java.io.InputStream; | |
a3b510ab NR |
6 | import java.util.HashMap; |
7 | import java.util.Map; | |
176a8327 | 8 | import java.util.MissingResourceException; |
ed91f27a | 9 | import java.util.Properties; |
2a96e7b2 | 10 | |
f06c8100 NR |
11 | import be.nikiroo.jvcard.resources.ColorBundle; |
12 | import be.nikiroo.jvcard.resources.ColorOption; | |
a3b510ab NR |
13 | |
14 | import com.googlecode.lanterna.TextColor; | |
ed91f27a NR |
15 | import com.googlecode.lanterna.graphics.PropertiesTheme; |
16 | import com.googlecode.lanterna.graphics.Theme; | |
17 | import com.googlecode.lanterna.gui2.AbstractTextGUI; | |
a3b510ab NR |
18 | import com.googlecode.lanterna.gui2.Label; |
19 | ||
20 | /** | |
21 | * All colour information must come from here. | |
22 | * | |
23 | * @author niki | |
24 | * | |
25 | */ | |
f06c8100 | 26 | public class UiColors { |
a3b510ab NR |
27 | static private Object lock = new Object(); |
28 | static private UiColors instance = null; | |
29 | ||
2a96e7b2 | 30 | private Map<String, TextColor> colorMap = null; |
f06c8100 | 31 | private ColorBundle bundle; |
a3b510ab | 32 | |
2a96e7b2 | 33 | private UiColors() { |
f06c8100 | 34 | bundle = new ColorBundle(); |
2a96e7b2 | 35 | colorMap = new HashMap<String, TextColor>(); |
2a96e7b2 NR |
36 | } |
37 | ||
bdcf4306 NR |
38 | /** |
39 | * Return a {@link Theme} following the colours defined in | |
40 | * colors.properties. | |
41 | * | |
42 | * @return the {@link Theme} | |
43 | */ | |
44 | static public Theme getCustomTheme() { | |
45 | // Create a properties-theme with our own custom values for some of it | |
46 | Properties properties = new Properties(); | |
47 | try { | |
48 | ClassLoader classLoader = AbstractTextGUI.class.getClassLoader(); | |
49 | InputStream resourceAsStream = classLoader | |
50 | .getResourceAsStream("default-theme.properties"); | |
51 | if (resourceAsStream == null) { | |
52 | resourceAsStream = new FileInputStream( | |
53 | "src/main/resources/default-theme.properties"); | |
54 | } | |
55 | properties.load(resourceAsStream); | |
56 | resourceAsStream.close(); | |
57 | } catch (IOException e) { | |
58 | } | |
59 | ||
60 | // default colours: | |
61 | String fg = getForegroundColor(ColorOption.DEFAULT).toString(); | |
62 | String bg = getBackgroundColor(ColorOption.DEFAULT).toString(); | |
63 | for (String def : new String[] { "com.googlecode.lanterna", | |
64 | "com.googlecode.lanterna.gui2.TextBox", | |
65 | "com.googlecode.lanterna.gui2.AbstractListBox", | |
66 | "com.googlecode.lanterna.gui2.Borders$StandardBorder" }) { | |
67 | properties.put(def + ".foreground", fg); | |
68 | properties.put(def + ".background", bg); | |
69 | } | |
70 | ||
71 | // no bold on borders prelight: | |
72 | properties | |
73 | .put("com.googlecode.lanterna.gui2.Borders$StandardBorder.sgr[PRELIGHT]", | |
74 | ""); | |
75 | ||
76 | // line answers: | |
77 | fg = getForegroundColor(ColorOption.LINE_MESSAGE_ANS).toString(); | |
78 | bg = getBackgroundColor(ColorOption.LINE_MESSAGE_ANS).toString(); | |
79 | String prop = "com.googlecode.lanterna.gui2.TextBox"; | |
80 | properties.put(prop + ".foreground[ACTIVE]", fg); | |
81 | properties.put(prop + ".background[ACTIVE]", bg); | |
82 | ||
83 | PropertiesTheme theme = new PropertiesTheme(properties); | |
84 | return theme; | |
85 | } | |
86 | ||
a3b510ab | 87 | /** |
ed91f27a NR |
88 | * Create a new {@link Label} with the colours of the given |
89 | * {@link ColorOption}. | |
2a96e7b2 NR |
90 | * |
91 | * @param el | |
e119a1c1 | 92 | * the {@link ColorOption} |
2a96e7b2 NR |
93 | * @param text |
94 | * the text of the {@link Label} | |
95 | * | |
96 | * @return the new {@link Label} | |
97 | */ | |
e119a1c1 | 98 | static public Label createLabel(ColorOption 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 | 107 | /** |
e119a1c1 | 108 | * Theme a {@link Label} with the colours of the given {@link ColorOption}. |
2a96e7b2 NR |
109 | * |
110 | * @param el | |
e119a1c1 | 111 | * the {@link ColorOption} |
2a96e7b2 NR |
112 | * @param lbl |
113 | * the {@link Label} | |
114 | */ | |
e119a1c1 NR |
115 | static public void themeLabel(ColorOption el, Label lbl) { |
116 | lbl.setForegroundColor(getForegroundColor(el)); | |
117 | lbl.setBackgroundColor(getBackgroundColor(el)); | |
a3b510ab NR |
118 | } |
119 | ||
2a96e7b2 NR |
120 | /** |
121 | * Return the background colour of the given element. | |
122 | * | |
123 | * @param el | |
e119a1c1 | 124 | * the {@link ColorOption} |
2a96e7b2 NR |
125 | * |
126 | * @return its background colour | |
127 | */ | |
e119a1c1 NR |
128 | static public TextColor getBackgroundColor(ColorOption el) { |
129 | if (!getInstance().colorMap.containsKey(el.name() + "_BG")) { | |
176a8327 NR |
130 | String value = null; |
131 | try { | |
f06c8100 | 132 | value = getInstance().bundle.getStringX(el, "BG"); |
176a8327 NR |
133 | } catch (MissingResourceException mre) { |
134 | value = null; | |
135 | } | |
e119a1c1 | 136 | getInstance().colorMap.put(el.name() + "_BG", |
2a96e7b2 | 137 | convertToColor(value, TextColor.ANSI.BLACK)); |
a3b510ab NR |
138 | } |
139 | ||
e119a1c1 | 140 | return getInstance().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 | |
e119a1c1 | 147 | * the {@link ColorOption} |
2a96e7b2 NR |
148 | * |
149 | * @return its foreground colour | |
150 | */ | |
e119a1c1 NR |
151 | static public TextColor getForegroundColor(ColorOption el) { |
152 | if (!getInstance().colorMap.containsKey(el.name() + "_FG")) { | |
176a8327 NR |
153 | String value = null; |
154 | try { | |
f06c8100 | 155 | value = getInstance().bundle.getStringX(el, "FG"); |
176a8327 NR |
156 | } catch (MissingResourceException mre) { |
157 | value = null; | |
158 | } | |
e119a1c1 | 159 | getInstance().colorMap.put(el.name() + "_FG", |
2a96e7b2 | 160 | convertToColor(value, TextColor.ANSI.WHITE)); |
a3b510ab NR |
161 | } |
162 | ||
e119a1c1 | 163 | return getInstance().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 | */ | |
e119a1c1 | 171 | static private UiColors getInstance() { |
2a96e7b2 NR |
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 | } |
a3b510ab | 215 | } |