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/14 15:28:21 UTC
svn commit: r1372875 - in /pivot/trunk/tests/src/org/apache/pivot/tests:
JavascriptConsoleTest.java JavascriptConsoleTest.json
javascript_console_test.bxml javascript_console_test.js
Author: smartini
Date: Tue Aug 14 13:28:21 2012
New Revision: 1372875
URL: http://svn.apache.org/viewvc?rev=1372875&view=rev
Log:
merge fixes from 2.0.x
Modified:
pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java
pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.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
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=1372875&r1=1372874&r2=1372875&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java (original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java Tue Aug 14 13:28:21 2012
@@ -30,6 +30,16 @@ import org.apache.pivot.wtk.DesktopAppli
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.Window;
+/**
+ * Sample Test Application (not fully working) to show how to interact
+ * between Java code and JavaScript code (interpreted by the JVM) inside bxml files.
+ * Many things usually done in Java code are here shown but from the JS side.
+ * <p>
+ * Some utility methods present here could be moved in a dedicated utility class.
+ * <p>
+ * Note that there are many comments inside this and related sources,
+ * and it's to show different ways to do the same things, even as iterative development.
+ */
public class JavascriptConsoleTest extends Application.Adapter {
public static final String LANGUAGE_KEY = "language";
public static final String MAIN_CLASS_NAME = JavascriptConsoleTest.class.getName();
@@ -75,16 +85,28 @@ public class JavascriptConsoleTest exten
return false;
}
+
+ /**
+ * Utility method to initialize secondary fields/elements during application startup.
+ *
+ * @param serializer the BXMLSerializer instance to use
+ */
private void initializeFields(BXMLSerializer serializer) {
logObject("initializeFields: start");
logObject("got BXMLSerializer instance = " + serializer);
- buildResources(MAIN_CLASS_NAME);
+ loadResources(MAIN_CLASS_NAME);
logObject("initializeFields: end");
}
- private void buildResources(String className) {
+
+ /**
+ * Load resource files for the given classname, or if null a default will be used.
+ *
+ * @param className the full class name (to use as a base name), for loading resources
+ */
+ private void loadResources(String className) {
if (className == null || className.length() < 1) {
className = MAIN_CLASS_NAME; // set a useful default
}
@@ -196,7 +218,7 @@ public class JavascriptConsoleTest exten
// 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);
+ loadResources(null);
}
label = (String) resources.get(name);
Modified: 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=1372875&r1=1372874&r2=1372875&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.json (original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.json Tue Aug 14 13:28:21 2012
@@ -24,6 +24,7 @@
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 ...
+ valuesTemplates_num: "3",
labelRun: "Run",
labelClear: "Clear",
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=1372875&r1=1372874&r2=1372875&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 Tue Aug 14 13:28:21 2012
@@ -76,9 +76,10 @@ limitations under the License.
<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.collections); // for collections
importPackage(org.apache.pivot.beans); // for BXMLSerializer
- importPackage(org.apache.pivot.util);
- importPackage(org.apache.pivot.wtk);
+ importPackage(org.apache.pivot.util); // for some useful stuff
+ importPackage(org.apache.pivot.wtk); // for application and other wtk classes
log("inline script 2 - start");
@@ -111,9 +112,14 @@ limitations under the License.
// 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
+ var valueFromLabel = // get the label value from resources
+ // application.getLabel("labelText"); // calling the application method directly ...
+ decodeLabel("labelText"); // using a JS wrapper ...
log("valueFromLabel (access via dedicated Java Helper) = \"" + valueFromLabel + "\"");
+ // decode some labels and set as list data, then reference it in the component ...
+ var templateButtonListData = buildListDataFromLabels();
+
// test 5, TODO
// comment test 4, and copy its source inside the switch statements below (if possible), to make it really dynamic ...
@@ -142,10 +148,17 @@ limitations under the License.
<Label text=" " />
<Label text="%labelTemplates"/>
+ <!-- // ok, using static data values
<ListButton bxml:id="templateButton"
listData="[' - Choose a template - ', 'Open Frame Local', 'Open Frame Remote (from Pivot Web Site)']"
selectedIndex="-1"
>
+ //-->
+ <!-- // new, using decoded labels as data values -->
+ <ListButton bxml:id="templateButton"
+ listData="$templateButtonListData"
+ selectedIndex="-1"
+ >
<listButtonSelectionListeners>
function selectedIndexChanged(listButton, previousSelectedIndex) {
var selectedIndex = listButton.selectedIndex;
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=1372875&r1=1372874&r2=1372875&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 Tue Aug 14 13:28:21 2012
@@ -15,6 +15,7 @@
* limitations under the License.
*/
importPackage(java.lang); // required to use System.out and System.err
+importPackage(org.apache.pivot.collections); // required to use Pivot class ArrayList and other collections
importPackage(org.apache.pivot.util); // required to use Pivot Utility class Console
importPackage(org.apache.pivot.wtk); // required to use Pivot WTK classes
@@ -26,6 +27,13 @@ function log(msg) {
System.out.println(msg);
}
+function logObject(msg) {
+ if (msg == undefined || msg == null)
+ return ;
+
+ application.logObject(msg);
+}
+
function updateStatus(msg) {
if (msg == undefined || msg == null || typeof msg != "string")
return ;
@@ -48,6 +56,26 @@ function clearConsole() {
clearStatus();
}
+function decodeLabel(name) {
+ return application.getLabel(name)
+}
+
+function buildListDataFromLabels() {
+ var templateButtonListData = new ArrayList();
+
+ // templateButtonListData.add(decodeLabel("valuesTemplates_all")); // no, because it can't be added later ... but keep here, just as idea
+ var numValues = decodeLabel("valuesTemplates_num");
+ logObject(numValues);
+ log("Expected " + numValues + " number of values for list");
+ if (numValues != null && numValues.length() > 0) {
+ for (var i = 0; i < numValues; i++) {
+ templateButtonListData.add(decodeLabel("valuesTemplates_" + i));
+ }
+ }
+
+ return templateButtonListData;
+}
+
function runConsole() {
var text = textJSSource.text;
@@ -60,7 +88,7 @@ function runConsole() {
updateStatus(msg + " ...");
textJSOutput.text = "";
-// TODO: continue here ...
+// TODO: continue here, but calling a Java method inside the application, with the stuff inside ...
;
}