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/08 16:42:24 UTC
svn commit: r1370791 - in /pivot/trunk: core/src/org/apache/pivot/beans/
tests/src/org/apache/pivot/tests/
Author: smartini
Date: Wed Aug 8 14:42:23 2012
New Revision: 1370791
URL: http://svn.apache.org/viewvc?rev=1370791&view=rev
Log:
merge fixes from 2.0.x
Modified:
pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java
pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java
pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.bxml
pivot/trunk/tests/src/org/apache/pivot/tests/javascript_console_test.js
Modified: pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java?rev=1370791&r1=1370790&r2=1370791&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java Wed Aug 8 14:42:23 2012
@@ -823,6 +823,16 @@ public class BXMLSerializer implements S
}
String src = element.properties.get(INCLUDE_SRC_ATTRIBUTE);
+ if (src.charAt(0) == OBJECT_REFERENCE_PREFIX) {
+ src = src.substring(1);
+ if (src.length() > 0) {
+ if (!JSON.containsKey(namespace, src)) {
+ throw new SerializationException("Value \"" + src + "\" is not defined.");
+ }
+ String variableValue = JSON.get(namespace, src);
+ src = variableValue;
+ }
+ }
Resources resourcesLocal = this.resources;
if (element.properties.containsKey(INCLUDE_RESOURCES_ATTRIBUTE)) {
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=1370791&r1=1370790&r2=1370791&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java (original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java Wed Aug 8 14:42:23 2012
@@ -17,6 +17,8 @@
package org.apache.pivot.tests;
import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
import org.apache.pivot.beans.BXMLSerializer;
import org.apache.pivot.collections.Map;
@@ -32,7 +34,7 @@ public class JavascriptConsoleTest exten
@Override
public void startup(Display displayArgument, Map<String, String> properties) throws Exception {
- System.out.println("startup: start");
+ logObject("startup: start");
this.display = displayArgument;
@@ -40,13 +42,13 @@ public class JavascriptConsoleTest exten
// add a reference to the application itself in bxml namespace, to be used by JS inside bxml files
bxmlSerializer.getNamespace().put("application", this);
- System.out.println("put a reference to application in serializer namespace");
+ logObject("put a reference to application in serializer namespace");
window = loadWindow("javascript_console_test.bxml", bxmlSerializer);
initializeFields(bxmlSerializer);
window.open(display);
- System.out.println("startup: end");
+ logObject("startup: end");
}
@Override
@@ -59,21 +61,87 @@ public class JavascriptConsoleTest exten
}
private void initializeFields(BXMLSerializer serializer) {
- System.out.println("initializeFields: start");
+ logObject("initializeFields: start");
- System.out.println("got BXMLSerializer instance = " + serializer);
+ logObject("got BXMLSerializer instance = " + serializer);
- System.out.println("initializeFields: end");
+ logObject("initializeFields: end");
}
- protected Window loadWindow(String fileName, BXMLSerializer bxmlSerializer)
+ /**
+ * Load (and returns) a Window, given its file name and serializer to use
+ *
+ * @param fileName the file name for the bxml file to load
+ * @param bxmlSerializer the serializer to use, or if null a new one will be created
+ * @return the Window instance
+ * @throws SerializationException in case of error
+ * @throws IOException in case of error
+ */
+ private Window loadWindow(String fileName, BXMLSerializer bxmlSerializer)
throws SerializationException, IOException {
+ logObject("loadWindow from \"" + fileName + "\", with the serializer " + bxmlSerializer);
+
if (bxmlSerializer == null) {
bxmlSerializer = new BXMLSerializer();
}
+
return (Window)bxmlSerializer.readObject(JavascriptConsoleTest.class, fileName);
}
+ /**
+ * Load (and returns) a Window, given its URL and serializer to use
+ * <p>
+ * Note that if public this method could be called even from JS in a bxml file
+ * (but a reference to the current application has to be put in serializer namespace).
+ * <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 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);
+
+ if (bxmlSerializer == null) {
+ bxmlSerializer = new BXMLSerializer();
+ }
+
+ Window loadedWindow = null;
+ try {
+ loadedWindow = (Window)bxmlSerializer.readObject(new URL(url));
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (SerializationException e) {
+ e.printStackTrace();
+ }
+
+ return loadedWindow;
+ }
+
+
+ /**
+ * 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),
+ * even to make some tests on it from JS code.
+ *
+ * @param msg the object (or message) to log
+ */
+ public static final void logObject(Object obj) {
+ if (obj != null) {
+ System.out.println(new java.util.Date() + ", log: { class: \"" + obj.getClass().getName() + "\", msg:\"" + obj + "\" }");
+ }
+ }
+
+
+ /**
+ * Application entry point, when run as a Standard (Desktop) Java Application.
+ *
+ * @param args command line arguments
+ */
public static void main(String[] args) {
DesktopApplicationContext.main(JavascriptConsoleTest.class, args);
}
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=1370791&r1=1370790&r2=1370791&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 Wed Aug 8 14:42:23 2012
@@ -23,12 +23,81 @@ limitations under the License.
>
<bxml:script src="javascript_console_test.js"/>
+ <bxml:script>
+ <![CDATA[
+ // note that this script could be moved in an external js file, like that already included in this file ...
+ log("inline script 1 - start");
+
+ // JS variables for (remote) resources
+ // needed for test 2 and 4
+ var baseURL = "http://pivot.apache.org/assets-for-tests";
+ var frameURL = baseURL + "/frame.bxml";
+ // 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 ...
+
+ log("inline script 1 - end");
+ ]]>
+ </bxml:script>
+
<bxml:define>
- <!-- temp, then keep these definitions commented //-->
+ <!-- temp, then keep these definitions commented, do NOT removed them from here //-->
<bxml:include bxml:id="testLocalFrame" src="script_application_test.frame.bxml"/>
+
+ <!--
+ // test 1, ok
+ // load the bxml file from a fixed URL
+ // note that the bxml file will be loaded when this bxml file is loaded, so at application startup time, slowing it ...
<bxml:include bxml:id="testRemoteFrame" src="http://pivot.apache.org/assets-for-tests/frame.bxml"/>
+
+ // test 2, ok
+ // load the bxml file from a URL set in a JS variable, feature just added
+ // note that the bxml file will be loaded when this bxml file is loaded, so at application startup time, slowing it ...
+ <bxml:include bxml:id="testRemoteFrame" src="$frameURL"/>
+
+ // test 3, no, it's not possible
+ // try to set src later in JS ...
+ // <bxml:include bxml:id="testRemoteFrame" src=""/>
+ // <bxml:include bxml:id="testRemoteFrame"/>
+ //-->
</bxml:define>
+ <bxml:script>
+ <![CDATA[
+ // note that this script could be moved in an external js file, like that already included in this file ...
+ importPackage(org.apache.pivot.beans); // for BXMLSerializer
+ importPackage(org.apache.pivot.util);
+ importPackage(org.apache.pivot.wtk);
+
+ log("inline script 2 - start");
+
+ // test 4, ok
+ // define the variable in JS, and load it by instancing Java classes from here
+ // log("frameURL is \"" + frameURL + "\""); // show that the required value is already defined
+ log("application is " + application); // reference to the application itself, set in main class, in Java Code
+
+ // JS variables for (remote) resources
+ var bxmlSerializer = new BXMLSerializer(); // trick, I hope a good one ...
+ log("bxmlSerializer is " + bxmlSerializer);
+ // load the window/frame, but using an utility method defined in the application ...
+ log("load the window/frame now, by JS code (calling Java code from the application) ...");
+ var loadedWindow = application.loadWindowFromURL(frameURL, bxmlSerializer);
+ log("loadedWindow is " + loadedWindow + ", and its JavaScript typeof here is " + typeof loadedWindow); // note that typeof loadedWindow is object
+
+ var testRemoteFrame = loadedWindow;
+ // log formatted info but using a static method defined in the application, just for test ...
+ // note that this call is done in the usual (non-static) way, and works the same even from here
+ application.logObject(testRemoteFrame);
+
+
+ // test 5, TODO
+ // isolate a JS snippet (but self-contained) derive from test 4, and
+ // find a way to execute it as the text written in the textArea in a JS interpreter,
+ // then put it in a fixed template, and generalize it inside this test/sample application ...
+
+ log("inline script 2 - end");
+ ]]>
+ </bxml:script>
+
<TablePane styles="{padding:6, horizontalSpacing:6, verticalSpacing:8}">
<columns>
<TablePane.Column width="100"/>
@@ -50,7 +119,7 @@ limitations under the License.
<listButtonSelectionListeners>
function selectedIndexChanged(listButton, previousSelectedIndex) {
var selectedIndex = listButton.selectedIndex;
- log("Previous Selection was " + previousSelectedIndex + ", now Selected " + selectedIndex);
+ // log("Previous Selection was " + previousSelectedIndex + ", now Selected " + selectedIndex);
var templateText = "";
// simple logic to set template text, just for sample
@@ -64,11 +133,11 @@ limitations under the License.
case 2:
// TODO: put the right text to run in console ...
templateText = "testRemoteFrame.open(window);";
- // note that testRemoteFrame is loaded when this bxml is load, so at application startup time ...
+ // note that testRemoteFrame is already loaded, so here it's only to display it ...
testRemoteFrame.open(window); // temp, then keep this commented
break;
default:
- templateText = "";
+ clearConsole();
break;
}
textArea.text = templateText;
@@ -81,10 +150,7 @@ limitations under the License.
<PushButton bxml:id="clearButton" buttonData="Clear">
<buttonPressListeners>
function buttonPressed(button) {
- textArea.text = "";
- var msg = "Console cleared";
- log(msg);
- clearStatus();
+ clearConsole();
}
</buttonPressListeners>
</PushButton>
@@ -103,7 +169,12 @@ limitations under the License.
>
<textAreaContentListeners>
function textChanged(textArea) {
- // log("length = " + textArea.characterCount);
+ var numChars = textArea.characterCount;
+ // log("numChars = " + numChars);
+ if (numChars > 0) {
+ // runButton.enabled = true; // ok
+ runButton.setEnabled(true); // explicit usage of the setter
+ }
}
</textAreaContentListeners>
</TextArea>
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=1370791&r1=1370790&r2=1370791&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 Wed Aug 8 14:42:23 2012
@@ -38,6 +38,14 @@ function clearStatus() {
updateStatus("");
}
+function clearConsole() {
+ textArea.text = "";
+ // runButton.enabled = false; // ok
+ runButton.setEnabled(false); // explicit usage of the setter
+ log("Console cleared");
+ clearStatus();
+}
+
function runConsole() {
var text = textArea.text;