You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/04/03 21:08:39 UTC

svn commit: r761765 - in /incubator/pivot/trunk: charts/src/pivot/charts/ core/src/pivot/util/ tutorials/ tutorials/src/pivot/tutorials/ tutorials/src/pivot/tutorials/layout/ wtk/src/META-INF/ wtk/src/META-INF/services/ wtk/src/pivot/wtk/ wtk/src/pivot...

Author: gbrown
Date: Fri Apr  3 19:08:38 2009
New Revision: 761765

URL: http://svn.apache.org/viewvc?rev=761765&view=rev
Log:
Use the service provide architecture to identify and load WTK themes.

Added:
    incubator/pivot/trunk/core/src/pivot/util/Service.java
    incubator/pivot/trunk/wtk/src/META-INF/
    incubator/pivot/trunk/wtk/src/META-INF/services/
    incubator/pivot/trunk/wtk/src/META-INF/services/pivot.wtk.Theme
    incubator/pivot/trunk/wtk/src/pivot/wtk/ThemeNotFoundException.java
Modified:
    incubator/pivot/trunk/charts/src/pivot/charts/ChartView.java
    incubator/pivot/trunk/charts/src/pivot/charts/Provider.java
    incubator/pivot/trunk/charts/src/pivot/charts/ProviderNotFoundException.java
    incubator/pivot/trunk/tutorials/.classpath
    incubator/pivot/trunk/tutorials/src/pivot/tutorials/Demo.java
    incubator/pivot/trunk/tutorials/src/pivot/tutorials/layout/FlowPanes.java
    incubator/pivot/trunk/tutorials/src/pivot/tutorials/layout/stackpanes.wtkx
    incubator/pivot/trunk/wtk/src/pivot/wtk/ApplicationContext.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/Theme.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/skin/terra/TerraTheme.java

Modified: incubator/pivot/trunk/charts/src/pivot/charts/ChartView.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/charts/src/pivot/charts/ChartView.java?rev=761765&r1=761764&r2=761765&view=diff
==============================================================================
--- incubator/pivot/trunk/charts/src/pivot/charts/ChartView.java (original)
+++ incubator/pivot/trunk/charts/src/pivot/charts/ChartView.java Fri Apr  3 19:08:38 2009
@@ -16,10 +16,6 @@
  */
 package pivot.charts;
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.util.Comparator;
 
 import pivot.collections.ArrayList;
@@ -27,6 +23,7 @@
 import pivot.collections.ListListener;
 import pivot.collections.Sequence;
 import pivot.util.ListenerList;
