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 | |
e119a1c1 NR |
11 | import be.nikiroo.jvcard.resources.bundles.ColorBundle; |
12 | import be.nikiroo.jvcard.resources.enums.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 | */ | |
e119a1c1 | 26 | public class UiColors extends ColorBundle { |
a3b510ab NR |
27 | static private Object lock = new Object(); |
28 | static private UiColors instance = null; | |
29 | ||
2a96e7b2 | 30 | private Map<String, TextColor> colorMap = null; |
a3b510ab | 31 | |
2a96e7b2 | 32 | private UiColors() { |
e119a1c1 | 33 | super(); |
2a96e7b2 | 34 | colorMap = new HashMap<String, TextColor>(); |
2a96e7b2 NR |
35 | } |
36 | ||
a3b510ab | 37 | /** |
ed91f27a NR |
38 | * Create a new {@link Label} with the colours of the given |
39 | * {@link ColorOption}. | |
2a96e7b2 NR |
40 | * |
41 | * @param el | |
e119a1c1 | 42 | * the {@link ColorOption} |
2a96e7b2 NR |
43 | * @param text |
44 | * the text of the {@link Label} | |
45 | * | |
46 | * @return the new {@link Label} | |
47 | */ | |
e119a1c1 | 48 | static public Label createLabel(ColorOption el, String text) { |
f82bad11 NR |
49 | if (text == null) |
50 | text = ""; | |
51 | ||
a3b510ab NR |
52 | Label lbl = new Label(text); |
53 | themeLabel(el, lbl); | |
54 | return lbl; | |
55 | } | |
56 | ||
2a96e7b2 | 57 | /** |
e119a1c1 | 58 | * Theme a {@link Label} with the colours of the given {@link ColorOption}. |
2a96e7b2 NR |
59 | * |
60 | * @param el | |
e119a1c1 | 61 | * the {@link ColorOption} |
2a96e7b2 NR |
62 | * @param lbl |
63 | * the {@link Label} | |
64 | */ | |
e119a1c1 NR |
65 | static public void themeLabel(ColorOption el, Label lbl) { |
66 | lbl.setForegroundColor(getForegroundColor(el)); | |
67 | lbl.setBackgroundColor(getBackgroundColor(el)); | |
a3b510ab NR |
68 | } |
69 | ||
2a96e7b2 NR |
70 | /** |
71 | * Return the background colour of the given element. | |
72 | * | |
73 | * @param el | |
e119a1c1 | 74 | * the {@link ColorOption} |
2a96e7b2 NR |
75 | * |
76 | * @return its background colour | |
77 | */ | |
e119a1c1 NR |
78 | static public TextColor getBackgroundColor(ColorOption el) { |
79 | if (!getInstance().colorMap.containsKey(el.name() + "_BG")) { | |
176a8327 NR |
80 | String value = null; |
81 | try { | |
e119a1c1 | 82 | value = getInstance().map.getString(el.name() + "_BG"); |
176a8327 NR |
83 | } catch (MissingResourceException mre) { |
84 | value = null; | |
85 | } | |
e119a1c1 | 86 | getInstance().colorMap.put(el.name() + "_BG", |
2a96e7b2 | 87 | convertToColor(value, TextColor.ANSI.BLACK)); |
a3b510ab NR |
88 | } |
89 | ||
e119a1c1 | 90 | return getInstance().colorMap.get(el.name() + "_BG"); |
a3b510ab NR |
91 | } |
92 | ||
2a96e7b2 NR |
93 | /** |
94 | * Return the foreground colour of the given element. | |
95 | * | |
96 | * @param el | |
e119a1c1 | 97 | * the {@link ColorOption} |
2a96e7b2 NR |
98 | * |
99 | * @return its foreground colour | |
100 | */ | |
e119a1c1 NR |
101 | static public TextColor getForegroundColor(ColorOption el) { |
102 | if (!getInstance().colorMap.containsKey(el.name() + "_FG")) { | |
176a8327 NR |
103 | String value = null; |
104 | try { | |
e119a1c1 | 105 | value = getInstance().map.getString(el.name() + "_FG"); |
176a8327 NR |
106 | } catch (MissingResourceException mre) { |
107 | value = null; | |
108 | } | |
e119a1c1 | 109 | getInstance().colorMap.put(el.name() + "_FG", |
2a96e7b2 | 110 | convertToColor(value, TextColor.ANSI.WHITE)); |
a3b510ab NR |
111 | } |
112 | ||
e119a1c1 | 113 | return getInstance().colorMap.get(el.name() + "_FG"); |
a3b510ab NR |
114 | } |
115 | ||
ed91f27a NR |
116 | /** |
117 | * Return a {@link Theme} following the colours defined in | |
118 | * display.properties. | |
119 | * | |
120 | * @return the {@link Theme} | |
121 | */ | |
122 | static Theme getCustomTheme() { | |
123 | // Create a properties-theme with our own custom values for some of it | |
124 | Properties properties = new Properties(); | |
125 | try { | |
126 | ClassLoader classLoader = AbstractTextGUI.class.getClassLoader(); | |
127 | InputStream resourceAsStream = classLoader | |
128 | .getResourceAsStream("default-theme.properties"); | |
129 | if (resourceAsStream == null) { | |
130 | resourceAsStream = new FileInputStream( | |
131 | "src/main/resources/default-theme.properties"); | |
132 | } | |
133 | properties.load(resourceAsStream); | |
134 | resourceAsStream.close(); | |
135 | } catch (IOException e) { | |
136 | } | |
137 | properties.put("com.googlecode.lanterna.background", "black"); | |
138 | PropertiesTheme theme = new PropertiesTheme(properties); | |
139 | ||
140 | return theme; | |
141 | } | |
142 | ||
2a96e7b2 NR |
143 | /** |
144 | * Get the (unique) instance of this class. | |
145 | * | |
146 | * @return the (unique) instance | |
147 | */ | |
e119a1c1 | 148 | static private UiColors getInstance() { |
2a96e7b2 NR |
149 | synchronized (lock) { |
150 | if (instance == null) | |
151 | instance = new UiColors(); | |
152 | } | |
153 | ||
154 | return instance; | |
a3b510ab NR |
155 | } |
156 | ||
2a96e7b2 NR |
157 | /** |
158 | * Convert the given {@link String} value to a {@link TextColor}. | |
159 | * | |
160 | * @param value | |
161 | * the {@link String} to convert | |
162 | * @param defaultColor | |
163 | * the default {@link TextColor} to return if the conversion | |
164 | * failed | |
165 | * | |
166 | * @return the converted colour | |
167 | */ | |
168 | static private TextColor convertToColor(String value, TextColor defaultColor) { | |
169 | try { | |
170 | if (value.startsWith("@")) { | |
171 | int r = Integer.parseInt(value.substring(1, 3), 16); | |
172 | int g = Integer.parseInt(value.substring(3, 5), 16); | |
173 | int b = Integer.parseInt(value.substring(5, 7), 16); | |
174 | return TextColor.Indexed.fromRGB(r, g, b); | |
175 | } else if (value.startsWith("#")) { | |
176 | int r = Integer.parseInt(value.substring(1, 3), 16); | |
177 | int g = Integer.parseInt(value.substring(3, 5), 16); | |
178 | int b = Integer.parseInt(value.substring(5, 7), 16); | |
179 | return new TextColor.RGB(r, g, b); | |
1c03abaf NR |
180 | } else if (value.replaceAll("[0-9]*", "").length() == 0) { |
181 | return new TextColor.Indexed(Integer.parseInt(value)); | |
2a96e7b2 NR |
182 | } else { |
183 | return TextColor.ANSI.valueOf(value); | |
184 | } | |
185 | } catch (Exception e) { | |
186 | new Exception("Cannot convert value to colour: " + value, e) | |
187 | .printStackTrace(); | |
188 | } | |
189 | ||
190 | return defaultColor; | |
a3b510ab | 191 | } |
a3b510ab | 192 | } |