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