Mercurial > cgi-bin > hgweb.cgi > ClipMan
annotate src/name/blackcap/clipman/Menus.kt @ 30:0e88c6bed11e
Remove the troublesome delete command(s).
author | David Barts <n5jrn@me.com> |
---|---|
date | Wed, 29 Jan 2020 21:56:12 -0800 |
parents | f1fcc1281dad |
children | 0c6c18a733b7 |
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 | |
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) | |
30
0e88c6bed11e
Remove the troublesome delete command(s).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
20 "Edit.Clone" -> onlyIfSelected { PasteboardItem.write(it.contents) } |
27 | 21 "Edit.Coerce" -> onlyIfSelected { doCoerceFonts(it) } |
22 "Edit.Find" -> searchDialog.setVisible(true) | |
23 "Edit.FindAgain" -> searchDialog.find() | |
24 else -> throw RuntimeException("unexpected actionCommand!") | |
25 } | |
26 } | |
27 | |
28 private fun onlyIfSelected(block: (QueueItem) -> Unit) { | |
29 val selected = queue.v.getSelected() | |
30 if (selected == null) { | |
31 JOptionPane.showMessageDialog(frame.v, | |
32 "No item selected.", | |
33 "Error", | |
34 JOptionPane.ERROR_MESSAGE); | |
35 } else { | |
36 block(selected) | |
37 } | |
38 } | |
39 | |
40 private fun doCoerceFonts(item: QueueItem) { | |
41 /* not finished */ | |
42 } | |
43 } | |
44 | |
45 val menuItemListener = MenuItemListener() | |
46 | |
47 /** | |
48 * Our menu bar. What we display depends somewhat on the system type, as | |
49 * the Mac gives us a gratuitous menu bar entry for handling some stuff. | |
50 */ | |
51 class MyMenuBar: JMenuBar() { | |
52 init { | |
53 if (OS.type != OS.MAC) { | |
54 add(JMenu("File").apply { | |
55 add(JMenuItem("Quit").apply { | |
56 actionCommand = "File.Quit" | |
57 addActionListener(menuItemListener) | |
58 makeShortcut(KeyEvent.VK_Q) | |
59 }) | |
60 }) | |
61 } | |
62 add(JMenu("Edit").apply { | |
63 add(SelectionRequired.add(JMenuItem("Clone").apply { | |
64 setEnabled(false) | |
65 actionCommand = "Edit.Clone" | |
66 addActionListener(menuItemListener) | |
67 makeShortcut(KeyEvent.VK_C) | |
68 })) | |
69 add(SelectionRequired.add(JMenuItem("Coerce…").apply { | |
70 setEnabled(false) | |
71 actionCommand = "Edit.Coerce" | |
72 addActionListener(menuItemListener) | |
73 makeShortcut(KeyEvent.VK_K) | |
74 })) | |
75 add(JMenuItem("Find…").apply { | |
76 actionCommand = "Edit.Find" | |
77 addActionListener(menuItemListener) | |
78 makeShortcut(KeyEvent.VK_F) | |
79 }) | |
80 add(JMenuItem("Find Again").apply { | |
81 actionCommand = "Edit.FindAgain" | |
82 addActionListener(menuItemListener) | |
83 makeShortcut(KeyEvent.VK_G) | |
84 }) | |
85 }) | |
86 if (OS.type != OS.MAC) { | |
87 add(JMenu("Help").apply { | |
88 add(JMenuItem("About ClipMan…").apply { | |
89 actionCommand = "Help.About" | |
90 addActionListener(menuItemListener) | |
91 }) | |
92 }) | |
93 } | |
94 } | |
95 | |
96 fun getMenu(name: String): JMenu? { | |
97 subElements.forEach { | |
98 val jmenu = it.component as? JMenu | |
99 if (jmenu?.text == name) { | |
100 return jmenu | |
101 } | |
102 } | |
103 return null | |
104 } | |
105 } | |
106 | |
107 val menuBar = MyMenuBar() | |
108 | |
109 /** | |
110 * The popup menu we display when the user requests so atop a clipboard | |
111 * item. | |
112 */ | |
113 class MyPopupMenu: JPopupMenu() { | |
114 init { | |
115 add(SelectionRequired.add(JMenuItem("Clone").apply { | |
116 actionCommand = "Edit.Clone" | |
117 addActionListener(menuItemListener) | |
118 })) | |
119 add(SelectionRequired.add(JMenuItem("Coerce…").apply { | |
120 actionCommand = "Edit.Coerce" | |
121 addActionListener(menuItemListener) | |
122 })) | |
123 } | |
124 } | |
125 | |
126 val popupMenu = MyPopupMenu() | |
127 | |
128 /** | |
129 * Track menu items that require something to be selected in order | |
130 * to work, and allow them to be enabled and disabled en masse. | |
131 */ | |
132 object SelectionRequired { | |
133 private val cache = LinkedList<JMenuItem>() | |
134 | |
135 fun add(item: JMenuItem): JMenuItem { | |
136 cache.add(item) | |
137 return item | |
138 } | |
139 | |
140 fun enable() { | |
141 cache.forEach { | |
142 it.setEnabled(true) | |
143 } | |
144 } | |
145 | |
146 fun disable() { | |
147 cache.forEach { | |
148 it.setEnabled(false) | |
149 } | |
150 } | |
151 } |