| 1 | package be.nikiroo.jvcard.tui; |
| 2 | |
| 3 | import java.io.FileInputStream; |
| 4 | import java.io.IOException; |
| 5 | import java.io.InputStream; |
| 6 | import java.util.HashMap; |
| 7 | import java.util.Map; |
| 8 | import java.util.MissingResourceException; |
| 9 | import java.util.Properties; |
| 10 | |
| 11 | import be.nikiroo.jvcard.resources.bundles.ColorBundle; |
| 12 | import be.nikiroo.jvcard.resources.enums.ColorOption; |
| 13 | |
| 14 | import com.googlecode.lanterna.TextColor; |
| 15 | import com.googlecode.lanterna.graphics.PropertiesTheme; |
| 16 | import com.googlecode.lanterna.graphics.Theme; |
| 17 | import com.googlecode.lanterna.gui2.AbstractTextGUI; |
| 18 | import com.googlecode.lanterna.gui2.Label; |
| 19 | |
| 20 | /** |
| 21 | * All colour information must come from here. |
| 22 | * |
| 23 | * @author niki |
| 24 | * |
| 25 | */ |
| 26 | public class UiColors extends ColorBundle { |
| 27 | static private Object lock = new Object(); |
| 28 | static private UiColors instance = null; |
| 29 | |
| 30 | private Map<String, TextColor> colorMap = null; |
| 31 | |
| 32 | private UiColors() { |
| 33 | super(); |
| 34 | colorMap = new HashMap<String, TextColor>(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Return a {@link Theme} following the colours defined in |
| 39 | * colors.properties. |
| 40 | * |
| 41 | * @return the {@link Theme} |
| 42 | */ |
| 43 | static public Theme getCustomTheme() { |
| 44 | // Create a properties-theme with our own custom values for some of it |
| 45 | Properties properties = new Properties(); |
| 46 | try { |
| 47 | ClassLoader classLoader = AbstractTextGUI.class.getClassLoader(); |
| 48 | InputStream resourceAsStream = classLoader |
| 49 | .getResourceAsStream("default-theme.properties"); |
| 50 | if (resourceAsStream == null) { |
| 51 | resourceAsStream = new FileInputStream( |
| 52 | "src/main/resources/default-theme.properties"); |
| 53 | } |
| 54 | properties.load(resourceAsStream); |
| 55 | resourceAsStream.close(); |
| 56 | } catch (IOException e) { |
| 57 | } |
| 58 | |
| 59 | // default colours: |
| 60 | String fg = getForegroundColor(ColorOption.DEFAULT).toString(); |
| 61 | String bg = getBackgroundColor(ColorOption.DEFAULT).toString(); |
| 62 | for (String def : new String[] { "com.googlecode.lanterna", |
| 63 | "com.googlecode.lanterna.gui2.TextBox", |
| 64 | "com.googlecode.lanterna.gui2.AbstractListBox", |
| 65 | "com.googlecode.lanterna.gui2.Borders$StandardBorder" }) { |
| 66 | properties.put(def + ".foreground", fg); |
| 67 | properties.put(def + ".background", bg); |
| 68 | } |
| 69 | |
| 70 | // no bold on borders prelight: |
| 71 | properties |
| 72 | .put("com.googlecode.lanterna.gui2.Borders$StandardBorder.sgr[PRELIGHT]", |
| 73 | ""); |
| 74 | |
| 75 | // line answers: |
| 76 | fg = getForegroundColor(ColorOption.LINE_MESSAGE_ANS).toString(); |
| 77 | bg = getBackgroundColor(ColorOption.LINE_MESSAGE_ANS).toString(); |
| 78 | String prop = "com.googlecode.lanterna.gui2.TextBox"; |
| 79 | properties.put(prop + ".foreground[ACTIVE]", fg); |
| 80 | properties.put(prop + ".background[ACTIVE]", bg); |
| 81 | |
| 82 | PropertiesTheme theme = new PropertiesTheme(properties); |
| 83 | return theme; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Create a new {@link Label} with the colours of the given |
| 88 | * {@link ColorOption}. |
| 89 | * |
| 90 | * @param el |
| 91 | * the {@link ColorOption} |
| 92 | * @param text |
| 93 | * the text of the {@link Label} |
| 94 | * |
| 95 | * @return the new {@link Label} |
| 96 | */ |
| 97 | static public Label createLabel(ColorOption el, String text) { |
| 98 | if (text == null) |
| 99 | text = ""; |
| 100 | |
| 101 | Label lbl = new Label(text); |
| 102 | themeLabel(el, lbl); |
| 103 | return lbl; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Theme a {@link Label} with the colours of the given {@link ColorOption}. |
| 108 | * |
| 109 | * @param el |
| 110 | * the {@link ColorOption} |
| 111 | * @param lbl |
| 112 | * the {@link Label} |
| 113 | */ |
| 114 | static public void themeLabel(ColorOption el, Label lbl) { |
| 115 | lbl.setForegroundColor(getForegroundColor(el)); |
| 116 | lbl.setBackgroundColor(getBackgroundColor(el)); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Return the background colour of the given element. |
| 121 | * |
| 122 | * @param el |
| 123 | * the {@link ColorOption} |
| 124 | * |
| 125 | * @return its background colour |
| 126 | */ |
| 127 | static public TextColor getBackgroundColor(ColorOption el) { |
| 128 | if (!getInstance().colorMap.containsKey(el.name() + "_BG")) { |
| 129 | String value = null; |
| 130 | try { |
| 131 | value = getInstance().map.getString(el.name() + "_BG"); |
| 132 | } catch (MissingResourceException mre) { |
| 133 | value = null; |
| 134 | } |
| 135 | getInstance().colorMap.put(el.name() + "_BG", |
| 136 | convertToColor(value, TextColor.ANSI.BLACK)); |
| 137 | } |
| 138 | |
| 139 | return getInstance().colorMap.get(el.name() + "_BG"); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Return the foreground colour of the given element. |
| 144 | * |
| 145 | * @param el |
| 146 | * the {@link ColorOption} |
| 147 | * |
| 148 | * @return its foreground colour |
| 149 | */ |
| 150 | static public TextColor getForegroundColor(ColorOption el) { |
| 151 | if (!getInstance().colorMap.containsKey(el.name() + "_FG")) { |
| 152 | String value = null; |
| 153 | try { |
| 154 | value = getInstance().map.getString(el.name() + "_FG"); |
| 155 | } catch (MissingResourceException mre) { |
| 156 | value = null; |
| 157 | } |
| 158 | getInstance().colorMap.put(el.name() + "_FG", |
| 159 | convertToColor(value, TextColor.ANSI.WHITE)); |
| 160 | } |
| 161 | |
| 162 | return getInstance().colorMap.get(el.name() + "_FG"); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Get the (unique) instance of this class. |
| 167 | * |
| 168 | * @return the (unique) instance |
| 169 | */ |
| 170 | static private UiColors getInstance() { |
| 171 | synchronized (lock) { |
| 172 | if (instance == null) |
| 173 | instance = new UiColors(); |
| 174 | } |
| 175 | |
| 176 | return instance; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Convert the given {@link String} value to a {@link TextColor}. |
| 181 | * |
| 182 | * @param value |
| 183 | * the {@link String} to convert |
| 184 | * @param defaultColor |
| 185 | * the default {@link TextColor} to return if the conversion |
| 186 | * failed |
| 187 | * |
| 188 | * @return the converted colour |
| 189 | */ |
| 190 | static private TextColor convertToColor(String value, TextColor defaultColor) { |
| 191 | try { |
| 192 | if (value.startsWith("@")) { |
| 193 | int r = Integer.parseInt(value.substring(1, 3), 16); |
| 194 | int g = Integer.parseInt(value.substring(3, 5), 16); |
| 195 | int b = Integer.parseInt(value.substring(5, 7), 16); |
| 196 | return TextColor.Indexed.fromRGB(r, g, b); |
| 197 | } else if (value.startsWith("#")) { |
| 198 | int r = Integer.parseInt(value.substring(1, 3), 16); |
| 199 | int g = Integer.parseInt(value.substring(3, 5), 16); |
| 200 | int b = Integer.parseInt(value.substring(5, 7), 16); |
| 201 | return new TextColor.RGB(r, g, b); |
| 202 | } else if (value.replaceAll("[0-9]*", "").length() == 0) { |
| 203 | return new TextColor.Indexed(Integer.parseInt(value)); |
| 204 | } else { |
| 205 | return TextColor.ANSI.valueOf(value); |
| 206 | } |
| 207 | } catch (Exception e) { |
| 208 | new Exception("Cannot convert value to colour: " + value, e) |
| 209 | .printStackTrace(); |
| 210 | } |
| 211 | |
| 212 | return defaultColor; |
| 213 | } |
| 214 | } |