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