| 1 | package be.nikiroo.jvcard.tui; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.Map; |
| 5 | import java.util.MissingResourceException; |
| 6 | |
| 7 | import be.nikiroo.jvcard.resources.bundles.ColorBundle; |
| 8 | import be.nikiroo.jvcard.resources.enums.ColorOption; |
| 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 extends ColorBundle { |
| 20 | static private Object lock = new Object(); |
| 21 | static private UiColors instance = null; |
| 22 | |
| 23 | private Map<String, TextColor> colorMap = null; |
| 24 | |
| 25 | private UiColors() { |
| 26 | super(); |
| 27 | colorMap = new HashMap<String, TextColor>(); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Create a new {@link Label} with the colours of the given {@link ColorOption}. |
| 32 | * |
| 33 | * @param el |
| 34 | * the {@link ColorOption} |
| 35 | * @param text |
| 36 | * the text of the {@link Label} |
| 37 | * |
| 38 | * @return the new {@link Label} |
| 39 | */ |
| 40 | static public Label createLabel(ColorOption el, String text) { |
| 41 | if (text == null) |
| 42 | text = ""; |
| 43 | |
| 44 | Label lbl = new Label(text); |
| 45 | themeLabel(el, lbl); |
| 46 | return lbl; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Theme a {@link Label} with the colours of the given {@link ColorOption}. |
| 51 | * |
| 52 | * @param el |
| 53 | * the {@link ColorOption} |
| 54 | * @param lbl |
| 55 | * the {@link Label} |
| 56 | */ |
| 57 | static public void themeLabel(ColorOption el, Label lbl) { |
| 58 | lbl.setForegroundColor(getForegroundColor(el)); |
| 59 | lbl.setBackgroundColor(getBackgroundColor(el)); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Return the background colour of the given element. |
| 64 | * |
| 65 | * @param el |
| 66 | * the {@link ColorOption} |
| 67 | * |
| 68 | * @return its background colour |
| 69 | */ |
| 70 | static public TextColor getBackgroundColor(ColorOption el) { |
| 71 | if (!getInstance().colorMap.containsKey(el.name() + "_BG")) { |
| 72 | String value = null; |
| 73 | try { |
| 74 | value = getInstance().map.getString(el.name() + "_BG"); |
| 75 | } catch (MissingResourceException mre) { |
| 76 | value = null; |
| 77 | } |
| 78 | getInstance().colorMap.put(el.name() + "_BG", |
| 79 | convertToColor(value, TextColor.ANSI.BLACK)); |
| 80 | } |
| 81 | |
| 82 | return getInstance().colorMap.get(el.name() + "_BG"); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Return the foreground colour of the given element. |
| 87 | * |
| 88 | * @param el |
| 89 | * the {@link ColorOption} |
| 90 | * |
| 91 | * @return its foreground colour |
| 92 | */ |
| 93 | static public TextColor getForegroundColor(ColorOption el) { |
| 94 | if (!getInstance().colorMap.containsKey(el.name() + "_FG")) { |
| 95 | String value = null; |
| 96 | try { |
| 97 | value = getInstance().map.getString(el.name() + "_FG"); |
| 98 | } catch (MissingResourceException mre) { |
| 99 | value = null; |
| 100 | } |
| 101 | getInstance().colorMap.put(el.name() + "_FG", |
| 102 | convertToColor(value, TextColor.ANSI.WHITE)); |
| 103 | } |
| 104 | |
| 105 | return getInstance().colorMap.get(el.name() + "_FG"); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get the (unique) instance of this class. |
| 110 | * |
| 111 | * @return the (unique) instance |
| 112 | */ |
| 113 | static private UiColors getInstance() { |
| 114 | synchronized (lock) { |
| 115 | if (instance == null) |
| 116 | instance = new UiColors(); |
| 117 | } |
| 118 | |
| 119 | return instance; |
| 120 | } |
| 121 | |
| 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); |
| 145 | } else if (value.replaceAll("[0-9]*", "").length() == 0) { |
| 146 | return new TextColor.Indexed(Integer.parseInt(value)); |
| 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; |
| 156 | } |
| 157 | } |