Permalink
Browse files

fixed menu :content arrow. Added popup-menu

  • Loading branch information...
1 parent 9409cd7 commit 81d7abb499c8ef988a26f5dc88259062599feb2d @odyssomay committed Jun 9, 2011
Showing with 43 additions and 16 deletions.
  1. +28 −12 src/Hafni/swing/menu.clj
  2. +15 −4 src/Hafni/test/swing/menu.clj
View
@@ -1,13 +1,14 @@
(ns Hafni.swing.menu
(:use (Hafni utils)
- (Hafni.swing component))
+ (Hafni.swing component container view))
(:import (javax.swing JMenu JMenuBar
- JMenuItem JToolBar)))
+ JMenuItem JToolBar SwingConstants)))
-(defn- add-content [cont x]
- (dorun (map #(if (and (coll? %) (empty? %))
- (.addSeparator cont)
- (.add cont (component %))) x)))
+(defn- change-menu-content [m last_items new_items]
+ (change-container-content #(if (= %2 [])
+ (.insertSeparator m %1)
+ (.insert m (component %2) %1))
+ #(.remove m %) last_items new_items))
(defn menu
"Create a JMenu
@@ -16,19 +17,33 @@ Fields:
:mnemonic - mnemonic key (see docstring for Hafni.swing.action/action) | Char"
[title & options]
(let [m (JMenu.)
+ last_items (atom [])
arrs {:mnemonic #(.setMnemonic m (int %))
- :content (partial add-content m)}]
+ :content (partial change-menu-content m last_items)}]
(.setText m title)
- (init-comp m arrs nil (parse-options options))))
+ (init-comp m arrs {} (parse-options options))))
(defn menu-bar
"Create a JMenuBar
Fields:
:content - content of menu bar | [Component]"
[& options]
(let [m (JMenuBar.)
- arrs {:content (partial add-content m)}]
- (init-comp m arrs nil (parse-options options))))
+ last_items (atom [])
+ arrs {:content (partial change-menu-content m last_items)}]
+ (init-comp m arrs {} (parse-options options))))
+
+(defn popup-menu
+ "DOES NOT create a JPopupMenu
+Displays menu at the mousepointers position.
+Parent can be nil, but will in that case not
+close when an item is choosed."
+ [parent menu]
+ (let [pmenu (.getPopupMenu (component menu))
+ mpos (mouse-position)
+ x (first mpos)
+ y (second mpos)]
+ (.show pmenu (component parent) x y)))
(defn tool-bar
"Create a JToolBar
@@ -38,8 +53,9 @@ Fields:
:rollover - if true, shows a visual indicator under the cursor | Bool"
[& options]
(let [t (JToolBar.)
- arrs {:content (partial add-content t)
+ last_items (atom [])
+ arrs {:content (partial change-menu-content t last_items)
:floatable #(.setFloatable t %)
:rollover #(.setRollover t %)}]
- (init-comp t arrs nil (parse-options options))))
+ (init-comp t arrs {} (parse-options options))))
@@ -1,8 +1,19 @@
(ns Hafni.test.swing.menu
- (:use (Hafni.swing action menu view)))
+ (:use (Hafni arrow utils)
+ (Hafni.swing action component layout menu view)))
(defn menu-toolbar-test []
- (frame :menu_bar (menu-bar :content [(menu "menu 1" :content [(menu-item :name "item 1")
- (menu-item :name "item 2")])])
- :size 300 300 :dont_exit_on_close))
+ (let [m (menu "menu 1" :content [(menu-item :text "item 1") []
+ (menu-item :text "item 2")])]
+ (frame :menu_bar (menu-bar :content [m])
+ :content (flow-layout
+ :content [(comp-and-events (button :text "add")
+ :act (flow (output-arr m :content) >>>
+ (arr #(conj % (menu-item :text "new item!"))) >>>
+ (input-arr m :content)))
+ (comp-and-events (button :text "remove")
+ :act (flow (output-arr m :content) >>>
+ (arr butlast) >>>
+ (input-arr m :content)))])
+ :size 300 300 :dont_exit_on_close)))

0 comments on commit 81d7abb

Please sign in to comment.