Mercurial > cgi-bin > hgweb.cgi > ClipMan
annotate src/name/blackcap/clipman/Menus.kt @ 62:c56a0747c256 default tip
Add make plain feature.
author | David Barts <n5jrn@me.com> |
---|---|
date | Thu, 28 Apr 2022 20:53:39 -0700 |
parents | c6cccbe2f393 |
children |
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() |
62 | 31 "Edit.Plain" -> onlyIfSelected { if (suitedForCoercing(it)) { makePlain(it.contents) } } |
56
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
49
diff
changeset
|
32 "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
|
33 "Help.About" -> showAboutDialog() |
27 | 34 else -> throw RuntimeException("unexpected actionCommand!") |
35 } | |
36 } | |
37 | |
38 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
|
39 val selected = Application.queue.getSelected() |
27 | 40 if (selected == null) { |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
41 JOptionPane.showMessageDialog(Application.frame, |
27 | 42 "No item selected.", |
43 "Error", | |
31 | 44 JOptionPane.ERROR_MESSAGE) |
45 } else { | |
27 | 46 block(selected) |
47 } | |
48 } | |
49 } | |
50 | |
51 /** | |
31 | 52 * Track menu items that require something to be selected in order |
53 * to work, and allow them to be enabled and disabled en masse. | |
54 */ | |
55 class SelectionRequired { | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
56 private val controls = HashSet<JMenuItem>() |
31 | 57 |
58 fun add(item: JMenuItem): JMenuItem { | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
59 controls.add(item) |
31 | 60 return item |
61 } | |
62 | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
63 private fun setEnabled(state: Boolean) { |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
64 controls.forEach { |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
65 it.setEnabled(state) |
31 | 66 } |
67 } | |
68 | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
69 fun enable() = setEnabled(true) |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
70 |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
71 fun disable() = setEnabled(false) |
31 | 72 } |
73 | |
74 /** | |
27 | 75 * Our menu bar. What we display depends somewhat on the system type, as |
76 * the Mac gives us a gratuitous menu bar entry for handling some stuff. | |
77 */ | |
78 class MyMenuBar: JMenuBar() { | |
79 init { | |
80 if (OS.type != OS.MAC) { | |
81 add(JMenu("File").apply { | |
82 add(JMenuItem("Quit").apply { | |
83 actionCommand = "File.Quit" | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
84 addActionListener(Application.menuItemListener) |
27 | 85 makeShortcut(KeyEvent.VK_Q) |
86 }) | |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
87 add(JMenuItem("Preferences…").apply { |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
88 actionCommand = "File.Preferences" |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
89 addActionListener(Application.menuItemListener) |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
90 makeShortcut(KeyEvent.VK_COMMA) |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
91 }) |
27 | 92 }) |
93 } | |
94 add(JMenu("Edit").apply { | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
95 add(Application.anyRequired.add(JMenuItem("Clone").apply { |
27 | 96 setEnabled(false) |
97 actionCommand = "Edit.Clone" | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
98 addActionListener(Application.menuItemListener) |
27 | 99 makeShortcut(KeyEvent.VK_C) |
100 })) | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
101 add(Application.styledRequired.add(JMenuItem("Coerce…").apply { |
27 | 102 setEnabled(false) |
103 actionCommand = "Edit.Coerce" | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
104 addActionListener(Application.menuItemListener) |
27 | 105 makeShortcut(KeyEvent.VK_K) |
106 })) | |
56
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
49
diff
changeset
|
107 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
|
108 setEnabled(false) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
49
diff
changeset
|
109 actionCommand = "Edit.Troff" |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
49
diff
changeset
|
110 addActionListener(Application.menuItemListener) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
49
diff
changeset
|
111 makeShortcut(KeyEvent.VK_T) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
49
diff
changeset
|
112 })) |
62 | 113 add(Application.styledRequired.add(JMenuItem("Make Plain").apply { |
114 setEnabled(false) | |
115 actionCommand = "Edit.Plain" | |
116 addActionListener(Application.menuItemListener) | |
117 makeShortcut(KeyEvent.VK_P) | |
118 })) | |
27 | 119 add(JMenuItem("Find…").apply { |
120 actionCommand = "Edit.Find" | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
121 addActionListener(Application.menuItemListener) |
27 | 122 makeShortcut(KeyEvent.VK_F) |
123 }) | |
124 add(JMenuItem("Find Again").apply { | |
125 actionCommand = "Edit.FindAgain" | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
126 addActionListener(Application.menuItemListener) |
27 | 127 makeShortcut(KeyEvent.VK_G) |
128 }) | |
129 }) | |
130 if (OS.type != OS.MAC) { | |
131 add(JMenu("Help").apply { | |
132 add(JMenuItem("About ClipMan…").apply { | |
133 actionCommand = "Help.About" | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
134 addActionListener(Application.menuItemListener) |
27 | 135 }) |
136 }) | |
137 } | |
138 } | |
139 | |
140 fun getMenu(name: String): JMenu? { | |
141 subElements.forEach { | |
142 val jmenu = it.component as? JMenu | |
143 if (jmenu?.text == name) { | |
144 return jmenu | |
145 } | |
146 } | |
147 return null | |
148 } | |
149 } | |
150 | |
151 /** | |
152 * The popup menu we display when the user requests so atop a clipboard | |
153 * item. | |
154 */ | |
155 class MyPopupMenu: JPopupMenu() { | |
156 init { | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
157 add(Application.anyRequired.add(JMenuItem("Clone").apply { |
27 | 158 actionCommand = "Edit.Clone" |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
159 addActionListener(Application.menuItemListener) |
27 | 160 })) |
57
c6cccbe2f393
Port to OpenJDK 15 (mostly done).
David Barts <n5jrn@me.com>
parents:
56
diff
changeset
|
161 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
|
162 actionCommand = "Edit.Troff" |
c6cccbe2f393
Port to OpenJDK 15 (mostly done).
David Barts <n5jrn@me.com>
parents:
56
diff
changeset
|
163 addActionListener(Application.menuItemListener) |
c6cccbe2f393
Port to OpenJDK 15 (mostly done).
David Barts <n5jrn@me.com>
parents:
56
diff
changeset
|
164 })) |
62 | 165 add(Application.styledRequired.add(JMenuItem("Make Plain").apply { |
166 actionCommand = "Edit.Plain" | |
167 addActionListener(Application.menuItemListener) | |
168 })) | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
169 add(Application.styledRequired.add(JMenuItem("Coerce…").apply { |
27 | 170 actionCommand = "Edit.Coerce" |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
171 addActionListener(Application.menuItemListener) |
27 | 172 })) |
173 } | |
174 } | |
175 | |
62 | 176 fun makePlain(item: PasteboardItem): Unit { |
177 val plain = when (item) { | |
178 is PasteboardItem.Plain -> | |
179 item.plain | |
180 is PasteboardItem.HTML -> | |
181 item.plain | |
182 is PasteboardItem.RTF -> | |
183 item.plain | |
184 } | |
185 PasteboardItem.write(PasteboardItem.Plain(plain)) | |
186 } | |
187 | |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
188 /** |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
189 * Show an About dialog. |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
190 */ |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
191 fun showAboutDialog() { |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
192 JOptionPane.showMessageDialog(Application.frame, |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
193 "ClipMan, a clipboard manager.\n" |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
194 + "© MMXX, David W. Barts", |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
195 "About ClipMan", |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
196 JOptionPane.PLAIN_MESSAGE) |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
197 } |