using listeners in groovy classes

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

using listeners in groovy classes

delta_vee
Hello -

I am trying to replicate some behavior in the SWT examples for the
DateTime widget.
Apparently you cannot create anonymous classes within a Groovy class,
hence breaking out and creating the class SA1, below.

Actually I would like to create a closure instead of the listener
class, but cannot find any examples.

Any help would be greatly appreciated.
Thanks!

Michael
---------------Example Start -------
package snippets

import org.eclipse.swt.*
import org.eclipse.swt.events.*
import org.eclipse.swt.layout.*
import org.eclipse.swt.widgets.*

class Snippet250 {

    def run() {

      Display display = new Display()
      Shell shell = new Shell(display)
      shell.setLayout(new RowLayout())

      DateTime dt = new DateTime(shell, SWT.CALENDAR)
      //
      def sa = new SA1(dt)
      dt.addSelectionListener(sa)
      //
      shell.pack()
      shell.open()

      while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
        display.sleep()
      }
      display.dispose()

    }

    static void main(String[] args) {
      def snip = new Snippet250()
      snip.run()
    }

}
class SA1 extends org.eclipse.swt.events.SelectionAdapter {
  DateTime dt
  SA1(DateTime adt){
    dt = adt
  }
  def widgetSelected(SelectionEvent e){
    println("Widget Selected")
    println(dt.dump())
        def day = dt.getDay() // Calendar.DAY_OF_MONTH
        def month = dt.getMonth() // Calendar.MONTH
        def year = dt.getYear() // Calendar.YEAR
        def yearStr = String.valueOf(year)
        def monthStr = String.valueOf(month)
        def dayStr = String.valueOf(day)
        println "Year: "+ yearStr+", Month: "+monthStr+", Day: "+ dayStr
  }
}
----------------example end ----------------------

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: using listeners in groovy classes

aalmiray
You can coerce a Closure to a single method interface or a Map to a multiple method interface with the following code:

def actionListener = { event -> /* do stuff */ } as ActionListener

def componentListener = [
   componentShown: { event -> /* do stuff */ },
   componentHidden: { event -> /* do stuff */ },
   componentResized: { event -> /* do stuff */ },
   componentMoved: { event -> /* do stuff */ }
] as ComponentListener

But you can not coerce them into an abstract or concrete subclass.

Cheers,
Andres

Michael McConnell-2 wrote
Hello -

I am trying to replicate some behavior in the SWT examples for the
DateTime widget.
Apparently you cannot create anonymous classes within a Groovy class,
hence breaking out and creating the class SA1, below.

Actually I would like to create a closure instead of the listener
class, but cannot find any examples.

Any help would be greatly appreciated.
Thanks!

Michael
---------------Example Start -------
package snippets

import org.eclipse.swt.*
import org.eclipse.swt.events.*
import org.eclipse.swt.layout.*
import org.eclipse.swt.widgets.*

class Snippet250 {

    def run() {

      Display display = new Display()
      Shell shell = new Shell(display)
      shell.setLayout(new RowLayout())

      DateTime dt = new DateTime(shell, SWT.CALENDAR)
      //
      def sa = new SA1(dt)
      dt.addSelectionListener(sa)
      //
      shell.pack()
      shell.open()

      while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
        display.sleep()
      }
      display.dispose()

    }

    static void main(String[] args) {
      def snip = new Snippet250()
      snip.run()
    }

}
class SA1 extends org.eclipse.swt.events.SelectionAdapter {
  DateTime dt
  SA1(DateTime adt){
    dt = adt
  }
  def widgetSelected(SelectionEvent e){
    println("Widget Selected")
    println(dt.dump())
        def day = dt.getDay() // Calendar.DAY_OF_MONTH
        def month = dt.getMonth() // Calendar.MONTH
        def year = dt.getYear() // Calendar.YEAR
        def yearStr = String.valueOf(year)
        def monthStr = String.valueOf(month)
        def dayStr = String.valueOf(day)
        println "Year: "+ yearStr+", Month: "+monthStr+", Day: "+ dayStr
  }
}
----------------example end ----------------------

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email
Reply | Threaded
Open this post in threaded view
|

Re: using listeners in groovy classes

Joachim Baumann
AFAIK, this will be implemented with the next release.

Cheers, Joachim

Andres Almiray schrieb:

