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