You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by Luiz Gustavo <lu...@gmail.com> on 2010/12/08 10:34:00 UTC

Default scripting Listeners

Is there a way to define a listener, as an element in bxml file, for use in
more than one component, as a dereference attribute value? For example:

We can define listeners for each component like this:

<Window xmlns="org.apache.pivot.wtk"
    xmlns:bxml="http://pivot.apache.org/bxml">
    <PushButton buttonData="Click Me!">
        *<buttonPressListeners>
        function buttonPressed(button) {
            // Handle event
        }
        </buttonPressListeners>*
    </PushButton>

    <PushButton buttonData="Click Me Again!">
        *<buttonPressListeners>
        function buttonPressed(button) {
            // Handle event
        }
        </buttonPressListeners>*
    </PushButton>
</Window>


But if the listener action is the same fo the two buttons above, could I
define something like the following?


<Window xmlns="org.apache.pivot.wtk"
    xmlns:bxml="http://pivot.apache.org/bxml">

   * <ButtonListener bxml:id="listener">
        function buttonPressed(button) {
            // Handle event
        }
    </ButtonListener>*

    <PushButton buttonData="Click Me!" *buttonPressListeners="$listener"*/>

    <PushButton buttonData="Click Me Again!" *
buttonPressListeners="$listener"*/>

</Window>



Cheers,
Luiz Gustavo S. de Souza

http://luizgustavoss.wordpress.com
http://luizgustavoss.blogspot.com
http://twitter.com/lugustso

Re: Default scripting Listeners

Posted by Greg Brown <gk...@verizon.net>.
Actually, the method name is defined by the attribute name: ButtonPressListener.buttonPressed. ButtonPressListener is the interface, and buttonPressed() is the handler method. The value of the attribute is the script that is executed when the buttonPressed() method is called. You can actually put any valid script code in there, but it is generally good to keep it to a minimum for readability.

On Dec 8, 2010, at 8:44 AM, Luiz Gustavo wrote:

> Hi Greg,
> 
> I undestood! methods used to register a listener can have any name... that's it!
> 
> <PushButton buttonData="Click Me!" ButtonPressListener.buttonPressed="onButtonPress1(); onButtonPress2()"/>
>  
> It answers my question. =)
> 
> Thanks!
> 
> 
> 2010/12/8 Greg Brown <gk...@verizon.net>
> > But I thought about the case of multiple common listeners, as I said. Do you think this could be an enhancement for future releases, or it's not worthy?
> 
> I don't think it is a common enough use case to justify extending BXML syntax to support it. It is fairly easy to create a listener instance in Java (or Scala, or even JavaScript) and attach it using getFooListeners().add(). Besides, a listener is nothing more than a method - you could do something like this if you really needed multiple "listeners:
> 
> <PushButton buttonData="Click Me!" ButtonPressListener.buttonPressed="onButtonPress1(); onButtonPress2()"/>
> 
> G
> 
> 
> 
> 
> -- 
> Luiz Gustavo S. de Souza
> 
> http://luizgustavoss.wordpress.com
> http://luizgustavoss.blogspot.com
> http://twitter.com/lugustso


Re: Default scripting Listeners

Posted by Luiz Gustavo <lu...@gmail.com>.
Hi Greg,

I undestood! methods used to register a listener can have any name... that's
it!

<PushButton buttonData="Click Me!" ButtonPressListener.buttonPressed="*
onButtonPress1()*; *onButtonPress2()*"/>

It answers my question. =)

Thanks!


2010/12/8 Greg Brown <gk...@verizon.net>

> > But I thought about the case of multiple common listeners, as I said. Do
> you think this could be an enhancement for future releases, or it's not
> worthy?
>
> I don't think it is a common enough use case to justify extending BXML
> syntax to support it. It is fairly easy to create a listener instance in
> Java (or Scala, or even JavaScript) and attach it using
> getFooListeners().add(). Besides, a listener is nothing more than a method -
> you could do something like this if you really needed multiple "listeners:
>
> <PushButton buttonData="Click Me!"
> ButtonPressListener.buttonPressed="onButtonPress1(); onButtonPress2()"/>
>
> G
>
>


