Hello,

I've started dabbling into Swing & Clojure, and for fun decided to
translate one of the Sun examples (requires Java SE 1.6 though)

It's located here:
   
http://java.sun.com/docs/books/tutorial/uiswing/examples/learn/index.html#CelsiusConverter

and the java code here:
  
http://java.sun.com/docs/books/tutorial/uiswing/examples/learn/CelsiusConverterProject/src/learn/CelsiusConverterGUI.java

I've translated the above java code to clojure here:
  http://paste.lisp.org/display/79061


But I feel I did not do good job on it. The hard part was this
convention of creating object inline, and calling it's method to
further add elements/extend the system. It reminds of TurboVision from
the glory TurboPascal days:

      layout.setHorizontalGroup(
            layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
......................... // more code omitted
                .addContainerGap(27, Short.MAX_VALUE))
        );

I've did my best:

    (doto layout
      (.setHorizontalGroup
       (.. layout (createParallelGroup GroupLayout$Alignment/LEADING)
           (addGroup
            (.. layout (createSequentialGroup)
                (addContainerGap)
                (addGroup
                 (.. layout (createParallelGroup GroupLayout$Alignment/LEADING)
                     (addGroup
................ ;; more code omitted
                          (addComponent    fahrenheit-label)))))
                (addContainerGap 27 Short/MAX_VALUE)))))

But I feel I can do it better (some fancy macro?). Any ideas how I can
improve it.

I understand from the Java comments that the layout code was
autogenerated by a tool, but still it's an interresting question.

Granted the ".." sugary macro helped a lot, but my brain melted a bit
while I was translating it :)




Here is the full code, in case the paste.lisp.org doesn't work:

(import '(javax.swing JFrame JLabel JTextField JButton GroupLayout
GroupLayout$Alignment SwingConstants LayoutStyle LayoutStyle
$ComponentPlacement)
        '(java.awt.event ActionListener)
        '(java.awt Component))

(defn celsius-converter-gui
  "Translation of the simple Celsius Converter GUI Swing Example from
Java to Clojure (requires Java SE 1.6)
   
http://java.sun.com/docs/books/tutorial/uiswing/examples/learn/CelsiusConverterProject/src/learn/CelsiusConverterGUI.java";
  []
  (let [frame            (new JFrame "Celsius Converter")
        pane             (. frame getContentPane)
        layout           (new GroupLayout pane)
        temp-text-field  (new JTextField)
        celsius-label    (new JLabel "Celsius")
        convert-button   (new JButton "Convert")
        fahrenheit-label (new JLabel "Fahrenheit")]
    (. convert-button addActionListener
       (proxy [ActionListener] []
         (actionPerformed [event]
                          (. fahrenheit-label setText
                             (str (+ 32.0
                                     (* 1.8
                                        (Double/parseDouble
                                         (. temp-text-field getText))))
                                  " Fahrenheit")))))
    (doto layout
      (.setHorizontalGroup
       (.. layout (createParallelGroup GroupLayout$Alignment/LEADING)
           (addGroup
            (.. layout (createSequentialGroup)
                (addContainerGap)
                (addGroup
                 (.. layout (createParallelGroup GroupLayout$Alignment/LEADING)
                     (addGroup
                      (.. layout (createSequentialGroup)
                          (addComponent    temp-text-field
                                           GroupLayout/PREFERRED_SIZE
                                           GroupLayout/DEFAULT_SIZE
                                           GroupLayout/PREFERRED_SIZE)
                          (addPreferredGap 
LayoutStyle$ComponentPlacement/RELATED)
                          (addComponent    celsius-label)))
                     (addGroup
                      (.. layout (createSequentialGroup)
                          (addComponent    convert-button)
                          (addPreferredGap 
LayoutStyle$ComponentPlacement/RELATED)
                          (addComponent    fahrenheit-label)))))
                (addContainerGap 27 Short/MAX_VALUE)))))
      (.linkSize SwingConstants/HORIZONTAL
                 (into-array Component [convert-button temp-text-field]))
      (.setVerticalGroup
       (.. layout (createParallelGroup GroupLayout$Alignment/LEADING)
           (addGroup
            (.. layout (createSequentialGroup)
                (addContainerGap)
                (addGroup
                 (.. layout (createParallelGroup GroupLayout$Alignment/BASELINE)
                     (addComponent temp-text-field
                                   GroupLayout/PREFERRED_SIZE
                                   GroupLayout/DEFAULT_SIZE
                                   GroupLayout/PREFERRED_SIZE)
                     (addComponent celsius-label)))
                (addPreferredGap   LayoutStyle$ComponentPlacement/RELATED)
                (addGroup
                 (.. layout (createParallelGroup GroupLayout$Alignment/BASELINE)
                     (addComponent convert-button)
                     (addComponent fahrenheit-label)))
                (addContainerGap 21 Short/MAX_VALUE))))))
    (. pane setLayout layout)
    (doto frame
      (.pack)
      (.setVisible true))))

Thanks,
Dimiter "malkia" Stanev
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to