Inner Classes not supported!!! Any alternatives??? Help!!!

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
46 messages Options
12345
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Inner Classes not supported!!! Any alternatives??? Help!!!

Cliff-3
I ran against my first Groovy stumbling block writing some swing code. Any
help is greatly appreciated. Consider the following non functional code:

class Gui {

    //Gui building logic...

    class MyFilter extends FileFilter {
        String descr, extension
        public MyFilter(String descr, String extension) {this.descr = descr;
this.extension = extension;}
        boolean accept(File f) { return f.name.endsWith(extension) }
        String getDescription() { return descr }
    }

    void selectFile()
    {
        def chooser = new JFileChooser();
        ['Text Files (*.txt)':'*.txt',
        'Properties files (*.properties)':'*.properties', 'All
Files':'*'].each {
            chooser.addChooseableFileFilter(new MyFilter(it.key, it.value))
        }
        //Cancel selected
        if(chooser.showDialog(frame)!=0) return
    }
}

I'm trying to implement a file chooser dialog that narrows selection to the
values in the inlined map. Without an inner class I don't see how to
accomplish this. Can somebody help?
---------------------------------------------------
Clifton C. Craig, Software Engineer
Intelligent Computer Systems -  A Division of GBG
[hidden email]
[hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Inner Classes not supported!!! Any alternatives??? Help!!!

Cliff-3
On Thursday 11 May 2006 3:35 pm, Clifton Craig wrote:

> I ran against my first Groovy stumbling block writing some swing code. Any
> help is greatly appreciated. Consider the following non functional code:
>
> class Gui {
>
>     //Gui building logic...
>
>     class MyFilter extends FileFilter {
>         String descr, extension
>         public MyFilter(String descr, String extension) {this.descr =
> descr; this.extension = extension;}
>         boolean accept(File f) { return f.name.endsWith(extension) }
>         String getDescription() { return descr }
>     }
>
>     void selectFile()
>     {
>         def chooser = new JFileChooser();
>         ['Text Files (*.txt)':'*.txt',
>         'Properties files (*.properties)':'*.properties', 'All
> Files':'*'].each {
>             chooser.addChooseableFileFilter(new MyFilter(it.key, it.value))
>         }
>         //Cancel selected
>         if(chooser.showDialog(frame)!=0) return
>     }
> }
>

I'm struglling with this more and more because as I code more Swing I see more
and more places where I'd want an Anonymous inner class. How do others handle
this?

---------------------------------------------------
Clifton C. Craig, Software Engineer
Intelligent Computer Systems -  A Division of GBG
[hidden email]
[hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Inner Classes not supported!!! Any alternatives??? Help!!!

Guillaume Laforge
Administrator
You might probably use the SwingBuilder instead of the good old raw Swing code?

On 5/11/06, Clifton Craig <[hidden email]> wrote:

> On Thursday 11 May 2006 3:35 pm, Clifton Craig wrote:
> > I ran against my first Groovy stumbling block writing some swing code. Any
> > help is greatly appreciated. Consider the following non functional code:
> >
> > class Gui {
> >
> >     //Gui building logic...
> >
> >     class MyFilter extends FileFilter {
> >         String descr, extension
> >         public MyFilter(String descr, String extension) {this.descr =
> > descr; this.extension = extension;}
> >         boolean accept(File f) { return f.name.endsWith(extension) }
> >         String getDescription() { return descr }
> >     }
> >
> >     void selectFile()
> >     {
> >         def chooser = new JFileChooser();
> >         ['Text Files (*.txt)':'*.txt',
> >         'Properties files (*.properties)':'*.properties', 'All
> > Files':'*'].each {
> >             chooser.addChooseableFileFilter(new MyFilter(it.key, it.value))
> >         }
> >         //Cancel selected
> >         if(chooser.showDialog(frame)!=0) return
> >     }
> > }
> >
>
> I'm struglling with this more and more because as I code more Swing I see more
> and more places where I'd want an Anonymous inner class. How do others handle
> this?
>
> ---------------------------------------------------
> Clifton C. Craig, Software Engineer
> Intelligent Computer Systems - A Division of GBG
> [hidden email]
> [hidden email]
>


--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Inner Classes not supported!!! Any alternatives??? Help!!!

Cliff-3
On Thursday 11 May 2006 4:23 pm, Guillaume Laforge wrote:
> You might probably use the SwingBuilder instead of the good old raw Swing
> code?

That's my problem. I don't know enough about what's available in SwingBuilder
and how it works? What would my example look like with SwingBuilder? Could
you give an example? Where can I find good documentation on SwingBuilder?
FWIW, I'm learning SwingBuilder from outdated articles on the web and I just
learned about the secret widget methodfrom an O'Rielly article.

---------------------------------------------------
Clifton C. Craig, Software Engineer
Intelligent Computer Systems -  A Division of GBG
[hidden email]
[hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Inner Classes not supported!!! Any alternatives??? Help!!!

Guillaume Laforge
Administrator
On 5/11/06, Clifton Craig <[hidden email]> wrote:
> On Thursday 11 May 2006 4:23 pm, Guillaume Laforge wrote:
> > You might probably use the SwingBuilder instead of the good old raw Swing
> > code?
>
> That's my problem. I don't know enough about what's available in SwingBuilder
> and how it works? What would my example look like with SwingBuilder?

I don't have much time tonight to rewrite your code in SwingBuilder,
because I'm packing my stuff for JavaOne ;-)

>Could
> you give an example? Where can I find good documentation on SwingBuilder?

In the forthcoming book "Groovy in Action"!
And there's currently nothing on the wiki or almost nothing.

> FWIW, I'm learning SwingBuilder from outdated articles on the web and I just
> learned about the secret widget methodfrom an O'Rielly article.

The O'Reilly articles are a pretty good start at least.

--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Inner Classes not supported!!! Any alternatives??? Help!!!

Cliff-3
On Thursday 11 May 2006 4:40 pm, Guillaume Laforge wrote:
> I don't have much time tonight to rewrite your code in SwingBuilder,
> because I'm packing my stuff for JavaOne ;-)

That's ok. I'll wait and see if anyone else chimes in with an example and play
around with it myself to see what I can learn.

> In the forthcoming book "Groovy in Action"!
> And there's currently nothing on the wiki or almost nothing.
When is the book due? Can I pre-order a copy?


---------------------------------------------------
Clifton C. Craig, Software Engineer
Intelligent Computer Systems -  A Division of GBG
[hidden email]
[hidden email]
tog
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Inner Classes not supported!!! Any alternatives??? Help!!!

tog


On 5/11/06, Clifton Craig <[hidden email]> wrote:
On Thursday 11 May 2006 4:40 pm, Guillaume Laforge wrote:
> I don't have much time tonight to rewrite your code in SwingBuilder,
> because I'm packing my stuff for JavaOne ;-)

That's ok. I'll wait and see if anyone else chimes in with an example and play
around with it myself to see what I can learn.

> In the forthcoming book "Groovy in Action"!
> And there's currently nothing on the wiki or almost nothing.
When is the book due? Can I pre-order a copy?

Mid Summer, as far as I can remember. There is no pre-ordering possible at that time but I think that Dierk is doing something in that direction.

cheers
tog
---------------------------------------------------
Clifton C. Craig, Software Engineer
Intelligent Computer Systems - A Division of GBG
[hidden email]
[hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Inner Classes not supported!!! Any alternatives??? Help!!!

Edward Povazan
In reply to this post by Cliff-3
Hi Clifton,

When I have code that would normally be implemented as an inner class, I write a
wrapper that takes a closure. In many cases it is much cleaner. In this one,
well, it uses some Groovy magic:

class MyFilter extends FileFilter {
   String desc; Closure accept;
   MyFilter(desc, accept) {
     this.desc = desc; this.accept = accept
   }
   boolean accept(File f) { return accept.call(f) }
}

Then in the code:
accept = { extension, file -> file.name.endsWith(extension) }
map = ['Text Files (*.txt)':'*.txt',
  'Properties files (*.properties)':'*.properties',
  'All]
map.each { desc, extension ->
   chooser.addChooseableFileFilter(new MyFilter(desc, accept.curry(extension)))
}

And if I was really doing this a lot, I would create a Category to make it cleaner:
use(JFileChooserCategory) {
   map.each { desc, extension ->
     chooser.addChooseableFileFilter(desc, accept.curry(extension))
   }
}

Perhaps overkill for your current need, but I find that I create categories and
Closure wrappers in many places, making life more Groovy :)
Note that I have not tested the above, but it looks ok to me.

-Ed

Clifton Craig wrote:

> On Thursday 11 May 2006 3:35 pm, Clifton Craig wrote:
>> I ran against my first Groovy stumbling block writing some swing code. Any
>> help is greatly appreciated. Consider the following non functional code:
>>
>> class Gui {
>>
>>     //Gui building logic...
>>
>>     class MyFilter extends FileFilter {
>>         String descr, extension
>>         public MyFilter(String descr, String extension) {this.descr =
>> descr; this.extension = extension;}
>>         boolean accept(File f) { return f.name.endsWith(extension) }
>>         String getDescription() { return descr }
>>     }
>>
>>     void selectFile()
>>     {
>>         def chooser = new JFileChooser();
>>         ['Text Files (*.txt)':'*.txt',
>>         'Properties files (*.properties)':'*.properties', 'All
>> Files':'*'].each {
>>             chooser.addChooseableFileFilter(new MyFilter(it.key, it.value))
>>         }
>>         //Cancel selected
>>         if(chooser.showDialog(frame)!=0) return
>>     }
>> }
>>
>
> I'm struglling with this more and more because as I code more Swing I see more
> and more places where I'd want an Anonymous inner class. How do others handle
> this?
>
> ---------------------------------------------------
> Clifton C. Craig, Software Engineer
> Intelligent Computer Systems -  A Division of GBG
> [hidden email]
> [hidden email]
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Inner Classes not supported!!! Any alternatives??? Help!!!

Russel Winder
In reply to this post by Cliff-3
On Thu, 2006-05-11 at 16:21 -0400, Clifton Craig wrote:

> I'm struglling with this more and more because as I code more Swing I see more
> and more places where I'd want an Anonymous inner class. How do others handle
> this?

The idiom is to use closures to replace anonymous class instances.  This
works well if you are using SwingBuilder to create the UI in a Groovy
script.
--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Inner Classes not supported!!! Any alternatives??? Help!!!

Russel Winder
In reply to this post by Cliff-3
On Thu, 2006-05-11 at 16:34 -0400, Clifton Craig wrote:
> On Thursday 11 May 2006 4:23 pm, Guillaume Laforge wrote:
> > You might probably use the SwingBuilder instead of the good old raw Swing
> > code?
>
> That's my problem. I don't know enough about what's available in SwingBuilder
> and how it works? What would my example look like with SwingBuilder? Could
> you give an example? Where can I find good documentation on SwingBuilder?
> FWIW, I'm learning SwingBuilder from outdated articles on the web and I just
> learned about the secret widget methodfrom an O'Rielly article.

It is certainly the case that SwingBuilder is under documented.  Really
though the only thing that needs documenting is the names of the
initialization keys for the maps that set all the properties of the
widgets being created.  Everything else about SwingBuilder is just
Builder.

Any widget property you would set with a widget.set...(...) method call
in Java is trivially just an initialization map element in SwingBuilder
-- this is just the standard Groovy Beans property thing and using the
extra Map parameter to initialize all the properties.

The real trick that makes SwingBuilder great is that it defines a whole
load of other keys that then map to methods such as addActionListener
and add to do with the constraints.  These are the ones that need
doucmenting.  I found some of them by hassling on this list and reading
the source code for SwingBuilder.

If you send me a bit of Swing code by direct mail, I have a few minutes
today and could spend it doing the transform.  We could then use this to
start a Wiki page with a few examples on as well as the list of map
keys.

--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
12345
Loading...