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