comparison src/name/blackcap/clipman/Menus.kt @ 47:19d9da731c43

Recoded; cleaned up root namespace, removed race conditions.
author David Barts <n5jrn@me.com>
date Sun, 12 Apr 2020 14:31:06 -0700
parents 33fbe3a78d84
children bb80148e2cb3
comparison
equal deleted inserted replaced
46:88066346f129 47:19d9da731c43
16 */ 16 */
17 class MenuItemListener: ActionListener { 17 class MenuItemListener: ActionListener {
18 override fun actionPerformed(e: ActionEvent) { 18 override fun actionPerformed(e: ActionEvent) {
19 when (e.actionCommand) { 19 when (e.actionCommand) {
20 "File.Quit" -> System.exit(0) 20 "File.Quit" -> System.exit(0)
21 "File.Preferences" -> settingsDialog.setVisible(true) 21 "File.Preferences" -> Application.settingsDialog.setVisible(true)
22 "Edit.Clone" -> onlyIfSelected { PasteboardItem.write(it.contents) } 22 "Edit.Clone" -> onlyIfSelected { PasteboardItem.write(it.contents) }
23 "Edit.Coerce" -> onlyIfSelected { coerceDialog.setVisible(true) } 23 "Edit.Coerce" -> onlyIfSelected { Application.coerceDialog.setVisible(true) }
24 "Edit.Find" -> searchDialog.setVisible(true) 24 "Edit.Find" -> Application.searchDialog.setVisible(true)
25 "Edit.FindAgain" -> searchDialog.find() 25 "Edit.FindAgain" -> Application.searchDialog.find()
26 "Help.About" -> showAboutDialog() 26 "Help.About" -> showAboutDialog()
27 else -> throw RuntimeException("unexpected actionCommand!") 27 else -> throw RuntimeException("unexpected actionCommand!")
28 } 28 }
29 } 29 }
30 30
31 private fun onlyIfSelected(block: (QueueItem) -> Unit) { 31 private fun onlyIfSelected(block: (QueueItem) -> Unit) {
32 val selected = queue.v.getSelected() 32 val selected = Application.queue.getSelected()
33 if (selected == null) { 33 if (selected == null) {
34 JOptionPane.showMessageDialog(frame.v, 34 JOptionPane.showMessageDialog(Application.frame,
35 "No item selected.", 35 "No item selected.",
36 "Error", 36 "Error",
37 JOptionPane.ERROR_MESSAGE) 37 JOptionPane.ERROR_MESSAGE)
38 } else { 38 } else {
39 block(selected) 39 block(selected)
40 } 40 }
41 } 41 }
42 } 42 }
43
44 val menuItemListener = MenuItemListener()
45 43
46 /** 44 /**
47 * Track menu items that require something to be selected in order 45 * Track menu items that require something to be selected in order
48 * to work, and allow them to be enabled and disabled en masse. 46 * to work, and allow them to be enabled and disabled en masse.
49 */ 47 */
64 fun enable() = setEnabled(true) 62 fun enable() = setEnabled(true)
65 63
66 fun disable() = setEnabled(false) 64 fun disable() = setEnabled(false)
67 } 65 }
68 66
69 val anyRequired = SelectionRequired()
70 val styledRequired = SelectionRequired()
71
72 /** 67 /**
73 * Our menu bar. What we display depends somewhat on the system type, as 68 * 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. 69 * the Mac gives us a gratuitous menu bar entry for handling some stuff.
75 */ 70 */
76 class MyMenuBar: JMenuBar() { 71 class MyMenuBar: JMenuBar() {
77 init { 72 init {
78 if (OS.type != OS.MAC) { 73 if (OS.type != OS.MAC) {
79 add(JMenu("File").apply { 74 add(JMenu("File").apply {
80 add(JMenuItem("Quit").apply { 75 add(JMenuItem("Quit").apply {
81 actionCommand = "File.Quit" 76 actionCommand = "File.Quit"
82 addActionListener(menuItemListener) 77 addActionListener(Application.menuItemListener)
83 makeShortcut(KeyEvent.VK_Q) 78 makeShortcut(KeyEvent.VK_Q)
84 }) 79 })
85 add(JMenuItem("Preferences…").apply { 80 add(JMenuItem("Preferences…").apply {
86 actionCommand = "File.Preferences" 81 actionCommand = "File.Preferences"
87 addActionListener(menuItemListener) 82 addActionListener(Application.menuItemListener)
88 makeShortcut(KeyEvent.VK_COMMA) 83 makeShortcut(KeyEvent.VK_COMMA)
89 }) 84 })
90 }) 85 })
91 } 86 }
92 add(JMenu("Edit").apply { 87 add(JMenu("Edit").apply {
93 add(anyRequired.add(JMenuItem("Clone").apply { 88 add(Application.anyRequired.add(JMenuItem("Clone").apply {
94 setEnabled(false) 89 setEnabled(false)
95 actionCommand = "Edit.Clone" 90 actionCommand = "Edit.Clone"
96 addActionListener(menuItemListener) 91 addActionListener(Application.menuItemListener)
97 makeShortcut(KeyEvent.VK_C) 92 makeShortcut(KeyEvent.VK_C)
98 })) 93 }))
99 add(styledRequired.add(JMenuItem("Coerce…").apply { 94 add(Application.styledRequired.add(JMenuItem("Coerce…").apply {
100 setEnabled(false) 95 setEnabled(false)
101 actionCommand = "Edit.Coerce" 96 actionCommand = "Edit.Coerce"
102 addActionListener(menuItemListener) 97 addActionListener(Application.menuItemListener)
103 makeShortcut(KeyEvent.VK_K) 98 makeShortcut(KeyEvent.VK_K)
104 })) 99 }))
105 add(JMenuItem("Find…").apply { 100 add(JMenuItem("Find…").apply {
106 actionCommand = "Edit.Find" 101 actionCommand = "Edit.Find"
107 addActionListener(menuItemListener) 102 addActionListener(Application.menuItemListener)
108 makeShortcut(KeyEvent.VK_F) 103 makeShortcut(KeyEvent.VK_F)
109 }) 104 })
110 add(JMenuItem("Find Again").apply { 105 add(JMenuItem("Find Again").apply {
111 actionCommand = "Edit.FindAgain" 106 actionCommand = "Edit.FindAgain"
112 addActionListener(menuItemListener) 107 addActionListener(Application.menuItemListener)
113 makeShortcut(KeyEvent.VK_G) 108 makeShortcut(KeyEvent.VK_G)
114 }) 109 })
115 }) 110 })
116 if (OS.type != OS.MAC) { 111 if (OS.type != OS.MAC) {
117 add(JMenu("Help").apply { 112 add(JMenu("Help").apply {
118 add(JMenuItem("About ClipMan…").apply { 113 add(JMenuItem("About ClipMan…").apply {
119 actionCommand = "Help.About" 114 actionCommand = "Help.About"
120 addActionListener(menuItemListener) 115 addActionListener(Application.menuItemListener)
121 }) 116 })
122 }) 117 })
123 } 118 }
124 } 119 }
125 120
132 } 127 }
133 return null 128 return null
134 } 129 }
135 } 130 }
136 131
137 val menuBar = MyMenuBar()
138
139 /** 132 /**
140 * The popup menu we display when the user requests so atop a clipboard 133 * The popup menu we display when the user requests so atop a clipboard
141 * item. 134 * item.
142 */ 135 */
143 class MyPopupMenu: JPopupMenu() { 136 class MyPopupMenu: JPopupMenu() {
144 init { 137 init {
145 add(anyRequired.add(JMenuItem("Clone").apply { 138 add(Application.anyRequired.add(JMenuItem("Clone").apply {
146 actionCommand = "Edit.Clone" 139 actionCommand = "Edit.Clone"
147 addActionListener(menuItemListener) 140 addActionListener(Application.menuItemListener)
148 })) 141 }))
149 add(styledRequired.add(JMenuItem("Coerce…").apply { 142 add(Application.styledRequired.add(JMenuItem("Coerce…").apply {
150 actionCommand = "Edit.Coerce" 143 actionCommand = "Edit.Coerce"
151 addActionListener(menuItemListener) 144 addActionListener(Application.menuItemListener)
152 })) 145 }))
153 } 146 }
154 } 147 }
155
156 val popupMenu = MyPopupMenu()
157 148
158 /** 149 /**
159 * Show an About dialog. 150 * Show an About dialog.
160 */ 151 */
161 fun showAboutDialog() { 152 fun showAboutDialog() {
162 JOptionPane.showMessageDialog(frame.v, 153 JOptionPane.showMessageDialog(Application.frame,
163 "ClipMan, a clipboard manager.\n" 154 "ClipMan, a clipboard manager.\n"
164 + "© MMXX, David W. Barts", 155 + "© MMXX, David W. Barts",
165 "About ClipMan", 156 "About ClipMan",
166 JOptionPane.PLAIN_MESSAGE) 157 JOptionPane.PLAIN_MESSAGE)
167 } 158 }