You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by sm...@apache.org on 2013/10/28 00:56:19 UTC

svn commit: r1536227 - in /pivot/trunk: tests/src/org/apache/pivot/tests/ wtk/src/org/apache/pivot/wtk/

Author: smartini
Date: Sun Oct 27 23:56:18 2013
New Revision: 1536227

URL: http://svn.apache.org/r1536227
Log:
PIVOT-926, initial version and related minimal test application. 
Note that all changes are mostly additions, and changes in DesktopApplicationContext are triggered only when used the new (additional) main method.

Added:
    pivot/trunk/tests/src/org/apache/pivot/tests/ApplicationWithPropertiesTest.java
    pivot/trunk/tests/src/org/apache/pivot/tests/application_with_properties_test.bxml
    pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationWithProperties.java
Modified:
    pivot/trunk/wtk/src/org/apache/pivot/wtk/DesktopApplicationContext.java

Added: pivot/trunk/tests/src/org/apache/pivot/tests/ApplicationWithPropertiesTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/ApplicationWithPropertiesTest.java?rev=1536227&view=auto
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/ApplicationWithPropertiesTest.java (added)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/ApplicationWithPropertiesTest.java Sun Oct 27 23:56:18 2013
@@ -0,0 +1,104 @@
+/*
+ * 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 org.apache.pivot.tests;
+
+import org.apache.pivot.beans.BXMLSerializer;
+import org.apache.pivot.collections.Map;
+import org.apache.pivot.wtk.ApplicationWithProperties;
+import org.apache.pivot.wtk.DesktopApplicationContext;
+import org.apache.pivot.wtk.Display;
+import org.apache.pivot.wtk.Label;
+import org.apache.pivot.wtk.Window;
+
+public class ApplicationWithPropertiesTest extends ApplicationWithProperties.Adapter {
+    public static final String SAMPLE_PROP_KEY = "sample_object_from_external_env";
+    public static final String SAMPLE_CLASSLOADER_PROP_KEY = "sample_classloader_from_external_env";
+
+    private Window window = null;
+    private Label sampleLabel = null;
+
+    @Override
+    public void startup(final Display display, final Map<String, String> properties)
+        throws Exception {
+        System.out.println("application startup(...)");
+
+        // initializeProperties(properties);
+
+        BXMLSerializer bxmlSerializer = new BXMLSerializer();
+        window = (Window) bxmlSerializer.readObject(ApplicationWithPropertiesTest.class,
+            "application_with_properties_test.bxml");
+        initializeFields(bxmlSerializer);
+        window.open(display);
+    }
+
+    @Override
+    public boolean shutdown(boolean optional) {
+        System.out.println("application shutdown(" + optional + ")");
+
+        if (window != null) {
+            window.close();
+        }
+
+        return false;
+    }
+
+    private void initializeFields(BXMLSerializer serializer) {
+        System.out.println("initializeFields: start");
+
+        sampleLabel = (Label) serializer.getNamespace().get("label");
+        System.out.println("label, original text (from bxml) = \"" + sampleLabel.getText() + "\"");
+
+        updateLabel((String) this.getProperties().get(SAMPLE_PROP_KEY));
+        setClassLoaderFromExternalProperty((ClassLoader) this.getProperties().get(SAMPLE_CLASSLOADER_PROP_KEY));
+
+        System.out.println("initializeFields: end");
+    }
+
+    private void updateLabel(String text) {
+        System.out.println("updateLabel with text = \"" + text + "\"");
+        sampleLabel.setText(text);
+    }
+
+    private void setClassLoaderFromExternalProperty(ClassLoader classLoader) {
+        System.out.println("setClassLoaderFromExternalProperty with value = " + classLoader);
+        // this.classLoader = classLoader;
+    }
+
+    public static void main(String[] args) {
+        System.out.println("main(...)");
+
+        String sample = "Hello from external environment.";
+
+        // instead of the usual call, here I create an instance of my
+        // application, and then set some properties (from this "external"
+        // environment) in it. Last, I set that instance in usual launcher, but
+        // using a new execution method.
+        ApplicationWithProperties application = new ApplicationWithPropertiesTest();
+        System.out.println("application instance = " + application);
+
+        application.getProperties().put(SAMPLE_PROP_KEY, sample);
+        System.out.println("application sample external property \"" + SAMPLE_PROP_KEY + "\" = \""
+            + application.getProperties().get(SAMPLE_PROP_KEY) + "\"");
+        final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+        application.getProperties().put(SAMPLE_CLASSLOADER_PROP_KEY, classLoader);
+        System.out.println("application sample external classloader property \"" + SAMPLE_CLASSLOADER_PROP_KEY + "\" = \""
+                + application.getProperties().get(SAMPLE_CLASSLOADER_PROP_KEY) + "\"");
+
+        System.out.println("Executing application ...");
+        DesktopApplicationContext.main(application, args);
+    }
+}

Added: pivot/trunk/tests/src/org/apache/pivot/tests/application_with_properties_test.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/application_with_properties_test.bxml?rev=1536227&view=auto
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/application_with_properties_test.bxml (added)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/application_with_properties_test.bxml Sun Oct 27 23:56:18 2013
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+-->
+
+<Window title="Application instance and properties from External, Test" maximized="true"
+    xmlns:bxml="http://pivot.apache.org/bxml"
+    xmlns="org.apache.pivot.wtk">
+    <Label bxml:id="label" text="Sample label."
+        styles="{wrapText:true, padding:8, horizontalAlignment:'center', verticalAlignment:'top'}"/>
+</Window>

Added: pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationWithProperties.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationWithProperties.java?rev=1536227&view=auto
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationWithProperties.java (added)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationWithProperties.java Sun Oct 27 23:56:18 2013
@@ -0,0 +1,74 @@
+/*
+ * 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 org.apache.pivot.wtk;
+
+import org.apache.pivot.collections.HashMap;
+import org.apache.pivot.collections.Map;
+
+/**
+ * Represents the entry point for a WTK application that has a container for
+ * application-scope properties. In general, all methods derived from
+ * Application should not be invoked directly by the application.
+ *
+ * @see Application
+ * @see Application.Adapter
+ */
+public interface ApplicationWithProperties extends Application {
+    /**
+     * ApplicationWithProperties adapter.
+     */
+    public static class Adapter extends Application.Adapter implements ApplicationWithProperties {
+        // TODO: verify if change with something (but general) like
+        // ResourceCacheDictionary, and then synchronize its methods ...
+        private Map<String, Object> properties = new HashMap<>();
+
+        @Override
+        public Map<String, Object> getProperties() {
+            return properties;
+        }
+    }
+
+    /**
+     * Optional interface that allows an application to present information
+     * about itself.
+     */
+    public interface AboutHandler extends Application.AboutHandler {
+    }
+
+    /**
+     * Optional interface that allows an application to handle unprocessed key
+     * events (keystrokes that are processed when no component has the input
+     * focus).
+     */
+    public interface UnprocessedKeyHandler extends Application.UnprocessedKeyHandler {
+    }
+
+    /**
+     * Optional interface that allows an application to handle uncaught
+     * exceptions thrown during a user input event.
+     */
+    public interface UncaughtExceptionHandler extends Application.UncaughtExceptionHandler {
+    }
+
+    /**
+     * Get the property container at application-level scope.
+     *
+     * @return the container, with all values inside (if any).
+     */
+    public Map<String, Object> getProperties();
+
+}

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/DesktopApplicationContext.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/DesktopApplicationContext.java?rev=1536227&r1=1536226&r2=1536227&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/DesktopApplicationContext.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/DesktopApplicationContext.java Sun Oct 27 23:56:18 2013
@@ -384,6 +384,7 @@ public final class DesktopApplicationCon
     public static final String FULL_SCREEN_ARGUMENT = "fullScreen";
     public static final String PRESERVE_SPLASH_SCREEN_ARGUMENT = "preserveSplashScreen";
     public static final String ORIGIN_ARGUMENT = "origin";
