Mercurial > cgi-bin > hgweb.cgi > ClipMan
annotate src/name/blackcap/clipman/Menus.kt @ 34:376643a09b52
Finally fixed the alignment. Wotta PITA...
author | David Barts <n5jrn@me.com> |
---|---|
date | Thu, 30 Jan 2020 19:38:59 -0800 |
parents | 0c6c18a733b7 |
children | 5f8475b37e23 |
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 | |
6 import java.awt.Toolkit | |
7 import java.awt.event.ActionEvent | |
8 import java.awt.event.ActionListener | |
9 import java.awt.event.KeyEvent | |
10 import java.util.LinkedList | |
11 import javax.swing.* | |
12 | |
13 /** | |
14 * Listen to actionEvents from both menu bar and popup menu selections. | |
15 */ | |
16 class MenuItemListener: ActionListener { | |
17 override fun actionPerformed(e: ActionEvent) { | |
18 when (e.actionCommand) { | |
19 "File.Quit" -> System.exit(0) | |
30
0e88c6bed11e
Remove the troublesome delete command(s).
David Barts <n5jrn@me.com>
parents:
28
diff
changeset
|
20 "Edit.Clone" -> onlyIfSelected { PasteboardItem.write(it.contents) } |
31 | 21 "Edit.Coerce" -> onlyIfSelected { coerceDialog.setVisible(true) } |
27 | 22 "Edit.Find" -> searchDialog.setVisible(true) |
23 "Edit.FindAgain" -> searchDialog.find() | |
24 else -> throw RuntimeException("unexpected actionCommand!") | |
25 } | |
26 } | |
27 | |
28 private fun onlyIfSelected(block: (QueueItem) -> Unit) { | |
29 val selected = queue.v.getSelected() | |
30 if (selected == null) { | |
31 JOptionPane.showMessageDialog(frame.v, | |
32 "No item selected.", | |
33 "Error", | |
31 | 34 JOptionPane.ERROR_MESSAGE) |
35 } else { | |
27 | 36 block(selected) |
37 } | |
38 } | |
39 | |
31 | 40 private fun clone(contents: PasteboardItem) { |
41 val (plain, html) = when(contents) { | |
42 is PasteboardItem.Plain -> Pair(contents.plain, null) | |
43 is PasteboardItem.HTML -> Pair(contents.plain, contents.html) | |
44 is PasteboardItem.RTF -> Pair(contents.plain, contents.html) | |
45 } | |
46 if (html == null) { | |
47 PasteboardItem.write(PasteboardItem.Plain(plain)) | |
48 } else { | |
49 PasteboardItem.write(PasteboardItem.HTML(plain, scrub(html))) | |
50 } | |
27 | 51 } |
52 } | |
53 | |
54 val menuItemListener = MenuItemListener() | |
55 | |
56 /** | |
31 | 57 * Track menu items that require something to be selected in order |
58 * to work, and allow them to be enabled and disabled en masse. | |
59 */ | |
60 class SelectionRequired { | |
61 private val cache = LinkedList<JMenuItem>() | |
62 | |
63 fun add(item: JMenuItem): JMenuItem { | |
64 cache.add(item) | |
65 return item | |
66 } | |
67 | |
68 fun enable() { | |
69 cache.forEach { | |
70 it.setEnabled(true) | |
71 } | |
72 } | |
73 | |
74 fun disable() { | |
75 cache.forEach { | |
76 it.setEnabled(false) | |
77 } | |
78 } | |
79 } | |
80 | |
81 val anyRequired = SelectionRequired() | |
82 val styledRequired = SelectionRequired() | |
83 | |
84 /** | |
27 | 85 * Our menu bar. What we display depends somewhat on the system type, as |
86 * the Mac gives us a gratuitous menu bar entry for handling some stuff. | |
87 */ | |
88 class MyMenuBar: JMenuBar() { | |
89 init { | |
90 if (OS.type != OS.MAC) { | |
91 add(JMenu("File").apply { | |
92 add(JMenuItem("Quit").apply { | |
93 actionCommand = "File.Quit" | |
94 addActionListener(menuItemListener) | |
95 makeShortcut(KeyEvent.VK_Q) | |
96 }) | |
97 }) | |
98 } | |
99 add(JMenu("Edit").apply { | |
31 | 100 add(anyRequired.add(JMenuItem("Clone").apply { |
27 | 101 setEnabled(false) |
102 actionCommand = "Edit.Clone" | |
103 addActionListener(menuItemListener) | |
104 makeShortcut(KeyEvent.VK_C) | |
105 })) | |
31 | 106 add(styledRequired.add(JMenuItem("Coerce…").apply { |
27 | 107 setEnabled(false) |
108 actionCommand = "Edit.Coerce" | |
109 addActionListener(menuItemListener) | |
110 makeShortcut(KeyEvent.VK_K) | |
111 })) | |
112 add(JMenuItem("Find…").apply { | |
113 actionCommand = "Edit.Find" | |
114 addActionListener(menuItemListener) | |
115 makeShortcut(KeyEvent.VK_F) | |
116 }) | |
117 add(JMenuItem("Find Again").apply { | |
118 actionCommand = "Edit.FindAgain" | |
119 addActionListener(menuItemListener) | |
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" | |
127 addActionListener(menuItemListener) | |
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 val menuBar = MyMenuBar() | |
145 | |
146 /** | |
147 * The popup menu we display when the user requests so atop a clipboard | |
148 * item. | |
149 */ | |
150 class MyPopupMenu: JPopupMenu() { | |
151 init { | |
31 | 152 add(anyRequired.add(JMenuItem("Clone").apply { |
27 | 153 actionCommand = "Edit.Clone" |
154 addActionListener(menuItemListener) | |
155 })) | |
31 | 156 add(styledRequired.add(JMenuItem("Coerce…").apply { |
27 | 157 actionCommand = "Edit.Coerce" |
158 addActionListener(menuItemListener) | |
159 })) | |
160 } | |
161 } | |
162 | |
163 val popupMenu = MyPopupMenu() |