You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/03/13 19:04:43 UTC

svn commit: r922633 - in /pivot/trunk: tutorials/src/org/apache/pivot/tutorials/scripting/scripting.wtkx tutorials/www/wtkx-primer.xml wtk/src/org/apache/pivot/wtk/TextInput.java wtk/src/org/apache/pivot/wtkx/WTKXSerializer.java

Author: gbrown
Date: Sat Mar 13 18:04:43 2010
New Revision: 922633

URL: http://svn.apache.org/viewvc?rev=922633&view=rev
Log:
Provide access to event arguments in attribute-based event handlers.

Modified:
    pivot/trunk/tutorials/src/org/apache/pivot/tutorials/scripting/scripting.wtkx
    pivot/trunk/tutorials/www/wtkx-primer.xml
    pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java
    pivot/trunk/wtk/src/org/apache/pivot/wtkx/WTKXSerializer.java

Modified: pivot/trunk/tutorials/src/org/apache/pivot/tutorials/scripting/scripting.wtkx
URL: http://svn.apache.org/viewvc/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/scripting/scripting.wtkx?rev=922633&r1=922632&r2=922633&view=diff
==============================================================================
--- pivot/trunk/tutorials/src/org/apache/pivot/tutorials/scripting/scripting.wtkx (original)
+++ pivot/trunk/tutorials/src/org/apache/pivot/tutorials/scripting/scripting.wtkx Sat Mar 13 18:04:43 2010
@@ -16,7 +16,7 @@ See the License for the specific languag
 limitations under the License.
 -->
 
-<Window wtkx:id="window" title="Scripting Demo" maximized="true"
+<Window title="Scripting Demo" maximized="true"
     WindowStateListener.windowOpened="java.lang.System.out.println('Window opened: ' + x)"
     WindowStateListener.windowClosed="java.lang.System.out.println('Window closed: ' + y)"
     xmlns:wtkx="http://pivot.apache.org/wtkx"
@@ -33,8 +33,8 @@ limitations under the License.
     var x = 10;
     var y = 20;
 
-    function buttonClicked() {
-        Prompt.prompt("y = " + y, window);
+    function buttonClicked(button) {
+        Prompt.prompt("y = " + y, button.window);
     }
     ]]>
     </wtkx:script>
@@ -59,7 +59,7 @@ limitations under the License.
                     </PushButton>
 
                     <PushButton buttonData="No, Click Me!"
-                        ButtonPressListener.buttonPressed="buttonClicked()"/>
+                        ButtonPressListener.buttonPressed="buttonClicked(arguments[0])"/>
 
                     <Border styles="{color:10}">
                         <content>

Modified: pivot/trunk/tutorials/www/wtkx-primer.xml
URL: http://svn.apache.org/viewvc/pivot/trunk/tutorials/www/wtkx-primer.xml?rev=922633&r1=922632&r2=922633&view=diff
==============================================================================
--- pivot/trunk/tutorials/www/wtkx-primer.xml (original)
+++ pivot/trunk/tutorials/www/wtkx-primer.xml Sat Mar 13 18:04:43 2010
@@ -717,11 +717,19 @@ limitations under the License.
             <PushButton xmlns="org.apache.pivot.wtk"
                 xmlns:wtkx="http://pivot.apache.org/wtkx"
                 buttonData="Click Me!"
-                ButtonPressListener.buttonPressed="// Handle event"/>
+                ButtonPressListener.buttonPressed="handleEvent(arguments[0])"/>
             ]]>
         </source>
 
         <p>
+            Note that the handler function is passed a value of <tt>arguments[0]</tt>. The
+            <tt>arguments</tt> array contains the arguments that were originally passed to the event
+            listener method, and only exists within the scope of the event handler.
+            <tt>arguments[0]</tt> contains the first argument passed to the listener method, which
+            in this case is a reference to the button that fired the event.
+        </p>
+
+        <p>
             Attribute-based event handlers are well suited to short handler code that, ideally,
             fits on a single line. Longer event handler code may be better suited to an
             element-based listener list, or, depending on the level of complexity, a Java-based

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java?rev=922633&r1=922632&r2=922633&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java Sat Mar 13 18:04:43 2010
@@ -546,12 +546,16 @@ public class TextInput extends Component
      * The length of the selection.
      */
     public void setSelection(int selectionStart, int selectionLength) {
+        if (textNode == null) {
+            throw new IllegalStateException();
+        }
+
         if (selectionLength < 0) {
             throw new IllegalArgumentException("selectionLength is negative.");
         }
 
         if (selectionStart < 0
-            || selectionStart + selectionLength > getTextLength()) {
+            || selectionStart + selectionLength > textNode.getCharacterCount()) {
             throw new IndexOutOfBoundsException();
         }
 
@@ -587,7 +591,11 @@ public class TextInput extends Component
      * Selects all text.
      */
     public void selectAll() {
-        setSelection(0, getTextLength());
+        if (textNode == null) {
+            throw new IllegalStateException();
+        }
+
+        setSelection(0, textNode.getCharacterCount());
     }
 
     /**

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtkx/WTKXSerializer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtkx/WTKXSerializer.java?rev=922633&r1=922632&r2=922633&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtkx/WTKXSerializer.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtkx/WTKXSerializer.java Sat Mar 13 18:04:43 2010
@@ -199,6 +199,8 @@ public class WTKXSerializer implements S
         private String event;
         private String script;
 
+        private static final String ARGUMENTS_KEY = "arguments";
+
         public AttributeInvocationHandler(ScriptEngine scriptEngine, String event, String script) {
             this.scriptEngine = scriptEngine;
             this.event = event;
@@ -213,6 +215,9 @@ public class WTKXSerializer implements S
             String methodName = method.getName();
             if (methodName.equals(event)) {
                 try {
+                    SimpleBindings bindings = new SimpleBindings();
+                    bindings.put(ARGUMENTS_KEY, args);
+                    scriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
                     scriptEngine.eval(script);
                 } catch (ScriptException exception) {
                     System.err.println(exception);
@@ -817,11 +822,8 @@ public class WTKXSerializer implements S
                                 throw new SerializationException(exception);
                             }
 
-                            // Don't pollute the engine namespace with the listener functions
-                            ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(language);
-                            scriptEngine.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
-
                             // Create an invocation handler for this listener
+                            ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(language);
                             AttributeInvocationHandler handler =
                                 new AttributeInvocationHandler(scriptEngine,
                                     attribute.localName.substring(attribute.localName.lastIndexOf(".") + 1),