+    public static final String USE_APPLICATION_INSTANCE_ARGUMENT = "useApplicationInstance";
 
     private static final String INVALID_PROPERTY_FORMAT_MESSAGE = "\"%s\" is not a valid startup "
         + "property (expected format is \"--name=value\").";
@@ -455,7 +456,7 @@ public final class DesktopApplicationCon
     /**
      * Primary application entry point.
      *
-     * @param args
+     * @param args application arguments
      */
     public static void main(String[] args) {
         // Get the application class name
@@ -479,6 +480,7 @@ public final class DesktopApplicationCon
         boolean undecorated = false;
         boolean fullScreen = false;
         boolean preserveSplashScreen = false;
+        boolean useApplicationInstance = false;
 
         try {
             Preferences preferences = Preferences.userNodeForPackage(DesktopApplicationContext.class);
@@ -539,6 +541,8 @@ public final class DesktopApplicationCon
                             preserveSplashScreen = Boolean.parseBoolean(value);
                         } else if (key.equals(ORIGIN_ARGUMENT)) {
                             origin = new URL(value);
+                        } else if (key.equals(USE_APPLICATION_INSTANCE_ARGUMENT)) {
+                            useApplicationInstance = Boolean.parseBoolean(value);
                         } else {
                             properties.put(key, value);
                         }
@@ -601,7 +605,11 @@ public final class DesktopApplicationCon
         // Load the application
         try {
             Class<?> applicationClass = Class.forName(applicationClassName);
-            application = (Application) applicationClass.newInstance();
+            if (useApplicationInstance == false) {
+                application = (Application) applicationClass.newInstance();
+            } else {
+                // application has already been set, before call this method
+            }
         } catch (ClassNotFoundException exception) {
             exception.printStackTrace();
         } catch (InstantiationException exception) {
@@ -847,8 +855,8 @@ public final class DesktopApplicationCon
      * Application { public static void main(String[] args) throws Exception {
      * DesktopApplicationContext.main(MyApp.class, args); } } </code>
      *
-     * @param applicationClass
-     * @param applicationArgs
+     * @param applicationClass the class of Application entry point
+     * @param applicationArgs application arguments
      */
     public static final void main(Class<? extends Application> applicationClass,
         String[] applicationArgs) {
@@ -858,4 +866,24 @@ public final class DesktopApplicationCon
         main(args);
     }
 
+    /**
+     * Utility method to make it easier to define <tt>main()</tt> entry-points
+     * into applications.<br/> This is useful if application instance has
+     * already been created, for example from a scripting environment and I set
+     * some external properties in the application for later reuse, so I must
+     * use that instance.<br/> But it's important to NOT call usual methods of
+     * application lifecycle before passing it here, to avoid side effects.
+     *
+     * @param applicationInstance an instance of Application entry point
+     * @param applicationArgs application arguments
+     */
+    public static final void main(Application applicationInstance, String[] applicationArgs) {
+        String[] args = new String[applicationArgs.length + 2];
+        System.arraycopy(applicationArgs, 0, args, 1, applicationArgs.length);
+        args[0] = applicationInstance.getClass().getName();
+        args[applicationArgs.length + 1] = "--" + USE_APPLICATION_INSTANCE_ARGUMENT + "=" + "true";
+        application = applicationInstance;
+        main(args);
+    }
+
 }