> You can coerce a Closure to a single method interface or a Map to a multiple
> method interface with the following code:
>
> def actionListener = { event -> /* do stuff */ } as ActionListener
>
> def componentListener = [
>    componentShown: { event -> /* do stuff */ },
>    componentHidden: { event -> /* do stuff */ },
>    componentResized: { event -> /* do stuff */ },
>    componentMoved: { event -> /* do stuff */ }
> ] as ComponentListener
>
> But you can not coerce them into an abstract or concrete subclass.
>
> Cheers,
> Andres
>
>
> Michael McConnell-2 wrote:
>  
>> Hello -
>>
>> I am trying to replicate some behavior in the SWT examples for the
>> DateTime widget.
>> Apparently you cannot create anonymous classes within a Groovy class,
>> hence breaking out and creating the class SA1, below.
>>
>> Actually I would like to create a closure instead of the listener
>> class, but cannot find any examples.
>>
>> Any help would be greatly appreciated.
>> Thanks!
>>
>> Michael
>> ---------------Example Start -------
>> package snippets
>>
>> import org.eclipse.swt.*
>> import org.eclipse.swt.events.*
>> import org.eclipse.swt.layout.*
>> import org.eclipse.swt.widgets.*
>>
>> class Snippet250 {
>>
>>     def run() {
>>
>>       Display display = new Display()
>>       Shell shell = new Shell(display)
>>       shell.setLayout(new RowLayout())
>>
>>       DateTime dt = new DateTime(shell, SWT.CALENDAR)
>>       //
>>       def sa = new SA1(dt)
>>       dt.addSelectionListener(sa)
>>       //
>>       shell.pack()
>>       shell.open()
>>
>>       while (!shell.isDisposed()) {
>>         if (!display.readAndDispatch())
>>         display.sleep()
>>       }
>>       display.dispose()
>>
>>     }
>>
>>     static void main(String[] args) {
>>       def snip = new Snippet250()
>>       snip.run()
>>     }
>>
>> }
>> class SA1 extends org.eclipse.swt.events.SelectionAdapter {
>>   DateTime dt
>>   SA1(DateTime adt){
>>     dt = adt
>>   }
>>   def widgetSelected(SelectionEvent e){
>>     println("Widget Selected")
>>     println(dt.dump())
>>         def day = dt.getDay() // Calendar.DAY_OF_MONTH
>>         def month = dt.getMonth() // Calendar.MONTH
>>         def year = dt.getYear() // Calendar.YEAR
>>         def yearStr = String.valueOf(year)
>>         def monthStr = String.valueOf(month)
>>         def dayStr = String.valueOf(day)
>>         println "Year: "+ yearStr+", Month: "+monthStr+", Day: "+ dayStr
>>   }
>> }
>> ----------------example end ----------------------
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>
>>    
>
>  

--
Xinaris GmbH
Flüsselweg 10, 76646 Bruchsal
Tel.: +(49)7251/39264-21    | Sitz der Gesellschaft: Bruchsal
Fax:  +(49)7251/39264-29    | Registergericht: AG Mannheim HRB 232791
WWW:  http://www.xinaris.de | Geschäftsführer: Dr. Joachim Baumann
Mail: [hidden email]       | USt-ID DE 242 975 278


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: using listeners in groovy classes

