Archive

Archive for the ‘open source’ Category

20 Minute File Explorer with Seesaw

April 30th, 2011 3 comments

Seesaw, my Clojure/Swing experiment has been making a lot of progress. Earlier today I added basic support for JTree which is kind of the last major Swing component that had no support in Seesaw. As an test, I thought I’d try making a file explorer with the directory tree on the left and file list on the right. It took about 20 minutes and was way less tedious than the equivalent raw Swing code you’d write in Java or Clojure.

The full code for the example can be found on github.

Here’s what it ended up looking like:

Code Highlights

There are three code highlights.

Set up a model for the tree using the same pattern as (cloure.core/tree-seq):

; Make a model for the directory tree
(def tree-model
  (simple-tree-model
    #(.isDirectory %)
    (fn [f] (filter #(.isDirectory %) (.listFiles f)))
    (File. ".")))

Set up the structure of the frame. I think this reads pretty nicely:

  (frame :title "File Explorer" :width 500 :height 500 :pack? false :content
    (border-panel :border 5 :hgap 5 :vgap 5
      :north (label :id :current-dir :text "Location")

      :center (left-right-split
                (scrollable (tree :id :tree :model tree-model :renderer render-file-item))
                (scrollable (listbox :id :list :renderer render-file-item)))

      :south (label :id :status :text "Ready")))

Set up selection event handling:

  (listen (select [:#tree]) :selection
    (fn [e]
      (if-let [dir (-> (selection e) first last)]
        (let [files (.listFiles dir)]
          (config (select [:#current-dir]) :text (.getAbsolutePath dir))
          (config (select [:#status]) :text (format "Ready (%d items)" (count files)))
          (config (select [:#list]) :model files))))))

Here, the widgets are being retrieved by id with the (select) function. Alternatively, I could have just bound them to variables and used them directly. I’m still thinking about how this should work.

Conclusion

This is obviously still a toy (no sorting, doesn’t update, etc, etc), but I think shows promise. I like the separation of the structure and behavior, but the selector stuff needs more thought. Probably the biggest gain over raw Swing is that Seesaw takes care of all the yucky reify and proxy stuff for you. In this example alone, a raw implementation would need two (reify)s and a (proxy) call. Building up the widget structure is also pretty tedious with raw Swing.

I’d love to to hear feedback and criticism of the API since this is my first attempt at real Clojure code. Thanks!

Can anything I write here help open source?

November 23rd, 2009 1 comment

I spend a lot of my time working with open source software and I like to think that I’ve picked up a number of the tricks, gotchas, and other secrets of these systems.  So, whenever I think of posting to this blog, it usually along the lines of “man, I just spent a day figuring out X. I should document that for the next poor sap that comes along.” Invariably, I don’t. Occasionally this is due to time constraints, but more often it’s because there’s already a wiki or something for whatever I’m dealing with and I believe that keeping related information in one place is a good idea. So, I don’t write it here, or there.

Unfortunately, writing open source documentation isn’t as fun as coding or as gratifying as blog traffic* so there’s a pretty good chance the open source knowledge diaspora will continue. Important, headache preventing, technical information will remain in change logs, mailing lists, and random blog posts while user guides and other documentation will be ignored or buried**.

Stack Overflow is helping a bit, but it would be nice if some of the best answers there were pulled back into (or at least linked to from) the how-tos and getting started guides of the projects they concern.

I, for my part, am going to start writing this stuff down. Where it belongs.

* yes, I recognize the irony here.
** many open source projects could do a hell of a lot better job at making documentation easier to find.

Categories: open source Tags: