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/05/04 16:06:49 UTC

svn commit: r771310 - in /incubator/pivot/trunk: core/src/pivot/util/ tutorials/src/pivot/tutorials/ tutorials/src/pivot/tutorials/drawing/ wtk/src/pivot/wtk/ wtk/src/pivot/wtkx/

Author: gbrown
Date: Mon May  4 14:06:48 2009
New Revision: 771310

URL: http://svn.apache.org/viewvc?rev=771310&view=rev
Log:
Fix potential NPE in pivot.util.Service; hard-code calendar locales in Kitchen Sink demo; move WTKX binding code to WTKXSerializer.bind().

Modified:
    incubator/pivot/trunk/core/src/pivot/util/Service.java
    incubator/pivot/trunk/core/src/pivot/util/ThreadUtilities.java
    incubator/pivot/trunk/tutorials/src/pivot/tutorials/calendars.wtkx
    incubator/pivot/trunk/tutorials/src/pivot/tutorials/drawing/RotateLine.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/ApplicationContext.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/BrowserApplicationContext.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/DesktopApplicationContext.java
    incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java

Modified: 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=771310&r1=771309&r2=771310&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/util/Service.java (original)
+++ incubator/pivot/trunk/core/src/pivot/util/Service.java Mon May  4 14:06:48 2009
@@ -86,12 +86,14 @@
         }
 
         Object provider = null;
