Author: rwhitcomb
Date: Mon Mar  5 23:10:49 2018
New Revision: 1825947

URL: http://svn.apache.org/viewvc?rev=1825947&view=rev
Log:
Allow test / demo / example applications that just load a BXML file
to easily subclass ScriptApplication by providing an override there
for the "--src=" command line argument, such that the test application
can be run on the desktop with just a few lines of code.

Modify SplitPaneTest to demonstrate the potential.

Cleanup a little messy code in DesktopApplicationContext along the way.


Modified:
    pivot/trunk/tests/src/org/apache/pivot/tests/SplitPaneTest.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/DesktopApplicationContext.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/ScriptApplication.java

Modified: pivot/trunk/tests/src/org/apache/pivot/tests/SplitPaneTest.java
URL: 
http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/SplitPaneTest.java?rev=1825947&r1=1825946&r2=1825947&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/SplitPaneTest.java (original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/SplitPaneTest.java Mon Mar  5 
23:10:49 2018
@@ -16,38 +16,15 @@
  */
 package org.apache.pivot.tests;
 
-import org.apache.pivot.beans.BXMLSerializer;
-import org.apache.pivot.collections.Map;
-import org.apache.pivot.wtk.Application;
-import org.apache.pivot.wtk.Component;
 import org.apache.pivot.wtk.DesktopApplicationContext;
-import org.apache.pivot.wtk.Display;
-import org.apache.pivot.wtk.Window;
+import org.apache.pivot.wtk.ScriptApplication;
 
-public class SplitPaneTest implements Application {
-    private Window window = null;
-
-    @Override
-    public void startup(Display display, Map<String, String> properties) 
throws Exception {
-        BXMLSerializer bxmlSerializer = new BXMLSerializer();
-        window = new Window((Component) 
bxmlSerializer.readObject(getClass().getResource(
-            "splitpane_test.bxml")));
-
-        window.setTitle("SplitPane Test");
-        window.setMaximized(true);
-        window.open(display);
-    }
-
-    @Override
-    public boolean shutdown(boolean optional) {
-        if (window != null) {
-            window.close();
-        }
-
-        return false;
+public final class SplitPaneTest extends ScriptApplication {
+    private SplitPaneTest() {
+        super("splitpane_test.bxml");
     }
 
-    public static void main(String[] args) {
-        DesktopApplicationContext.main(SplitPaneTest.class, args);
+    public static void main(final String[] args) {
+        DesktopApplicationContext.main(new SplitPaneTest(), args);
     }
 }

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=1825947&r1=1825946&r2=1825947&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/DesktopApplicationContext.java 
(original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/DesktopApplicationContext.java Mon 
Mar  5 23:10:49 2018
@@ -644,10 +644,10 @@ public final class DesktopApplicationCon
         // Load the application
         try {
             Class<?> applicationClass = Class.forName(applicationClassName);
-            if (useApplicationInstance == false) {
-                application = (Application) applicationClass.newInstance();
+            if (useApplicationInstance) {
+                // application has already been set, before calling this method
             } else {
-                // application has already been set, before call this method
+                application = (Application) applicationClass.newInstance();
             }
         } catch (ClassNotFoundException exception) {
             exception.printStackTrace();

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/ScriptApplication.java
URL: 
http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/ScriptApplication.java?rev=1825947&r1=1825946&r2=1825947&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/ScriptApplication.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/ScriptApplication.java Mon Mar  5 
23:10:49 2018
@@ -39,12 +39,17 @@ public class ScriptApplication implement
     public static final String RESOURCES_KEY = "resources";
     public static final String STYLESHEET_KEY = "stylesheet";
 
+    private String sourceDefault = null;
+
     @Override
     public void startup(Display display, Map<String, String> properties) 
throws Exception {
         // Get the location of the source file
         String src = properties.get(SRC_KEY);
         if (src == null) {
-            throw new IllegalArgumentException(SRC_KEY + " argument is 
required.");
+            if (sourceDefault == null) {
+                throw new IllegalArgumentException(SRC_KEY + " argument is 
required.");
+            }
+            src = sourceDefault;
         }
 
         ClassLoader classLoader = 
Thread.currentThread().getContextClassLoader();
@@ -110,7 +115,7 @@ public class ScriptApplication implement
     }
 
     @Override
-    public boolean shutdown(boolean optional) {
+    public boolean shutdown(final boolean optional) {
         if (this.window != null) {
             this.window.close();
         }
@@ -118,17 +123,22 @@ public class ScriptApplication implement
         return false;
     }
 
-    @Override
-    public void resume() {
-        // empty block
-    }
-
-    @Override
-    public void suspend() {
-        // empty block
+    /**
+     * Constructor for subclasses to preset the "SRC" argument for completely
+     * automatic application startup (mainly used for tests, demos, etc.).
+     *
+     * @param srcArgument The name of a source file that will be used if
+     * the <tt>--src=...</tt> argument is missing from the command line.
+     */
+    public ScriptApplication(String srcArgument) {
+        if (srcArgument.startsWith("@")) {
+            this.sourceDefault = srcArgument;
+        } else {
+            this.sourceDefault = 
getClass().getPackage().getName().replace('.', '/') + "/" + srcArgument;
+        }
     }
 
-    public static void main(String[] args) {
+    public static void main(final String[] args) {
         DesktopApplicationContext.main(ScriptApplication.class, args);
     }
 


Reply via email to