+import pivot.util.Service;
 import pivot.wtk.Component;
 
 /**
@@ -376,73 +373,16 @@
     private ChartViewSeriesListenerList chartViewSeriesListeners = new ChartViewSeriesListenerList();
 
     public static final String DEFAULT_SERIES_NAME_KEY = "name";
-    public static final String PROVIDER_PROPERTY_NAME = "pivot.charts.Provider";
+    public static final String PROVIDER_NAME = "pivot.charts.Provider";
 
     private static Provider provider = null;
 
     static {
-        // Load the chart provider
-        String providerClassName = null;
+        provider = (Provider)Service.getProvider(PROVIDER_NAME);
 
-        // First look for a system property
-        try {
-            providerClassName = System.getProperty(PROVIDER_PROPERTY_NAME);
-        } catch(SecurityException exception) {
-            // No-op
-        }
-
-        // Next look for a service descriptor on the classpath
-        if (providerClassName == null) {
-            String serviceName = "META-INF/services/" + PROVIDER_PROPERTY_NAME;
-
-            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-            InputStream serviceInputStream = classLoader.getResourceAsStream(serviceName);
-
-            if (serviceInputStream != null) {
-                try {
-                    BufferedReader reader = null;
-                    try {
-                        reader = new BufferedReader(new InputStreamReader(serviceInputStream, "UTF-8"));
-                        providerClassName = reader.readLine();
-                    } finally {
-                        if (reader != null) {
-                            reader.close();
-                        }
-                    }
-                } catch(IOException exception) {
-                    // No-op
-                }
-            }
-        }
-
-        // Try to load the provider class
-        Class<?> providerClass = null;
-
-        if (providerClassName != null) {
-            try {
-                providerClass = Class.forName(providerClassName);
-            } catch(ClassNotFoundException exception) {
-                // The specified class could not be found
-            }
-
-            if (providerClass != null
-                && !Provider.class.isAssignableFrom(providerClass)) {
-                // The specified class does not implement the Provider interface
-                providerClass = null;
-            }
-        }
-
-        if (providerClass == null) {
+        if (provider == null) {
             throw new ProviderNotFoundException();
         }
-
-        try {
-            provider = (Provider)providerClass.newInstance();
-        } catch(InstantiationException exception) {
-            throw new IllegalArgumentException(exception);
-        } catch(IllegalAccessException exception) {
-            throw new IllegalArgumentException(exception);
-        }
     }
 
     public ChartView() {

Modified: incubator/pivot/trunk/charts/src/pivot/charts/Provider.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/charts/src/pivot/charts/Provider.java?rev=761765&r1=761764&r2=761765&view=diff
==============================================================================
--- incubator/pivot/trunk/charts/src/pivot/charts/Provider.java (original)
+++ incubator/pivot/trunk/charts/src/pivot/charts/Provider.java Fri Apr  3 19:08:38 2009
@@ -1,9 +1,10 @@
 /*
- * Copyright (c) 2009 VMware, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *

Modified: incubator/pivot/trunk/charts/src/pivot/charts/ProviderNotFoundException.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/charts/src/pivot/charts/ProviderNotFoundException.java?rev=761765&r1=761764&r2=761765&view=diff
==============================================================================
--- incubator/pivot/trunk/charts/src/pivot/charts/ProviderNotFoundException.java (original)
+++ incubator/pivot/trunk/charts/src/pivot/charts/ProviderNotFoundException.java Fri Apr  3 19:08:38 2009
@@ -1,9 +1,10 @@
 /*
- * Copyright (c) 2009 VMware, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *

Added: incubator/pivot/trunk/core/src/pivot/util/Service.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/util/Service.java?rev=761765&view=auto
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/util/Service.java (added)
+++ incubator/pivot/trunk/core/src/pivot/util/Service.java Fri Apr  3 19:08:38 2009
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.util;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+/**
+ * Utility class for locating and instantiating service providers.
+ *
+ * @author gbrown
+ */
+public class Service {
+    /**
+     * Attempts to load a service provider.
+     *
+     * @param providerName
+     * The name of the provider to load. The method first looks for a system
+     * property with this name. The value of the property is expected to be the
+     * name of a class that implements the expected provider interface.
+     * <p>
+     * If the system property does not exist, the method then attempts to load
+     * a resource with this name from the META-INF/services directory. The
+     * resource is expected to be a text file containing a single line that is
+     * the name of the provider class.
+     */
+    public static Object getProvider(String providerName) {
+        String providerClassName = null;
+
+        // First look for a system property
+        try {
+            providerClassName = System.getProperty(providerName);
+        } catch(SecurityException exception) {
+            // No-op
+        }
+
+        // Next look for a service descriptor on the classpath
+        if (providerClassName == null) {
+            String serviceName = "META-INF/services/" + providerName;
+
+            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+            InputStream serviceInputStream = classLoader.getResourceAsStream(serviceName);
+
+            if (serviceInputStream != null) {
+                try {
+                    BufferedReader reader = null;
+                    try {
+                        reader = new BufferedReader(new InputStreamReader(serviceInputStream, "UTF-8"));
+                        providerClassName = reader.readLine();
+                    } finally {
+                        if (reader != null) {
+                            reader.close();
+                        }
+                    }
+                } catch(IOException exception) {
+                    // No-op
+                }
+            }
+        }
+
+        // Try to load the provider class
+        Class<?> providerClass = null;
+
+        if (providerClassName != null) {
+            try {
+                providerClass = Class.forName(providerClassName);
+            } catch(ClassNotFoundException exception) {
+                // The specified class could not be found
+            }
+        }
+
+        Object provider = null;
+        try {
+            provider = providerClass.newInstance();
+        } catch(InstantiationException exception) {
+            // No-op
+        } catch(IllegalAccessException exception) {
+            // No-op
+        }
+
+        return provider;
+    }
+}

Modified: incubator/pivot/trunk/tutorials/.classpath
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/.classpath?rev=761765&r1=761764&r2=761765&view=diff
==============================================================================
--- incubator/pivot/trunk/tutorials/.classpath (original)
+++ incubator/pivot/trunk/tutorials/.classpath Fri Apr  3 19:08:38 2009
@@ -5,5 +5,6 @@
 	<classpathentry combineaccessrules="false" kind="src" path="/core"/>
 	<classpathentry combineaccessrules="false" kind="src" path="/web"/>
 	<classpathentry combineaccessrules="false" kind="src" path="/wtk"/>
+	<classpathentry kind="lib" path="/wtk/src"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>

