Mercurial > cgi-bin > hgweb.cgi > ClipMan
annotate src/name/blackcap/clipman/Menus.kt @ 44:d14298ef8b0a
Fix some settings/preferences issues.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sun, 09 Feb 2020 22:13:26 -0700 |
parents | 33fbe3a78d84 |
children | 19d9da731c43 |
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) | |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
21 "File.Preferences" -> 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) } |
31 | 23 "Edit.Coerce" -> onlyIfSelected { coerceDialog.setVisible(true) } |
27 | 24 "Edit.Find" -> searchDialog.setVisible(true) |
25 "Edit.FindAgain" -> 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) { | |
32 val selected = queue.v.getSelected() | |
33 if (selected == null) { | |
34 JOptionPane.showMessageDialog(frame.v, | |
35 "No item selected.", | |
36 "Error", | |
31 | 37 JOptionPane.ERROR_MESSAGE) |
38 } else { | |
27 | 39 block(selected) |
40 } | |
41 } | |
42 } | |
43 | |
44 val menuItemListener = MenuItemListener() | |
45 | |
46 /** | |
31 | 47 * Track menu items that require something to be selected in order |
48 * to work, and allow them to be enabled and disabled en masse. | |
49 */ | |
50 class SelectionRequired { | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
51 private val controls = HashSet<JMenuItem>() |
31 | 52 |
53 fun add(item: JMenuItem): JMenuItem { | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
54 controls.add(item) |
31 | 55 return item |
56 } | |
57 | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
58 private fun setEnabled(state: Boolean) { |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
59 controls.forEach { |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
60 it.setEnabled(state) |
31 | 61 } |
62 } | |
63 | |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
64 fun enable() = setEnabled(true) |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
65 |
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
66 fun disable() = setEnabled(false) |
31 | 67 } |
68 | |
69 val anyRequired = SelectionRequired() | |
70 val styledRequired = SelectionRequired() | |
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" | |
82 addActionListener(menuItemListener) | |
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" |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
87 addActionListener(menuItemListener) |
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 { | |
31 | 93 add(anyRequired.add(JMenuItem("Clone").apply { |
27 | 94 setEnabled(false) |
95 actionCommand = "Edit.Clone" | |
96 addActionListener(menuItemListener) | |
97 makeShortcut(KeyEvent.VK_C) | |
98 })) | |
31 | 99 add(styledRequired.add(JMenuItem("Coerce…").apply { |
27 | 100 setEnabled(false) |
101 actionCommand = "Edit.Coerce" | |
102 addActionListener(menuItemListener) | |
103 makeShortcut(KeyEvent.VK_K) | |
104 })) | |
105 add(JMenuItem("Find…").apply { | |
106 actionCommand = "Edit.Find" | |
107 addActionListener(menuItemListener) | |
108 makeShortcut(KeyEvent.VK_F) | |
109 }) | |
110 add(JMenuItem("Find Again").apply { | |
111 actionCommand = "Edit.FindAgain" | |
112 addActionListener(menuItemListener) | |
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" | |
120 addActionListener(menuItemListener) | |
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 val menuBar = MyMenuBar() | |
138 | |
139 /** | |
140 * The popup menu we display when the user requests so atop a clipboard | |
141 * item. | |
142 */ | |
143 class MyPopupMenu: JPopupMenu() { | |
144 init { | |
31 | 145 add(anyRequired.add(JMenuItem("Clone").apply { |
27 | 146 actionCommand = "Edit.Clone" |
147 addActionListener(menuItemListener) | |
148 })) | |
31 | 149 add(styledRequired.add(JMenuItem("Coerce…").apply { |
27 | 150 actionCommand = "Edit.Coerce" |
151 addActionListener(menuItemListener) | |
152 })) | |
153 } | |
154 } | |
155 | |
156 val popupMenu = MyPopupMenu() | |
41
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
157 |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
158 /** |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
159 * Show an About dialog. |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
160 */ |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
161 fun showAboutDialog() { |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
162 JOptionPane.showMessageDialog(frame.v, |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
163 "ClipMan, a clipboard manager.\n" |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
164 + "© MMXX, David W. Barts", |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
165 "About ClipMan", |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
166 JOptionPane.PLAIN_MESSAGE) |
33fbe3a78d84
Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents:
39
diff
changeset
|
167 } |