-        try {
-            provider = providerClass.newInstance();
-        } catch(InstantiationException exception) {
-            // No-op
-        } catch(IllegalAccessException exception) {
-            // No-op
+        if (providerClass != null) {
+            try {
+                provider = providerClass.newInstance();
+            } catch(InstantiationException exception) {
+                // No-op
+            } catch(IllegalAccessException exception) {
+                // No-op
+            }
         }
 
         return provider;

Modified: incubator/pivot/trunk/core/src/pivot/util/ThreadUtilities.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/util/ThreadUtilities.java?rev=771310&r1=771309&r2=771310&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/util/ThreadUtilities.java (original)
+++ incubator/pivot/trunk/core/src/pivot/util/ThreadUtilities.java Mon May  4 14:06:48 2009
@@ -17,7 +17,7 @@
 package pivot.util;
 
 /**
- * Contains utility methods dealing with threads.
+ * Contains utility methods for dealing with threads.
  *
  * @author tvolkert
  */
@@ -36,10 +36,10 @@
         // TODO This method is only necessary if the Pivot classes (and by
         // association, the application classes and resources) were loaded
         // from a different class loader than the UI thread's class loader.
-        // Curently, this case exists in the IcedTea plug-in and when we load
-        // the Pivot classes manully using a bootstrap applet. If these cases
+        // Currently, this case exists in the IcedTea plug-in and when we load
+        // the Pivot classes manually using a bootstrap applet. If these cases
         // ever cease to exist, then this method becomes superfluous and should
-        // be replaced with Thread.currentThread().getContextClassLoader()
+        // be replaced with Thread.currentThread().getContextClassLoader().
         ClassLoader classLoader;
 
         try {

Modified: incubator/pivot/trunk/tutorials/src/pivot/tutorials/calendars.wtkx
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/src/pivot/tutorials/calendars.wtkx?rev=771310&r1=771309&r2=771310&view=diff
==============================================================================
--- incubator/pivot/trunk/tutorials/src/pivot/tutorials/calendars.wtkx (original)
+++ incubator/pivot/trunk/tutorials/src/pivot/tutorials/calendars.wtkx Mon May  4 14:06:48 2009
@@ -25,7 +25,7 @@
                     <Label text="English (US)" styles="{fontBold:true}"/>
                     <Border styles="{padding:0}">
                         <content>
-                            <Calendar/>
+                            <Calendar locale="{language:'en', country:'us'}"/>
                         </content>
                     </Border>
                 </FlowPane>
@@ -34,7 +34,7 @@
                     <Label text="Français (FR)" styles="{fontBold:true}"/>
                     <Border styles="{padding:0}">
                         <content>
-                            <Calendar locale="{language:'fr'}"/>
+                            <Calendar locale="{language:'fr', country:'fr'}"/>
                         </content>
                     </Border>
                 </FlowPane>
@@ -44,10 +44,10 @@
                     <Form>
                         <sections>
                             <Form.Section>
-                                <CalendarButton Form.name="English (US)"/>
+                                <CalendarButton Form.name="English (US)" locale="{language:'en', country:'us'}"/>
                                 <CalendarButton Form.name="Españole (US)" locale="{language:'es', country:'us'}"/>
                                 <CalendarButton Form.name="Españole (ES)" locale="{language:'es', country:'es'}"/>
-                                <CalendarButton Form.name="Français (FR)" locale="{language:'fr'}"/>
+                                <CalendarButton Form.name="Français (FR)" locale="{language:'fr', country:'fr'}"/>
                                 <CalendarButton Form.name="Français (CA)" locale="{language:'fr', country:'ca'}"/>
                             </Form.Section>
                         </sections>

Modified: incubator/pivot/trunk/tutorials/src/pivot/tutorials/drawing/RotateLine.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/src/pivot/tutorials/drawing/RotateLine.java?rev=771310&r1=771309&r2=771310&view=diff
==============================================================================
--- incubator/pivot/trunk/tutorials/src/pivot/tutorials/drawing/RotateLine.java (original)
+++ incubator/pivot/trunk/tutorials/src/pivot/tutorials/drawing/RotateLine.java Mon May  4 14:06:48 2009
@@ -11,6 +11,7 @@
 import pivot.wtk.media.drawing.Shape;
 import pivot.wtkx.Bind;
 import pivot.wtkx.Load;
+import pivot.wtkx.WTKXSerializer;
 
 public class RotateLine implements Application {
     @Load(name="rotate_line.wtkd")
@@ -23,6 +24,8 @@
 
     public void startup(Display display, Dictionary<String, String> properties)
         throws Exception{
+        WTKXSerializer.bind(this);
+
         ApplicationContext.scheduleRecurringCallback(new Runnable() {
             public void run() {
                 int angle = (int)rotation.getAngle();

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=771310&r1=771309&r2=771310&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/ApplicationContext.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/ApplicationContext.java Mon May  4 14:06:48 2009
@@ -41,11 +41,8 @@
 import java.awt.event.KeyEvent;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseWheelEvent;
-import java.io.IOException;
-import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
 import java.net.URI;
 import java.net.URL;
 import java.util.Iterator;
@@ -54,17 +51,12 @@
 
 import pivot.collections.Dictionary;
 import pivot.collections.HashMap;
-import pivot.serialization.SerializationException;
 import pivot.util.ImmutableIterator;
 import pivot.util.Version;
 import pivot.wtk.Component.DecoratorSequence;
 import pivot.wtk.Manifest;
 import pivot.wtk.RemoteManifest;
 import pivot.wtk.effects.Decorator;
-import pivot.wtkx.Bind;
-import pivot.wtkx.BindException;
-import pivot.wtkx.Load;
-import pivot.wtkx.WTKXSerializer;
 
 /**
  * Base class for application contexts.
@@ -1324,91 +1316,6 @@
         timer = null;
     }
 
-    protected static void bind(Application application)
-        throws IOException, BindException  {
-        assert(application != null);
-
-        // Maps resource field name to the serializer that loaded the resource
-        HashMap<String, WTKXSerializer> wtkxSerializers = new HashMap<String, WTKXSerializer>();
-
-        // Walk field lists and resolve WTKX annotations
-        Class<?> applicationClass = application.getClass();
-        Field[] fields = applicationClass.getDeclaredFields();
-        for (int i = 0; i < fields.length; i++) {
-            Field field = fields[i];
-            Load loadAnnotation = field.getAnnotation(Load.class);
-
-            if (loadAnnotation != null) {
-                // Create a serializer for the resource
-                String fieldName = field.getName();
-                assert(!wtkxSerializers.containsKey(fieldName));
-
-                WTKXSerializer wtkxSerializer = new WTKXSerializer();
-                wtkxSerializers.put(fieldName, wtkxSerializer);
-
-                // Load the resource
-                URL location = applicationClass.getResource(loadAnnotation.name());
-                Object resource;
-                try {
-                    resource = wtkxSerializer.readObject(location);
-                } catch(SerializationException exception) {
-                    throw new BindException(exception);
-                }
-
-                // Set the resource into the field
-                if ((field.getModifiers() & Modifier.PUBLIC) == 0) {
-                    field.setAccessible(true);
-                }
-
-                try {
-                    field.set(application, resource);
-                } catch(IllegalAccessException exception) {
-                    throw new BindException(exception);
-                }
-
-                if ((field.getModifiers() & Modifier.PUBLIC) == 0) {
-                    field.setAccessible(false);
-                }
-            }
-
-            Bind bindAnnotation = field.getAnnotation(Bind.class);
-            if (bindAnnotation != null) {
-                if (loadAnnotation != null) {
-                    throw new BindException("Cannot combine " + Load.class.getName()
-                        + " and " + Bind.class.getName() + " annotations.");
-                }
-
-                // Bind to the value loaded by the field's serializer
-                String fieldName = bindAnnotation.resource();
-                WTKXSerializer wtkxSerializer = wtkxSerializers.get(fieldName);
-                if (wtkxSerializer == null) {
-                    throw new BindException("\"" + fieldName + "\" is not a valid resource name.");
-                }
-
-                String id = bindAnnotation.id();
-                Object value = wtkxSerializer.getObjectByName(id);
-                if (value == null) {
-                    throw new BindException("\"" + id + "\" does not exist.");
-                }
-
-                // Set the value into the field
-                if ((field.getModifiers() & Modifier.PUBLIC) == 0) {
-                    field.setAccessible(true);
-                }
-
-                try {
-                    field.set(application, value);
-                } catch(IllegalAccessException exception) {
-                    throw new BindException(exception);
-                }
-
-                if ((field.getModifiers() & Modifier.PUBLIC) == 0) {
-                    field.setAccessible(false);
-                }
-            }
-        }
-    }
-
     private static DropAction getUserDropAction(InputEvent event) {
         DropAction userDropAction;
 

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/BrowserApplicationContext.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/BrowserApplicationContext.java?rev=771310&r1=771309&r2=771310&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/BrowserApplicationContext.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/BrowserApplicationContext.java Mon May  4 14:06:48 2009
@@ -172,7 +172,6 @@
 
                 if (application != null) {
                     try {
-                        bind(application);
                         application.startup(applicationContext.getDisplay(), propertyDictionary);
                     } catch(Exception exception) {
                         Alert.alert(MessageType.ERROR, exception.getMessage(),

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/DesktopApplicationContext.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/DesktopApplicationContext.java?rev=771310&r1=771309&r2=771310&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/DesktopApplicationContext.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/DesktopApplicationContext.java Mon May  4 14:06:48 2009
@@ -69,7 +69,6 @@
                     createTimer();
 
                     try {
-                        bind(application);
                         application.startup(applicationContext.getDisplay(),
                             new ImmutableMap<String, String>(properties));
                     } catch(Exception exception) {

Modified: incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java?rev=771310&r1=771309&r2=771310&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java Mon May  4 14:06:48 2009
@@ -159,7 +159,9 @@
         return readObject(location);
     }
 
-    public Object readObject(URL location) throws IOException, SerializationException {
+    @SuppressWarnings("unchecked")
+    public Object readObject(URL location) throws IOException,
+        SerializationException {
         if (location == null) {
             throw new IllegalArgumentException("location is null.");
         }
@@ -623,6 +625,9 @@
     /**
      * Retrieves a named object.
      *
+     * @param <T>
+     * The type of the object to return.
+     *
      * @param name
      * The name of the object, relative to this loader. The values's name is the
      * concatentation of its parent namespaces and its ID, separated by periods
@@ -631,7 +636,8 @@
      * @return The named object, or <tt>null</tt> if an object with the given
      * name does not exist.
      */
-    public Object getObjectByName(String name) {
+    @SuppressWarnings("unchecked")
+    public <T> T getObjectByName(String name) {
         if (name == null) {
             throw new IllegalArgumentException("name is null.");
         }
@@ -651,7 +657,7 @@
         	object = serializer.getObjectByID(namespacePath[i]);
         }
 
-        return object;
+        return (T)object;
     }
 
     private Object getObjectByID(String id) {
@@ -923,4 +929,86 @@
 
         return method;
     }
+
+    /**
+     * Applies WTKX binding annotations to an object.
+     *
+     * @param object
+     */
+    public static void bind(Object object)
+        throws IOException, BindException  {
+        assert(object != null);
+
+        // Maps resource field name to the serializer that loaded the resource
+        HashMap<String, WTKXSerializer> wtkxSerializers = new HashMap<String, WTKXSerializer>();
+
+        // Walk field lists and resolve WTKX annotations
+        Class<?> type = object.getClass();
+        Field[] fields = type.getDeclaredFields();
+        for (int i = 0; i < fields.length; i++) {
+            Field field = fields[i];
+            Load loadAnnotation = field.getAnnotation(Load.class);
+
+            if (loadAnnotation != null) {
+                // Create a serializer for the resource
+                String fieldName = field.getName();
+                assert(!wtkxSerializers.containsKey(fieldName));
+
+                WTKXSerializer wtkxSerializer = new WTKXSerializer();
+                wtkxSerializers.put(fieldName, wtkxSerializer);
+
+                // Load the resource
+                URL location = type.getResource(loadAnnotation.name());
+                Object resource;
+                try {
+                    resource = wtkxSerializer.readObject(location);
+                } catch(SerializationException exception) {
+                    throw new BindException(exception);
+                }
+
+                // Set the resource into the field
+                if (!field.isAccessible()) {
+                    field.setAccessible(true);
+                }
+
+                try {
+                    field.set(object, resource);
+                } catch(IllegalAccessException exception) {
+                    throw new BindException(exception);
+                }
+            }
+
+            Bind bindAnnotation = field.getAnnotation(Bind.class);
+            if (bindAnnotation != null) {
+                if (loadAnnotation != null) {
+                    throw new BindException("Cannot combine " + Load.class.getName()
+                        + " and " + Bind.class.getName() + " annotations.");
+                }
+
+                // Bind to the value loaded by the field's serializer
+                String fieldName = bindAnnotation.resource();
+                WTKXSerializer wtkxSerializer = wtkxSerializers.get(fieldName);
+                if (wtkxSerializer == null) {
+                    throw new BindException("\"" + fieldName + "\" is not a valid resource name.");
+                }
+
+                String id = bindAnnotation.id();
+                Object value = wtkxSerializer.getObjectByName(id);
+                if (value == null) {
+                    throw new BindException("\"" + id + "\" does not exist.");
+                }
+
+                // Set the value into the field
+                if (!field.isAccessible()) {
+                    field.setAccessible(true);
+                }
+
+                try {
+                    field.set(object, value);
+                } catch(IllegalAccessException exception) {
+                    throw new BindException(exception);
+                }
+            }
+        }
+    }
 }