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 2012/08/13 18:16:44 UTC

svn commit: r1372494 - in /pivot/trunk/tests/src/org/apache/pivot/tests: JavascriptConsoleTest.java JavascriptConsoleTest.json ResolveTest.json javascript_console_test.bxml javascript_console_test.js javascript_console_test.json resolve_test.bxml

Author: smartini
Date: Mon Aug 13 16:16:43 2012
New Revision: 1372494

URL: http://svn.apache.org/viewvc?rev=1372494&view=rev
Log:
merge fixes from 2.0.x

Added:
    pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.json
    pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.json
Modified:
    pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java
    pivot/trunk/tests/src/org/apache/pivot/tests/ResolveTest.json
    pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.bxml
    pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.js
    pivot/trunk/tests/src/org/apache/pivot/tests/resolve_test.bxml

Modified: pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java?rev=1372494&r1=1372493&r2=1372494&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java (original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java Mon Aug 13 16:16:43 2012
@@ -19,30 +19,45 @@ package org.apache.pivot.tests;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.Locale;
 
 import org.apache.pivot.beans.BXMLSerializer;
 import org.apache.pivot.collections.Map;
 import org.apache.pivot.serialization.SerializationException;
+import org.apache.pivot.util.Resources;
 import org.apache.pivot.wtk.Application;
 import org.apache.pivot.wtk.DesktopApplicationContext;
 import org.apache.pivot.wtk.Display;
 import org.apache.pivot.wtk.Window;
 
 public class JavascriptConsoleTest extends Application.Adapter {
+    public static final String LANGUAGE_KEY = "language";
+    public static final String MAIN_CLASS_NAME = JavascriptConsoleTest.class.getName();
+
     private Display display = null;
     private Window window = null;
 
+    private Locale locale = null;
+    private Resources resources = null;
+
     @Override
     public void startup(Display displayArgument, Map<String, String> properties) throws Exception {
         logObject("startup: start");
 
         this.display = displayArgument;
 
+        // get the locale from startup properties, or use the default
+        String language = properties.get(LANGUAGE_KEY);
+        locale = (language == null) ? Locale.getDefault() : new Locale(language);
+        logObject("running with the locale " + locale);
+
         BXMLSerializer bxmlSerializer = new BXMLSerializer();
 
         // add a reference to the application itself in bxml namespace, to be used by JS inside bxml files
         bxmlSerializer.getNamespace().put("application", this);
         logObject("put a reference to application in serializer namespace");
+        bxmlSerializer.getNamespace().put("mainClassName", MAIN_CLASS_NAME);
+        logObject("put a reference to main class name in serializer namespace \"" + MAIN_CLASS_NAME + "\"");
 
         window = loadWindow("javascript_console_test.bxml", bxmlSerializer);
         initializeFields(bxmlSerializer);
@@ -62,12 +77,32 @@ public class JavascriptConsoleTest exten
 
     private void initializeFields(BXMLSerializer serializer) {
         logObject("initializeFields: start");
-
         logObject("got BXMLSerializer instance = " + serializer);
 
+        buildResources(MAIN_CLASS_NAME);
+
         logObject("initializeFields: end");
     }
 
+    private void buildResources(String className) {
+        if (className == null || className.length() < 1) {
+            className = MAIN_CLASS_NAME;  // set a useful default
+        }
+
+        try {
+            // load some resources here, just to show its usage from JS files,
+            // but only if not already loaded ...
+            if (resources == null) {
+                resources = new Resources(MAIN_CLASS_NAME, locale);
+                logObject("buildResources, load resources from \"" + className + "\", with locale " + locale);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+    }
+
+
     /**
      * Load (and returns) a Window, given its file name
      * <p>
@@ -101,7 +136,9 @@ public class JavascriptConsoleTest exten
             bxmlSerializer = new BXMLSerializer();
         }
 
-        return (Window)bxmlSerializer.readObject(JavascriptConsoleTest.class, fileName);
+        // return (Window)bxmlSerializer.readObject(JavascriptConsoleTest.class, fileName); // ok
+        // better, to allow usage of resources (without having to call setLocation or setResources in the serializer) ...
+        return (Window)bxmlSerializer.readObject(JavascriptConsoleTest.class, fileName, true);
     }
 
     /**
@@ -112,12 +149,12 @@ public class JavascriptConsoleTest exten
      * <p>
      * Note that all Exceptions are catched inside this method, to not expose them to JS code.
      *
-     * @param url the URL of the bxml file to load
+     * @param urlString the URL of the bxml file to load, as a String
      * @param bxmlSerializer the serializer to use, or if null a new one will be created
      * @return the Window instance
      */
-    public Window loadWindowFromURL(String url, BXMLSerializer bxmlSerializer) {
-        logObject("loadWindow from \"" + url + "\", with the serializer " + bxmlSerializer);
+    public Window loadWindowFromURL(String urlString, BXMLSerializer bxmlSerializer) {
+        logObject("loadWindow from \"" + urlString + "\", with the serializer " + bxmlSerializer);
 
         if (bxmlSerializer == null) {
             bxmlSerializer = new BXMLSerializer();
@@ -125,7 +162,12 @@ public class JavascriptConsoleTest exten
 
         Window loadedWindow = null;
         try {
-            loadedWindow = (Window)bxmlSerializer.readObject(new URL(url));
+            URL url = new URL(urlString);
+
+            // force the location, so it will be possible to decode resources like labels ...
+            bxmlSerializer.setLocation(url);
+
+            loadedWindow = (Window)bxmlSerializer.readObject(url);
         } catch (MalformedURLException e) {
             e.printStackTrace();
         } catch (IOException e) {
@@ -139,6 +181,32 @@ public class JavascriptConsoleTest exten
 
 
     /**
+     * Return the value for the given label,
+     * from the resource file loaded at application startup.
+     *
+     * @param name the label name
+     * @return the value or the label, or empty string if not found
+     */
+    public String getLabel(String name) {
+        String label = "";
+        if (name == null || name.length() < 1) {
+            throw new IllegalArgumentException("name must be a valid string");
+        }
+
+        // note that if called from bxml files, resources could be not already loaded,
+        // so try to force its load with a default value ...
+        if (resources == null) {
+            buildResources(null);
+        }
+
+        label = (String) resources.get(name);
+        logObject("search label with name \"" + name + "\", find value \"" + label + "\"");
+
+        return ((label == null) ? "": label);
+    }
+
+
+    /**
      * Sample utility method to log a formatted dump of the given object to System.out .
      * <p>
      * Note that it has been set public, static, and accepting Object (and not String as usual),

Added: pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.json
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.json?rev=1372494&view=auto
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.json (added)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.json Mon Aug 13 16:16:43 2012
@@ -0,0 +1,35 @@
+/*
+ * 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.
+ */
+{
+    title: "JavaScript Console Test, from JavascriptConsoleTest.json",
+    labelText: "Hello, from JavascriptConsoleTest.json",
+    labelInfo: "JavaScript (interpreted by the JVM) Console:",
+    labelTemplates: "Templates:",
+
+    valuesTemplates_0: " - Choose a template - ",  // future use
+    valuesTemplates_1: "Open Frame Local",  // future use
+    valuesTemplates_2: "Open Frame Remote (from Pivot Web Site)",  // future use
+    valuesTemplates_all: "[' - Choose a template - ', 'Open Frame Local', 'Open Frame Remote (from Pivot Web Site)']",  // future use, as alternative for singles valuesTemplates_* values ...
+
+    labelRun: "Run",
+    labelClear: "Clear",
+    labelJSSource: "JS Source:",
+    labelJSOutput: "JS Output:",
+    labelStatus: "Status:",
+
+    last: ""
+}

Modified: pivot/trunk/tests/src/org/apache/pivot/tests/ResolveTest.json
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/ResolveTest.json?rev=1372494&r1=1372493&r2=1372494&view=diff
==============================================================================
Binary files - no diff available.

Modified: pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.bxml?rev=1372494&r1=1372493&r2=1372494&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.bxml (original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.bxml Mon Aug 13 16:16:43 2012
@@ -17,7 +17,7 @@ limitations under the License.
 -->
 
 <Window bxml:id="window"
-    title="JavaScript Console Test" maximized="true"
+    title="%title" maximized="true"
     xmlns:bxml="http://pivot.apache.org/bxml"
     xmlns="org.apache.pivot.wtk"
 >
@@ -35,7 +35,7 @@ limitations under the License.
         // "file:///D:/work/pivot/site/trunk/deploy/assets-for-tests" + "/";  // local test
         // "http://pivot.apache.org/assets-for-tests" + "/";  // production url
         "http://pivot.apache.org/assets-for-tests" + "/";
-    var frameName = 
+    var frameName =
         // "frame.bxml";    // the first level bxml
         // "palette.bxml";  // the second level bxml (loaded usually be frame.bxml), just to test here directly
         "frame.bxml";
@@ -43,6 +43,10 @@ limitations under the License.
     // note that bxml files must have the MIME Type "application/bxml" (as requested by BXMLSerializer),
     // or (BXMLSerializer) will try to get it from its file extension ...
 
+    // assign a label value to a JS variable
+    // var valueFromLabel = "%labelText";  // note that this doesn't work as expected, must be handled from the bxmlSerializer ...
+    // log("valueFromLabel (direct access) = \"" + valueFromLabel + "\"");
+
     log("inline script 1 - end");
     ]]>
     </bxml:script>
@@ -104,6 +108,12 @@ limitations under the License.
     // note that this call is done in the usual (non-static) way, and works the same even from here
     application.logObject(testRemoteFrame);
 
+    // decode and assign a label value to a JS variable (from the usual json resource file just used)
+    // var valueFromLabel = bxmlSerializer.getNamespace().get("labelText");  // get the label value from the bxmlSerializer, but doesn't work here (in an all-JS block)...
+    // working solution: get from resources, and to simplify things here use a Java helper method, but should be possible to do all from here ...
+    var valueFromLabel = application.getLabel("labelText");  // get the label value from resources
+    log("valueFromLabel (access via dedicated Java Helper) = \"" + valueFromLabel + "\"");
+
 
     // test 5, TODO
     // comment test 4, and copy its source inside the switch statements below (if possible), to make it really dynamic ...
@@ -127,11 +137,11 @@ limitations under the License.
 
         <TablePane.Row height="-1">
             <BoxPane orientation="horizontal" TablePane.columnSpan="3">
-                <Label styles="{horizontalAlignment:'center', verticalAlignment:'center'}" text="JavaScript (interpreted by the JVM) Console:"
+                <Label styles="{horizontalAlignment:'center', verticalAlignment:'center'}" text="%labelInfo"
                 />
                 <Label text=" " />
 
-                <Label text="Templates:"/>
+                <Label text="%labelTemplates"/>
                 <ListButton bxml:id="templateButton"
                     listData="[' - Choose a template - ', 'Open Frame Local', 'Open Frame Remote (from Pivot Web Site)']"
                     selectedIndex="-1"
@@ -161,14 +171,14 @@ limitations under the License.
                                     clearConsole();
                                     break;
                             }
-                            textArea.text = templateText;
+                            textJSSource.text = templateText;
                         }
                     </listButtonSelectionListeners>
                 </ListButton>
                 <Label text=" " />
 
-                <PushButton bxml:id="runButton"   buttonData="Run" ButtonPressListener.buttonPressed="runConsole()"/>
-                <PushButton bxml:id="clearButton" buttonData="Clear">
+                <PushButton bxml:id="runButton"   buttonData="%labelRun" ButtonPressListener.buttonPressed="runConsole()" enabled="false" />
+                <PushButton bxml:id="clearButton" buttonData="%labelClear">
                     <buttonPressListeners>
                     function buttonPressed(button) {
                         clearConsole();
@@ -178,12 +188,12 @@ limitations under the License.
             </BoxPane>
         </TablePane.Row>
         <TablePane.Row height="1*">
-            <Label styles="{horizontalAlignment:'right', verticalAlignment:'center'}" text="JS Source:"/>
+            <Label styles="{horizontalAlignment:'right', verticalAlignment:'center'}" text="%labelJSSource"/>
             <Border>
                 <ScrollPane horizontalScrollBarPolicy="fill" verticalScrollBarPolicy="fill_to_capacity"
                     preferredHeight="200"
                 >
-                    <TextArea bxml:id="textArea"
+                    <TextArea bxml:id="textJSSource"
                         styles="{wrapText:false}"
                         editable="true"
                         text=""
@@ -202,8 +212,23 @@ limitations under the License.
                 </ScrollPane>
             </Border>
         </TablePane.Row>
+        <TablePane.Row height="1*">
+            <Label styles="{horizontalAlignment:'right', verticalAlignment:'center'}" text="%labelJSOutput"/>
+            <Border>
+                <ScrollPane horizontalScrollBarPolicy="fill" verticalScrollBarPolicy="fill_to_capacity"
+                    preferredHeight="100"
+                >
+                    <TextArea bxml:id="textJSOutput"
+                        styles="{wrapText:false}"
+                        editable="false"
+                        text=""
+                    >
+                    </TextArea>
+                </ScrollPane>
+            </Border>
+        </TablePane.Row>
         <TablePane.Row height="-1">
-            <Label styles="{horizontalAlignment:'right', verticalAlignment:'center'}" text="Status:"/>
+            <Label styles="{horizontalAlignment:'right', verticalAlignment:'center'}" text="%labelStatus"/>
             <Label bxml:id="textStatus" styles="{horizontalAlignment:'left', verticalAlignment:'center'}" text=""/>
         </TablePane.Row>
     </TablePane>

Modified: pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.js
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.js?rev=1372494&r1=1372493&r2=1372494&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.js (original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.js Mon Aug 13 16:16:43 2012
@@ -40,7 +40,8 @@ function clearStatus() {
 
 function clearConsole() {
 	templateButton.selectedIndex = 0;
-	textArea.text = "";
+	textJSSource.text = "";
+	textJSOutput.text = "";
 	// runButton.enabled = false;  // ok
 	runButton.setEnabled(false);   // explicit usage of the setter
 	log("Console cleared");
@@ -49,7 +50,7 @@ function clearConsole() {
 
 
 function runConsole() {
-	var text = textArea.text;
+	var text = textJSSource.text;
 	log("Console Text length = " + text.length());
 	if (text.length() < 1)
 		return ;
@@ -57,6 +58,7 @@ function runConsole() {
 	var msg = "Run JS Code in Console";
 	log(msg);
 	updateStatus(msg + " ...");
+	textJSOutput.text = "";
 
 // TODO: continue here ...
 	;

Added: pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.json
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.json?rev=1372494&view=auto
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.json (added)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.json Mon Aug 13 16:16:43 2012
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+{
+    title: "JavaScript Console Test, from javascript_console_test.json",
+    labelText: "Hello, from javascript_console_test.json",
+    labelInfo: "JavaScript (interpreted by the JVM) Console:",
+
+    last: ""
+}

Modified: pivot/trunk/tests/src/org/apache/pivot/tests/resolve_test.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/resolve_test.bxml?rev=1372494&r1=1372493&r2=1372494&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/resolve_test.bxml (original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/resolve_test.bxml Mon Aug 13 16:16:43 2012
@@ -16,11 +16,12 @@ See the License for the specific languag
 limitations under the License.
 -->
 
-<Window title="Resolve Test" maximized="true"
+<Window title="%title" maximized="true"
     xmlns:bxml="http://pivot.apache.org/bxml"
     xmlns="org.apache.pivot.wtk">
     <bxml:script>
-    var foo = "ABC";
+    var foo = "Label from the JS variable 'foo'";
+    var bar = "Label from the JS variable 'bar'";
     </bxml:script>
 
     <BoxPane orientation="vertical">