1 package be
.nikiroo
.jvcard
.tui
;
3 import java
.util
.HashMap
;
5 import java
.util
.ResourceBundle
;
7 import be
.nikiroo
.jvcard
.resources
.Bundles
;
9 import com
.googlecode
.lanterna
.TextColor
;
10 import com
.googlecode
.lanterna
.gui2
.Label
;
13 * All colour information must come from here.
18 public class UiColors
{
19 static private Object lock
= new Object();
20 static private UiColors instance
= null;
22 private ResourceBundle bundle
= null;
23 private Map
<String
, TextColor
> colorMap
= null;
24 private boolean utf
= true;
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 * Check if unicode characters should be used.
91 * @return TRUE to allow unicode
93 public boolean isUnicode() {
98 * Allow or disallow unicode characters in the program.
101 * TRUE to allow unuciode, FALSE to only allow ASCII characters
103 public void setUnicode(boolean utf
) {
108 * Create a new {@link Label} with the colours of the given {@link Element}.
111 * the {@link Element}
113 * the text of the {@link Label}
115 * @return the new {@link Label}
117 private Label
createLabel(Element el
, String text
) {
121 Label lbl
= new Label(text
);
127 * Theme a {@link Label} with the colours of the given {@link Element}.
130 * the {@link Element}
134 private void themeLabel(Element el
, Label lbl
) {
135 lbl
.setForegroundColor(el
.getForegroundColor());
136 lbl
.setBackgroundColor(el
.getBackgroundColor());
140 * Return the background colour of the given element.
143 * the {@link Element}
145 * @return its background colour
147 private TextColor
getBackgroundColor(Element el
) {
148 if (!colorMap
.containsKey(el
.name() + "_BG")) {
149 String value
= bundle
.getString(el
.name() + "_BG");
150 colorMap
.put(el
.name() + "_BG",
151 convertToColor(value
, TextColor
.ANSI
.BLACK
));
154 return colorMap
.get(el
.name() + "_BG");
158 * Return the foreground colour of the given element.
161 * the {@link Element}
163 * @return its foreground colour
165 private TextColor
getForegroundColor(Element el
) {
166 if (!colorMap
.containsKey(el
.name() + "_FG")) {
167 String value
= bundle
.getString(el
.name() + "_FG");
168 colorMap
.put(el
.name() + "_FG",
169 convertToColor(value
, TextColor
.ANSI
.WHITE
));
172 return colorMap
.get(el
.name() + "_FG");
176 * Get the (unique) instance of this class.
178 * @return the (unique) instance
180 static public UiColors
getInstance() {
181 synchronized (lock
) {
182 if (instance
== null)
183 instance
= new UiColors();
190 * Convert the given {@link String} value to a {@link TextColor}.
193 * the {@link String} to convert
194 * @param defaultColor
195 * the default {@link TextColor} to return if the conversion
198 * @return the converted colour
200 static private TextColor
convertToColor(String value
, TextColor defaultColor
) {
202 if (value
.startsWith("@")) {
203 int r
= Integer
.parseInt(value
.substring(1, 3), 16);
204 int g
= Integer
.parseInt(value
.substring(3, 5), 16);
205 int b
= Integer
.parseInt(value
.substring(5, 7), 16);
206 return TextColor
.Indexed
.fromRGB(r
, g
, b
);
207 } else if (value
.startsWith("#")) {
208 int r
= Integer
.parseInt(value
.substring(1, 3), 16);
209 int g
= Integer
.parseInt(value
.substring(3, 5), 16);
210 int b
= Integer
.parseInt(value
.substring(5, 7), 16);
211 return new TextColor
.RGB(r
, g
, b
);
213 return TextColor
.ANSI
.valueOf(value
);
215 } catch (Exception e
) {
216 new Exception("Cannot convert value to colour: " + value
, e
)