You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2020/08/25 11:55:25 UTC

svn commit: r1881183 - in /pivot/trunk: wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTheme.java wtk/src/org/apache/pivot/wtk/FontUtilities.java wtk/src/org/apache/pivot/wtk/Theme.java

Author: rwhitcomb
Date: Tue Aug 25 11:55:25 2020
New Revision: 1881183

URL: http://svn.apache.org/viewvc?rev=1881183&view=rev
Log:
PIVOT-1032: Fix all the style errors in Theme and TerraTheme
(add Javadoc, rename some parameters to fix visibility issues, make
parameters final, etc.)
Change color palette in TerraTheme to an array instead of a list (for speed).
Add a new "decodeFontSize" method in FontUtilities and use that from
Theme as needed.


Modified:
    pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTheme.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Theme.java

Modified: pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTheme.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTheme.java?rev=1881183&r1=1881182&r2=1881183&view=diff
==============================================================================
--- pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTheme.java (original)
+++ pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTheme.java Tue Aug 25 11:55:25 2020
@@ -22,7 +22,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 
-import org.apache.pivot.collections.ArrayList;
 import org.apache.pivot.collections.HashMap;
 import org.apache.pivot.collections.List;
 import org.apache.pivot.collections.Map;
@@ -96,38 +95,72 @@ import org.apache.pivot.wtk.util.ColorUt
  * as far as color and style using configuration files and runtime attributes.
  */
 public final class TerraTheme extends Theme {
+    /** The current font used by this theme. */
     private Font font = null;
-    private List<Color> colors = null;
+    /** The current color palette for the theme. */
+    private Color[] colors = null;
+    /** The number of base colors in the theme palette. */
     private int numberOfPaletteColors = 0;
+    /** Map of message type to icon images. */
     private Map<MessageType, Image> messageIcons = null;
+    /** Map of message types to small icon images. */
     private Map<MessageType, Image> smallMessageIcons = null;
 
-    private static float colorMultiplier = 0.1f;
+    /** The default color multiplier (in HSV space) to use for {@link #brighten} and {@link #darken}. */
+    private static final float DEFAULT_COLOR_MULTIPLIER = 0.1f;
+    /** Number of columns in the theme's color table (darker, base, lighter). */
+    private static final int COLOR_TABLE_COLUMNS = 3;
+
+    /** Current multiplier (HSV space) for brightening colors. */
+    private static float brightenColorMultiplier = DEFAULT_COLOR_MULTIPLIER;
+    /** Current multiplier (HSV space) for darkening colors. */
+    private static float darkenColorMultiplier;
+    /** Whether this theme is a "dark" theme -- meaning bright colors on a dark background. */
     private static boolean themeIsDark = false;
+    /** Whether this theme is "flat" -- meaning no shadows or depth effect for buttons, etc. */
     private static boolean themeIsFlat = false;
+    /** Whether this theme allows animations during transitions. */
     private static boolean transitionEnabled = true;
 
+    /** What the default background of a component should be according to this theme. */
     private Color defaultBackgroundColor;
+    /** What the default foreground color of a component should be in this theme. */
     private Color defaultForegroundColor;
 
+    /** Property map key for the location of the theme classes. */
     public static final String LOCATION_PROPERTY = "location";
 
+    /** Property map key for the default font for the theme. */
     public static final String FONT_PROPERTY = "font";
+    /** Property map key for the default color multiplier to use for the theme. */
     public static final String COLOR_MULTIPLIER_PROPERTY = "colorMultiplier";
+    /** Property map key for the "isDark" flag for the theme. */
     public static final String THEME_IS_DARK_PROPERTY = "themeIsDark";
+    /** Property map key for the "isFlat" flag for this theme. */
     public static final String THEME_IS_FLAT_PROPERTY = "themeIsFlat";
+    /** Property map key for the "transitionEnabled" flag for the theme. */
     public static final String TRANSITION_ENABLED_PROPERTY = "transitionEnabled";
+    /** Property map key for the list of base colors for the theme. */
     public static final String COLORS_PROPERTY = "colors";
+    /** Property map key for the name of the default styles file. */
     public static final String DEFAULT_STYLES_PROPERTY = "defaultStylesFile";
+    /** Property map key for the name of the named styles file. */
     public static final String NAMED_STYLES_PROPERTY = "namedStylesFile";
+    /** Property map key for the message icons. */
     public static final String MESSAGE_ICONS_PROPERTY = "messageIcons";
+    /** Property map key for the small message icons. */
     public static final String SMALL_MESSAGE_ICONS_PROPERTY = "smallMessageIcons";
+    /** Property map key for the default background color for this theme. */
     public static final String DEFAULT_BACKGROUND_COLOR_PROPERTY = "defaultBackgroundColor";
+    /** Property map key for the default foreground color for the theme. */
     public static final String DEFAULT_FOREGROUND_COLOR_PROPERTY = "defaultForegroundColor";
 
+    /** The style name for our "command" buttons (such as "OK" and "Cancel"). */
     public static final String COMMAND_BUTTON_STYLE = "commandButton";
 
+    /** Name of the file to read for setting default styles for each component skin. */
     public static final String DEFAULT_STYLES_FILE = "terra_theme_defaults.json";
+    /** Name of the file containing our theme's default style definitions. */
     public static final String NAMED_STYLES_FILE = "terra_theme_styles.json";
 
     /** Can be overridden by {@link #DEFAULT_STYLES_PROPERTY} property. */
@@ -135,6 +168,7 @@ public final class TerraTheme extends Th
     /** Can be overridden by {@link #NAMED_STYLES_PROPERTY} property. */
     private String namedStylesFile = NAMED_STYLES_FILE;
 
+    /** The loaded default styles map (key is the skin's simple name). */
     private Map<String, Map<String, ?>> themeDefaultStyles = null;
 
 
@@ -272,6 +306,22 @@ public final class TerraTheme extends Th
 
     /**
      * Get the given color property from the color properties map, doing the color decode from
+     * the string property value. If the property is not found, return the given default value.
+     *
+     * @param properties The color properties map.
+     * @param colorPropertyName Name of the color property to get.
+     * @param defaultColor The value to use if the property is not found in the map.
+     * @return The decoded color value or the default value if not found.
+     * @see GraphicsUtilities#decodeColor
+     */
+    private Color getColorProperty(final Map<String, ?> properties, final String colorPropertyName,
+        final Color defaultColor) {
+        Color propertyColor = getColorProperty(properties, colorPropertyName);
+        return propertyColor == null ? defaultColor : propertyColor;
+    }
+
+    /**
+     * Get the given color property from the color properties map, doing the color decode from
      * the string property value.
      *
      * @param properties The color properties map.
@@ -314,34 +364,39 @@ public final class TerraTheme extends Th
             @SuppressWarnings("unchecked")
             List<String> colorCodes = (List<String>) properties.get(COLORS_PROPERTY);
             numberOfPaletteColors = colorCodes.getLength();
-            int numberOfColors = numberOfPaletteColors * 3;
-            colors = new ArrayList<>(numberOfColors);
+            int numberOfColors = numberOfPaletteColors * COLOR_TABLE_COLUMNS;
+            colors = new Color[numberOfColors];
 
             Double mult = (Double) properties.get(COLOR_MULTIPLIER_PROPERTY);
             if (mult != null) {
-                colorMultiplier = mult.floatValue();
+                brightenColorMultiplier = mult.floatValue();
             }
 
             themeIsDark = properties.getBoolean(THEME_IS_DARK_PROPERTY, false);
             themeIsFlat = properties.getBoolean(THEME_IS_FLAT_PROPERTY, false);
             transitionEnabled = properties.getBoolean(TRANSITION_ENABLED_PROPERTY, true);
 
+            // For dark themes, reverse the brighten/darken multipliers so "darker" -> brighter, and vice-versa
+            if (themeIsDark) {
+                brightenColorMultiplier = -brightenColorMultiplier;
+            }
+            // So, "darken" is always the reverse direction of "brighten" (whichever way that goes)
+            darkenColorMultiplier = -brightenColorMultiplier;
+
+            int colorIndex = 0;
             for (String colorCode : colorCodes) {
                 Color baseColor = GraphicsUtilities.decodeColor(colorCode, "baseColor");
-                colors.add(darken(baseColor));
-                colors.add(baseColor);
-                colors.add(brighten(baseColor));
+                setBaseColor(colorIndex++, baseColor);
             }
 
             messageIcons = loadMessageIcons(properties, MESSAGE_ICONS_PROPERTY);
             smallMessageIcons = loadMessageIcons(properties, SMALL_MESSAGE_ICONS_PROPERTY);
 
-            if ((defaultBackgroundColor = getColorProperty(properties, DEFAULT_BACKGROUND_COLOR_PROPERTY)) == null) {
-                defaultBackgroundColor = super.getDefaultBackgroundColor();
-            }
-            if ((defaultForegroundColor = getColorProperty(properties, DEFAULT_FOREGROUND_COLOR_PROPERTY)) == null) {
-                defaultForegroundColor = super.getDefaultForegroundColor();
-            }
+            defaultBackgroundColor = getColorProperty(properties, DEFAULT_BACKGROUND_COLOR_PROPERTY,
+                super.getDefaultBackgroundColor());
+            defaultForegroundColor = getColorProperty(properties, DEFAULT_FOREGROUND_COLOR_PROPERTY,
+                super.getDefaultForegroundColor());
+
         } catch (IOException | SerializationException exception) {
             throw new RuntimeException(exception);
         }
@@ -385,13 +440,13 @@ public final class TerraTheme extends Th
     /**
      * Sets the theme's font.
      *
-     * @param font the font
+     * @param newFont The new font to use for this theme.
      */
     @Override
-    public void setFont(final Font font) {
-        Utils.checkNull(font, "Font");
+    public void setFont(final Font newFont) {
+        Utils.checkNull(newFont, "Font");
 
-        this.font = font;
+        this.font = newFont;
     }
 
     /**
@@ -425,7 +480,7 @@ public final class TerraTheme extends Th
     public Color getColor(final int index) {
         checkColorIndex(index);
 
-        return colors.get(index);
+        return colors[index];
     }
 
     /**
@@ -440,7 +495,7 @@ public final class TerraTheme extends Th
         checkColorIndex(index);
         Utils.checkNull(color, "Color");
 
-        colors.update(index, color);
+        colors[index] = color;
     }
 
     /**
@@ -452,7 +507,7 @@ public final class TerraTheme extends Th
     public Color getBaseColor(final int index) {
         checkColorIndex(index, numberOfPaletteColors);
 
-        return colors.get(index * 3 + 1);
+        return colors[index * COLOR_TABLE_COLUMNS + 1];
     }
 
     /**
@@ -466,10 +521,10 @@ public final class TerraTheme extends Th
         checkColorIndex(index, numberOfPaletteColors);
         Utils.checkNull(baseColor, "Base color");
 
-        int offset = index * 3;
-        colors.update(offset, darken(baseColor));
-        colors.update(offset + 1, baseColor);
-        colors.update(offset + 2, brighten(baseColor));
+        int offset = index * COLOR_TABLE_COLUMNS + 1;
+        colors[offset - 1] = darken(baseColor);
+        colors[offset] = baseColor;
+        colors[offset + 1] = brighten(baseColor);
     }
 
     /**
@@ -485,11 +540,11 @@ public final class TerraTheme extends Th
     /**
      * Gets the total number of Colors (including derived colors, if any).
      *
-     * @return the number
+     * @return Total number of theme colors.
      */
     @Override
     public int getNumberOfColors() {
-        return colors == null ? 0 : colors.getLength();
+        return colors == null ? 0 : colors.length;
     }
 
     /**
@@ -604,10 +659,7 @@ public final class TerraTheme extends Th
      * @param color The color to brighten.
      */
     public static Color brighten(final Color color) {
-        if (!themeIsDark) {
-            return ColorUtilities.adjustBrightness(color, colorMultiplier);
-        }
-        return ColorUtilities.adjustBrightness(color, (colorMultiplier * -1.0f));
+        return ColorUtilities.adjustBrightness(color, brightenColorMultiplier);
     }
 
     /**
@@ -617,10 +669,7 @@ public final class TerraTheme extends Th
      * @param color The color to darken.
      */
     public static Color darken(final Color color) {
-        if (!themeIsDark) {
-            return ColorUtilities.adjustBrightness(color, (colorMultiplier * -1.0f));
-        }
-        return ColorUtilities.adjustBrightness(color, colorMultiplier);
+        return ColorUtilities.adjustBrightness(color, darkenColorMultiplier);
     }
 
     /**

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java?rev=1881183&r1=1881182&r2=1881183&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java Tue Aug 25 11:55:25 2020
@@ -30,6 +30,9 @@ public final class FontUtilities {
     /** The standard name for the {@code Arial} font. */
     public static final String ARIAL = "Arial";
 
+    /** The obvious factor needed to convert a number to a percentage factor. */
+    private static final float PERCENT_SCALE = 100.0f;
+
     /**
      * Private constructor for utility class.
      */
@@ -63,4 +66,41 @@ public final class FontUtilities {
         return font;
     }
 
+    /**
+     * Decode a font size specification, taking into account "nnn%" form, and the existing size.
+     *
+     * @param sizeValue The input size value, could be a number or a numeric string, or a number
+     * followed by "%".
+     * @param existingSize The existing font size, which will be adjusted by the percentage.
+     * Can be null, in which case the original size is returned. Otherwise it is unused.
+     * @return The new font size value.
+     * @throws IllegalArgumentException if the sizeValue cannot be decoded.
+     */
+    public static int decodeFontSize(final Object sizeValue, final int existingSize) {
+        int adjustedSize = existingSize;
+
+        if (sizeValue != null) {
+            if (sizeValue instanceof String) {
+                String string = (String) sizeValue;
+
+                try {
+                    if (string.endsWith("%")) {
+                        float percentage = Float.parseFloat(string.substring(0, string.length() - 1)) / PERCENT_SCALE;
+                        adjustedSize = Math.round(existingSize * percentage);
+                    } else {
+                        adjustedSize = Float.valueOf(string).intValue();
+                    }
+                } catch (NumberFormatException nfe) {
+                    throw new IllegalArgumentException("\"" + sizeValue + "\" is not a valid font size!");
+                }
+            } else if (sizeValue instanceof Number) {
+                adjustedSize = ((Number) sizeValue).intValue();
+            } else {
+                throw new IllegalArgumentException("\"" + sizeValue + "\" is not a valid font size!");
+            }
+        }
+
+        return adjustedSize;
+    }
+
 }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Theme.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Theme.java?rev=1881183&r1=1881182&r2=1881183&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Theme.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Theme.java Tue Aug 25 11:55:25 2020
@@ -72,9 +72,13 @@ public abstract class Theme {
      */
     private static Theme theme = null;
 
+    /** Key for the font name in a font dictionary. */
     public static final String NAME_KEY = "name";
+    /** Key for the font size in a font dictionary. */
     public static final String SIZE_KEY = "size";
+    /** Key for the font bold style in a font dictionary. */
     public static final String BOLD_KEY = "bold";
+    /** Key for the font italic style in a font dictionary. */
     public static final String ITALIC_KEY = "italic";
 
     /**
@@ -86,7 +90,7 @@ public abstract class Theme {
         theme = (Theme) Service.getProvider(PROVIDER_NAME);
 
         if (theme == null) {
-            throw new ThemeNotFoundException();
+            throw new ThemeNotFoundException("Unable to find a service provider for " + PROVIDER_NAME + "!");
         }
     }
 
@@ -120,8 +124,7 @@ public abstract class Theme {
     }
 
     /**
-     * Returns the skin class responsible for skinning the specified component
-     * class.
+     * Returns the skin class responsible for skinning the specified component class.
      *
      * @param componentClass The component class.
      * @return The skin class, or <tt>null</tt> if no skin mapping exists for
@@ -129,6 +132,8 @@ public abstract class Theme {
      * @throws IllegalArgumentException if the given component class is <tt>null</tt>.
      */
     public final Class<? extends Skin> getSkinClass(final Class<? extends Component> componentClass) {
+        Utils.checkNull(componentClass, "Component class");
+
         return componentSkinMap.get(componentClass);
     }
 
@@ -179,8 +184,8 @@ public abstract class Theme {
     /**
      * Sets a color in the theme's base color palette.
      *
-     * @param index the index of the color, starting from 0
-     * @param baseColor the color to set
+     * @param index The index of the color, starting from 0.
+     * @param baseColor The color value to set.
      */
     public abstract void setBaseColor(int index, Color baseColor);
 
@@ -196,8 +201,8 @@ public abstract class Theme {
      * Sets a value in the theme's complete color palette (including derived
      * colors, if any).
      *
-     * @param index the index of the color, starting from 0
-     * @param color the color to set
+     * @param index The index of the color, starting from 0.
+     * @param color The color value to set.
      */
     public abstract void setColor(int index, Color color);
 
@@ -309,20 +314,7 @@ public abstract class Theme {
 
         int size = font.getSize();
         if (dictionary.containsKey(SIZE_KEY)) {
-            Object value = dictionary.get(SIZE_KEY);
-
-            if (value instanceof String) {
-                String string = (String) value;
-
-                if (string.endsWith("%")) {
-                    float percentage = Float.parseFloat(string.substring(0, string.length() - 1)) / 100f;
-                    size = Math.round(font.getSize() * percentage);
-                } else {
-                    throw new IllegalArgumentException(value + " is not a valid font size.");
-                }
-            } else {
-                size = ((Integer) value).intValue();
-            }
+            size = FontUtilities.decodeFontSize(dictionary.get(SIZE_KEY), size);
         }
 
         int style = font.getStyle();