comparison src/name/blackcap/clipman/Menus.kt @ 31:0c6c18a733b7

Compiles, new menu still a mess.
author David Barts <n5jrn@me.com>
date Thu, 30 Jan 2020 16:01:51 -0800
parents 0e88c6bed11e
children 5f8475b37e23
comparison
equal deleted inserted replaced
30:0e88c6bed11e 31:0c6c18a733b7
16 class MenuItemListener: ActionListener { 16 class MenuItemListener: ActionListener {
17 override fun actionPerformed(e: ActionEvent) { 17 override fun actionPerformed(e: ActionEvent) {
18 when (e.actionCommand) { 18 when (e.actionCommand) {
19 "File.Quit" -> System.exit(0) 19 "File.Quit" -> System.exit(0)
20 "Edit.Clone" -> onlyIfSelected { PasteboardItem.write(it.contents) } 20 "Edit.Clone" -> onlyIfSelected { PasteboardItem.write(it.contents) }
21 "Edit.Coerce" -> onlyIfSelected { doCoerceFonts(it) } 21 "Edit.Coerce" -> onlyIfSelected { coerceDialog.setVisible(true) }
22 "Edit.Find" -> searchDialog.setVisible(true) 22 "Edit.Find" -> searchDialog.setVisible(true)
23 "Edit.FindAgain" -> searchDialog.find() 23 "Edit.FindAgain" -> searchDialog.find()
24 else -> throw RuntimeException("unexpected actionCommand!") 24 else -> throw RuntimeException("unexpected actionCommand!")
25 } 25 }
26 } 26 }
29 val selected = queue.v.getSelected() 29 val selected = queue.v.getSelected()
30 if (selected == null) { 30 if (selected == null) {
31 JOptionPane.showMessageDialog(frame.v, 31 JOptionPane.showMessageDialog(frame.v,
32 "No item selected.", 32 "No item selected.",
33 "Error", 33 "Error",
34 JOptionPane.ERROR_MESSAGE); 34 JOptionPane.ERROR_MESSAGE)
35 } else { 35 } else {
36 block(selected) 36 block(selected)
37 } 37 }
38 } 38 }
39 39
40 private fun doCoerceFonts(item: QueueItem) { 40 private fun clone(contents: PasteboardItem) {
41 /* not finished */ 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 }
42 } 51 }
43 } 52 }
44 53
45 val menuItemListener = MenuItemListener() 54 val menuItemListener = MenuItemListener()
55
56 /**
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()
46 83
47 /** 84 /**
48 * Our menu bar. What we display depends somewhat on the system type, as 85 * Our menu bar. What we display depends somewhat on the system type, as
49 * the Mac gives us a gratuitous menu bar entry for handling some stuff. 86 * the Mac gives us a gratuitous menu bar entry for handling some stuff.
50 */ 87 */
58 makeShortcut(KeyEvent.VK_Q) 95 makeShortcut(KeyEvent.VK_Q)
59 }) 96 })
60 }) 97 })
61 } 98 }
62 add(JMenu("Edit").apply { 99 add(JMenu("Edit").apply {
63 add(SelectionRequired.add(JMenuItem("Clone").apply { 100 add(anyRequired.add(JMenuItem("Clone").apply {
64 setEnabled(false) 101 setEnabled(false)
65 actionCommand = "Edit.Clone" 102 actionCommand = "Edit.Clone"
66 addActionListener(menuItemListener) 103 addActionListener(menuItemListener)
67 makeShortcut(KeyEvent.VK_C) 104 makeShortcut(KeyEvent.VK_C)
68 })) 105 }))
69 add(SelectionRequired.add(JMenuItem("Coerce…").apply { 106 add(styledRequired.add(JMenuItem("Coerce…").apply {
70 setEnabled(false) 107 setEnabled(false)
71 actionCommand = "Edit.Coerce" 108 actionCommand = "Edit.Coerce"
72 addActionListener(menuItemListener) 109 addActionListener(menuItemListener)
73 makeShortcut(KeyEvent.VK_K) 110 makeShortcut(KeyEvent.VK_K)
74 })) 111 }))
110 * The popup menu we display when the user requests so atop a clipboard 147 * The popup menu we display when the user requests so atop a clipboard
111 * item. 148 * item.
112 */ 149 */
113 class MyPopupMenu: JPopupMenu() { 150 class MyPopupMenu: JPopupMenu() {
114 init { 151 init {
115 add(SelectionRequired.add(JMenuItem("Clone").apply { 152 add(anyRequired.add(JMenuItem("Clone").apply {
116 actionCommand = "Edit.Clone" 153 actionCommand = "Edit.Clone"
117 addActionListener(menuItemListener) 154 addActionListener(menuItemListener)
118 })) 155 }))
119 add(SelectionRequired.add(JMenuItem("Coerce…").apply { 156 add(styledRequired.add(JMenuItem("Coerce…").apply {
120 actionCommand = "Edit.Coerce" 157 actionCommand = "Edit.Coerce"
121 addActionListener(menuItemListener) 158 addActionListener(menuItemListener)
122 })) 159 }))
123 } 160 }
124 } 161 }
125 162
126 val popupMenu = MyPopupMenu() 163 val popupMenu = MyPopupMenu()
127
128 /**
129 * Track menu items that require something to be selected in order
130 * to work, and allow them to be enabled and disabled en masse.
131 */
132 object SelectionRequired {
133 private val cache = LinkedList<JMenuItem>()
134
135 fun add(item: JMenuItem): JMenuItem {
136 cache.add(item)
137 return item
138 }
139
140 fun enable() {
141 cache.forEach {
142 it.setEnabled(true)
143 }
144 }
145
146 fun disable() {
147 cache.forEach {
148 it.setEnabled(false)
149 }
150 }
151 }