Modified: incubator/pivot/trunk/tutorials/src/pivot/tutorials/Demo.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/src/pivot/tutorials/Demo.java?rev=761765&r1=761764&r2=761765&view=diff
==============================================================================
--- incubator/pivot/trunk/tutorials/src/pivot/tutorials/Demo.java (original)
+++ incubator/pivot/trunk/tutorials/src/pivot/tutorials/Demo.java Fri Apr  3 19:08:38 2009
@@ -59,6 +59,7 @@
 import pivot.wtk.TableView;
 import pivot.wtk.TableViewHeader;
 import pivot.wtk.TextArea;
+import pivot.wtk.Theme;
 import pivot.wtk.Visual;
 import pivot.wtk.Window;
 import pivot.wtk.content.CalendarDateSpinnerData;
@@ -68,6 +69,7 @@
 import pivot.wtk.effects.ReflectionDecorator;
 import pivot.wtk.effects.WatermarkDecorator;
 import pivot.wtk.media.Image;
+import pivot.wtk.skin.terra.TerraTheme;
 import pivot.wtk.text.Document;
 import pivot.wtk.text.PlainTextSerializer;
 import pivot.wtkx.WTKXSerializer;
@@ -91,7 +93,9 @@
     private Window window = null;
 
     public void startup(final Display display, Dictionary<String, String> properties) throws Exception {
-        // pivot.wtk.Theme.setTheme(new pivot.wtk.skin.terra.TerraTheme("test"));
+        TerraTheme terraTheme = (TerraTheme)Theme.getTheme();
+        URL schemeLocation = TerraTheme.class.getResource("TerraTheme_default.json");
+        terraTheme.loadScheme(schemeLocation);
 
         WTKXSerializer wtkxSerializer = new WTKXSerializer();
         Component content = (Component)wtkxSerializer.readObject("pivot/tutorials/demo.wtkx");

Modified: incubator/pivot/trunk/tutorials/src/pivot/tutorials/layout/FlowPanes.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/src/pivot/tutorials/layout/FlowPanes.java?rev=761765&r1=761764&r2=761765&view=diff
==============================================================================
--- incubator/pivot/trunk/tutorials/src/pivot/tutorials/layout/FlowPanes.java (original)
+++ incubator/pivot/trunk/tutorials/src/pivot/tutorials/layout/FlowPanes.java Fri Apr  3 19:08:38 2009
@@ -26,7 +26,6 @@
 import pivot.wtk.HorizontalAlignment;
 import pivot.wtk.Orientation;
 import pivot.wtk.RadioButton;
-import pivot.wtk.Theme;
 import pivot.wtk.VerticalAlignment;
 import pivot.wtk.Window;
 import pivot.wtkx.WTKXSerializer;
@@ -47,13 +46,6 @@
     private Window window = null;
 
     public void startup(Display display, Dictionary<String, String> properties) throws Exception {
-        String themeClassName = properties.get("themeClassName");
-
-        if (themeClassName != null) {
-            Class<?> themeClass = Class.forName(themeClassName);
-            Theme.setTheme((Theme)themeClass.newInstance());
-        }
-
         WTKXSerializer wtkxSerializer = new WTKXSerializer();
         Component content =
             (Component)wtkxSerializer.readObject("pivot/tutorials/layout/flowpanes.wtkx");

Modified: incubator/pivot/trunk/tutorials/src/pivot/tutorials/layout/stackpanes.wtkx
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/src/pivot/tutorials/layout/stackpanes.wtkx?rev=761765&r1=761764&r2=761765&view=diff
==============================================================================
--- incubator/pivot/trunk/tutorials/src/pivot/tutorials/layout/stackpanes.wtkx (original)
+++ incubator/pivot/trunk/tutorials/src/pivot/tutorials/layout/stackpanes.wtkx Fri Apr  3 19:08:38 2009
@@ -18,8 +18,7 @@
 
 <StackPane xmlns:wtkx="http://incubator.apache.org/pivot/wtkx/1.1" xmlns="pivot.wtk">
 	<ImageView image="pivot/tutorials/layout/03big.jpg"
-	    styles="{horizontalAlignment:'left', verticalAlignment:'top',
-	        scaleX:1.5, scaleY:1.25}"/>
+	    styles="{horizontalAlignment:'justify', verticalAlignment:'justify'}"/>
 	<Label text="StackPane Demo"
    		styles="{font:'Helvetica bold 64', color:'#ffffff', wrapText:true,
    		    horizontalAlignment:'center', verticalAlignment:'center'}"/>

