Mercurial > cgi-bin > hgweb.cgi > ClipMan
annotate src/name/blackcap/clipman/Menus.kt @ 35:5f8475b37e23
Got it correctly enabling and disabling menu items.
author | David Barts <n5jrn@me.com> |
---|---|
date | Thu, 30 Jan 2020 20:21:42 -0800 |
parents | 0c6c18a733b7 |
children | fcf82e3b7e31 |
rev | line source |
---|---|
27 | 1 /* |
2 * Menu-related stuff, pertaining to both the menu bar and popup menus. | |
3 */ | |
4 package name.blackcap.clipman | |
5 | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
6 import java.awt.Container |
27 | 7 import java.awt.Toolkit |
8 import java.awt.event.ActionEvent | |
9 import java.awt.event.ActionListener | |
10 import java.awt.event.KeyEvent | |
11 import javax.swing.* | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
12 import kotlin.collections.HashSet |
27 | 13 |
14 /** | |
15 * Listen to actionEvents from both menu bar and popup menu selections. | |
16 */ | |
17 class MenuItemListener: ActionListener { | |
18 override fun actionPerformed(e: ActionEvent) { | |
19 when (e.actionCommand) { | |
20 "File.Quit" -> System.exit(0) | |
30
0e88c6bed11e
Remove the troublesome delete command(s).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
21 "Edit.Clone" -> onlyIfSelected { PasteboardItem.write(it.contents) } |
31 | 22 "Edit.Coerce" -> onlyIfSelected { coerceDialog.setVisible(true) } |
27 | 23 "Edit.Find" -> searchDialog.setVisible(true) |
24 "Edit.FindAgain" -> searchDialog.find() | |
25 else -> throw RuntimeException("unexpected actionCommand!") | |
26 } | |
27 } | |
28 | |
29 private fun onlyIfSelected(block: (QueueItem) -> Unit) { | |
30 val selected = queue.v.getSelected() | |
31 if (selected == null) { | |
32 JOptionPane.showMessageDialog(frame.v, | |
33 "No item selected.", | |
34 "Error", | |
31 | 35 JOptionPane.ERROR_MESSAGE) |
36 } else { | |
27 | 37 block(selected) |
38 } | |
39 } | |
40 | |
31 | 41 private fun clone(contents: PasteboardItem) { |
42 val (plain, html) = when(contents) { | |
43 is PasteboardItem.Plain -> Pair(contents.plain, null) | |
44 is PasteboardItem.HTML -> Pair(contents.plain, contents.html) | |
45 is PasteboardItem.RTF -> Pair(contents.plain, contents.html) | |
46 } | |
47 if (html == null) { | |
48 PasteboardItem.write(PasteboardItem.Plain(plain)) | |
49 } else { | |
50 PasteboardItem.write(PasteboardItem.HTML(plain, scrub(html))) | |
51 } | |
27 | 52 } |
53 } | |
54 | |
55 val menuItemListener = MenuItemListener() | |
56 | |
57 /** | |
31 | 58 * Track menu items that require something to be selected in order |
59 * to work, and allow them to be enabled and disabled en masse. | |
60 */ | |
61 class SelectionRequired { | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
62 private val controls = HashSet<JMenuItem>() |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
63 /* private val parents = HashSet<Container>() */ |
31 | 64 |
65 fun add(item: JMenuItem): JMenuItem { | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
66 controls.add(item) |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
67 /* var p = item.parent |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
68 if (p != null) { |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
69 parents.add(p) |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
70 } */ |
31 | 71 return item |
72 } | |
73 | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
74 private fun setEnabled(state: Boolean) { |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
75 controls.forEach { |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
76 it.setEnabled(state) |
31 | 77 } |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
78 /* parents.forEach { |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
79 it.validate() |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
80 } */ |
31 | 81 } |
82 | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
83 fun enable() = setEnabled(true) |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
84 |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
85 fun disable() = setEnabled(false) |
31 | 86 } |
87 | |
88 val anyRequired = SelectionRequired() | |
89 val styledRequired = SelectionRequired() | |
90 | |
91 /** | |
27 | 92 * Our menu bar. What we display depends somewhat on the system type, as |
93 * the Mac gives us a gratuitous menu bar entry for handling some stuff. | |
94 */ | |
95 class MyMenuBar: JMenuBar() { | |
96 init { | |
97 if (OS.type != OS.MAC) { | |
98 add(JMenu("File").apply { | |
99 add(JMenuItem("Quit").apply { | |
100 actionCommand = "File.Quit" | |
101 addActionListener(menuItemListener) | |
102 makeShortcut(KeyEvent.VK_Q) | |
103 }) | |
104 }) | |
105 } | |
106 add(JMenu("Edit").apply { | |
31 | 107 add(anyRequired.add(JMenuItem("Clone").apply { |
27 | 108 setEnabled(false) |
109 actionCommand = "Edit.Clone" | |
110 addActionListener(menuItemListener) | |
111 makeShortcut(KeyEvent.VK_C) | |
112 })) | |
31 | 113 add(styledRequired.add(JMenuItem("Coerce…").apply { |
27 | 114 setEnabled(false) |
115 actionCommand = "Edit.Coerce" | |
116 addActionListener(menuItemListener) | |
117 makeShortcut(KeyEvent.VK_K) | |
118 })) | |
119 add(JMenuItem("Find…").apply { | |
120 actionCommand = "Edit.Find" | |
121 addActionListener(menuItemListener) | |
122 makeShortcut(KeyEvent.VK_F) | |
123 }) | |
124 add(JMenuItem("Find Again").apply { | |
125 actionCommand = "Edit.FindAgain" | |
126 addActionListener(menuItemListener) | |
127 makeShortcut(KeyEvent.VK_G) | |
128 }) | |
129 }) | |
130 if (OS.type != OS.MAC) { | |
131 add(JMenu("Help").apply { | |
132 add(JMenuItem("About ClipMan…").apply { | |
133 actionCommand = "Help.About" | |
134 addActionListener(menuItemListener) | |
135 }) | |
136 }) | |
137 } | |
138 } | |
139 | |
140 fun getMenu(name: String): JMenu? { | |
141 subElements.forEach { | |
142 val jmenu = it.component as? JMenu | |
143 if (jmenu?.text == name) { | |
144 return jmenu | |
145 } | |
146 } | |
147 return null | |
148 } | |
149 } | |
150 | |
151 val menuBar = MyMenuBar() | |
152 | |
153 /** | |
154 * The popup menu we display when the user requests so atop a clipboard | |
155 * item. | |
156 */ | |
157 class MyPopupMenu: JPopupMenu() { | |
158 init { | |
31 | 159 add(anyRequired.add(JMenuItem("Clone").apply { |
27 | 160 actionCommand = "Edit.Clone" |
161 addActionListener(menuItemListener) | |
162 })) | |
31 | 163 add(styledRequired.add(JMenuItem("Coerce…").apply { |
27 | 164 actionCommand = "Edit.Coerce" |
165 addActionListener(menuItemListener) | |
166 })) | |
167 } | |
168 } | |
169 | |
170 val popupMenu = MyPopupMenu() |