Commit | Line | Data |
---|---|---|
a3b510ab NR |
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; | |
296a0b75 | 21 | private boolean utf = true; |
a3b510ab NR |
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 { | |
296a0b75 | 38 | DEFAULT, // |
0b0b2b0f NR |
39 | TITLE_MAIN, TITLE_VARIABLE, TITLE_COUNT, // |
40 | ACTION_KEY, ACTION_DESC, // | |
296a0b75 | 41 | LINE_MESSAGE, LINE_MESSAGE_ERR, LINE_MESSAGE_QUESTION, LINE_MESSAGE_ANS, // |
f82bad11 NR |
42 | CONTACT_LINE, CONTACT_LINE_SEPARATOR, CONTACT_LINE_SELECTED, CONTACT_LINE_SEPARATOR_SELECTED, CONTACT_LINE_DIRTY, CONTACT_LINE_DIRTY_SELECTED, // |
43 | VIEW_CONTACT_NAME, VIEW_CONTACT_NORMAL, VIEW_CONTACT_NOTES_TITLE, // | |
44 | ; | |
a3b510ab NR |
45 | |
46 | /** | |
47 | * Get the foreground colour of this element. | |
48 | * | |
49 | * @return the colour | |
50 | */ | |
51 | public TextColor getForegroundColor() { | |
52 | return UiColors.getInstance().getForegroundColor(this); | |
53 | } | |
54 | ||
55 | /** | |
56 | * Get the background colour of this element. | |
57 | * | |
58 | * @return the colour | |
59 | */ | |
60 | public TextColor getBackgroundColor() { | |
61 | return UiColors.getInstance().getBackgroundColor(this); | |
62 | } | |
63 | ||
64 | public Label createLabel(String text) { | |
65 | return UiColors.getInstance().createLabel(this, text); | |
66 | } | |
67 | ||
68 | public void themeLabel(Label lbl) { | |
69 | UiColors.getInstance().themeLabel(this, lbl); | |
70 | } | |
71 | } | |
72 | ||
296a0b75 NR |
73 | /** |
74 | * Check if unicode characters should be used. | |
75 | * | |
76 | * @return TRUE to allow unicode | |
77 | */ | |
78 | public boolean isUnicode() { | |
79 | return utf; | |
80 | } | |
81 | ||
82 | /** | |
83 | * Allow or disallow unicode characters in the program. | |
84 | * | |
85 | * @param utf | |
86 | * TRUE to allow unuciode, FALSE to only allow ASCII characters | |
87 | */ | |
88 | public void setUnicode(boolean utf) { | |
89 | this.utf = utf; | |
90 | } | |
91 | ||
a3b510ab | 92 | private Label createLabel(Element el, String text) { |
f82bad11 NR |
93 | if (text == null) |
94 | text = ""; | |
95 | ||
a3b510ab NR |
96 | Label lbl = new Label(text); |
97 | themeLabel(el, lbl); | |
98 | return lbl; | |
99 | } | |
100 | ||
101 | private void themeLabel(Element el, Label lbl) { | |
102 | lbl.setForegroundColor(el.getForegroundColor()); | |
103 | lbl.setBackgroundColor(el.getBackgroundColor()); | |
104 | } | |
105 | ||
106 | private TextColor getForegroundColor(Element el) { | |
107 | if (mapForegroundColor.containsKey(el)) { | |
108 | return mapForegroundColor.get(el); | |
109 | } | |
110 | ||
9c8baf0c | 111 | return TextColor.ANSI.BLACK; |
a3b510ab NR |
112 | } |
113 | ||
114 | private TextColor getBackgroundColor(Element el) { | |
115 | if (mapBackgroundColor.containsKey(el)) { | |
116 | return mapBackgroundColor.get(el); | |
117 | } | |
118 | ||
9c8baf0c | 119 | return TextColor.ANSI.WHITE; |
a3b510ab NR |
120 | } |
121 | ||
122 | private UiColors() { | |
123 | mapForegroundColor = new HashMap<Element, TextColor>(); | |
124 | mapBackgroundColor = new HashMap<Element, TextColor>(); | |
125 | ||
126 | // TODO: get from a file instead? | |
127 | // TODO: use a theme that doesn't give headaches... | |
128 | addEl(Element.ACTION_KEY, TextColor.ANSI.WHITE, TextColor.ANSI.RED); | |
129 | addEl(Element.ACTION_DESC, TextColor.ANSI.WHITE, TextColor.ANSI.BLUE); | |
130 | addEl(Element.CONTACT_LINE, TextColor.ANSI.WHITE, TextColor.ANSI.BLACK); | |
131 | addEl(Element.CONTACT_LINE_SELECTED, TextColor.ANSI.WHITE, | |
132 | TextColor.ANSI.BLUE); | |
0b0b2b0f NR |
133 | addEl(Element.CONTACT_LINE_SEPARATOR, TextColor.ANSI.RED, |
134 | TextColor.ANSI.BLACK); | |
9c8baf0c NR |
135 | addEl(Element.CONTACT_LINE_SEPARATOR_SELECTED, TextColor.ANSI.RED, |
136 | TextColor.ANSI.BLUE); | |
a3b510ab NR |
137 | addEl(Element.LINE_MESSAGE, TextColor.ANSI.BLUE, TextColor.ANSI.WHITE); |
138 | addEl(Element.LINE_MESSAGE_ERR, TextColor.ANSI.RED, | |
139 | TextColor.ANSI.WHITE); | |
140 | addEl(Element.LINE_MESSAGE_QUESTION, TextColor.ANSI.BLUE, | |
141 | TextColor.ANSI.WHITE); | |
142 | addEl(Element.LINE_MESSAGE_ANS, TextColor.ANSI.BLUE, | |
143 | TextColor.ANSI.BLACK); | |
0b0b2b0f | 144 | addEl(Element.TITLE_MAIN, TextColor.ANSI.WHITE, TextColor.ANSI.BLUE); |
296a0b75 | 145 | addEl(Element.TITLE_VARIABLE, TextColor.ANSI.GREEN, TextColor.ANSI.BLUE); |
0b0b2b0f | 146 | addEl(Element.TITLE_COUNT, TextColor.ANSI.RED, TextColor.ANSI.BLUE); |
f82bad11 NR |
147 | addEl(Element.VIEW_CONTACT_NAME, TextColor.ANSI.BLACK, |
148 | TextColor.ANSI.WHITE); | |
149 | addEl(Element.VIEW_CONTACT_NORMAL, TextColor.ANSI.WHITE, | |
150 | TextColor.ANSI.BLACK); | |
151 | addEl(Element.VIEW_CONTACT_NOTES_TITLE, TextColor.ANSI.BLACK, | |
152 | TextColor.ANSI.WHITE); | |
a3b510ab NR |
153 | } |
154 | ||
155 | private void addEl(Element el, TextColor fore, TextColor back) { | |
156 | mapForegroundColor.put(el, fore); | |
157 | mapBackgroundColor.put(el, back); | |
158 | } | |
a3b510ab | 159 | } |