Added: incubator/pivot/trunk/wtk/src/META-INF/services/pivot.wtk.Theme
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/META-INF/services/pivot.wtk.Theme?rev=761765&view=auto
==============================================================================
--- incubator/pivot/trunk/wtk/src/META-INF/services/pivot.wtk.Theme (added)
+++ incubator/pivot/trunk/wtk/src/META-INF/services/pivot.wtk.Theme Fri Apr  3 19:08:38 2009
@@ -0,0 +1 @@
+pivot.wtk.skin.terra.TerraTheme

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/ApplicationContext.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/ApplicationContext.java?rev=761765&r1=761764&r2=761765&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/ApplicationContext.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/ApplicationContext.java Fri Apr  3 19:08:38 2009
@@ -1084,22 +1084,10 @@
 
     private static Timer timer = null;
 
-    private static final String DEFAULT_THEME_CLASS_NAME = "pivot.wtk.skin.terra.TerraTheme";
-
     protected ApplicationContext() {
         // Create the display host and display
         displayHost = new DisplayHost();
         display = new Display(displayHost);
-
-        try {
-            // Load and instantiate the default theme, if possible
-            Class<?> themeClass = Class.forName(DEFAULT_THEME_CLASS_NAME);
-            Theme.setTheme((Theme)themeClass.newInstance());
-        } catch (Exception exception) {
-            // No-op; assume that a custom theme will be installed later
-            // by the caller
-            System.err.println("Warning: Unable to load default theme.");
-        }
     }
 
     protected DisplayHost getDisplayHost() {

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/Theme.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/Theme.java?rev=761765&r1=761764&r2=761765&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/Theme.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/Theme.java Fri Apr  3 19:08:38 2009
@@ -21,6 +21,7 @@
 
 import pivot.collections.ArrayList;
 import pivot.collections.HashMap;
+import pivot.util.Service;
 import pivot.wtk.media.Image;
 import pivot.wtk.skin.BorderSkin;
 import pivot.wtk.skin.CardPaneSkin;
@@ -50,12 +51,18 @@
     protected HashMap<Class<? extends Component>, Class<? extends Skin>> componentSkinMap =
         new HashMap<Class<? extends Component>, Class<? extends Skin>>();
 
+    public static final String PROVIDER_NAME = "pivot.wtk.Theme";
+
     private static Theme theme = null;
-    private static final Package DEFAULT_SKIN_PACKAGE;
 
     static {
-        DEFAULT_SKIN_PACKAGE = Package.getPackage("pivot.wtk.skin");
-        assert (DEFAULT_SKIN_PACKAGE != null) : "Default skin package not found.";
+        Theme theme = (Theme)Service.getProvider(PROVIDER_NAME);
+
+        if (theme == null) {
+            throw new ThemeNotFoundException();
+        }
+
+        setTheme(theme);
     }
 
     public Theme() {

Added: incubator/pivot/trunk/wtk/src/pivot/wtk/ThemeNotFoundException.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/ThemeNotFoundException.java?rev=761765&view=auto
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/ThemeNotFoundException.java (added)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/ThemeNotFoundException.java Fri Apr  3 19:08:38 2009
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.wtk;
+
+/**
+ * Thrown when a suitable theme cannot be located.
+ *
+ * @author gbrown
+ */
+public class ThemeNotFoundException extends RuntimeException {
+    private static final long serialVersionUID = 0;
+
+    public ThemeNotFoundException() {
+        super("A theme could not be located.");
+    }
+}

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/skin/terra/TerraTheme.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/skin/terra/TerraTheme.java?rev=761765&r1=761764&r2=761765&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/skin/terra/TerraTheme.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/skin/terra/TerraTheme.java Fri Apr  3 19:08:38 2009
@@ -135,7 +135,6 @@
      * @param location
      * The location of the JSON file that defines the theme's font and colors.
      */
-    @SuppressWarnings("unchecked")
     public TerraTheme(URL location) {
         if (location == null) {
             throw new IllegalArgumentException("location is null.");
@@ -198,6 +197,11 @@
         componentSkinMap.put(TerraSplitPaneSkin.SplitterShadow.class, TerraSplitPaneSkin.SplitterShadowSkin.class);
         componentSkinMap.put(TerraTabPaneSkin.TabButton.class, TerraTabPaneSkin.TabButtonSkin.class);
 
+        loadScheme(location);
+    }
+
+    @SuppressWarnings("unchecked")
+    public void loadScheme(URL location) {
         try {
             InputStream inputStream = location.openStream();