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/07 18:32:57 UTC

svn commit: r1370348 - in /pivot/branches/2.0.x/tests/src/org/apache/pivot/tests: JavascriptConsoleTest.java javascript_console_test.bxml javascript_console_test.js

Author: smartini
Date: Tue Aug  7 16:32:57 2012
New Revision: 1370348

URL: http://svn.apache.org/viewvc?rev=1370348&view=rev
Log:
add a new Test Application (or small, still not completely working sample, but it's just to give the idea) with a minimal amount of Java code, and most of the code in JavaScript (but the JS interpreted by the JVM). This should show better how to interact with remote bxml files (for example generated or simply hosted at server-side) and do all the processing client side but in JS, more aligned to the HTML 5 way to do things.

Added:
    pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java
    pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/javascript_console_test.bxml
    pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/javascript_console_test.js

Added: pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java
URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java?rev=1370348&view=auto
==============================================================================
--- pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java (added)
+++ pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/JavascriptConsoleTest.java Tue Aug  7 16:32:57 2012
@@ -0,0 +1,81 @@
+/*
+ * 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 java.io.IOException;
+
+import org.apache.pivot.beans.BXMLSerializer;
+import org.apache.pivot.collections.Map;
+import org.apache.pivot.serialization.SerializationException;
+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 {
+    private Display display = null;
+    private Window window = null;
+
+    @Override
+    public void startup(Display displayArgument, Map<String, String> properties) throws Exception {
+        System.out.println("startup: start");
+
+        this.display = displayArgument;
+
+        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);
+        System.out.println("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");
+    }
+
+    @Override
+    public boolean shutdown(boolean optional) {
+        if (window != null) {
+            window.close();
+        }
+
+        return false;
+    }
+
+    private void initializeFields(BXMLSerializer serializer) {
+        System.out.println("initializeFields: start");
+
+        System.out.println("got BXMLSerializer instance = " + serializer);
+
+        System.out.println("initializeFields: end");
+    }
+
+    protected Window loadWindow(String fileName, BXMLSerializer bxmlSerializer)
+        throws SerializationException, IOException {
+        if (bxmlSerializer == null) {
+            bxmlSerializer = new BXMLSerializer();
+        }
+        return (Window)bxmlSerializer.readObject(JavascriptConsoleTest.class, fileName);
+    }
+
+    public static void main(String[] args) {
+        DesktopApplicationContext.main(JavascriptConsoleTest.class, args);
+    }
+
+}

Added: pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/javascript_console_test.bxml
URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/javascript_console_test.bxml?rev=1370348&view=auto
==============================================================================
--- pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/javascript_console_test.bxml (added)
+++ pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/javascript_console_test.bxml Tue Aug  7 16:32:57 2012
@@ -0,0 +1,119 @@
+<?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 bxml:id="window"
+    title="JavaScript Console Test" maximized="true"
+    xmlns:bxml="http://pivot.apache.org/bxml"
+    xmlns="org.apache.pivot.wtk"
+>
+    <bxml:script src="javascript_console_test.js"/>
+
+    <bxml:define>
+        <!-- temp, then keep these definitions commented //-->
+        <bxml:include bxml:id="testLocalFrame"  src="script_application_test.frame.bxml"/>
+        <bxml:include bxml:id="testRemoteFrame" src="http://pivot.apache.org/assets-for-tests/frame.bxml"/>
+    </bxml:define>
+
+    <TablePane styles="{padding:6, horizontalSpacing:6, verticalSpacing:8}">
+        <columns>
+            <TablePane.Column width="100"/>
+            <TablePane.Column width="1*"/>
+            <TablePane.Column width="50"/>
+        </columns>
+
+        <TablePane.Row height="-1">
+            <BoxPane orientation="horizontal" TablePane.columnSpan="3">
+                <Label styles="{horizontalAlignment:'center', verticalAlignment:'center'}" text="JavaScript (interpreted by the JVM) Console:"
+                />
+                <Label text=" " />
+
+                <Label text="Templates:"/>
+                <ListButton bxml:id="templateButton"
+                    listData="['Choose a template', 'Open Frame Local', 'Open Frame Remote (from Pivot Web Site)']"
+                    selectedIndex="-1"
+                >
+                    <listButtonSelectionListeners>
+                        function selectedIndexChanged(listButton, previousSelectedIndex) {
+                            var selectedIndex = listButton.selectedIndex;
+                            log("Previous Selection was " + previousSelectedIndex + ", now Selected " + selectedIndex);
+
+                            var templateText = "";
+                            // simple logic to set template text, just for sample
+                            switch(selectedIndex) {
+                                // case 0:  // handle with default ...
+                                case 1:
+// TODO: put the right text to run in console ...
+                                    templateText = "testLocalFrame.open(window);";
+                                    testLocalFrame.open(window);  // temp, then keep this commented
+                                    break;
+                                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 ...
+                                    testRemoteFrame.open(window);  // temp, then keep this commented
+                                    break;
+                                default:
+                                    templateText = "";
+                                    break;
+                            }
+                            textArea.text = templateText;
+                        }
+                    </listButtonSelectionListeners>
+                </ListButton>
+                <Label text=" " />
+
+                <PushButton bxml:id="runButton"   buttonData="Run" ButtonPressListener.buttonPressed="runConsole()"/>
+                <PushButton bxml:id="clearButton" buttonData="Clear">
+                    <buttonPressListeners>
+                    function buttonPressed(button) {
+                        textArea.text = "";
+                        var msg = "Console cleared";
+                        log(msg);
+                        clearStatus();
+                    }
+                    </buttonPressListeners>
+                </PushButton>
+            </BoxPane>
+        </TablePane.Row>
+        <TablePane.Row height="1*">
+            <Label styles="{horizontalAlignment:'right', verticalAlignment:'center'}" text="JS Source:"/>
+            <Border>
+                <ScrollPane horizontalScrollBarPolicy="fill" verticalScrollBarPolicy="fill_to_capacity"
+                    preferredHeight="200"
+                >
+                    <TextArea bxml:id="textArea"
+                        styles="{wrapText:false}"
+                        editable="true"
+                        text=""
+                    >
+                       <textAreaContentListeners>
+                        function textChanged(textArea) {
+                            // log("length = " + textArea.characterCount);
+                        }
+                        </textAreaContentListeners>
+                    </TextArea>
+                </ScrollPane>
+            </Border>
+        </TablePane.Row>
+        <TablePane.Row height="-1">
+            <Label styles="{horizontalAlignment:'right', verticalAlignment:'center'}" text="Status:"/>
+            <Label bxml:id="textStatus" styles="{horizontalAlignment:'left', verticalAlignment:'center'}" text=""/>
+        </TablePane.Row>
+    </TablePane>
+
+</Window>

Added: pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/javascript_console_test.js
URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/javascript_console_test.js?rev=1370348&view=auto
==============================================================================
--- pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/javascript_console_test.js (added)
+++ pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/javascript_console_test.js Tue Aug  7 16:32:57 2012
@@ -0,0 +1,61 @@
+/*
+ * 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.
+ */
+importPackage(java.lang);  // required to use System.out and System.err
+importPackage(org.apache.pivot.util);  // required to use Pivot Utility class Console
+importPackage(org.apache.pivot.wtk);   // required to use Pivot WTK classes
+
+
+function log(msg) {
+	if (msg == undefined || msg == null || typeof msg != "string")
+		return ;
+
+    System.out.println(msg);
+}
+
+function updateStatus(msg) {
+	if (msg == undefined || msg == null || typeof msg != "string")
+		return ;
+
+	if (msg.length < 50)
+		textStatus.text = msg;
+}
+
+function clearStatus() {
+	updateStatus("");
+}
+
+
+function runConsole() {
+	var text = textArea.text;
+	log("Console Text length = " + text.length());
+	if (text.length() < 1)
+		return ;
+	// else
+	var msg = "Run JS Code in Console";
+	log(msg);
+	updateStatus(msg + " ...");
+
+// TODO: continue here ...
+	;
+}
+
+//TODO: temp ...
+function openFrame(button) {
+    testFrame.open(window);
+    java.lang.System.out.println(label1.getText());
+    java.lang.System.out.println(label2.getText());
+}