comparison src/name/blackcap/clipman/Menus.kt @ 27:8aa2dfac27eb

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