Guillaume Laforge
Administrator
In reply to this post by delta_vee
I think someone mentioned that SWT listeners aren't compliant with the
JavaBean convention. Hence, Groovy can't use the closures as listeners
:-(

On 8/6/07, Michael McConnell <[hidden email]> wrote:

> Hello -
>
> I am trying to replicate some behavior in the SWT examples for the
> DateTime widget.
> Apparently you cannot create anonymous classes within a Groovy class,
> hence breaking out and creating the class SA1, below.
>
> Actually I would like to create a closure instead of the listener
> class, but cannot find any examples.
>
> Any help would be greatly appreciated.
> Thanks!
>
> Michael
> ---------------Example Start -------
> package snippets
>
> import org.eclipse.swt.*
> import org.eclipse.swt.events.*
> import org.eclipse.swt.layout.*
> import org.eclipse.swt.widgets.*
>
> class Snippet250 {
>
>     def run() {
>
>       Display display = new Display()
>       Shell shell = new Shell(display)
>       shell.setLayout(new RowLayout())
>
>       DateTime dt = new DateTime(shell, SWT.CALENDAR)
>       //
>       def sa = new SA1(dt)
>       dt.addSelectionListener(sa)
>       //
>       shell.pack()
>       shell.open()
>
>       while (!shell.isDisposed()) {
>         if (!display.readAndDispatch())
>         display.sleep()
>       }
>       display.dispose()
>
>     }
>
>     static void main(String[] args) {
>       def snip = new Snippet250()
>       snip.run()
>     }
>
> }
> class SA1 extends org.eclipse.swt.events.SelectionAdapter {
>   DateTime dt
>   SA1(DateTime adt){
>     dt = adt
>   }
>   def widgetSelected(SelectionEvent e){
>     println("Widget Selected")
>     println(dt.dump())
>         def day = dt.getDay() // Calendar.DAY_OF_MONTH
>         def month = dt.getMonth() // Calendar.MONTH
>         def year = dt.getYear() // Calendar.YEAR
>         def yearStr = String.valueOf(year)
>         def monthStr = String.valueOf(month)
>         def dayStr = String.valueOf(day)
>         println "Year: "+ yearStr+", Month: "+monthStr+", Day: "+ dayStr
>   }
> }
> ----------------example end ----------------------
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: using listeners in groovy classes

Christopher Brown-8
In reply to this post by aalmiray
This seems to be what I've run into, without personally having found a solution, when doing stuff in Swing.

For example, it's possible to define a WindowListener like this (a listener with multiple methods) but there doesn't appear to be any compact way to extend a WindowAdapter to override a specific method to handle the one event you're interested in (as WindowAdapter has empty implementations of all methods in WindowListener).  This is about the only example I've found so far of the Java anonymous class code being more compact than the equivalent Groovy code.

- Chris


On 07/08/07, Andres Almiray <[hidden email]> wrote:

You can coerce a Closure to a single method interface or a Map to a multiple
method interface with the following code:

def actionListener = { event -> /* do stuff */ } as ActionListener

def componentListener = [
   componentShown: { event -> /* do stuff */ },
   componentHidden: { event -> /* do stuff */ },
   componentResized: { event -> /* do stuff */ },
   componentMoved: { event -> /* do stuff */ }
] as ComponentListener

But you can not coerce them into an abstract or concrete subclass.

Cheers,
Andres


Michael McConnell-2 wrote:

>
> Hello -
>
> I am trying to replicate some behavior in the SWT examples for the
> DateTime widget.
> Apparently you cannot create anonymous classes within a Groovy class,
> hence breaking out and creating the class SA1, below.
>
> Actually I would like to create a closure instead of the listener
> class, but cannot find any examples.
>
> Any help would be greatly appreciated.
> Thanks!
>
> Michael
> ---------------Example Start -------
> package snippets
>
> import org.eclipse.swt.*
> import org.eclipse.swt.events.*
> import org.eclipse.swt.layout.*
> import org.eclipse.swt.widgets.*
>
> class Snippet250 {
>
>     def run() {
>

>       Display display = new Display()
>       Shell shell = new Shell(display)
>       shell.setLayout(new RowLayout())
>
>       DateTime dt = new DateTime(shell, SWT.CALENDAR)
>       //
>       def sa = new SA1(dt)
>       dt.addSelectionListener(sa)
>       //
>       shell.pack()
>       shell.open()
>
>       while (!shell.isDisposed()) {
>         if (!display.readAndDispatch())
>         display.sleep()
>       }
>       display.dispose()
>
>     }
>
>     static void main(String[] args) {
>       def snip = new Snippet250()
>       snip.run ()
>     }
>
> }
> class SA1 extends org.eclipse.swt.events.SelectionAdapter {
>   DateTime dt
>   SA1(DateTime adt){
>     dt = adt
>   }
>   def widgetSelected(SelectionEvent e){
>     println("Widget Selected")
>     println(dt.dump())
>         def day = dt.getDay() // Calendar.DAY_OF_MONTH
>         def month = dt.getMonth() // Calendar.MONTH
>         def year = dt.getYear() // Calendar.YEAR
>         def yearStr = String.valueOf(year)
>         def monthStr = String.valueOf(month)
>         def dayStr = String.valueOf(day)
>         println "Year: "+ yearStr+", Month: "+monthStr+", Day: "+ dayStr
>   }
> }
> ----------------example end ----------------------
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>

--
View this message in context: http://www.nabble.com/using-listeners-in-groovy-classes-tf4224792.html#a12026559
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: using listeners in groovy classes

Guillaume Laforge
Administrator
Regarding the WindowAdapter, the closure coertion mechanism currently
works only on interfaces... so as WindowAdapter is a concrete class,
it won't work.

However, you can use Map coercion there instead:

foo.addWindowListener( [ windowClosed: { ... } ] as WindowListner ] )


On 8/7/07, Christopher Brown <[hidden email]> wrote:

