Mercurial > cgi-bin > hgweb.cgi > ClipMan
annotate src/name/blackcap/clipman/Menus.kt @ 60:066f05ebcc00
Fix a few more build bugs.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sun, 03 Apr 2022 16:08:55 -0700 |
parents | c6cccbe2f393 |
children | c56a0747c256 |
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 | |
49 | 11 import java.util.logging.Level |
12 import java.util.logging.Logger | |
27 | 13 import javax.swing.* |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
14 import kotlin.collections.HashSet |
27 | 15 |
16 /** | |
17 * Listen to actionEvents from both menu bar and popup menu selections. | |
18 */ | |
19 class MenuItemListener: ActionListener { | |
20 override fun actionPerformed(e: ActionEvent) { | |
21 when (e.actionCommand) { | |
49 | 22 "File.Quit" -> { |
23 LOGGER.log(Level.INFO, "execution complete") | |
24 System.exit(0) | |
25 } | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
26 "File.Preferences" -> Application.settingsDialog.setVisible(true) |
30
0e88c6bed11e
Remove the troublesome delete command(s).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
27 "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
|
28 "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
|
29 "Edit.Find" -> Application.searchDialog.setVisible(true) |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
30 "Edit.FindAgain" -> Application.searchDialog.find() |
56
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
49
diff
changeset
|
31 "Edit.Troff" -> onlyIfSelected { if (suitedForCoercing(it)) { troffize(it.contents) } } |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
32 "Help.About" -> showAboutDialog() |
27 | 33 else -> throw RuntimeException("unexpected actionCommand!") |
34 } | |
35 } | |
36 | |
37 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
|
38 val selected = Application.queue.getSelected() |
27 | 39 if (selected == null) { |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
40 JOptionPane.showMessageDialog(Application.frame, |
27 | 41 "No item selected.", |
42 "Error", | |
31 | 43 JOptionPane.ERROR_MESSAGE) |
44 } else { | |
27 | 45 block(selected) |
46 } | |
47 } | |
48 } | |
49 | |
50 /** | |
31 | 51 * Track menu items that require something to be selected in order |
52 * to work, and allow them to be enabled and disabled en masse. | |
53 */ | |
54 class SelectionRequired { | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
55 private val controls = HashSet<JMenuItem>() |
31 | 56 |
57 fun add(item: JMenuItem): JMenuItem { | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
58 controls.add(item) |
31 | 59 return item |
60 } | |
61 | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
62 private fun setEnabled(state: Boolean) { |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
63 controls.forEach { |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
64 it.setEnabled(state) |
31 | 65 } |
66 } | |
67 | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
68 fun enable() = setEnabled(true) |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
69 |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
70 fun disable() = setEnabled(false) |
31 | 71 } |
72 | |
73 /** | |
27 | 74 * Our menu bar. What we display depends somewhat on the system type, as |
75 * the Mac gives us a gratuitous menu bar entry for handling some stuff. | |
76 */ | |
77 class MyMenuBar: JMenuBar() { | |
78 init { | |
79 if (OS.type != OS.MAC) { | |
80 add(JMenu("File").apply { | |
81 add(JMenuItem("Quit").apply { | |
82 actionCommand = "File.Quit" | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
83 addActionListener(Application.menuItemListener) |
27 | 84 makeShortcut(KeyEvent.VK_Q) |
85 }) | |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
86 add(JMenuItem("Preferences…").apply { |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
87 actionCommand = "File.Preferences" |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
88 addActionListener(Application.menuItemListener) |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
89 makeShortcut(KeyEvent.VK_COMMA) |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
90 }) |
27 | 91 }) |
92 } | |
93 add(JMenu("Edit").apply { | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
94 add(Application.anyRequired.add(JMenuItem("Clone").apply { |
27 | 95 setEnabled(false) |
96 actionCommand = "Edit.Clone" | |
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_C) |
99 })) | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
100 add(Application.styledRequired.add(JMenuItem("Coerce…").apply { |
27 | 101 setEnabled(false) |
102 actionCommand = "Edit.Coerce" | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
103 addActionListener(Application.menuItemListener) |
27 | 104 makeShortcut(KeyEvent.VK_K) |
105 })) | |
56
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
49
diff
changeset
|
106 add(Application.styledRequired.add(JMenuItem("Convert to Troff").apply { |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
49
diff
changeset
|
107 setEnabled(false) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
49
diff
changeset
|
108 actionCommand = "Edit.Troff" |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
49
diff
changeset
|
109 addActionListener(Application.menuItemListener) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
49
diff
changeset
|
110 makeShortcut(KeyEvent.VK_T) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
49
diff
changeset
|
111 })) |
27 | 112 add(JMenuItem("Find…").apply { |
113 actionCommand = "Edit.Find" | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
114 addActionListener(Application.menuItemListener) |
27 | 115 makeShortcut(KeyEvent.VK_F) |
116 }) | |
117 add(JMenuItem("Find Again").apply { | |
118 actionCommand = "Edit.FindAgain" | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
119 addActionListener(Application.menuItemListener) |
27 | 120 makeShortcut(KeyEvent.VK_G) |
121 }) | |
122 }) | |
123 if (OS.type != OS.MAC) { | |
124 add(JMenu("Help").apply { | |
125 add(JMenuItem("About ClipMan…").apply { | |
126 actionCommand = "Help.About" | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
127 addActionListener(Application.menuItemListener) |
27 | 128 }) |
129 }) | |
130 } | |
131 } | |
132 | |
133 fun getMenu(name: String): JMenu? { | |
134 subElements.forEach { | |
135 val jmenu = it.component as? JMenu | |
136 if (jmenu?.text == name) { | |
137 return jmenu | |
138 } | |
139 } | |
140 return null | |
141 } | |
142 } | |
143 | |
144 /** | |
145 * The popup menu we display when the user requests so atop a clipboard | |
146 * item. | |
147 */ | |
148 class MyPopupMenu: JPopupMenu() { | |
149 init { | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
150 add(Application.anyRequired.add(JMenuItem("Clone").apply { |
27 | 151 actionCommand = "Edit.Clone" |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
152 addActionListener(Application.menuItemListener) |
27 | 153 })) |
57
c6cccbe2f393
Port to OpenJDK 15 (mostly done).
David Barts <n5jrn@me.com>
parents:
56
diff
changeset
|
154 add(Application.styledRequired.add(JMenuItem("Convert to Troff").apply { |
c6cccbe2f393
Port to OpenJDK 15 (mostly done).
David Barts <n5jrn@me.com>
parents:
56
diff
changeset
|
155 actionCommand = "Edit.Troff" |
c6cccbe2f393
Port to OpenJDK 15 (mostly done).
David Barts <n5jrn@me.com>
parents:
56
diff
changeset
|
156 addActionListener(Application.menuItemListener) |
c6cccbe2f393
Port to OpenJDK 15 (mostly done).
David Barts <n5jrn@me.com>
parents:
56
diff
changeset
|
157 })) |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
158 add(Application.styledRequired.add(JMenuItem("Coerce…").apply { |
27 | 159 actionCommand = "Edit.Coerce" |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
160 addActionListener(Application.menuItemListener) |
27 | 161 })) |
162 } | |
163 } | |
164 | |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
165 /** |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
166 * Show an About dialog. |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
167 */ |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
168 fun showAboutDialog() { |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
169 JOptionPane.showMessageDialog(Application.frame, |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
170 "ClipMan, a clipboard manager.\n" |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
171 + "© MMXX, David W. Barts", |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
172 "About ClipMan", |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
173 JOptionPane.PLAIN_MESSAGE) |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
174 } |