Colours are now taken from a .properties file
authorNiki Roo <niki@nikiroo.be>
Thu, 3 Mar 2016 12:52:40 +0000 (13:52 +0100)
committerNiki Roo <niki@nikiroo.be>
Thu, 3 Mar 2016 12:52:40 +0000 (13:52 +0100)
14 files changed:
src/be/nikiroo/jvcard/i18n/Trans.java
src/be/nikiroo/jvcard/resources/Bundles.java [new file with mode: 0644]
src/be/nikiroo/jvcard/resources/FixedResourceBundleControl.java [moved from src/be/nikiroo/jvcard/i18n/FixedResourceBundleControl.java with 87% similarity]
src/be/nikiroo/jvcard/resources/colors.properties [new file with mode: 0644]
src/be/nikiroo/jvcard/resources/resources.properties [moved from src/be/nikiroo/jvcard/i18n/resources.properties with 100% similarity]
src/be/nikiroo/jvcard/resources/resources_en.properties [moved from src/be/nikiroo/jvcard/i18n/resources_en.properties with 100% similarity]
src/be/nikiroo/jvcard/resources/resources_fr.properties [moved from src/be/nikiroo/jvcard/i18n/resources_fr.properties with 100% similarity]
src/be/nikiroo/jvcard/tui/Main.java
src/be/nikiroo/jvcard/tui/UiColors.java
src/be/nikiroo/jvcard/tui/panes/ContactDetails.java
src/be/nikiroo/jvcard/tui/panes/ContactDetailsRaw.java
src/be/nikiroo/jvcard/tui/panes/ContactList.java
src/be/nikiroo/jvcard/tui/panes/FileList.java
src/be/nikiroo/jvcard/tui/panes/MainContentList.java

index 1046d4cdbc982c06f061f0e36830a93305de8ff2..210cffca302dbe1cfb0d7d2eaf1ae53d2c2ea80f 100644 (file)
@@ -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 (file)
index 0000000..76c471f
--- /dev/null
@@ -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());
+       }
+}
similarity index 87%
rename from src/be/nikiroo/jvcard/i18n/FixedResourceBundleControl.java
rename to src/be/nikiroo/jvcard/resources/FixedResourceBundleControl.java
index d29f597be271d89618acf99d79e73fc08d9e495d..e67ace1c485c2d347df069ea6b231cfb5d28cca9 100644 (file)
@@ -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 (file)
index 0000000..c57a99e
--- /dev/null
@@ -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
index eda0c69407059a7a200fa4e43d6dce2186eeae4e..fd6d7de4e6e4e5583a36f742ff44fbb65ef277f2 100644 (file)
@@ -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<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();
-       }
 }
index eadcd2985ca0546af2ab39126d4cb048ac5181ae..a646dbead669ed9dafaf3f549b0eee783eb7f0e5 100644 (file)
@@ -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<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, //
@@ -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<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;
        }
+
 }
index 8aeac19f27002419035c58ba6218cf36a692bc82..08df4034165c761931a0fedebcf5eebf241d0058 100644 (file)
@@ -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
                                        }
                                }
 
index 47b5e8addb103c25c3f63f4658f70751609fd5a1..ec1a13640beffc4c001a4341a2dfdbe61e94f405 100644 (file)
@@ -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;
 
index bbd1f77a73f267d286d5ec93165161511e3ab066..ae1a65b2ee52ed54bc788d26614677aa67250bb1 100644 (file)
@@ -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");
index 636ec431a8e0935c7ff94b859d9f953adb014e9e..a59311b86819b1a7983eefb25539d037ac12abf3 100644 (file)
@@ -23,9 +23,6 @@ public class FileList extends MainContentList {
        private List<Card> cards;
 
        public FileList(List<File> files) {
-               super(UiColors.Element.CONTACT_LINE,
-                               UiColors.Element.CONTACT_LINE_SELECTED);
-
                setFiles(files);
        }
 
index 23892e58cd13a1cf654905b893f574408dce5e64..e1358eff66f9659ada469ade30dc0a0f97d886d8 100644 (file)
@@ -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();