| 1 | package be.nikiroo.jvcard.tui; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.Map; |
| 5 | |
| 6 | import com.googlecode.lanterna.TextColor; |
| 7 | import com.googlecode.lanterna.gui2.Label; |
| 8 | |
| 9 | /** |
| 10 | * All colour information must come from here. |
| 11 | * |
| 12 | * @author niki |
| 13 | * |
| 14 | */ |
| 15 | public class UiColors { |
| 16 | static private Object lock = new Object(); |
| 17 | static private UiColors instance = null; |
| 18 | |
| 19 | private Map<Element, TextColor> mapForegroundColor = null; |
| 20 | private Map<Element, TextColor> mapBackgroundColor = null; |
| 21 | private boolean utf = true; |
| 22 | |
| 23 | /** |
| 24 | * Get the (unique) instance of this class. |
| 25 | * |
| 26 | * @return the (unique) instance |
| 27 | */ |
| 28 | static public UiColors getInstance() { |
| 29 | synchronized (lock) { |
| 30 | if (instance == null) |
| 31 | instance = new UiColors(); |
| 32 | } |
| 33 | |
| 34 | return instance; |
| 35 | } |
| 36 | |
| 37 | public enum Element { |
| 38 | DEFAULT, // |
| 39 | TITLE_MAIN, TITLE_VARIABLE, TITLE_COUNT, // |
| 40 | ACTION_KEY, ACTION_DESC, // |
| 41 | LINE_MESSAGE, LINE_MESSAGE_ERR, LINE_MESSAGE_QUESTION, LINE_MESSAGE_ANS, // |
| 42 | CONTACT_LINE, CONTACT_LINE_SEPARATOR, CONTACT_LINE_SELECTED, CONTACT_LINE_SEPARATOR_SELECTED, CONTACT_LINE_DIRTY, CONTACT_LINE_DIRTY_SELECTED; |
| 43 | |
| 44 | /** |
| 45 | * Get the foreground colour of this element. |
| 46 | * |
| 47 | * @return the colour |
| 48 | */ |
| 49 | public TextColor getForegroundColor() { |
| 50 | return UiColors.getInstance().getForegroundColor(this); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get the background colour of this element. |
| 55 | * |
| 56 | * @return the colour |
| 57 | */ |
| 58 | public TextColor getBackgroundColor() { |
| 59 | return UiColors.getInstance().getBackgroundColor(this); |
| 60 | } |
| 61 | |
| 62 | public Label createLabel(String text) { |
| 63 | return UiColors.getInstance().createLabel(this, text); |
| 64 | } |
| 65 | |
| 66 | public void themeLabel(Label lbl) { |
| 67 | UiColors.getInstance().themeLabel(this, lbl); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Check if unicode characters should be used. |
| 73 | * |
| 74 | * @return TRUE to allow unicode |
| 75 | */ |
| 76 | public boolean isUnicode() { |
| 77 | return utf; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Allow or disallow unicode characters in the program. |
| 82 | * |
| 83 | * @param utf |
| 84 | * TRUE to allow unuciode, FALSE to only allow ASCII characters |
| 85 | */ |
| 86 | public void setUnicode(boolean utf) { |
| 87 | this.utf = utf; |
| 88 | } |
| 89 | |
| 90 | private Label createLabel(Element el, String text) { |
| 91 | Label lbl = new Label(text); |
| 92 | themeLabel(el, lbl); |
| 93 | return lbl; |
| 94 | } |
| 95 | |
| 96 | private void themeLabel(Element el, Label lbl) { |
| 97 | lbl.setForegroundColor(el.getForegroundColor()); |
| 98 | lbl.setBackgroundColor(el.getBackgroundColor()); |
| 99 | } |
| 100 | |
| 101 | private TextColor getForegroundColor(Element el) { |
| 102 | if (mapForegroundColor.containsKey(el)) { |
| 103 | return mapForegroundColor.get(el); |
| 104 | } |
| 105 | |
| 106 | return TextColor.ANSI.BLACK; |
| 107 | } |
| 108 | |
| 109 | private TextColor getBackgroundColor(Element el) { |
| 110 | if (mapBackgroundColor.containsKey(el)) { |
| 111 | return mapBackgroundColor.get(el); |
| 112 | } |
| 113 | |
| 114 | return TextColor.ANSI.WHITE; |
| 115 | } |
| 116 | |
| 117 | private UiColors() { |
| 118 | mapForegroundColor = new HashMap<Element, TextColor>(); |
| 119 | mapBackgroundColor = new HashMap<Element, TextColor>(); |
| 120 | |
| 121 | // TODO: get from a file instead? |
| 122 | // TODO: use a theme that doesn't give headaches... |
| 123 | addEl(Element.ACTION_KEY, TextColor.ANSI.WHITE, TextColor.ANSI.RED); |
| 124 | addEl(Element.ACTION_DESC, TextColor.ANSI.WHITE, TextColor.ANSI.BLUE); |
| 125 | addEl(Element.CONTACT_LINE, TextColor.ANSI.WHITE, TextColor.ANSI.BLACK); |
| 126 | addEl(Element.CONTACT_LINE_SELECTED, TextColor.ANSI.WHITE, |
| 127 | TextColor.ANSI.BLUE); |
| 128 | addEl(Element.CONTACT_LINE_SEPARATOR, TextColor.ANSI.RED, |
| 129 | TextColor.ANSI.BLACK); |
| 130 | addEl(Element.CONTACT_LINE_SEPARATOR_SELECTED, TextColor.ANSI.RED, |
| 131 | TextColor.ANSI.BLUE); |
| 132 | addEl(Element.LINE_MESSAGE, TextColor.ANSI.BLUE, TextColor.ANSI.WHITE); |
| 133 | addEl(Element.LINE_MESSAGE_ERR, TextColor.ANSI.RED, |
| 134 | TextColor.ANSI.WHITE); |
| 135 | addEl(Element.LINE_MESSAGE_QUESTION, TextColor.ANSI.BLUE, |
| 136 | TextColor.ANSI.WHITE); |
| 137 | addEl(Element.LINE_MESSAGE_ANS, TextColor.ANSI.BLUE, |
| 138 | TextColor.ANSI.BLACK); |
| 139 | addEl(Element.TITLE_MAIN, TextColor.ANSI.WHITE, TextColor.ANSI.BLUE); |
| 140 | addEl(Element.TITLE_VARIABLE, TextColor.ANSI.GREEN, TextColor.ANSI.BLUE); |
| 141 | addEl(Element.TITLE_COUNT, TextColor.ANSI.RED, TextColor.ANSI.BLUE); |
| 142 | } |
| 143 | |
| 144 | private void addEl(Element el, TextColor fore, TextColor back) { |
| 145 | mapForegroundColor.put(el, fore); |
| 146 | mapBackgroundColor.put(el, back); |
| 147 | } |
| 148 | } |