Home / Blog / From Swing to Jwt

History always repeats itself and sometimes that's good. Here's a second chance to be part of it: tmdtc.com

From Swing to Jwt

The other day, I read the blog post Agents of Swing by Stuart Sierra describing the use of agents in combination with a Swing UI. As soon as I read it I wanted to implement it with Jwt in the hope it would be easy. And I wasn't disappointed!

As I had expected, I was able to reuse the agents code from Stuart and integrate it very easily in my web app. Actually, the development op the web interface was similar to the development of the swing interface. It's so similar I can only paste it here without further explanations:

; Create the Timer that will update results on the page 
; by calling the function passed
(defn create-timer [wapp fn-update-results]
  (let [timer (WTimer.)]
        (.setInterval timer 1000)
   (.. timer timeout (addListener wapp (create-listener [event] 
                                                                      (fn-update-results))))
   timer))

(defn make-flipper-app [env]
  (let [wapp (new WApplication env)
        root (.getRoot wapp)
        result-text (WText. "")
        flipper (new-flipper)
        fn-update-results (fn []
               (.setText result-text (
                   str "error: " (error @flipper) " after " 
                       (:total @flipper)  " flips, of which " 
                       (:heads @flipper) " where heads"  )))
        timer (create-timer wapp fn-update-results)
       ]
    (.setTitle wapp "Agents of Jwt")
    (.. (WPushButton. "Start counter" root) clicked
               (addListener  wapp ( create-listener [mouse-event] 
                   (send flipper start) 
                   (fn-update-results)  
                   (.start timer)  )))    
   (.. (WPushButton. "Stop counter" root) clicked 
              (addListener  wapp ( create-listener [mouse-event] 
                   (send flipper stop) 
                   (.stop timer) 
                   (fn-update-results)   )))
    (.addWidget  root (WBreak.))
    (.addWidget  root result-text)
    (.addWidget  root (WBreak.))
    wapp))


The only thing needed in addition to this is importing the necessary jwt classes, the definition of the servlet, and starting the server, the last two being done with this code:

(def my-servlet
  (proxy [WtServlet] [] 
    (createApplication [env]
      (make-flipper-app env))))


(def my-server (jetty-server))
(add-servlet my-server my-servlet "/*")

;(org.bitumenframework.jettify.clj_jettify/start-server 
       jwt-toss/my-server)


Here is the file with the complete code that I was able to run:

If someone's interested, I can possibly bundle a complete environment to be able to run it immediately. Just give me a sign.

Commentaires

%login_link_starAuthentifiez vous ou créez un nouveau compte utilisateur pour commenter.

Disponible pour intervention 

Si vous recherchez quelqu'un pour renforcer temporairement votre équipe ou si vous avez besoin d'aide pour que l'IT aide votre société, n'hésitez pas, contactez-moi !

Lun Mar Merc Jeu Ven Sam Dim
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31