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