Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Commit

Permalink
Swing: Added custom adders support and example. For #1
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrw committed May 15, 2011
1 parent 98c1b6e commit c00fa84
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
9 changes: 0 additions & 9 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
1. Add parent-style passing to gui-creator

(in gui-creator-args-dispatch check for instance?
guiftw.props.Property)

2. Swing: Add *adder support (Issue #1)

- Factor out default adder to default-adder fn

3. Add support for matching by class in class selectors.

4. Swing: Create default style sheet
Expand Down
33 changes: 33 additions & 0 deletions src/guiftw/examples/swing/custom_adders.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns guiftw.examples.swing.custom-adders
(:gen-class)
(:use (guiftw swing styles))
(:import (javax.swing JFrame JButton JLabel
JTabbedPane JScrollPane
JSplitPane)))

(def window
(swing [JFrame [:*id :main-window
:title "Custom Adders Demo"
:size ^unroll (500 400)
:visible true]
[JSplitPane [:resize-weight 0.5]
[JTabbedPane [:*lay JSplitPane/LEFT
:*adder (fn [parent parent-style child child-style]
(.addTab parent
(-> child-style :specials :*tab-title)
child))]
[JButton [:*tab-title "Tab title FTW!"
:text "This is a button."]]
[JLabel [:*tab-title "Second TAB"
:text "YEAH!"]]]
[JScrollPane [:*lay JSplitPane/RIGHT
:*adder (fn [parent parent-style child child-style]
(.setViewportView parent child))]
[JLabel [:text (str "<html><pre>"
(->> "Veeeeeeeery Looooooong Teeeeext"
(interleave (repeat "\n "))
(reduce str))
"</pre></html>")]]]]]))

(defn -main [& args]
(-> @(window) :root .validate))
22 changes: 13 additions & 9 deletions src/guiftw/swing.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
(:require (guiftw [tree :as tree]
[props :as props])))

(defn default-adder
[parent parent-style child child-style]
(let [layout-data (-> child-style :specials :*lay)]
(cond (and parent layout-data) (.add parent child layout-data)
parent (.add parent child))))

(defn swing-create
"Function that instantiates object in Swing-specific manner. Calls
ctor using optionally :*cons from style as parameters to create
object. Then calls parent.add(object) or parent.add(object,
layout_constraints) if :*lay is present in style. Parent can be nil
and then no adding happens. Returns created object."
[ctor style parent parent-style]
(let [specials (-> style props/get-value :specials)
obj (apply ctor (:*cons specials))
layout-data (:*lay specials)]
(cond (and parent layout-data) (.add parent obj layout-data)
parent (.add parent obj))
object. Then calls :*adder from parent-style or
default-adder. Parent can be nil and then no adding happens. Returns
created object." [ctor style parent parent-style]
(let [obj-specials (-> style props/get-value :specials)
obj (apply ctor (:*cons obj-specials))
adder (or (-> parent-style :specials :*adder)
default-adder)]
(adder parent parent-style obj style)
obj))

(defmacro swing
Expand Down

0 comments on commit c00fa84

Please sign in to comment.