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/01 22:16:34 UTC

svn commit: r770813 - in /incubator/pivot/trunk/wtk: src/pivot/wtk/ src/pivot/wtkx/ test/pivot/wtk/media/drawing/test/

Author: gbrown
Date: Fri May  1 20:16:33 2009
New Revision: 770813

URL: http://svn.apache.org/viewvc?rev=770813&view=rev
Log:
Add WTKX binding annotations.

Added:
    incubator/pivot/trunk/wtk/src/pivot/wtkx/Bind.java
    incubator/pivot/trunk/wtk/src/pivot/wtkx/BindException.java
    incubator/pivot/trunk/wtk/src/pivot/wtkx/Load.java
Modified:
    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/test/pivot/wtk/media/drawing/test/RotationTest.java

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=770813&r1=770812&r2=770813&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/ApplicationContext.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/ApplicationContext.java Fri May  1 20:16:33 2009
@@ -41,6 +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.net.URI;
@@ -51,12 +53,17 @@
 
 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.
@@ -1316,6 +1323,83 @@
         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
+                field.setAccessible(true);
+
+                try {
+                    field.set(application, resource);
+                } catch(IllegalAccessException exception) {
+                    throw new BindException(exception);
+                }
+
+                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
+                field.setAccessible(true);
+
+                try {
+                    field.set(application, value);
+                } catch(IllegalAccessException exception) {
+                    throw new BindException(exception);
+                }
+
+                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=770813&r1=770812&r2=770813&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/BrowserApplicationContext.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/BrowserApplicationContext.java Fri May  1 20:16:33 2009
@@ -172,6 +172,7 @@
 
                 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=770813&r1=770812&r2=770813&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/DesktopApplicationContext.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/DesktopApplicationContext.java Fri May  1 20:16:33 2009
@@ -69,6 +69,7 @@
                     createTimer();
 
                     try {
+                        bind(application);
                         application.startup(applicationContext.getDisplay(),
                             new ImmutableMap<String, String>(properties));
                     } catch(Exception exception) {

Added: incubator/pivot/trunk/wtk/src/pivot/wtkx/Bind.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtkx/Bind.java?rev=770813&view=auto
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtkx/Bind.java (added)
+++ incubator/pivot/trunk/wtk/src/pivot/wtkx/Bind.java Fri May  1 20:16:33 2009
@@ -0,0 +1,31 @@
+/*
+ * 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.wtkx;
+
+import java.lang.annotation.*;
+
+/**
+ * WTKX binding annotation.
+ *
+ * @author gbrown
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface Bind {
+    public String resource();
+    public String id();
+}

Added: incubator/pivot/trunk/wtk/src/pivot/wtkx/BindException.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtkx/BindException.java?rev=770813&view=auto
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtkx/BindException.java (added)
+++ incubator/pivot/trunk/wtk/src/pivot/wtkx/BindException.java Fri May  1 20:16:33 2009
@@ -0,0 +1,42 @@
+/*
+ * 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.wtkx;
+
+/**
+ * Thrown when an error is encountered during binding.
+ *
+ * @author gbrown
+ */
+public class BindException extends Exception {
+    private static final long serialVersionUID = 0;
+
+    public BindException() {
+        this(null, null);
+    }
+
+    public BindException(String message) {
+        this(message, null);
+    }
+
+    public BindException(Throwable cause) {
+        this(null, cause);
+    }
+
+    public BindException(String message, Throwable cause) {
+        super(message, cause);
+    }
+}

Added: incubator/pivot/trunk/wtk/src/pivot/wtkx/Load.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtkx/Load.java?rev=770813&view=auto
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtkx/Load.java (added)
+++ incubator/pivot/trunk/wtk/src/pivot/wtkx/Load.java Fri May  1 20:16:33 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.wtkx;
+
+import java.lang.annotation.*;
+
+/**
+ * WTKX binding annotation.
+ *
+ * @author gbrown
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface Load {
+    public String name();
+}

Modified: incubator/pivot/trunk/wtk/test/pivot/wtk/media/drawing/test/RotationTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/test/pivot/wtk/media/drawing/test/RotationTest.java?rev=770813&r1=770812&r2=770813&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/test/pivot/wtk/media/drawing/test/RotationTest.java (original)
+++ incubator/pivot/trunk/wtk/test/pivot/wtk/media/drawing/test/RotationTest.java Fri May  1 20:16:33 2009
@@ -7,19 +7,22 @@
 import pivot.wtk.Display;
 import pivot.wtk.ImageView;
 import pivot.wtk.Window;
-import pivot.wtk.media.Image;
+import pivot.wtk.media.Drawing;
 import pivot.wtk.media.drawing.Shape;
-import pivot.wtkx.WTKXSerializer;
+import pivot.wtkx.Bind;
+import pivot.wtkx.Load;
 
 public class RotationTest implements Application {
+    @Load(name="rotate.wtkd")
+    private Drawing drawing = null;
+
+    @Bind(resource="drawing", id="rotation")
+    private Shape.Rotate rotation = null;
+
     private Window window = null;
 
     public void startup(Display display, Dictionary<String, String> properties)
         throws Exception{
-        WTKXSerializer wtkxSerializer = new WTKXSerializer();
-        Image image = (Image)wtkxSerializer.readObject(getClass().getResource("rotate.wtkd"));
-
-        final Shape.Rotate rotation = (Shape.Rotate)wtkxSerializer.getObjectByName("rotation");
         ApplicationContext.scheduleRecurringCallback(new Runnable() {
             public void run() {
                 int angle = (int)rotation.getAngle();
@@ -28,7 +31,7 @@
             }
         }, 1000);
 
-        window = new Window(new ImageView(image));
+        window = new Window(new ImageView(drawing));
         window.setMaximized(true);
         window.open(display);
     }