> This seems to be what I've run into, without personally having found a
> solution, when doing stuff in Swing.
>
> For example, it's possible to define a WindowListener like this (a listener
> with multiple methods) but there doesn't appear to be any compact way to
> extend a WindowAdapter to override a specific method to handle the one event
> you're interested in (as WindowAdapter has empty implementations of all
> methods in WindowListener).  This is about the only example I've found so
> far of the Java anonymous class code being more compact than the equivalent
> Groovy code.
>
> - Chris
>
>
>
> On 07/08/07, Andres Almiray <[hidden email]> wrote:
> >
> > You can coerce a Closure to a single method interface or a Map to a
> multiple
> > method interface with the following code:
> >
> > def actionListener = { event -> /* do stuff */ } as ActionListener
> >
> > def componentListener = [
> >    componentShown: { event -> /* do stuff */ },
> >    componentHidden: { event -> /* do stuff */ },
> >    componentResized: { event -> /* do stuff */ },
> >    componentMoved: { event -> /* do stuff */ }
> > ] as ComponentListener
> >
> > But you can not coerce them into an abstract or concrete subclass.
> >
> > Cheers,
> > Andres
> >
> >
> > Michael McConnell-2 wrote:
> > >
> > > Hello -
> > >
> > > I am trying to replicate some behavior in the SWT examples for the
> > > DateTime widget.
> > > Apparently you cannot create anonymous classes within a Groovy class,
> > > hence breaking out and creating the class SA1, below.
> > >
> > > Actually I would like to create a closure instead of the listener
> > > class, but cannot find any examples.
> > >
> > > Any help would be greatly appreciated.
> > > Thanks!
> > >
> > > Michael
> > > ---------------Example Start -------
> > > package snippets
> > >
> > > import org.eclipse.swt.*
> > > import org.eclipse.swt.events.*
> > > import org.eclipse.swt.layout.*
> > > import org.eclipse.swt.widgets.*
> > >
> > > class Snippet250 {
> > >
> > >     def run() {
> > >
> > >       Display display = new Display()
> > >       Shell shell = new Shell(display)
> > >       shell.setLayout(new RowLayout())
> > >
> > >       DateTime dt = new DateTime(shell, SWT.CALENDAR)
> > >       //
> > >       def sa = new SA1(dt)
> > >       dt.addSelectionListener(sa)
> > >       //
> > >       shell.pack()
> > >       shell.open()
> > >
> > >       while (!shell.isDisposed()) {
> > >         if (!display.readAndDispatch())
> > >         display.sleep()
> > >       }
> > >       display.dispose()
> > >
> > >     }
> > >
> > >     static void main(String[] args) {
> > >       def snip = new Snippet250()
> > >       snip.run ()
> > >     }
> > >
> > > }
> > > class SA1 extends
> org.eclipse.swt.events.SelectionAdapter {
> > >   DateTime dt
> > >   SA1(DateTime adt){
> > >     dt = adt
> > >   }
> > >   def widgetSelected(SelectionEvent e){
> > >     println("Widget Selected")
> > >     println(dt.dump())
> > >         def day = dt.getDay() // Calendar.DAY_OF_MONTH
> > >         def month = dt.getMonth() // Calendar.MONTH
> > >         def year = dt.getYear() // Calendar.YEAR
> > >         def yearStr = String.valueOf(year)
> > >         def monthStr = String.valueOf(month)
> > >         def dayStr = String.valueOf(day)
> > >         println "Year: "+ yearStr+", Month: "+monthStr+", Day: "+ dayStr
> > >   }
> > > }
> > > ----------------example end ----------------------
> > >
> > >
> ---------------------------------------------------------------------
> > > To unsubscribe from this list please visit:
> > >
> > >     http://xircles.codehaus.org/manage_email
> > >
> > >
> > >
> >
> > --
> > View this message in context:
> http://www.nabble.com/using-listeners-in-groovy-classes-tf4224792.html#a12026559
> > Sent from the groovy - user mailing list archive at Nabble.com.
> >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> >     http://xircles.codehaus.org/manage_email
> >
> >
>
>


--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: using listeners in groovy classes

Randall R Schulz
In reply to this post by Guillaume Laforge
On Monday 06 August 2007 23:05, Guillaume Laforge wrote:
> I think someone mentioned that SWT listeners aren't compliant with
> the JavaBean convention. Hence, Groovy can't use the closures as
> listeners
>
> :-(

If it's important enough to you, you could write BeanInfo classes to
make classes that don't use those ugly bean method naming conventions
compatible with the bean introspection mechanisms...


Randall Schulz

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: using listeners in groovy classes

Glen P
In reply to this post by delta_vee


See the article on my site!

http://glenp.net

Glen