Improve UI, take "dirty" check into account, move launcher to Main.java
[jvcard.git] / src / be / nikiroo / jvcard / tui / UiColors.java
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
22 /**
23 * Get the (unique) instance of this class.
24 *
25 * @return the (unique) instance
26 */
27 static public UiColors getInstance() {
28 synchronized (lock) {
29 if (instance == null)
30 instance = new UiColors();
31 }
32
33 return instance;
34 }
35
36 public enum Element {
37 DEFAULT, //
38 TITLE_MAIN, TITLE_VARIABLE, TITLE_COUNT, //
39 ACTION_KEY, ACTION_DESC, //
40 LINE_MESSAGE, LINE_MESSAGE_ERR, LINE_MESSAGE_QUESTION, LINE_MESSAGE_ANS, //
41 CONTACT_LINE, CONTACT_LINE_SEPARATOR, CONTACT_LINE_SELECTED, CONTACT_LINE_SEPARATOR_SELECTED, CONTACT_LINE_DIRTY, CONTACT_LINE_DIRTY_SELECTED;
42
43 /**
44 * Get the foreground colour of this element.
45 *
46 * @return the colour
47 */
48 public TextColor getForegroundColor() {
49 return UiColors.getInstance().getForegroundColor(this);
50 }
51
52 /**
53 * Get the background colour of this element.
54 *
55 * @return the colour
56 */
57 public TextColor getBackgroundColor() {
58 return UiColors.getInstance().getBackgroundColor(this);
59 }
60
61 public Label createLabel(String text) {
62 return UiColors.getInstance().createLabel(this, text);
63 }
64
65 public void themeLabel(Label lbl) {
66 UiColors.getInstance().themeLabel(this, lbl);
67 }
68 }
69
70 private Label createLabel(Element el, String text) {
71 Label lbl = new Label(text);
72 themeLabel(el, lbl);
73 return lbl;
74 }
75
76 private void themeLabel(Element el, Label lbl) {
77 lbl.setForegroundColor(el.getForegroundColor());
78 lbl.setBackgroundColor(el.getBackgroundColor());
79 }
80
81 private TextColor getForegroundColor(Element el) {
82 if (mapForegroundColor.containsKey(el)) {
83 return mapForegroundColor.get(el);
84 }
85
86 return TextColor.ANSI.BLACK;
87 }
88
89 private TextColor getBackgroundColor(Element el) {
90 if (mapBackgroundColor.containsKey(el)) {
91 return mapBackgroundColor.get(el);
92 }
93
94 return TextColor.ANSI.WHITE;
95 }
96
97 private UiColors() {
98 mapForegroundColor = new HashMap<Element, TextColor>();
99 mapBackgroundColor = new HashMap<Element, TextColor>();
100
101 // TODO: get from a file instead?
102 // TODO: use a theme that doesn't give headaches...
103 addEl(Element.ACTION_KEY, TextColor.ANSI.WHITE, TextColor.ANSI.RED);
104 addEl(Element.ACTION_DESC, TextColor.ANSI.WHITE, TextColor.ANSI.BLUE);
105 addEl(Element.CONTACT_LINE, TextColor.ANSI.WHITE, TextColor.ANSI.BLACK);
106 addEl(Element.CONTACT_LINE_SELECTED, TextColor.ANSI.WHITE,
107 TextColor.ANSI.BLUE);
108 addEl(Element.CONTACT_LINE_SEPARATOR, TextColor.ANSI.RED,
109 TextColor.ANSI.BLACK);
110 addEl(Element.CONTACT_LINE_SEPARATOR_SELECTED, TextColor.ANSI.RED,
111 TextColor.ANSI.BLUE);
112 addEl(Element.LINE_MESSAGE, TextColor.ANSI.BLUE, TextColor.ANSI.WHITE);
113 addEl(Element.LINE_MESSAGE_ERR, TextColor.ANSI.RED,
114 TextColor.ANSI.WHITE);
115 addEl(Element.LINE_MESSAGE_QUESTION, TextColor.ANSI.BLUE,
116 TextColor.ANSI.WHITE);
117 addEl(Element.LINE_MESSAGE_ANS, TextColor.ANSI.BLUE,
118 TextColor.ANSI.BLACK);
119 addEl(Element.TITLE_MAIN, TextColor.ANSI.WHITE, TextColor.ANSI.BLUE);
120 addEl(Element.TITLE_VARIABLE, TextColor.ANSI.GREEN,
121 TextColor.ANSI.BLUE);
122 addEl(Element.TITLE_COUNT, TextColor.ANSI.RED, TextColor.ANSI.BLUE);
123 }
124
125 private void addEl(Element el, TextColor fore, TextColor back) {
126 mapForegroundColor.put(el, fore);
127 mapBackgroundColor.put(el, back);
128 }
129
130 }