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;
25 private boolean utf
= true;
28 colorMap
= new HashMap
<String
, TextColor
>();
29 bundle
= Bundles
.getBundle("colors");
33 * Represent an element that can be coloured (foreground/background
41 TITLE_MAIN
, TITLE_VARIABLE
, TITLE_COUNT
, //
42 ACTION_KEY
, ACTION_DESC
, //
43 LINE_MESSAGE
, LINE_MESSAGE_ERR
, LINE_MESSAGE_QUESTION
, LINE_MESSAGE_ANS
, //
44 CONTACT_LINE
, CONTACT_LINE_SEPARATOR
, CONTACT_LINE_SELECTED
, CONTACT_LINE_SEPARATOR_SELECTED
, CONTACT_LINE_DIRTY
, CONTACT_LINE_DIRTY_SELECTED
, //
45 VIEW_CONTACT_NAME
, VIEW_CONTACT_NORMAL
, VIEW_CONTACT_NOTES_TITLE
, //
49 * Get the foreground colour of this element.
53 public TextColor
getForegroundColor() {
54 return UiColors
.getInstance().getForegroundColor(this);
58 * Get the background colour of this element.
62 public TextColor
getBackgroundColor() {
63 return UiColors
.getInstance().getBackgroundColor(this);
67 * Create a new {@link Label} with the colours of this {@link Element}.
70 * the text of the {@link Label}
72 * @return the new {@link Label}
74 public Label
createLabel(String text
) {
75 return UiColors
.getInstance().createLabel(this, text
);
79 * Theme a {@link Label} with the colours of this {@link Element}.
84 public void themeLabel(Label lbl
) {
85 UiColors
.getInstance().themeLabel(this, lbl
);
90 * Check if unicode characters should be used.
92 * @return TRUE to allow unicode
94 public boolean isUnicode() {
99 * Allow or disallow unicode characters in the program.
102 * TRUE to allow unuciode, FALSE to only allow ASCII characters
104 public void setUnicode(boolean utf
) {
109 * Create a new {@link Label} with the colours of the given {@link Element}.
112 * the {@link Element}
114 * the text of the {@link Label}
116 * @return the new {@link Label}
118 private Label
createLabel(Element el
, String text
) {
122 Label lbl
= new Label(text
);
128 * Theme a {@link Label} with the colours of the given {@link Element}.
131 * the {@link Element}
135 private void themeLabel(Element el
, Label lbl
) {
136 lbl
.setForegroundColor(el
.getForegroundColor());
137 lbl
.setBackgroundColor(el
.getBackgroundColor());
141 * Return the background colour of the given element.
144 * the {@link Element}
146 * @return its background colour
148 private TextColor
getBackgroundColor(Element el
) {
149 if (!colorMap
.containsKey(el
.name() + "_BG")) {
152 value
= bundle
.getString(el
.name() + "_BG");
153 } catch (MissingResourceException mre
) {
156 colorMap
.put(el
.name() + "_BG",
157 convertToColor(value
, TextColor
.ANSI
.BLACK
));
160 return colorMap
.get(el
.name() + "_BG");
164 * Return the foreground colour of the given element.
167 * the {@link Element}
169 * @return its foreground colour
171 private TextColor
getForegroundColor(Element el
) {
172 if (!colorMap
.containsKey(el
.name() + "_FG")) {
175 value
= bundle
.getString(el
.name() + "_FG");
176 } catch (MissingResourceException mre
) {
179 colorMap
.put(el
.name() + "_FG",
180 convertToColor(value
, TextColor
.ANSI
.WHITE
));
183 return colorMap
.get(el
.name() + "_FG");
187 * Get the (unique) instance of this class.
189 * @return the (unique) instance
191 static public UiColors
getInstance() {
192 synchronized (lock
) {
193 if (instance
== null)
194 instance
= new UiColors();
201 * Convert the given {@link String} value to a {@link TextColor}.
204 * the {@link String} to convert
205 * @param defaultColor
206 * the default {@link TextColor} to return if the conversion
209 * @return the converted colour
211 static private TextColor
convertToColor(String value
, TextColor defaultColor
) {
213 if (value
.startsWith("@")) {
214 int r
= Integer
.parseInt(value
.substring(1, 3), 16);
215 int g
= Integer
.parseInt(value
.substring(3, 5), 16);
216 int b
= Integer
.parseInt(value
.substring(5, 7), 16);
217 return TextColor
.Indexed
.fromRGB(r
, g
, b
);
218 } else if (value
.startsWith("#")) {
219 int r
= Integer
.parseInt(value
.substring(1, 3), 16);
220 int g
= Integer
.parseInt(value
.substring(3, 5), 16);
221 int b
= Integer
.parseInt(value
.substring(5, 7), 16);
222 return new TextColor
.RGB(r
, g
, b
);
223 } else if (value
.replaceAll("[0-9]*", "").length() == 0) {
224 return new TextColor
.Indexed(Integer
.parseInt(value
));
226 return TextColor
.ANSI
.valueOf(value
);
228 } catch (Exception e
) {
229 new Exception("Cannot convert value to colour: " + value
, e
)