| 1 | package be.nikiroo.jvcard.tui; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.Map; |
| 5 | import java.util.MissingResourceException; |
| 6 | import java.util.ResourceBundle; |
| 7 | |
| 8 | import be.nikiroo.jvcard.resources.Bundles; |
| 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 | */ |
| 19 | public class UiColors { |
| 20 | static private Object lock = new Object(); |
| 21 | static private UiColors instance = null; |
| 22 | |
| 23 | private ResourceBundle bundle = null; |
| 24 | private Map<String, TextColor> colorMap = null; |
| 25 | private boolean utf = true; |
| 26 | |
| 27 | private UiColors() { |
| 28 | colorMap = new HashMap<String, TextColor>(); |
| 29 | bundle = Bundles.getBundle("colors"); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Represent an element that can be coloured (foreground/background |
| 34 | * colours). |
| 35 | * |
| 36 | * @author niki |
| 37 | * |
| 38 | */ |
| 39 | public enum Element { |
| 40 | DEFAULT, // |
| 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, // |
| 46 | ; |
| 47 | |
| 48 | /** |
| 49 | * Get the foreground colour of this element. |
| 50 | * |
| 51 | * @return the colour |
| 52 | */ |
| 53 | public TextColor getForegroundColor() { |
| 54 | return UiColors.getInstance().getForegroundColor(this); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get the background colour of this element. |
| 59 | * |
| 60 | * @return the colour |
| 61 | */ |
| 62 | public TextColor getBackgroundColor() { |
| 63 | return UiColors.getInstance().getBackgroundColor(this); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Create a new {@link Label} with the colours of this {@link Element}. |
| 68 | * |
| 69 | * @param text |
| 70 | * the text of the {@link Label} |
| 71 | * |
| 72 | * @return the new {@link Label} |
| 73 | */ |
| 74 | public Label createLabel(String text) { |
| 75 | return UiColors.getInstance().createLabel(this, text); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Theme a {@link Label} with the colours of this {@link Element}. |
| 80 | * |
| 81 | * @param lbl |
| 82 | * the {@link Label} |
| 83 | */ |
| 84 | public void themeLabel(Label lbl) { |
| 85 | UiColors.getInstance().themeLabel(this, lbl); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Check if unicode characters should be used. |
| 91 | * |
| 92 | * @return TRUE to allow unicode |
| 93 | */ |
| 94 | public boolean isUnicode() { |
| 95 | return utf; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Allow or disallow unicode characters in the program. |
| 100 | * |
| 101 | * @param utf |
| 102 | * TRUE to allow unuciode, FALSE to only allow ASCII characters |
| 103 | */ |
| 104 | public void setUnicode(boolean utf) { |
| 105 | this.utf = utf; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Create a new {@link Label} with the colours of the given {@link Element}. |
| 110 | * |
| 111 | * @param el |
| 112 | * the {@link Element} |
| 113 | * @param text |
| 114 | * the text of the {@link Label} |
| 115 | * |
| 116 | * @return the new {@link Label} |
| 117 | */ |
| 118 | private Label createLabel(Element el, String text) { |
| 119 | if (text == null) |
| 120 | text = ""; |
| 121 | |
| 122 | Label lbl = new Label(text); |
| 123 | themeLabel(el, lbl); |
| 124 | return lbl; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Theme a {@link Label} with the colours of the given {@link Element}. |
| 129 | * |
| 130 | * @param el |
| 131 | * the {@link Element} |
| 132 | * @param lbl |
| 133 | * the {@link Label} |
| 134 | */ |
| 135 | private void themeLabel(Element el, Label lbl) { |
| 136 | lbl.setForegroundColor(el.getForegroundColor()); |
| 137 | lbl.setBackgroundColor(el.getBackgroundColor()); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Return the background colour of the given element. |
| 142 | * |
| 143 | * @param el |
| 144 | * the {@link Element} |
| 145 | * |
| 146 | * @return its background colour |
| 147 | */ |
| 148 | private TextColor getBackgroundColor(Element el) { |
| 149 | if (!colorMap.containsKey(el.name() + "_BG")) { |
| 150 | String value = null; |
| 151 | try { |
| 152 | value = bundle.getString(el.name() + "_BG"); |
| 153 | } catch (MissingResourceException mre) { |
| 154 | value = null; |
| 155 | } |
| 156 | colorMap.put(el.name() + "_BG", |
| 157 | convertToColor(value, TextColor.ANSI.BLACK)); |
| 158 | } |
| 159 | |
| 160 | return colorMap.get(el.name() + "_BG"); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Return the foreground colour of the given element. |
| 165 | * |
| 166 | * @param el |
| 167 | * the {@link Element} |
| 168 | * |
| 169 | * @return its foreground colour |
| 170 | */ |
| 171 | private TextColor getForegroundColor(Element el) { |
| 172 | if (!colorMap.containsKey(el.name() + "_FG")) { |
| 173 | String value = null; |
| 174 | try { |
| 175 | value = bundle.getString(el.name() + "_FG"); |
| 176 | } catch (MissingResourceException mre) { |
| 177 | value = null; |
| 178 | } |
| 179 | colorMap.put(el.name() + "_FG", |
| 180 | convertToColor(value, TextColor.ANSI.WHITE)); |
| 181 | } |
| 182 | |
| 183 | return colorMap.get(el.name() + "_FG"); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Get the (unique) instance of this class. |
| 188 | * |
| 189 | * @return the (unique) instance |
| 190 | */ |
| 191 | static public UiColors getInstance() { |
| 192 | synchronized (lock) { |
| 193 | if (instance == null) |
| 194 | instance = new UiColors(); |
| 195 | } |
| 196 | |
| 197 | return instance; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Convert the given {@link String} value to a {@link TextColor}. |
| 202 | * |
| 203 | * @param value |
| 204 | * the {@link String} to convert |
| 205 | * @param defaultColor |
| 206 | * the default {@link TextColor} to return if the conversion |
| 207 | * failed |
| 208 | * |
| 209 | * @return the converted colour |
| 210 | */ |
| 211 | static private TextColor convertToColor(String value, TextColor defaultColor) { |
| 212 | try { |
| 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 { |
| 224 | return TextColor.ANSI.valueOf(value); |
| 225 | } |
| 226 | } catch (Exception e) { |
| 227 | new Exception("Cannot convert value to colour: " + value, e) |
| 228 | .printStackTrace(); |
| 229 | } |
| 230 | |
| 231 | return defaultColor; |
| 232 | } |
| 233 | |
| 234 | } |