Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | package be.nikiroo.jvcard.tui; |
2 | ||
3 | import java.util.HashMap; | |
4 | import java.util.Map; | |
176a8327 | 5 | import java.util.MissingResourceException; |
2a96e7b2 | 6 | |
e119a1c1 NR |
7 | import be.nikiroo.jvcard.resources.bundles.ColorBundle; |
8 | import be.nikiroo.jvcard.resources.enums.ColorOption; | |
a3b510ab NR |
9 | |
10 | import com.googlecode.lanterna.TextColor; | |
11 | import com.googlecode.lanterna.gui2.Label; | |
12 | ||
13 | /** | |
14 | * All colour information must come from here. | |
15 | * | |
16 | * @author niki | |
17 | * | |
18 | */ | |
e119a1c1 | 19 | public class UiColors extends ColorBundle { |
a3b510ab NR |
20 | static private Object lock = new Object(); |
21 | static private UiColors instance = null; | |
22 | ||
2a96e7b2 | 23 | private Map<String, TextColor> colorMap = null; |
a3b510ab | 24 | |
2a96e7b2 | 25 | private UiColors() { |
e119a1c1 | 26 | super(); |
2a96e7b2 | 27 | colorMap = new HashMap<String, TextColor>(); |
2a96e7b2 NR |
28 | } |
29 | ||
a3b510ab | 30 | /** |
e119a1c1 | 31 | * Create a new {@link Label} with the colours of the given {@link ColorOption}. |
2a96e7b2 NR |
32 | * |
33 | * @param el | |
e119a1c1 | 34 | * the {@link ColorOption} |
2a96e7b2 NR |
35 | * @param text |
36 | * the text of the {@link Label} | |
37 | * | |
38 | * @return the new {@link Label} | |
39 | */ | |
e119a1c1 | 40 | static public Label createLabel(ColorOption el, String text) { |
f82bad11 NR |
41 | if (text == null) |
42 | text = ""; | |
43 | ||
a3b510ab NR |
44 | Label lbl = new Label(text); |
45 | themeLabel(el, lbl); | |
46 | return lbl; | |
47 | } | |
48 | ||
2a96e7b2 | 49 | /** |
e119a1c1 | 50 | * Theme a {@link Label} with the colours of the given {@link ColorOption}. |
2a96e7b2 NR |
51 | * |
52 | * @param el | |
e119a1c1 | 53 | * the {@link ColorOption} |
2a96e7b2 NR |
54 | * @param lbl |
55 | * the {@link Label} | |
56 | */ | |
e119a1c1 NR |
57 | static public void themeLabel(ColorOption el, Label lbl) { |
58 | lbl.setForegroundColor(getForegroundColor(el)); | |
59 | lbl.setBackgroundColor(getBackgroundColor(el)); | |
a3b510ab NR |
60 | } |
61 | ||
2a96e7b2 NR |
62 | /** |
63 | * Return the background colour of the given element. | |
64 | * | |
65 | * @param el | |
e119a1c1 | 66 | * the {@link ColorOption} |
2a96e7b2 NR |
67 | * |
68 | * @return its background colour | |
69 | */ | |
e119a1c1 NR |
70 | static public TextColor getBackgroundColor(ColorOption el) { |
71 | if (!getInstance().colorMap.containsKey(el.name() + "_BG")) { | |
176a8327 NR |
72 | String value = null; |
73 | try { | |
e119a1c1 | 74 | value = getInstance().map.getString(el.name() + "_BG"); |
176a8327 NR |
75 | } catch (MissingResourceException mre) { |
76 | value = null; | |
77 | } | |
e119a1c1 | 78 | getInstance().colorMap.put(el.name() + "_BG", |
2a96e7b2 | 79 | convertToColor(value, TextColor.ANSI.BLACK)); |
a3b510ab NR |
80 | } |
81 | ||
e119a1c1 | 82 | return getInstance().colorMap.get(el.name() + "_BG"); |
a3b510ab NR |
83 | } |
84 | ||
2a96e7b2 NR |
85 | /** |
86 | * Return the foreground colour of the given element. | |
87 | * | |
88 | * @param el | |
e119a1c1 | 89 | * the {@link ColorOption} |
2a96e7b2 NR |
90 | * |
91 | * @return its foreground colour | |
92 | */ | |
e119a1c1 NR |
93 | static public TextColor getForegroundColor(ColorOption el) { |
94 | if (!getInstance().colorMap.containsKey(el.name() + "_FG")) { | |
176a8327 NR |
95 | String value = null; |
96 | try { | |
e119a1c1 | 97 | value = getInstance().map.getString(el.name() + "_FG"); |
176a8327 NR |
98 | } catch (MissingResourceException mre) { |
99 | value = null; | |
100 | } | |
e119a1c1 | 101 | getInstance().colorMap.put(el.name() + "_FG", |
2a96e7b2 | 102 | convertToColor(value, TextColor.ANSI.WHITE)); |
a3b510ab NR |
103 | } |
104 | ||
e119a1c1 | 105 | return getInstance().colorMap.get(el.name() + "_FG"); |
a3b510ab NR |
106 | } |
107 | ||
2a96e7b2 NR |
108 | /** |
109 | * Get the (unique) instance of this class. | |
110 | * | |
111 | * @return the (unique) instance | |
112 | */ | |
e119a1c1 | 113 | static private UiColors getInstance() { |
2a96e7b2 NR |
114 | synchronized (lock) { |
115 | if (instance == null) | |
116 | instance = new UiColors(); | |
117 | } | |
118 | ||
119 | return instance; | |
a3b510ab NR |
120 | } |
121 | ||
2a96e7b2 NR |
122 | /** |
123 | * Convert the given {@link String} value to a {@link TextColor}. | |
124 | * | |
125 | * @param value | |
126 | * the {@link String} to convert | |
127 | * @param defaultColor | |
128 | * the default {@link TextColor} to return if the conversion | |
129 | * failed | |
130 | * | |
131 | * @return the converted colour | |
132 | */ | |
133 | static private TextColor convertToColor(String value, TextColor defaultColor) { | |
134 | try { | |
135 | if (value.startsWith("@")) { | |
136 | int r = Integer.parseInt(value.substring(1, 3), 16); | |
137 | int g = Integer.parseInt(value.substring(3, 5), 16); | |
138 | int b = Integer.parseInt(value.substring(5, 7), 16); | |
139 | return TextColor.Indexed.fromRGB(r, g, b); | |
140 | } else if (value.startsWith("#")) { | |
141 | int r = Integer.parseInt(value.substring(1, 3), 16); | |
142 | int g = Integer.parseInt(value.substring(3, 5), 16); | |
143 | int b = Integer.parseInt(value.substring(5, 7), 16); | |
144 | return new TextColor.RGB(r, g, b); | |
1c03abaf NR |
145 | } else if (value.replaceAll("[0-9]*", "").length() == 0) { |
146 | return new TextColor.Indexed(Integer.parseInt(value)); | |
2a96e7b2 NR |
147 | } else { |
148 | return TextColor.ANSI.valueOf(value); | |
149 | } | |
150 | } catch (Exception e) { | |
151 | new Exception("Cannot convert value to colour: " + value, e) | |
152 | .printStackTrace(); | |
153 | } | |
154 | ||
155 | return defaultColor; | |
a3b510ab | 156 | } |
a3b510ab | 157 | } |