Add more warnings source to 1.6) and fix warnings
[jvcard.git] / src / be / nikiroo / jvcard / tui / UiColors.java
index 50c2bf4fafdb698d1d7dd5ac94f5bff99ae7857c..61bb69239d8d726d38cade740baf8cf1c91c869d 100644 (file)
@@ -1,13 +1,20 @@
 package be.nikiroo.jvcard.tui;
 
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.MissingResourceException;
-import java.util.ResourceBundle;
+import java.util.Properties;
 
-import be.nikiroo.jvcard.resources.Bundles;
+import be.nikiroo.jvcard.resources.ColorBundle;
+import be.nikiroo.jvcard.resources.ColorOption;
 
 import com.googlecode.lanterna.TextColor;
+import com.googlecode.lanterna.graphics.PropertiesTheme;
+import com.googlecode.lanterna.graphics.Theme;
+import com.googlecode.lanterna.gui2.AbstractTextGUI;
 import com.googlecode.lanterna.gui2.Label;
 
 /**
@@ -20,102 +27,75 @@ public class UiColors {
        static private Object lock = new Object();
        static private UiColors instance = null;
 
-       private ResourceBundle bundle = null;
        private Map<String, TextColor> colorMap = null;
-       private boolean utf = true;
+       private ColorBundle bundle;
 
        private UiColors() {
+               bundle = new ColorBundle();
                colorMap = new HashMap<String, TextColor>();
-               bundle = Bundles.getBundle("colors");
        }
 
        /**
-        * Represent an element that can be coloured (foreground/background
-        * colours).
+        * Return a {@link Theme} following the colours defined in
+        * colors.properties.
         * 
-        * @author niki
-        *
+        * @return the {@link Theme}
         */
