1 package be
.nikiroo
.jvcard
.tui
;
3 import java
.util
.HashMap
;
5 import java
.util
.MissingResourceException
;
6 import java
.util
.ResourceBundle
;
8 import be
.nikiroo
.jvcard
.resources
.Bundles
;
10 import com
.googlecode
.lanterna
.TextColor
;
11 import com
.googlecode
.lanterna
.gui2
.Label
;
14 * All colour information must come from here.
19 public class UiColors
{
20 static private Object lock
= new Object();
21 static private UiColors instance
= null;
23 private ResourceBundle bundle
= null;
24 private Map
<String
, TextColor
> colorMap
= null;
27 colorMap
= new HashMap
<String
, TextColor
>();
28 bundle
= Bundles
.getBundle("colors");
32 * Represent an element that can be coloured (foreground/background
40 TITLE_MAIN
, TITLE_VARIABLE
, TITLE_COUNT
, //
41 ACTION_KEY
, ACTION_DESC
, //
42 LINE_MESSAGE
, LINE_MESSAGE_ERR
, LINE_MESSAGE_QUESTION
, LINE_MESSAGE_ANS
, //
43 CONTACT_LINE
, CONTACT_LINE_SEPARATOR
, CONTACT_LINE_SELECTED
, CONTACT_LINE_SEPARATOR_SELECTED
, CONTACT_LINE_DIRTY
, CONTACT_LINE_DIRTY_SELECTED
, //
44 VIEW_CONTACT_NAME
, VIEW_CONTACT_NORMAL
, VIEW_CONTACT_NOTES_TITLE
, //
48 * Get the foreground colour of this element.
52 public TextColor
getForegroundColor() {
53 return UiColors
.getInstance().getForegroundColor(this);
57 * Get the background colour of this element.
61 public TextColor
getBackgroundColor() {
62 return UiColors
.getInstance().getBackgroundColor(this);
66 * Create a new {@link Label} with the colours of this {@link Element}.
69 * the text of the {@link Label}
71 * @return the new {@link Label}
73 public Label
createLabel(String text
) {
74 return UiColors
.getInstance().createLabel(this, text
);
78 * Theme a {@link Label} with the colours of this {@link Element}.
83 public void themeLabel(Label lbl
) {
84 UiColors
.getInstance().themeLabel(this, lbl
);
89 * Create a new {@link Label} with the colours of the given {@link Element}.
94 * the text of the {@link Label}
96 * @return the new {@link Label}
98 private Label
createLabel(Element el
, String text
) {
102 Label lbl
= new Label(text
);
108 * Theme a {@link Label} with the colours of the given {@link Element}.
111 * the {@link Element}
115 private void themeLabel(Element el
, Label lbl
) {
116 lbl
.setForegroundColor(el
.getForegroundColor());
117 lbl
.setBackgroundColor(el
.getBackgroundColor());
121 * Return the background colour of the given element.
124 * the {@link Element}
126 * @return its background colour
128 private TextColor
getBackgroundColor(Element el
) {
129 if (!colorMap
.containsKey(el
.name() + "_BG")) {
132 value
= bundle
.getString(el
.name() + "_BG");
133 } catch (MissingResourceException mre
) {
136 colorMap
.put(el
.name() + "_BG",
137 convertToColor(value
, TextColor
.ANSI
.BLACK
));
140 return colorMap
.get(el
.name() + "_BG");
144 * Return the foreground colour of the given element.
147 * the {@link Element}
149 * @return its foreground colour
151 private TextColor
getForegroundColor(Element el
) {
152 if (!colorMap
.containsKey(el
.name() + "_FG")) {
155 value
= bundle
.getString(el
.name() + "_FG");
156 } catch (MissingResourceException mre
) {
159 colorMap
.put(el
.name() + "_FG",
160 convertToColor(value
, TextColor
.ANSI
.WHITE
));
163 return colorMap
.get(el
.name() + "_FG");
167 * Get the (unique) instance of this class.
169 * @return the (unique) instance
171 static public UiColors
getInstance() {
172 synchronized (lock
) {
173 if (instance
== null)
174 instance
= new UiColors();
181 * Convert the given {@link String} value to a {@link TextColor}.
184 * the {@link String} to convert
185 * @param defaultColor
186 * the default {@link TextColor} to return if the conversion
189 * @return the converted colour
191 static private TextColor
convertToColor(String value
, TextColor defaultColor
) {
193 if (value
.startsWith("@")) {
194 int r
= Integer
.parseInt(value
.substring(1, 3), 16);
195 int g
= Integer
.parseInt(value
.substring(3, 5), 16);
196 int b
= Integer
.parseInt(value
.substring(5, 7), 16);
197 return TextColor
.Indexed
.fromRGB(r
, g
, b
);
198 } else if (value
.startsWith("#")) {
199 int r
= Integer
.parseInt(value
.substring(1, 3), 16);
200 int g
= Integer
.parseInt(value
.substring(3, 5), 16);
201 int b
= Integer
.parseInt(value
.substring(5, 7), 16);
202 return new TextColor
.RGB(r
, g
, b
);
203 } else if (value
.replaceAll("[0-9]*", "").length() == 0) {
204 return new TextColor
.Indexed(Integer
.parseInt(value
));
206 return TextColor
.ANSI
.valueOf(value
);
208 } catch (Exception e
) {
209 new Exception("Cannot convert value to colour: " + value
, e
)