Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/kotarak/seesaw into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
daveray committed Jul 6, 2011
2 parents cbaa313 + 0719ab9 commit 80d2ed4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 16 deletions.
35 changes: 35 additions & 0 deletions src/seesaw/examples/forms.clj
@@ -0,0 +1,35 @@
; Copyright (c) Dave Ray, Meikel Brandmeyer 2011. All rights reserved.

; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this
; distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.

(ns seesaw.examples.forms
(:use [seesaw [core :exclude (separator)] forms]))

; Example similar to miglayout

(defn frame-content
[]
(forms-panel
"pref,4dlu,80dlu,8dlu,pref,4dlu,80dlu"
:column-groups [[1 5]]
:items [(separator "General")
"Company" (span (text) 5)
"Contact" (span (text) 5)
(separator "Propeller")
"PTI/kW" (text :columns 10) "Power/kW" (text :columns 10)
"R/mm" (text :columns 10) "D/mm" (text :columns 10)]
:default-dialog-border? true))

(defn app []
(frame :title "jGoodies FormLayout Example"
:resizable? false
:content (frame-content)))

(defn -main [& args]
(invoke-later (-> (app) pack! show!)))
65 changes: 49 additions & 16 deletions src/seesaw/forms.clj
@@ -1,21 +1,30 @@
; Copyright (c) Dave Ray, Meikel Brandmeyer 2011. All rights reserved.

; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this
; distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.

(ns seesaw.forms
(:import
java.awt.Component
javax.swing.JPanel
com.jgoodies.forms.builder.DefaultFormBuilder
com.jgoodies.forms.layout.FormLayout)
(:require
seesaw.core)
(:use
[seesaw.util :only (apply-options)]))
[seesaw.util :only (apply-options ignore-options)]))

(defprotocol ComponentSpec
(append [this builder] "Add the given component to the form builder"))

(extend-protocol ComponentSpec
Component
Object
(append [this builder]
(.append builder this))
(.append builder (seesaw.core/to-widget this true)))
String
(append [this builder]
(.append builder this)))
Expand All @@ -28,13 +37,23 @@
(append [this builder]
(.append builder component column-span))))

(def
^{:doc "Continue with the next line in the builder."}
next-line
(reify
ComponentSpec
(append [this builder]
(.nextLine builder))))
(defn next-line
"Continue with the nth next line in the builder."
([] (next-line 1))
([n]
(reify
ComponentSpec
(append [this builder]
(.nextLine builder n)))))

(defn next-column
"Continue with the nth next column in the builder."
([] (next-column 1))
([n]
(reify
ComponentSpec
(append [this builder]
(.nextLine builder n)))))

(defn title
"Adds the given titel to the form."
Expand All @@ -55,7 +74,7 @@
(reify
ComponentSpec
(append [this builder]
(.appendSeparator builder this)))))
(.appendSeparator builder label)))))

(defn group
"Group the rows of the contained items into a row group."
Expand All @@ -67,6 +86,12 @@
(doseq [item items] (append item builder))
(.setRowGroupingEnabled builder false))))

(def ^{:private true} layout-options
{:column-groups #(.setColumnGroups %1 (into-array (map int-array %2)))})

(def ^{:private true} ignore-layout-options
(ignore-options layout-options))

(def ^{:private true} builder-options
{:items #(doseq [item %2] (append item %1))
:default-dialog-border? #(when %2 (.setDefaultDialogBorder %1))
Expand All @@ -75,14 +100,17 @@
:line-gap-size #(.setLineGapSize %1 %2)
:paragraph-gap-size #(.setParagraphGapSize %1 %2)})

(def ^{:private true} ignore-builder-options
(ignore-options builder-options))

(defn ^JPanel forms-panel
"Construct a panel with a FormLayout. The column spec is
expected to be a FormLayout column spec in string form.
The items are a list of strings, components or any of the
combinators. For example:
:items [\"Login\" (text) next-line
:items [\"Login\" (text) (next-line)
\"Password\" (span (text) 3)]
Takes the following special properties. They correspond
Expand All @@ -98,8 +126,13 @@
{:seesaw {:class `JPanel}}
[column-spec & opts]
(let [layout (FormLayout. column-spec "")
builder (DefaultFormBuilder. layout)]
(apply-options builder opts builder-options)
panel (#'seesaw.core/construct JPanel opts)
builder (DefaultFormBuilder. layout panel)]
(apply-options layout opts
(merge layout-options ignore-builder-options))
(apply-options builder opts
(merge builder-options ignore-layout-options))
(doto (.getPanel builder)
(apply-options opts (merge @#'seesaw.core/default-options
{:items (fn [x _] x)})))))
ignore-layout-options
ignore-builder-options)))))
10 changes: 10 additions & 0 deletions src/seesaw/util.clj
Expand Up @@ -168,3 +168,13 @@
children
root))

(defn ignore
"Might be used to explicitly ignore the default behaviour of options."
[x _]
x)

(defn ignore-options
"Create a ignore-map for options, which should be ignored. Ready to
be merged into default option maps."
[source-options]
(zipmap (keys source-options) (repeat ignore)))

0 comments on commit 80d2ed4

Please sign in to comment.