1 package be
.nikiroo
.jvcard
.tui
;
3 import java
.io
.FileInputStream
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
7 import java
.util
.HashMap
;
9 import java
.util
.MissingResourceException
;
10 import java
.util
.Properties
;
12 import be
.nikiroo
.jvcard
.resources
.ColorBundle
;
13 import be
.nikiroo
.jvcard
.resources
.ColorOption
;
14 import be
.nikiroo
.jvcard
.resources
.Target
;
15 import be
.nikiroo
.utils
.resources
.Bundle
;
17 import com
.googlecode
.lanterna
.TextColor
;
18 import com
.googlecode
.lanterna
.graphics
.PropertiesTheme
;
19 import com
.googlecode
.lanterna
.graphics
.Theme
;
20 import com
.googlecode
.lanterna
.gui2
.AbstractTextGUI
;
21 import com
.googlecode
.lanterna
.gui2
.Label
;
24 * All colour information must come from here.
29 public class UiColors
{
30 static private Object lock
= new Object();
31 static private UiColors instance
= null;
33 private Map
<String
, TextColor
> colorMap
= null;
34 private ColorBundle bundle
;
37 bundle
= new ColorBundle();
38 colorMap
= new HashMap
<String
, TextColor
>();
42 * Return a {@link Theme} following the colours defined in
45 * @return the {@link Theme}
47 static public Theme
getCustomTheme() {
48 // Create a properties-theme with our own custom values for some of it
49 Properties properties
= new Properties();
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");
58 properties
.load(resourceAsStream
);
59 resourceAsStream
.close();
60 } catch (IOException e
) {
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
);
74 // no bold on borders prelight:
76 .put("com.googlecode.lanterna.gui2.Borders$StandardBorder.sgr[PRELIGHT]",
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
);
86 PropertiesTheme theme
= new PropertiesTheme(properties
);
91 * Create a new {@link Label} with the colours of the given
92 * {@link ColorOption}.
95 * the {@link ColorOption}
97 * the text of the {@link Label}
99 * @return the new {@link Label}
101 static public Label
createLabel(ColorOption el
, String text
) {
105 Label lbl
= new Label(text
);
111 * Theme a {@link Label} with the colours of the given {@link ColorOption}.
114 * the {@link ColorOption}
118 static public void themeLabel(ColorOption el
, Label lbl
) {
119 lbl
.setForegroundColor(getForegroundColor(el
));
120 lbl
.setBackgroundColor(getBackgroundColor(el
));
124 * Return the background colour of the given element.
127 * the {@link ColorOption}
129 * @return its background colour
131 static public TextColor
getBackgroundColor(ColorOption el
) {
132 if (!getInstance().colorMap
.containsKey(el
.name() + "_BG")) {
135 value
= getInstance().bundle
.getStringX(el
, "BG");
136 } catch (MissingResourceException mre
) {
139 getInstance().colorMap
.put(el
.name() + "_BG",
140 convertToColor(value
, TextColor
.ANSI
.BLACK
));
143 return getInstance().colorMap
.get(el
.name() + "_BG");
147 * Return the foreground colour of the given element.
150 * the {@link ColorOption}
152 * @return its foreground colour
154 static public TextColor
getForegroundColor(ColorOption el
) {
155 if (!getInstance().colorMap
.containsKey(el
.name() + "_FG")) {
158 value
= getInstance().bundle
.getStringX(el
, "FG");
159 } catch (MissingResourceException mre
) {
162 getInstance().colorMap
.put(el
.name() + "_FG",
163 convertToColor(value
, TextColor
.ANSI
.WHITE
));
166 return getInstance().colorMap
.get(el
.name() + "_FG");
170 * Get the (unique) instance of this class.
172 * @return the (unique) instance
174 static private UiColors
getInstance() {
175 synchronized (lock
) {
176 if (instance
== null)
177 instance
= new UiColors();
184 * Convert the given {@link String} value to a {@link TextColor}.
187 * the {@link String} to convert
188 * @param defaultColor
189 * the default {@link TextColor} to return if the conversion
192 * @return the converted colour
194 static private TextColor
convertToColor(String value
, TextColor defaultColor
) {
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
);
206 } else if (value
.replaceAll("[0-9]*", "").length() == 0) {
207 return new TextColor
.Indexed(Integer
.parseInt(value
));
209 return TextColor
.ANSI
.valueOf(value
);
211 } catch (Exception e
) {
212 new Exception("Cannot convert value to colour: " + value
, e
)