Fix some default colours
[jvcard.git] / src / be / nikiroo / jvcard / tui / UiColors.java
CommitLineData
a3b510ab
NR
1package be.nikiroo.jvcard.tui;
2
ed91f27a
NR
3import java.io.FileInputStream;
4import java.io.IOException;
5import java.io.InputStream;
a3b510ab
NR
6import java.util.HashMap;
7import java.util.Map;
176a8327 8import java.util.MissingResourceException;
ed91f27a 9import java.util.Properties;
2a96e7b2 10
e119a1c1
NR
11import be.nikiroo.jvcard.resources.bundles.ColorBundle;
12import be.nikiroo.jvcard.resources.enums.ColorOption;
a3b510ab
NR
13
14import com.googlecode.lanterna.TextColor;
ed91f27a
NR
15import com.googlecode.lanterna.graphics.PropertiesTheme;
16import com.googlecode.lanterna.graphics.Theme;
17import com.googlecode.lanterna.gui2.AbstractTextGUI;
a3b510ab
NR
18import com.googlecode.lanterna.gui2.Label;
19
20/**
21 * All colour information must come from here.
22 *
23 * @author niki
24 *
25 */
e119a1c1 26public 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
bdcf4306
NR
37 /**
38 * Return a {@link Theme} following the colours defined in
39 * colors.properties.
40 *
41 * @return the {@link Theme}
42 */
43 static public Theme getCustomTheme() {
44 // Create a properties-theme with our own custom values for some of it
45 Properties properties = new Properties();
46 try {
47 ClassLoader classLoader = AbstractTextGUI.class.getClassLoader();
48 InputStream resourceAsStream = classLoader
49 .getResourceAsStream("default-theme.properties");
50 if (resourceAsStream == null) {
51 resourceAsStream = new FileInputStream(
52 "src/main/resources/default-theme.properties");
53 }
54 properties.load(resourceAsStream);
55 resourceAsStream.close();
56 } catch (IOException e) {
57 }
58
59 // default colours:
60 String fg = getForegroundColor(ColorOption.DEFAULT).toString();
61 String bg = getBackgroundColor(ColorOption.DEFAULT).toString();
62 for (String def : new String[] { "com.googlecode.lanterna",
63 "com.googlecode.lanterna.gui2.TextBox",
64 "com.googlecode.lanterna.gui2.AbstractListBox",
65 "com.googlecode.lanterna.gui2.Borders$StandardBorder" }) {
66 properties.put(def + ".foreground", fg);
67 properties.put(def + ".background", bg);
68 }
69
70 // no bold on borders prelight:
71 properties
72 .put("com.googlecode.lanterna.gui2.Borders$StandardBorder.sgr[PRELIGHT]",
73 "");
74
75 // line answers:
76 fg = getForegroundColor(ColorOption.LINE_MESSAGE_ANS).toString();
77 bg = getBackgroundColor(ColorOption.LINE_MESSAGE_ANS).toString();
78 String prop = "com.googlecode.lanterna.gui2.TextBox";
79 properties.put(prop + ".foreground[ACTIVE]", fg);
80 properties.put(prop + ".background[ACTIVE]", bg);
81
82 PropertiesTheme theme = new PropertiesTheme(properties);
83 return theme;
84 }
85
a3b510ab 86 /**
ed91f27a
NR
87 * Create a new {@link Label} with the colours of the given
88 * {@link ColorOption}.
2a96e7b2
NR
89 *
90 * @param el
e119a1c1 91 * the {@link ColorOption}
2a96e7b2
NR
92 * @param text
93 * the text of the {@link Label}
94 *
95 * @return the new {@link Label}
96 */
e119a1c1 97 static public Label createLabel(ColorOption el, String text) {
f82bad11
NR
98 if (text == null)
99 text = "";
100
a3b510ab
NR
101 Label lbl = new Label(text);
102 themeLabel(el, lbl);
103 return lbl;
104 }
105
2a96e7b2 106 /**
e119a1c1 107 * Theme a {@link Label} with the colours of the given {@link ColorOption}.
2a96e7b2
NR
108 *
109 * @param el
e119a1c1 110 * the {@link ColorOption}
2a96e7b2
NR
111 * @param lbl
112 * the {@link Label}
113 */
e119a1c1
NR
114 static public void themeLabel(ColorOption el, Label lbl) {
115 lbl.setForegroundColor(getForegroundColor(el));
116 lbl.setBackgroundColor(getBackgroundColor(el));
a3b510ab
NR
117 }
118
2a96e7b2
NR
119 /**
120 * Return the background colour of the given element.
121 *
122 * @param el
e119a1c1 123 * the {@link ColorOption}
2a96e7b2
NR
124 *
125 * @return its background colour
126 */
e119a1c1
NR
127 static public TextColor getBackgroundColor(ColorOption el) {
128 if (!getInstance().colorMap.containsKey(el.name() + "_BG")) {
176a8327
NR
129 String value = null;
130 try {
e119a1c1 131 value = getInstance().map.getString(el.name() + "_BG");
176a8327
NR
132 } catch (MissingResourceException mre) {
133 value = null;
134 }
e119a1c1 135 getInstance().colorMap.put(el.name() + "_BG",
2a96e7b2 136 convertToColor(value, TextColor.ANSI.BLACK));
a3b510ab
NR
137 }
138
e119a1c1 139 return getInstance().colorMap.get(el.name() + "_BG");
a3b510ab
NR
140 }
141
2a96e7b2
NR
142 /**
143 * Return the foreground colour of the given element.
144 *
145 * @param el
e119a1c1 146 * the {@link ColorOption}
2a96e7b2
NR
147 *
148 * @return its foreground colour
149 */
e119a1c1
NR
150 static public TextColor getForegroundColor(ColorOption el) {
151 if (!getInstance().colorMap.containsKey(el.name() + "_FG")) {
176a8327
NR
152 String value = null;
153 try {
e119a1c1 154 value = getInstance().map.getString(el.name() + "_FG");
176a8327
NR
155 } catch (MissingResourceException mre) {
156 value = null;
157 }
e119a1c1 158 getInstance().colorMap.put(el.name() + "_FG",
2a96e7b2 159 convertToColor(value, TextColor.ANSI.WHITE));
a3b510ab
NR
160 }
161
e119a1c1 162 return getInstance().colorMap.get(el.name() + "_FG");
a3b510ab
NR
163 }
164
2a96e7b2
NR
165 /**
166 * Get the (unique) instance of this class.
167 *
168 * @return the (unique) instance
169 */
e119a1c1 170 static private UiColors getInstance() {
2a96e7b2
NR
171 synchronized (lock) {
172 if (instance == null)
173 instance = new UiColors();
174 }
175
176 return instance;
a3b510ab
NR
177 }
178
2a96e7b2
NR
179 /**
180 * Convert the given {@link String} value to a {@link TextColor}.
181 *
182 * @param value
183 * the {@link String} to convert
184 * @param defaultColor
185 * the default {@link TextColor} to return if the conversion
186 * failed
187 *
188 * @return the converted colour
189 */
190 static private TextColor convertToColor(String value, TextColor defaultColor) {
191 try {
192 if (value.startsWith("@")) {
193 int r = Integer.parseInt(value.substring(1, 3), 16);
194 int g = Integer.parseInt(value.substring(3, 5), 16);
195 int b = Integer.parseInt(value.substring(5, 7), 16);
196 return TextColor.Indexed.fromRGB(r, g, b);
197 } else if (value.startsWith("#")) {
198 int r = Integer.parseInt(value.substring(1, 3), 16);
199 int g = Integer.parseInt(value.substring(3, 5), 16);
200 int b = Integer.parseInt(value.substring(5, 7), 16);
201 return new TextColor.RGB(r, g, b);
1c03abaf
NR
202 } else if (value.replaceAll("[0-9]*", "").length() == 0) {
203 return new TextColor.Indexed(Integer.parseInt(value));
2a96e7b2
NR
204 } else {
205 return TextColor.ANSI.valueOf(value);
206 }
207 } catch (Exception e) {
208 new Exception("Cannot convert value to colour: " + value, e)
209 .printStackTrace();
210 }
211
212 return defaultColor;
a3b510ab 213 }
a3b510ab 214}