import be.nikiroo.jvcard.i18n.Trans.StringId;
import be.nikiroo.jvcard.tui.panes.FileList;
-import com.googlecode.lanterna.TextColor;
-import com.googlecode.lanterna.gui2.BasicWindow;
-import com.googlecode.lanterna.gui2.DefaultWindowManager;
-import com.googlecode.lanterna.gui2.EmptySpace;
-import com.googlecode.lanterna.gui2.MultiWindowTextGUI;
import com.googlecode.lanterna.gui2.Window;
-import com.googlecode.lanterna.gui2.table.Table;
import com.googlecode.lanterna.input.KeyStroke;
-import com.googlecode.lanterna.screen.Screen;
-import com.googlecode.lanterna.screen.TerminalScreen;
-import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
-import com.googlecode.lanterna.terminal.Terminal;
/**
* This class contains the runnable Main method. It will parse the user supplied
| IllegalArgumentException | IllegalAccessException e) {
}
}
-
- static private void fullTestTable() throws IOException {
- final Table<String> table = new Table<String>("Column 1", "Column 2",
- "Column 3");
- table.getTableModel().addRow("1", "2", "3");
- table.setSelectAction(new Runnable() {
- @Override
- public void run() {
- List<String> data = table.getTableModel().getRow(
- table.getSelectedRow());
- for (int i = 0; i < data.size(); i++) {
- System.out.println(data.get(i));
- }
- }
- });
-
- Window win = new BasicWindow();
- win.setComponent(table);
-
- DefaultTerminalFactory factory = new DefaultTerminalFactory();
- Terminal terminal = factory.createTerminal();
-
- Screen screen = new TerminalScreen(terminal);
- screen.startScreen();
-
- // Create gui and start gui
- MultiWindowTextGUI gui = new MultiWindowTextGUI(screen,
- new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.BLUE));
- gui.addWindowAndWait(win);
-
- screen.stopScreen();
- }
}
import java.util.HashMap;
import java.util.Map;
+import java.util.ResourceBundle;
+
+import be.nikiroo.jvcard.resources.Bundles;
import com.googlecode.lanterna.TextColor;
import com.googlecode.lanterna.gui2.Label;
static private Object lock = new Object();
static private UiColors instance = null;
- private Map<Element, TextColor> mapForegroundColor = null;
- private Map<Element, TextColor> mapBackgroundColor = null;
+ private ResourceBundle bundle = null;
+ private Map<String, TextColor> colorMap = null;
private boolean utf = true;
+ private UiColors() {
+ colorMap = new HashMap<String, TextColor>();
+ bundle = Bundles.getBundle("colors");
+ }
+
/**
- * Get the (unique) instance of this class.
+ * Represent an element that can be coloured (foreground/background
+ * colours).
*
- * @return the (unique) instance
+ * @author niki
+ *
*/
- static public UiColors getInstance() {
- synchronized (lock) {
- if (instance == null)
- instance = new UiColors();
- }
-
- return instance;
- }
-
public enum Element {
DEFAULT, //
TITLE_MAIN, TITLE_VARIABLE, TITLE_COUNT, //
return UiColors.getInstance().getBackgroundColor(this);
}
+ /**
+ * Create a new {@link Label} with the colours of this {@link Element}.
+ *
+ * @param text
+ * the text of the {@link Label}
+ *
+ * @return the new {@link Label}
+ */
public Label createLabel(String text) {
return UiColors.getInstance().createLabel(this, text);
}
+ /**
+ * Theme a {@link Label} with the colours of this {@link Element}.
+ *
+ * @param lbl
+ * the {@link Label}
+ */
public void themeLabel(Label lbl) {
UiColors.getInstance().themeLabel(this, lbl);
}
this.utf = utf;
}
+ /**
+ * Create a new {@link Label} with the colours of the given {@link Element}.
+ *
+ * @param el
+ * the {@link Element}
+ * @param text
+ * the text of the {@link Label}
+ *
+ * @return the new {@link Label}
+ */
private Label createLabel(Element el, String text) {
if (text == null)
text = "";
return lbl;
}
+ /**
+ * Theme a {@link Label} with the colours of the given {@link Element}.
+ *
+ * @param el
+ * the {@link Element}
+ * @param lbl
+ * the {@link Label}
+ */
private void themeLabel(Element el, Label lbl) {
lbl.setForegroundColor(el.getForegroundColor());
lbl.setBackgroundColor(el.getBackgroundColor());
}
- private TextColor getForegroundColor(Element el) {
- if (mapForegroundColor.containsKey(el)) {
- return mapForegroundColor.get(el);
+ /**
+ * Return the background colour of the given element.
+ *
+ * @param el
+ * the {@link Element}
+ *
+ * @return its background colour
+ */
+ private TextColor getBackgroundColor(Element el) {
+ if (!colorMap.containsKey(el.name() + "_BG")) {
+ String value = bundle.getString(el.name() + "_BG");
+ colorMap.put(el.name() + "_BG",
+ convertToColor(value, TextColor.ANSI.BLACK));
}
- return TextColor.ANSI.BLACK;
+ return colorMap.get(el.name() + "_BG");
}
- private TextColor getBackgroundColor(Element el) {
- if (mapBackgroundColor.containsKey(el)) {
- return mapBackgroundColor.get(el);
+ /**
+ * Return the foreground colour of the given element.
+ *
+ * @param el
+ * the {@link Element}
+ *
+ * @return its foreground colour
+ */
+ private TextColor getForegroundColor(Element el) {
+ if (!colorMap.containsKey(el.name() + "_FG")) {
+ String value = bundle.getString(el.name() + "_FG");
+ colorMap.put(el.name() + "_FG",
+ convertToColor(value, TextColor.ANSI.WHITE));
}
- return TextColor.ANSI.WHITE;
+ return colorMap.get(el.name() + "_FG");
}
- private UiColors() {
- mapForegroundColor = new HashMap<Element, TextColor>();
- mapBackgroundColor = new HashMap<Element, TextColor>();
-
- // TODO: get from a file instead?
- // TODO: use a theme that doesn't give headaches...
- addEl(Element.ACTION_KEY, TextColor.ANSI.WHITE, TextColor.ANSI.RED);
- addEl(Element.ACTION_DESC, TextColor.ANSI.WHITE, TextColor.ANSI.BLUE);
- addEl(Element.CONTACT_LINE, TextColor.ANSI.WHITE, TextColor.ANSI.BLACK);
- addEl(Element.CONTACT_LINE_SELECTED, TextColor.ANSI.WHITE,
- TextColor.ANSI.BLUE);
- addEl(Element.CONTACT_LINE_SEPARATOR, TextColor.ANSI.RED,
- TextColor.ANSI.BLACK);
- addEl(Element.CONTACT_LINE_SEPARATOR_SELECTED, TextColor.ANSI.RED,
- TextColor.ANSI.BLUE);
- addEl(Element.LINE_MESSAGE, TextColor.ANSI.BLUE, TextColor.ANSI.WHITE);
- addEl(Element.LINE_MESSAGE_ERR, TextColor.ANSI.RED,
- TextColor.ANSI.WHITE);
- addEl(Element.LINE_MESSAGE_QUESTION, TextColor.ANSI.BLUE,
- TextColor.ANSI.WHITE);
- addEl(Element.LINE_MESSAGE_ANS, TextColor.ANSI.BLUE,
- TextColor.ANSI.BLACK);
- addEl(Element.TITLE_MAIN, TextColor.ANSI.WHITE, TextColor.ANSI.BLUE);
- addEl(Element.TITLE_VARIABLE, TextColor.ANSI.GREEN, TextColor.ANSI.BLUE);
- addEl(Element.TITLE_COUNT, TextColor.ANSI.RED, TextColor.ANSI.BLUE);
- addEl(Element.VIEW_CONTACT_NAME, TextColor.ANSI.BLACK,
- TextColor.ANSI.WHITE);
- addEl(Element.VIEW_CONTACT_NORMAL, TextColor.ANSI.WHITE,
- TextColor.ANSI.BLACK);
- addEl(Element.VIEW_CONTACT_NOTES_TITLE, TextColor.ANSI.BLACK,
- TextColor.ANSI.WHITE);
+ /**
+ * Get the (unique) instance of this class.
+ *
+ * @return the (unique) instance
+ */
+ static public UiColors getInstance() {
+ synchronized (lock) {
+ if (instance == null)
+ instance = new UiColors();
+ }
+
+ return instance;
}
- private void addEl(Element el, TextColor fore, TextColor back) {
- mapForegroundColor.put(el, fore);
- mapBackgroundColor.put(el, back);
+ /**
+ * Convert the given {@link String} value to a {@link TextColor}.
+ *
+ * @param value
+ * the {@link String} to convert
+ * @param defaultColor
+ * the default {@link TextColor} to return if the conversion
+ * failed
+ *
+ * @return the converted colour
+ */
+ static private TextColor convertToColor(String value, TextColor defaultColor) {
+ try {
+ if (value.startsWith("@")) {
+ int r = Integer.parseInt(value.substring(1, 3), 16);
+ int g = Integer.parseInt(value.substring(3, 5), 16);
+ int b = Integer.parseInt(value.substring(5, 7), 16);
+ return TextColor.Indexed.fromRGB(r, g, b);
+ } else if (value.startsWith("#")) {
+ int r = Integer.parseInt(value.substring(1, 3), 16);
+ int g = Integer.parseInt(value.substring(3, 5), 16);
+ int b = Integer.parseInt(value.substring(5, 7), 16);
+ return new TextColor.RGB(r, g, b);
+ } else {
+ return TextColor.ANSI.valueOf(value);
+ }
+ } catch (Exception e) {
+ new Exception("Cannot convert value to colour: " + value, e)
+ .printStackTrace();
+ }
+
+ return defaultColor;
}
+
}