-- 
Luiz Gustavo S. de Souza

http://luizgustavoss.wordpress.com
http://luizgustavoss.blogspot.com
http://twitter.com/lugustso

Re: Default scripting Listeners

Posted by Greg Brown <gk...@verizon.net>.
> But I thought about the case of multiple common listeners, as I said. Do you think this could be an enhancement for future releases, or it's not worthy?

I don't think it is a common enough use case to justify extending BXML syntax to support it. It is fairly easy to create a listener instance in Java (or Scala, or even JavaScript) and attach it using getFooListeners().add(). Besides, a listener is nothing more than a method - you could do something like this if you really needed multiple "listeners:

<PushButton buttonData="Click Me!" ButtonPressListener.buttonPressed="onButtonPress1(); onButtonPress2()"/>

G


Re: Default scripting Listeners

Posted by Luiz Gustavo <lu...@gmail.com>.
>
>
> You could do this in script as follows:
>
> <bxml:script>
> function onButtonPress() {
>     ...
> }
> </bxml:script>
>
> <PushButton buttonData="Click Me!"
> ButtonPressListener.buttonPressed="onButtonPress()"/>
> <PushButton buttonData="Click Me
> Again!" ButtonPressListener.buttonPressed="onButtonPress()"/>
>

Nice Greg, but imagine a scenario where I can have more than one listener
for the same event. In this case an bxml:id could help to identify it. For
example, if I had 20 buttons, and two different event handlers (one of each
used for 10 buttons).



> FYI, the "ButtonPressListener" qualifier is necessary so BXMLSerializer
> knows which interface defines the handler event.
>
> You could also do this in Java using the listener list directly:
>
> ButtonPressListener buttonPressListener = new ButtonPressListener() {
>     public void buttonPressed(Button button) {
>         ...
>     }
> }
>
> pushButton1.getButtonPressListeners().add(buttonPressListener);
> pushButton2.getButtonPressListeners().add(buttonPressListener);
>


This is the  way I'm doing it now, and is good for me, for what I'm using it
for. I just thought that I could do it in a different way, in bxml file. My
scenario is the Memory Game, that I've showed to you. I have 36 buttons, and
only one event listener for then. I could write the only listener needed
using the first example you showed:

<bxml:script>
function onButtonPress() {
    ...
}

But I thought about the case of multiple common listeners, as I said. Do you
think this could be an enhancement for future releases, or it's not worthy?




-- 
Luiz Gustavo S. de Souza

http://luizgustavoss.wordpress.com
http://luizgustavoss.blogspot.com
http://twitter.com/lugustso

Re: Default scripting Listeners

Posted by Greg Brown <gk...@verizon.net>.
> Is there a way to define a listener, as an element in bxml file, for use in more than one component, as a dereference attribute value? For example:
> ...
> <Window xmlns="org.apache.pivot.wtk"
>     xmlns:bxml="http://pivot.apache.org/bxml">
>     
>     <ButtonListener bxml:id="listener">
>         function buttonPressed(button) {
>             // Handle event
>         }
>     </ButtonListener>
>     
>     <PushButton buttonData="Click Me!" buttonPressListeners="$listener"/>    
>     <PushButton buttonData="Click Me Again!" buttonPressListeners="$listener"/>
>         
> </Window>

You could do this in script as follows:

<bxml:script>
function onButtonPress() {
    ...
}
</bxml:script>

<PushButton buttonData="Click Me!" ButtonPressListener.buttonPressed="onButtonPress()"/>    
<PushButton buttonData="Click Me Again!" ButtonPressListener.buttonPressed="onButtonPress()"/>

FYI, the "ButtonPressListener" qualifier is necessary so BXMLSerializer knows which interface defines the handler event.

You could also do this in Java using the listener list directly:

ButtonPressListener buttonPressListener = new ButtonPressListener() {
    public void buttonPressed(Button button) {
        ...
    }
}

pushButton1.getButtonPressListeners().add(buttonPressListener);
pushButton2.getButtonPressListeners().add(buttonPressListener);

G