Mercurial > cgi-bin > hgweb.cgi > ClipMan
annotate src/name/blackcap/clipman/Menus.kt @ 51:e8d2a7e6f6c7
So it builds under Windoze (at last!)
author | davidb |
---|---|
date | Tue, 14 Apr 2020 14:35:04 -0700 |
parents | bb80148e2cb3 |
children | 22725d4d7849 |
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() |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
31 "Help.About" -> showAboutDialog() |
27 | 32 else -> throw RuntimeException("unexpected actionCommand!") |
33 } | |
34 } | |
35 | |
36 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
|
37 val selected = Application.queue.getSelected() |
27 | 38 if (selected == null) { |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
39 JOptionPane.showMessageDialog(Application.frame, |
27 | 40 "No item selected.", |
41 "Error", | |
31 | 42 JOptionPane.ERROR_MESSAGE) |
43 } else { | |
27 | 44 block(selected) |
45 } | |
46 } | |
47 } | |
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 /** | |
27 | 73 * Our menu bar. What we display depends somewhat on the system type, as |
74 * the Mac gives us a gratuitous menu bar entry for handling some stuff. | |
75 */ | |
76 class MyMenuBar: JMenuBar() { | |
77 init { | |
78 if (OS.type != OS.MAC) { | |
79 add(JMenu("File").apply { | |
80 add(JMenuItem("Quit").apply { | |
81 actionCommand = "File.Quit" | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
82 addActionListener(Application.menuItemListener) |
27 | 83 makeShortcut(KeyEvent.VK_Q) |
84 }) | |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
85 add(JMenuItem("Preferences…").apply { |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
86 actionCommand = "File.Preferences" |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
87 addActionListener(Application.menuItemListener) |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
88 makeShortcut(KeyEvent.VK_COMMA) |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
89 }) |
27 | 90 }) |
91 } | |
92 add(JMenu("Edit").apply { | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
93 add(Application.anyRequired.add(JMenuItem("Clone").apply { |
27 | 94 setEnabled(false) |
95 actionCommand = "Edit.Clone" | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
96 addActionListener(Application.menuItemListener) |
27 | 97 makeShortcut(KeyEvent.VK_C) |
98 })) | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
99 add(Application.styledRequired.add(JMenuItem("Coerce…").apply { |
27 | 100 setEnabled(false) |
101 actionCommand = "Edit.Coerce" | |
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_K) |
104 })) | |
105 add(JMenuItem("Find…").apply { | |
106 actionCommand = "Edit.Find" | |
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_F) |
109 }) | |
110 add(JMenuItem("Find Again").apply { | |
111 actionCommand = "Edit.FindAgain" | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
112 addActionListener(Application.menuItemListener) |
27 | 113 makeShortcut(KeyEvent.VK_G) |
114 }) | |
115 }) | |
116 if (OS.type != OS.MAC) { | |
117 add(JMenu("Help").apply { | |
118 add(JMenuItem("About ClipMan…").apply { | |
119 actionCommand = "Help.About" | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
120 addActionListener(Application.menuItemListener) |
27 | 121 }) |
122 }) | |
123 } | |
124 } | |
125 | |
126 fun getMenu(name: String): JMenu? { | |
127 subElements.forEach { | |
128 val jmenu = it.component as? JMenu | |
129 if (jmenu?.text == name) { | |
130 return jmenu | |
131 } | |
132 } | |
133 return null | |
134 } | |
135 } | |
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 { | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
143 add(Application.anyRequired.add(JMenuItem("Clone").apply { |
27 | 144 actionCommand = "Edit.Clone" |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
145 addActionListener(Application.menuItemListener) |
27 | 146 })) |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
147 add(Application.styledRequired.add(JMenuItem("Coerce…").apply { |
27 | 148 actionCommand = "Edit.Coerce" |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
149 addActionListener(Application.menuItemListener) |
27 | 150 })) |
151 } | |
152 } | |
153 | |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
154 /** |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
155 * Show an About dialog. |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
156 */ |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
157 fun showAboutDialog() { |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
158 JOptionPane.showMessageDialog(Application.frame, |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
159 "ClipMan, a clipboard manager.\n" |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
160 + "© MMXX, David W. Barts", |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
161 "About ClipMan", |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
162 JOptionPane.PLAIN_MESSAGE) |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
163 } |