From 2a96e7b2cf3f155cccc633272b2f547ffd8f4672 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Thu, 3 Mar 2016 13:52:40 +0100 Subject: [PATCH] Colours are now taken from a .properties file --- src/be/nikiroo/jvcard/i18n/Trans.java | 10 +- src/be/nikiroo/jvcard/resources/Bundles.java | 41 +++++ .../FixedResourceBundleControl.java | 10 +- .../jvcard/resources/colors.properties | 46 +++++ .../{i18n => resources}/resources.properties | 0 .../resources_en.properties | 0 .../resources_fr.properties | 0 src/be/nikiroo/jvcard/tui/Main.java | 42 ----- src/be/nikiroo/jvcard/tui/UiColors.java | 174 ++++++++++++------ .../jvcard/tui/panes/ContactDetails.java | 14 +- .../jvcard/tui/panes/ContactDetailsRaw.java | 2 - .../nikiroo/jvcard/tui/panes/ContactList.java | 3 - src/be/nikiroo/jvcard/tui/panes/FileList.java | 3 - .../jvcard/tui/panes/MainContentList.java | 3 +- 14 files changed, 225 insertions(+), 123 deletions(-) create mode 100644 src/be/nikiroo/jvcard/resources/Bundles.java rename src/be/nikiroo/jvcard/{i18n => resources}/FixedResourceBundleControl.java (87%) create mode 100644 src/be/nikiroo/jvcard/resources/colors.properties rename src/be/nikiroo/jvcard/{i18n => resources}/resources.properties (100%) rename src/be/nikiroo/jvcard/{i18n => resources}/resources_en.properties (100%) rename src/be/nikiroo/jvcard/{i18n => resources}/resources_fr.properties (100%) diff --git a/src/be/nikiroo/jvcard/i18n/Trans.java b/src/be/nikiroo/jvcard/i18n/Trans.java index 1046d4c..210cffc 100644 --- a/src/be/nikiroo/jvcard/i18n/Trans.java +++ b/src/be/nikiroo/jvcard/i18n/Trans.java @@ -3,6 +3,7 @@ package be.nikiroo.jvcard.i18n; import java.util.Locale; import java.util.ResourceBundle; +import be.nikiroo.jvcard.resources.Bundles; import be.nikiroo.jvcard.tui.UiColors; import com.googlecode.lanterna.input.KeyStroke; @@ -65,7 +66,7 @@ public class Trans { StringId id = stringId; if (!UiColors.getInstance().isUnicode()) { try { - id = StringId.valueOf(stringId.toString() + "_NOUTF"); + id = StringId.valueOf(stringId.name() + "_NOUTF"); } catch (IllegalArgumentException iae) { // no special _NOUTF version found } @@ -79,8 +80,8 @@ public class Trans { return "[dummy]"; } - if (map.containsKey(id.toString())) { - return map.getString(id.toString()); + if (map.containsKey(id.name())) { + return map.getString(id.name()); } return id.toString(); @@ -146,7 +147,6 @@ public class Trans { locale = Locale.forLanguageTag(lang); } - map = ResourceBundle.getBundle(Trans.class.getPackage().getName() - + ".resources", locale, new FixedResourceBundleControl()); + map = Bundles.getBundle("resources", locale); } } diff --git a/src/be/nikiroo/jvcard/resources/Bundles.java b/src/be/nikiroo/jvcard/resources/Bundles.java new file mode 100644 index 0000000..76c471f --- /dev/null +++ b/src/be/nikiroo/jvcard/resources/Bundles.java @@ -0,0 +1,41 @@ +package be.nikiroo.jvcard.resources; + +import java.util.Locale; +import java.util.ResourceBundle; + +/** + * This class help you get UTF-8 bundles for this application. + * + * @author niki + * + */ +public class Bundles { + /** + * Return the non-localised bundle of the given name. + * + * @param name + * the name of the bundle to load + * + * @return the bundle + */ + static public ResourceBundle getBundle(String name) { + return ResourceBundle.getBundle(Bundles.class.getPackage().getName() + + "." + name, new FixedResourceBundleControl()); + } + + /** + * Return the localised bundle of the given name and {@link Locale}. + * + * @param name + * the name of the bundle to load + * @param locale + * the {@link Locale} to use + * + * @return the localised bundle + */ + static public ResourceBundle getBundle(String name, Locale locale) { + + return ResourceBundle.getBundle(Bundles.class.getPackage().getName() + + "." + name, locale, new FixedResourceBundleControl()); + } +} diff --git a/src/be/nikiroo/jvcard/i18n/FixedResourceBundleControl.java b/src/be/nikiroo/jvcard/resources/FixedResourceBundleControl.java similarity index 87% rename from src/be/nikiroo/jvcard/i18n/FixedResourceBundleControl.java rename to src/be/nikiroo/jvcard/resources/FixedResourceBundleControl.java index d29f597..e67ace1 100644 --- a/src/be/nikiroo/jvcard/i18n/FixedResourceBundleControl.java +++ b/src/be/nikiroo/jvcard/resources/FixedResourceBundleControl.java @@ -1,4 +1,4 @@ -package be.nikiroo.jvcard.i18n; +package be.nikiroo.jvcard.resources; import java.io.IOException; import java.io.InputStream; @@ -10,7 +10,13 @@ import java.util.PropertyResourceBundle; import java.util.ResourceBundle; import java.util.ResourceBundle.Control; -public class FixedResourceBundleControl extends Control { +/** + * Fixed ResourceBundle.Control class. It will use UTF-8 for the files to load. + * + * @author niki + * + */ +class FixedResourceBundleControl extends Control { public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException { diff --git a/src/be/nikiroo/jvcard/resources/colors.properties b/src/be/nikiroo/jvcard/resources/colors.properties new file mode 100644 index 0000000..c57a99e --- /dev/null +++ b/src/be/nikiroo/jvcard/resources/colors.properties @@ -0,0 +1,46 @@ +# application colours +# +# Note that you can define a colour in one of those 3 ways: +# - WHITE: one of the ANSI colour names, in uppercase +# - @RRGGBB: a RGB code we will try to match using one of the 256 Terminal colours +# - #RRGGBB: an exact RGB colour (please make sure your terminal supports this) +# +# ...and thus either for xxx_FG (foreground colour) or xxx_BG (background colour) + + +TITLE_MAIN_FG = WHITE +TITLE_MAIN_BG = BLUE +TITLE_VARIABLE_FG = GREEN +TITLE_VARIABLE_BG = BLUE +TITLE_COUNT_FG = RED +TITLE_COUNT_BG = BLUE + +DEFAULT_FG = BLACK +DEFAULT_BG = WHITE + +ACTION_KEY_FG = WHITE +ACTION_KEY_BG = RED +ACTION_DESC_FG = WHITE +ACTION_DESC_BG = BLUE +CONTACT_LINE_FG = WHITE +CONTACT_LINE_BG = BLACK +CONTACT_LINE_SELECTED_FG = WHITE +CONTACT_LINE_SELECTED_BG = BLUE +CONTACT_LINE_SEPARATOR_FG = RED +CONTACT_LINE_SEPARATOR_BG = BLACK +CONTACT_LINE_SEPARATOR_SELECTED_FG = RED +CONTACT_LINE_SEPARATOR_SELECTED_BG = BLUE +LINE_MESSAGE_FG = BLUE +LINE_MESSAGE_BG = WHITE +LINE_MESSAGE_ERR_FG = RED +LINE_MESSAGE_ERR_BG = WHITE +LINE_MESSAGE_QUESTION_FG = BLUE +LINE_MESSAGE_QUESTION_BG = WHITE +LINE_MESSAGE_ANS_FG = BLUE +LINE_MESSAGE_ANS_BG = BLACK +VIEW_CONTACT_NAME_FG = BLACK +VIEW_CONTACT_NAME_BG = WHITE +VIEW_CONTACT_NORMAL_FG = WHITE +VIEW_CONTACT_NORMAL_BG = BLACK +VIEW_CONTACT_NOTES_TITLE_FG = BLACK +VIEW_CONTACT_NOTES_TITLE_BG = WHITE diff --git a/src/be/nikiroo/jvcard/i18n/resources.properties b/src/be/nikiroo/jvcard/resources/resources.properties similarity index 100% rename from src/be/nikiroo/jvcard/i18n/resources.properties rename to src/be/nikiroo/jvcard/resources/resources.properties diff --git a/src/be/nikiroo/jvcard/i18n/resources_en.properties b/src/be/nikiroo/jvcard/resources/resources_en.properties similarity index 100% rename from src/be/nikiroo/jvcard/i18n/resources_en.properties rename to src/be/nikiroo/jvcard/resources/resources_en.properties diff --git a/src/be/nikiroo/jvcard/i18n/resources_fr.properties b/src/be/nikiroo/jvcard/resources/resources_fr.properties similarity index 100% rename from src/be/nikiroo/jvcard/i18n/resources_fr.properties rename to src/be/nikiroo/jvcard/resources/resources_fr.properties diff --git a/src/be/nikiroo/jvcard/tui/Main.java b/src/be/nikiroo/jvcard/tui/Main.java index eda0c69..fd6d7de 100644 --- a/src/be/nikiroo/jvcard/tui/Main.java +++ b/src/be/nikiroo/jvcard/tui/Main.java @@ -11,18 +11,8 @@ import be.nikiroo.jvcard.i18n.Trans; 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 @@ -185,36 +175,4 @@ public class Main { | IllegalArgumentException | IllegalAccessException e) { } } - - static private void fullTestTable() throws IOException { - final Table table = new Table("Column 1", "Column 2", - "Column 3"); - table.getTableModel().addRow("1", "2", "3"); - table.setSelectAction(new Runnable() { - @Override - public void run() { - List 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(); - } } diff --git a/src/be/nikiroo/jvcard/tui/UiColors.java b/src/be/nikiroo/jvcard/tui/UiColors.java index eadcd29..a646dbe 100644 --- a/src/be/nikiroo/jvcard/tui/UiColors.java +++ b/src/be/nikiroo/jvcard/tui/UiColors.java @@ -2,6 +2,9 @@ package be.nikiroo.jvcard.tui; 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; @@ -16,24 +19,22 @@ public class UiColors { static private Object lock = new Object(); static private UiColors instance = null; - private Map mapForegroundColor = null; - private Map mapBackgroundColor = null; + private ResourceBundle bundle = null; + private Map colorMap = null; private boolean utf = true; + private UiColors() { + colorMap = new HashMap(); + 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, // @@ -61,10 +62,24 @@ public class UiColors { 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); } @@ -89,6 +104,16 @@ public class UiColors { 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 = ""; @@ -98,62 +123,101 @@ public class UiColors { 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(); - mapBackgroundColor = new HashMap(); - - // 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; } + } diff --git a/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java b/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java index 8aeac19..08df403 100644 --- a/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java +++ b/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java @@ -62,19 +62,16 @@ public class ContactDetails extends MainContent { /** * Change the enclosed {@link Contact} from this {@link ContactDetails}. + * Also re-set the image. * * @param contact * the new {@link Contact} */ public void setContact(Contact contact) { - if (this.contact == contact) - return; - this.contact = contact; + image = null; - if (contact == null) { - image = null; - } else { + if (contact != null) { infoPanel.removeAllComponents(); String name = contact.getPreferredDataValue("FN"); @@ -106,14 +103,13 @@ public class ContactDetails extends MainContent { Data photo = contact.getPreferredData("PHOTO"); if (photo != null) { TypeInfo encoding = null; - TypeInfo type = null; for (int index = 0; index < photo.size(); index++) { TypeInfo info = photo.get(index); if (info.getName() != null) { if (info.getName().equalsIgnoreCase("ENCODING")) encoding = info; - if (info.getName().equalsIgnoreCase("TYPE")) - type = info; + // We don't check for the "TYPE" anymore, we just defer + // it to ImageIcon } } diff --git a/src/be/nikiroo/jvcard/tui/panes/ContactDetailsRaw.java b/src/be/nikiroo/jvcard/tui/panes/ContactDetailsRaw.java index 47b5e8a..ec1a136 100644 --- a/src/be/nikiroo/jvcard/tui/panes/ContactDetailsRaw.java +++ b/src/be/nikiroo/jvcard/tui/panes/ContactDetailsRaw.java @@ -21,8 +21,6 @@ public class ContactDetailsRaw extends MainContentList { private int mode; public ContactDetailsRaw(Contact contact) { - super(null, null); - this.contact = contact; this.mode = 0; diff --git a/src/be/nikiroo/jvcard/tui/panes/ContactList.java b/src/be/nikiroo/jvcard/tui/panes/ContactList.java index bbd1f77..ae1a65b 100644 --- a/src/be/nikiroo/jvcard/tui/panes/ContactList.java +++ b/src/be/nikiroo/jvcard/tui/panes/ContactList.java @@ -25,9 +25,6 @@ public class ContactList extends MainContentList { private String format = ""; public ContactList(Card card) { - super(UiColors.Element.CONTACT_LINE, - UiColors.Element.CONTACT_LINE_SELECTED); - // TODO: should get that in an INI file formats.add("NICKNAME@3|FN@+|EMAIL@30"); formats.add("FN@+|EMAIL@40"); diff --git a/src/be/nikiroo/jvcard/tui/panes/FileList.java b/src/be/nikiroo/jvcard/tui/panes/FileList.java index 636ec43..a59311b 100644 --- a/src/be/nikiroo/jvcard/tui/panes/FileList.java +++ b/src/be/nikiroo/jvcard/tui/panes/FileList.java @@ -23,9 +23,6 @@ public class FileList extends MainContentList { private List cards; public FileList(List files) { - super(UiColors.Element.CONTACT_LINE, - UiColors.Element.CONTACT_LINE_SELECTED); - setFiles(files); } diff --git a/src/be/nikiroo/jvcard/tui/panes/MainContentList.java b/src/be/nikiroo/jvcard/tui/panes/MainContentList.java index 23892e5..e1358ef 100644 --- a/src/be/nikiroo/jvcard/tui/panes/MainContentList.java +++ b/src/be/nikiroo/jvcard/tui/panes/MainContentList.java @@ -56,8 +56,7 @@ abstract public class MainContentList extends MainContent implements Runnable { } } - public MainContentList(final UiColors.Element normalStyle, - final UiColors.Element selectedStyle) { + public MainContentList() { super(Direction.VERTICAL); lines = new ActionListBox(); -- 2.27.0