-       public enum Element {
-               DEFAULT, //
-               TITLE_MAIN, TITLE_VARIABLE, TITLE_COUNT, //
-               ACTION_KEY, ACTION_DESC, //
-               LINE_MESSAGE, LINE_MESSAGE_ERR, LINE_MESSAGE_QUESTION, LINE_MESSAGE_ANS, //
-               CONTACT_LINE, CONTACT_LINE_SEPARATOR, CONTACT_LINE_SELECTED, CONTACT_LINE_SEPARATOR_SELECTED, CONTACT_LINE_DIRTY, CONTACT_LINE_DIRTY_SELECTED, //
-               VIEW_CONTACT_NAME, VIEW_CONTACT_NORMAL, VIEW_CONTACT_NOTES_TITLE, //
-               ;
-
-               /**
-                * Get the foreground colour of this element.
-                * 
-                * @return the colour
-                */
-               public TextColor getForegroundColor() {
-                       return UiColors.getInstance().getForegroundColor(this);
+       static public Theme getCustomTheme() {
+               // Create a properties-theme with our own custom values for some of it
+               Properties properties = new Properties();
+               try {
+                       ClassLoader classLoader = AbstractTextGUI.class.getClassLoader();
+                       InputStream resourceAsStream = classLoader
+                                       .getResourceAsStream("default-theme.properties");
+                       if (resourceAsStream == null) {
+                               resourceAsStream = new FileInputStream(
+                                               "src/main/resources/default-theme.properties");
+                       }
+                       properties.load(resourceAsStream);
+                       resourceAsStream.close();
+               } catch (IOException e) {
                }
 
-               /**
-                * Get the background colour of this element.
-                * 
-                * @return the colour
-                */
-               public TextColor getBackgroundColor() {
-                       return UiColors.getInstance().getBackgroundColor(this);
+               // default colours:
+               String fg = getForegroundColor(ColorOption.DEFAULT).toString();
+               String bg = getBackgroundColor(ColorOption.DEFAULT).toString();
+               for (String def : new String[] { "com.googlecode.lanterna",
+                               "com.googlecode.lanterna.gui2.TextBox",
+                               "com.googlecode.lanterna.gui2.AbstractListBox",
+                               "com.googlecode.lanterna.gui2.Borders$StandardBorder" }) {
+                       properties.put(def + ".foreground", fg);
+                       properties.put(def + ".background", bg);
                }
 
-               /**
-                * 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);
-               }
+               // no bold on borders prelight:
+               properties
+                               .put("com.googlecode.lanterna.gui2.Borders$StandardBorder.sgr[PRELIGHT]",
+                                               "");
 
-               /**
-                * 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);
-               }
-       }
-
-       /**
-        * Check if unicode characters should be used.
-        * 
-        * @return TRUE to allow unicode
-        */
-       public boolean isUnicode() {
-               return utf;
-       }
+               // line answers:
+               fg = getForegroundColor(ColorOption.LINE_MESSAGE_ANS).toString();
+               bg = getBackgroundColor(ColorOption.LINE_MESSAGE_ANS).toString();
+               String prop = "com.googlecode.lanterna.gui2.TextBox";
+               properties.put(prop + ".foreground[ACTIVE]", fg);
+               properties.put(prop + ".background[ACTIVE]", bg);
 
-       /**
-        * Allow or disallow unicode characters in the program.
-        * 
-        * @param utf
-        *            TRUE to allow unuciode, FALSE to only allow ASCII characters
-        */
-       public void setUnicode(boolean utf) {
-               this.utf = utf;
+               PropertiesTheme theme = new PropertiesTheme(properties);
+               return theme;
        }
 
        /**
-        * Create a new {@link Label} with the colours of the given {@link Element}.
+        * Create a new {@link Label} with the colours of the given
+        * {@link ColorOption}.
         * 
         * @param el
-        *            the {@link Element}
+        *            the {@link ColorOption}
         * @param text
         *            the text of the {@link Label}
         * 
         * @return the new {@link Label}
         */
-       private Label createLabel(Element el, String text) {
+       static public Label createLabel(ColorOption el, String text) {
                if (text == null)
                        text = "";
 
@@ -125,62 +105,62 @@ public class UiColors {
        }
 
        /**
-        * Theme a {@link Label} with the colours of the given {@link Element}.
+        * Theme a {@link Label} with the colours of the given {@link ColorOption}.
         * 
         * @param el
-        *            the {@link Element}
+        *            the {@link ColorOption}
         * @param lbl
         *            the {@link Label}
         */
-       private void themeLabel(Element el, Label lbl) {
-               lbl.setForegroundColor(el.getForegroundColor());
-               lbl.setBackgroundColor(el.getBackgroundColor());
+       static public void themeLabel(ColorOption el, Label lbl) {
+               lbl.setForegroundColor(getForegroundColor(el));
+               lbl.setBackgroundColor(getBackgroundColor(el));
        }
 
        /**
         * Return the background colour of the given element.
         * 
         * @param el
-        *            the {@link Element}
+        *            the {@link ColorOption}
         * 
         * @return its background colour
         */
-       private TextColor getBackgroundColor(Element el) {
-               if (!colorMap.containsKey(el.name() + "_BG")) {
+       static public TextColor getBackgroundColor(ColorOption el) {
+               if (!getInstance().colorMap.containsKey(el.name() + "_BG")) {
                        String value = null;
                        try {
-                               value = bundle.getString(el.name() + "_BG");
+                               value = getInstance().bundle.getStringX(el, "BG");
                        } catch (MissingResourceException mre) {
                                value = null;
                        }
-                       colorMap.put(el.name() + "_BG",
+                       getInstance().colorMap.put(el.name() + "_BG",
                                        convertToColor(value, TextColor.ANSI.BLACK));
                }
 
-               return colorMap.get(el.name() + "_BG");
+               return getInstance().colorMap.get(el.name() + "_BG");
        }
 
        /**
         * Return the foreground colour of the given element.
         * 
         * @param el
-        *            the {@link Element}
+        *            the {@link ColorOption}
         * 
         * @return its foreground colour
         */
-       private TextColor getForegroundColor(Element el) {
-               if (!colorMap.containsKey(el.name() + "_FG")) {
+       static public TextColor getForegroundColor(ColorOption el) {
+               if (!getInstance().colorMap.containsKey(el.name() + "_FG")) {
                        String value = null;
                        try {
-                               value = bundle.getString(el.name() + "_FG");
+                               value = getInstance().bundle.getStringX(el, "FG");
                        } catch (MissingResourceException mre) {
                                value = null;
                        }
-                       colorMap.put(el.name() + "_FG",
+                       getInstance().colorMap.put(el.name() + "_FG",
                                        convertToColor(value, TextColor.ANSI.WHITE));
                }
 
-               return colorMap.get(el.name() + "_FG");
+               return getInstance().colorMap.get(el.name() + "_FG");
        }
 
        /**
@@ -188,7 +168,7 @@ public class UiColors {
         * 
         * @return the (unique) instance
         */
-       static public UiColors getInstance() {
+       static private UiColors getInstance() {
                synchronized (lock) {
                        if (instance == null)
                                instance = new UiColors();
@@ -232,5 +212,4 @@ public class UiColors {
 
                return defaultColor;
        }
-
 }