Mercurial > cgi-bin > hgweb.cgi > ClipMan
annotate src/name/blackcap/clipman/Menus.kt @ 36:fcf82e3b7e31
Remove deadwood.
author | David Barts <n5jrn@me.com> |
---|---|
date | Thu, 30 Jan 2020 20:24:02 -0800 |
parents | 5f8475b37e23 |
children | 2a5808156f99 |
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>() |
31 | 63 |
64 fun add(item: JMenuItem): JMenuItem { | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
65 controls.add(item) |
31 | 66 return item |
67 } | |
68 | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
69 private fun setEnabled(state: Boolean) { |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
70 controls.forEach { |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
71 it.setEnabled(state) |
31 | 72 } |
73 } | |
74 | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
75 fun enable() = setEnabled(true) |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
76 |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
77 fun disable() = setEnabled(false) |
31 | 78 } |
79 | |
80 val anyRequired = SelectionRequired() | |
81 val styledRequired = SelectionRequired() | |
82 | |
83 /** | |
27 | 84 * Our menu bar. What we display depends somewhat on the system type, as |
85 * the Mac gives us a gratuitous menu bar entry for handling some stuff. | |
86 */ | |
87 class MyMenuBar: JMenuBar() { | |
88 init { | |
89 if (OS.type != OS.MAC) { | |
90 add(JMenu("File").apply { | |
91 add(JMenuItem("Quit").apply { | |
92 actionCommand = "File.Quit" | |
93 addActionListener(menuItemListener) | |
94 makeShortcut(KeyEvent.VK_Q) | |
95 }) | |
96 }) | |
97 } | |
98 add(JMenu("Edit").apply { | |
31 | 99 add(anyRequired.add(JMenuItem("Clone").apply { |
27 | 100 setEnabled(false) |
101 actionCommand = "Edit.Clone" | |
102 addActionListener(menuItemListener) | |
103 makeShortcut(KeyEvent.VK_C) | |
104 })) | |
31 | 105 add(styledRequired.add(JMenuItem("Coerce…").apply { |
27 | 106 setEnabled(false) |
107 actionCommand = "Edit.Coerce" | |
108 addActionListener(menuItemListener) | |
109 makeShortcut(KeyEvent.VK_K) | |
110 })) | |
111 add(JMenuItem("Find…").apply { | |
112 actionCommand = "Edit.Find" | |
113 addActionListener(menuItemListener) | |
114 makeShortcut(KeyEvent.VK_F) | |
115 }) | |
116 add(JMenuItem("Find Again").apply { | |
117 actionCommand = "Edit.FindAgain" | |
118 addActionListener(menuItemListener) | |
119 makeShortcut(KeyEvent.VK_G) | |
120 }) | |
121 }) | |
122 if (OS.type != OS.MAC) { | |
123 add(JMenu("Help").apply { | |
124 add(JMenuItem("About ClipMan…").apply { | |
125 actionCommand = "Help.About" | |
126 addActionListener(menuItemListener) | |
127 }) | |
128 }) | |
129 } | |
130 } | |
131 | |
132 fun getMenu(name: String): JMenu? { | |
133 subElements.forEach { | |
134 val jmenu = it.component as? JMenu | |
135 if (jmenu?.text == name) { | |
136 return jmenu | |
137 } | |
138 } | |
139 return null | |
140 } | |
141 } | |
142 | |
143 val menuBar = MyMenuBar() | |
144 | |
145 /** | |
146 * The popup menu we display when the user requests so atop a clipboard | |
147 * item. | |
148 */ | |
149 class MyPopupMenu: JPopupMenu() { | |
150 init { | |
31 | 151 add(anyRequired.add(JMenuItem("Clone").apply { |
27 | 152 actionCommand = "Edit.Clone" |
153 addActionListener(menuItemListener) | |
154 })) | |
31 | 155 add(styledRequired.add(JMenuItem("Coerce…").apply { |
27 | 156 actionCommand = "Edit.Coerce" |
157 addActionListener(menuItemListener) | |
158 })) | |
159 } | |
160 } | |
161 | |
162 val popupMenu = MyPopupMenu() |