This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: easier Kawa object building with Swing example


On Oct 1, 2010, at 11:47 AM, Per Bothner wrote:

For a while Kawa has supported:
(MyClass keyword1: value1 ... kewordN: valueN)
as shorthand for:
(let ((tmp (make MyClass)))
(set! tmp:keyword1 value1)
...
(set! tmp:keywordN valueN)
tmp)
where
(set! tmp:keywordI valueI)
could compile to either setting a field or calling a setKeywordI method.


Now, if there are extra non-keyword arguments, these get translated
to "add" calls.  These are useful for java.util Collections:
 (java.util.ArrayList 5 6 7)
They are also useful for java.awt.Container, including Swing's
JContainer, as shown in the example later.

Two more new features: First "SAM-conversion" is applied when a
lambda expression is provided in a context that requires a "SAM type",
which is an interface or abstract class with a single abstract method.
For example java.lang.Runnable or many XxxListener classes.
In that case an anonymous class is created.

Secondly, Kawa will also look for "addXxxYyy" methods if it sees
a keyword argument xxx-yyy:.

Putting it all together, we gt this demo:

(define-simple-class VBox (javax.swing.Box)
 ((*init*) (invoke-special javax.swing.Box (this) '*init* 1)))
(define-simple-class HBox (javax.swing.Box)
 ((*init*) (invoke-special javax.swing.Box (this) '*init* 0)))
(define-alias JFrame javax.swing.JFrame)
(define-alias JButton javax.swing.JButton)

(define value 0)

(define txt
 (javax.swing.JLabel
  text: "0"))

(define (set-value i)
 (set! value i)
 (set! txt:text (number->string i)))

(define fr
 (JFrame
    title: "Hello!"
    (VBox
     (javax.swing.Box:createGlue)
     txt
     (javax.swing.Box:createGlue)
     (HBox
      (JButton
	text: "Decrement"
	tool-tip-text: "decrement"
	action-listener: (lambda (e) (set-value (- value 1))))
      (javax.swing.Box:createGlue)
      (JButton
	text: "Increment"
	tool-tip-text: "increment"
	action-listener: (lambda (e) (set-value (+ value 1))))))))
(fr:setSize 200 100)
(set! fr:visible #t)

Very cool. I saw that you had an example like this in your JavaOne slides, but I wasn't sure if the features were really new or I had just never noticed them before. So presumably this is a generalization/ replacement of some of the functions in gnu/kawa/slib/gui.scm and swing.scm. This'll be very handy.


There's one addition I'd like to see, and that's a way to call addXxxYyy or setXxxYyy methods which don't take any arguments. Off the top of my head I can think of JMenu#addSeparator() and JGoodies Forms methods like ButtonBarBuilder2#addGlue() and PanelBuilder#setDefaultDialogBorder(). It wouldn't make sense for these to be keyword arguments, since they wouldn't be followed by an associated value. Perhaps if the argument is a symbol (e.g. 'separator, 'glue)?

Then we'd be able to do:
(JMenu text: "File"
       (JMenuItem text: "New")
       (JMenuItem text: "Open")
       'separator
       (JMenuItem text: "Close"))


The SAM-conversion also works if we explicitly call an addXxxListener method, right? So I could do something like:
(*:add-property-change-listener
my-java-bean
"Some Property Name"
(lambda (e :: java.beans.PropertyChangeEvent)
...))


Nice work,
Jamie

--
Jamison Hope
The PTR Group
www.theptrgroup.com




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]