Fix inclusion of lanterna's resources + fix warning
[jvcard.git] / src / be / nikiroo / jvcard / tui / UiColors.java
1 package be.nikiroo.jvcard.tui;
2
3 import java.io.FileInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.MissingResourceException;
9 import java.util.Properties;
10
11 import be.nikiroo.jvcard.resources.bundles.ColorBundle;
12 import be.nikiroo.jvcard.resources.enums.ColorOption;
13
14 import com.googlecode.lanterna.TextColor;
15 import com.googlecode.lanterna.graphics.PropertiesTheme;
16 import com.googlecode.lanterna.graphics.Theme;
17 import com.googlecode.lanterna.gui2.AbstractTextGUI;
18 import com.googlecode.lanterna.gui2.Label;
19
20 /**
21 * All colour information must come from here.
22 *
23 * @author niki
24 *
25 */
26 public class UiColors extends ColorBundle {
27 static private Object lock = new Object();
28 static private UiColors instance = null;
29
30 private Map<String, TextColor> colorMap = null;
31
32 private UiColors() {
33 super();
34 colorMap = new HashMap<String, TextColor>();
35 }
36
37 /**
38 * Create a new {@link Label} with the colours of the given
39 * {@link ColorOption}.
40 *
41 * @param el
42 * the {@link ColorOption}
43 * @param text
44 * the text of the {@link Label}
45 *
46 * @return the new {@link Label}
47 */
48 static public Label createLabel(ColorOption el, String text) {
49 if (text == null)
50 text = "";
51
52 Label lbl = new Label(text);
53 themeLabel(el, lbl);
54 return lbl;
55 }
56
57 /**
58 * Theme a {@link Label} with the colours of the given {@link ColorOption}.
59 *
60 * @param el
61 * the {@link ColorOption}
62 * @param lbl
63 * the {@link Label}
64 */
65 static public void themeLabel(ColorOption el, Label lbl) {
66 lbl.setForegroundColor(getForegroundColor(el));
67 lbl.setBackgroundColor(getBackgroundColor(el));
68 }
69
70 /**
71 * Return the background colour of the given element.
72 *
73 * @param el
74 * the {@link ColorOption}
75 *
76 * @return its background colour
77 */
78 static public TextColor getBackgroundColor(ColorOption el) {
79 if (!getInstance().colorMap.containsKey(el.name() + "_BG")) {
80 String value = null;
81 try {
82 value = getInstance().map.getString(el.name() + "_BG");
83 } catch (MissingResourceException mre) {
84 value = null;
85 }
86 getInstance().colorMap.put(el.name() + "_BG",
87 convertToColor(value, TextColor.ANSI.BLACK));
88 }
89
90 return getInstance().colorMap.get(el.name() + "_BG");
91 }
92
93 /**
94 * Return the foreground colour of the given element.
95 *
96 * @param el
97 * the {@link ColorOption}
98 *
99 * @return its foreground colour
100 */
101 static public TextColor getForegroundColor(ColorOption el) {
102 if (!getInstance().colorMap.containsKey(el.name() + "_FG")) {
103 String value = null;
104 try {
105 value = getInstance().map.getString(el.name() + "_FG");
106 } catch (MissingResourceException mre) {
107 value = null;
108 }
109 getInstance().colorMap.put(el.name() + "_FG",
110 convertToColor(value, TextColor.ANSI.WHITE));
111 }
112
113 return getInstance().colorMap.get(el.name() + "_FG");
114 }
115
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
143 /**
144 * Get the (unique) instance of this class.
145 *
146 * @return the (unique) instance
147 */
148 static private UiColors getInstance() {
149 synchronized (lock) {
150 if (instance == null)
151 instance = new UiColors();
152 }
153
154 return instance;
155 }
156
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);
180 } else if (value.replaceAll("[0-9]*", "").length() == 0) {
181 return new TextColor.Indexed(Integer.parseInt(value));
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;
191 }
192 }