view src/net/kryshen/indyvon/component.clj @ 85:e718a69f7d99

Observers: use weak keys, renamed some fns.
author Mikhail Kryshen <mikhail@kryshen.net>
date Fri, 01 Oct 2010 18:45:17 +0400
parents 5d2153e8a28d
children dd7b8dbb20bc
line source
1 ;;
2 ;; Copyright 2010 Mikhail Kryshen <mikhail@kryshen.net>
3 ;;
4 ;; This file is part of Indyvon.
5 ;;
6 ;; Indyvon is free software: you can redistribute it and/or modify it
7 ;; under the terms of the GNU Lesser General Public License version 3
8 ;; only, as published by the Free Software Foundation.
9 ;;
10 ;; Indyvon is distributed in the hope that it will be useful, but
11 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with Indyvon. If not, see
17 ;; <http://www.gnu.org/licenses/>.
18 ;;
20 (ns net.kryshen.indyvon.component
21 "Integrating Indyvon into AWT and Swing components."
22 (:use
23 net.kryshen.indyvon.core)
24 (:import
25 (net.kryshen.indyvon.core Size Bounds)
26 (java.awt Graphics2D Component Dimension Color)
27 (java.awt.geom Rectangle2D$Double)
28 (javax.swing JFrame JPanel)))
30 (defn- font-context [^Component component]
31 (.getFontRenderContext (.getFontMetrics component (.getFont component))))
33 (defn make-jpanel
34 ([layer]
35 (make-jpanel layer (root-event-dispatcher)))
36 ([layer event-dispatcher]
37 (let [panel
38 (proxy [JPanel] []
39 (paintComponent [g]
40 (let [size (.getSize ^Component this)]
41 (draw-root! layer g (.width size) (.height size)
42 event-dispatcher this)))
43 (getPreferredSize []
44 (let [s (root-size layer (font-context this) this)]
45 (Dimension. (:width s) (:height s)))))]
46 (.setBackground panel (:back-color *theme*))
47 (add-observer panel layer (fn [w _]
48 ;; Use the first observer argument
49 ;; instead of closing over panel to
50 ;; allow the panel and associated
51 ;; observer to be gc'd.
52 (.repaint ^Component w)))
53 (listen! event-dispatcher panel)
54 panel)))
56 (defn make-jframe [title layer]
57 (doto (JFrame. title)
58 (.. (getContentPane) (add (make-jpanel layer)))
59 (.pack)))