Permalink
Browse files

Merge master into clojure-1.3

  • Loading branch information...
1 parent 99b2a18 commit e4888ec76dd7d9e85a6c9ddf0e19b39d33c015de @stathissideris committed Dec 10, 2011
View
Oops, something went wrong.
View
@@ -5,7 +5,7 @@ Clojure GUI library, based on Swing. See the project wiki for details on how to
If you are using leiningen, add the following dependency to your
`project.clj`:
-`[clarity "0.5.0-SNAPSHOT"]`
+`[clarity "0.5.2"]`
Example of the form facilities:
@@ -15,6 +15,6 @@ Example of the form facilities:
[:text "Please fill in the **entire** form." :rich]
:first-name ""
:surname ""
- :gender ["male" "female"])
+ :gender ["male" "female" "other"])
["OK" "Cancel"])
`````
View
@@ -1,11 +1,20 @@
-(defproject clarity "0.5.0-SNAPSHOT"
+(defproject clarity "0.5.2"
:description "Clojure GUI library, based on Swing."
:dependencies [[org.clojure/clojure "1.3.0"]
[org.clojars.nakkaya/miglayout "3.7.3.1"]
+ :autodoc {:name "Clarity"
+ :page-title "Clarity API docs"
+ :web-src-dir "https://github.com/stathissideris/clarity/blob/"
+ :web-home "http://stathissideris.github.com/clarity/"
+ :output-path "autodoc"
+ :trim-prefix "clarity."
+ :copyright "Copyright 2011 by Stathis Sideris"
+ :load-except-list [#"test/" #"project\.clj" #"autodoc/" #"src/examples/"]}
+ :dependencies [[org.clojure/clojure "1.2.0"]
+ [org.clojure/clojure-contrib "1.2.0"]
+ [artem "0.5"]
[org.markdownj/markdownj "0.3.0-1.0.2b4"]]
:dev-dependencies [[lein-clojars "0.6.0"]
- #_[autodoc "0.7.1"
- :exclusions [org.clojure/clojure-contrib
- org.clojure/clojure]]]
+ [org.clojars.weavejester/autodoc "0.9.0"]]
:main clarity.core
:jvm-opts ["-Xdebug" "-Xrunjdwp:transport=dt_socket,server=y,suspend=n"])
@@ -18,9 +18,9 @@
(extend-type java.awt.Color
interpolatable
- (interpolate [start end d]
+ (interpolate [start end t]
(new java.awt.Color
(int (interpolate (.getRed start) (.getRed end) t))
(int (interpolate (.getGreen start) (.getGreen end) t))
(int (interpolate (.getBlue start) (.getBlue end) t))
- (int (interpolate (.getAlpha start) (.getAlpha end) t)))))
+ (int (interpolate (.getAlpha start) (.getAlpha end) t)))))
View
@@ -11,19 +11,26 @@
[clarity.util :as util]
[clojure.contrib.miglayout :as mig])
(:use [clarity.structure :only [$]])
- (:import [javax.swing UIManager JFrame ImageIcon]
+ (:import [javax.swing SwingUtilities UIManager JFrame ImageIcon]
+ [java.awt Frame MouseInfo]
[java.awt.image BufferedImage]))
(def *error-icon* (style/get-laf-property "OptionPane.errorIcon"))
(defn show-comp
"Show the passed component in a JFrame."
- [comp]
- (doto (JFrame.)
- (.add comp)
- (.pack)
- (.setVisible true)))
-
+ [comp & {on-top :on-top
+ :or {on-top false}}]
+ (if (nil? comp)
+ (throw (IllegalArgumentException.
+ "nil component passed to clarity.dev.show-comp"))
+ (c/do-component
+ (JFrame.)
+ (.add comp)
+ (.pack)
+ (:visible true)
+ (:always-on-top on-top))))
+
(defn- error-image [msg]
(let [i (BufferedImage. 600 300 BufferedImage/TYPE_INT_RGB)
g (.getGraphics i)
@@ -181,7 +188,7 @@
(update-indicator indicator)
(.setText time-label (str "Updated at: "
(str (java.util.Date.))))
- (.revalidate frame)
+ #_(.validate frame)
(.pack frame))))]
(c/do-component
spinner
@@ -211,7 +218,7 @@
(:on-window-closing
(send updater stop-action)))
(swing/do-swing (.toFront frame)
- (.replaint frame))
+ (.repaint frame))
(send updater start-action)
updater))
@@ -220,24 +227,61 @@
#_(defn f [] (c/make :panel
(.add
(form [:header "Personal info"]
- [:text "Make sure you enter all the info!!"]
+ [:header "Main" :level 3]
:first-name "Stathis"
:surname "Sideris"
- :address "122 essex road"
- :sex ["male" "female"]
+ :gender ["male" "female"]
+ [:header "Address" :level 3]
+ :line1 "50 Essex Road" [:label "Number and street"]
+ :line2 "" [:label "Line 2"]
+ :postcode ""
+ :city ""
+ :country ""
))))
#_(defn f2 [] (c/make :panel
(:layout (java.awt.FlowLayout.))
(.add (c/make :label "ddd"))
(.add (c/make :button "lalalala"))))
-#_(def p (watch-component 'f))
+#_(def p (watch-component #'f))
#_(def p (watch-component #(f)))
#_(def p (watch-component #($ (component-watcher-gui) :panel)))
#_(send p inject-fn f)
#_(send p start-action)
#_(send p stop-action)
+(defn all-frames
+ "Get all open frames."
+ []
+ (Frame/getFrames))
+
+(defn frame-titles
+ "Get the titles of all open frames."
+ []
+ (map #(.getTitle %) (all-frames)))
+
+(defn mouse-location
+ "Get the location of the mouse on the screen."
+ []
+ (.getLocation (MouseInfo/getPointerInfo)))
+
+(defn point-from-screen
+ "Convert a screen point to a point relative to the top-left corner
+ of the component."
+ [p component]
+ (let [p (mouse-location)]
+ (SwingUtilities/convertPointFromScreen p component)
+ p))
+
+(defn component-at-screen-point
+ "Returns the deepest component at the screen point p."
+ [p component]
+ (.findComponentAt component (point-from-screen p component)))
+(defn component-at-mouse
+ "Returns the deepest component at the position of the mouse,
+ contained within container."
+ [container]
+ (component-at-screen-point (mouse-location) container))
(defn dispose-all-frames
"Call .dispose on all frames."
View
@@ -3,7 +3,7 @@
:author "Stathis Sideris"}
clarity.form
(:require [clojure.string :as str]
- [artem.miglayout :as mig]
+ [clarity.layout :as layout]
[clarity.component :as c]
[clarity.widgets :as widgets]
[clarity.style :as style]
@@ -149,7 +149,7 @@
(defn make-form-panel [mig-params]
(let [panel
- (apply mig/miglayout (c/make :panel (:category :form))
+ (apply layout/mig (c/make :panel (:category :form))
:layout "wrap 2"
:column "[left][grow,fill]"
mig-params)]
View
@@ -1,6 +1,9 @@
-(ns clarity.layout)
+(ns clarity.layout
+ (:require [artem.miglayout :as artem]))
-(defmacro grid-bag-layout
+(def mig artem/miglayout)
+
+(defmacro grid-bag
"This macro was developed by Stuart Sierra and was first published
here: http://stuartsierra.com/2010/01/05/taming-the-gridbaglayout
View
@@ -2,14 +2,21 @@
(:require [clarity.component :as c])
(:import [javax.swing AbstractListModel]))
+(defprotocol CanProvideListModel
+ (^javax.swing.ListModel list-model [this]))
+
(defn immutable-list-model
"Constructs a list model of immutable data."
- [data]
+ [^clojure.lang.Indexed data]
(let [c (count data)]
(proxy [javax.swing.AbstractListModel] []
(getElementAt [index] (nth data index))
(getSize [] c))))
+(extend-type clojure.lang.Indexed
+ CanProvideListModel
+ (list-model [this] (immutable-list-model this)))
+
(defn mutable-list-model
"Constructs a list model that wraps the passed agent/atom/var/ref
reference. Also adds a watch to the data so that any changes cause
@@ -18,7 +25,7 @@
particular element being returned. This works OK for short lists,
but if the list will likely be large (or change very often), you
should consider writing a customised model."
- ([data]
+ ([^clojure.lang.ARef data]
(let [watch-key (gensym "list-watch-")
model (proxy [javax.swing.AbstractListModel] []
(getElementAt [index] (nth @data index))
@@ -27,7 +34,7 @@
(fn [k r old new]
(.fireContentsChanged model model 0 (dec (count new)))))
model))
- ([data fun]
+ ([^clojure.lang.ARef data fun]
(let [cache (atom (fun @data))
watch-key (gensym "list-watch-")
model (proxy [javax.swing.AbstractListModel] []
@@ -39,23 +46,38 @@
(.fireContentsChanged model model 0 (dec (count new)))))
model)))
+(extend-type clojure.lang.ARef
+ CanProvideListModel
+ (list-model [this] (mutable-list-model this)))
+
(defprotocol ListModel
(append [this item])
- (insert [this index item]))
+ (insert [this index item])
+ (delete [this index]))
-(defn list-model*
+(defn list-model-from-functions
[fmap]
(proxy [javax.swing.AbstractListModel
clarity.list.ListModel] []
(getElementAt [index] ((:get fmap) index))
(getSize [] ((:count fmap)))
(append [item]
- (do ((:add fmap) item)
- (let [i (dec (.getSize this))]
- (proxy-super fireIntervalAdded this i i))))
+ (when (:append fmap)
+ ((:append fmap) item)
+ (let [i (dec (.getSize this))]
+ (proxy-super fireIntervalAdded this i i))))
(insert [index item]
- (do ((:insert fmap) index item)
- (proxy-super fireIntervalAdded this index index)))))
+ (when (:insert fmap)
+ ((:insert fmap) index item)
+ (proxy-super fireIntervalAdded this index index)))
+ (delete [index item]
+ (when (:delete fmap)
+ ((:delete fmap) index item)
+ (proxy-super fireIntervalRemoved this index index)))))
+
+(extend-type clojure.lang.APersistentMap
+ CanProvideListModel
+ (list-model [this] (list-model-from-functions this)))
(defn list-model-seq
[^javax.swing.ListModel model]
@@ -81,33 +103,58 @@
(.getElementAt model (+ pointer index)))))))]
(lms model 0)))
-;;(defn list-model
+(defn- index-tuples [coll]
+ (partition 2 (interleave (range (count coll)) coll)))
(defn selections
[^javax.swing.JList lst]
(seq (.getSelectedValues lst)))
-(defn selection
+(defn first-selection
[^javax.swing.JList lst]
(.getSelectedValue lst))
(defn selected-indices
[^javax.swing.JList lst]
(seq (.getSelectedIndices lst)))
+(defn selected-tuples
+ [^javax.swing.JList lst]
+ (filter #(.isSelectedIndex lst (first %))
+ (index-tuples (list-model-seq (.getModel lst)))))
+
(defn selected-index
[^javax.swing.JList lst]
(.getSelectedIndex lst))
+(defn set-selected-index
+ [^javax.swing.JList lst index]
+ (.setSelectedIndex lst index))
+
+(defn set-selected-indices
+ [^javax.swing.JList lst indices]
+ (.setSelectedIndices lst (int-array indices)))
+
+(defn set-selected-value
+ ([^javax.swing.JList lst value] (set-selected-value lst value true))
+ ([^javax.swing.JList lst value scroll] (.setSelectedValue lst value scroll)))
+
+(defn select-by
+ [lst fn]
+ (let [model (list-model-seq (.getModel lst))]
+ (set-selected-indices
+ lst
+ (map first (filter #(fn (second %))
+ (index-tuples model))))))
+
(extend-type javax.swing.JList
c/HasValue
(value [this] (list-model-seq (.getModel this)))
(set-value [this value] (.setModel this value)))
-(let [data (atom [1 2 3 4 5 6 7 8])]
+(let [data (atom ["a" "b" "c" "d" "e" "f"])]
(def m
- (list-model*
+ (list-model
{:get (fn [index] (nth @data index))
- :add (fn [item] (swap! data conj item))
- :insert (fn [index item])
+ :append (fn [item] (swap! data conj item))
:count (fn [] (count @data))})))
View
@@ -22,10 +22,12 @@
(satisfies? c/HasValue %)) (.getComponents this)))))
(set-value [this value]
(let [components (comp-seq this)]
- (for [c components]
- (if (and (not (nil? (c/id c)))
- (satisfies? c/HasValue c)
- (contains? value (c/id c)))
+ (println "set-value called")
+ (println (count components))
+ (doseq [c components]
+ (when (and (not (nil? (c/id c)))
+ (satisfies? c/HasValue c)
+ (contains? value (c/id c)))
(c/set-value c (get value (c/id c))))))))
(defn find-by-id
Oops, something went wrong.

0 comments on commit e4888ec

Please sign in to comment.