comparison src/name/blackcap/clipman/Main.kt @ 24:dac8dfb4b549

Preliminary menu bar support.
author David Barts <n5jrn@me.com>
date Thu, 23 Jan 2020 19:25:17 -0800
parents c10a447b9e1b
children 8aa2dfac27eb
comparison
equal deleted inserted replaced
23:14049bc97a7c 24:dac8dfb4b549
8 import java.awt.Container 8 import java.awt.Container
9 import java.awt.Dimension 9 import java.awt.Dimension
10 import java.awt.Font 10 import java.awt.Font
11 import java.awt.Toolkit; 11 import java.awt.Toolkit;
12 import java.awt.datatransfer.* 12 import java.awt.datatransfer.*
13 import java.awt.event.ActionEvent
14 import java.awt.event.ActionListener
15 import java.awt.event.KeyEvent
13 import java.awt.event.WindowEvent 16 import java.awt.event.WindowEvent
14 import java.awt.event.WindowListener 17 import java.awt.event.WindowListener
15 import java.util.Date 18 import java.util.Date
16 import java.util.concurrent.Semaphore 19 import java.util.concurrent.Semaphore
17 import java.util.logging.Level 20 import java.util.logging.Level
18 import java.util.logging.Logger 21 import java.util.logging.Logger
19 import javax.swing.* 22 import javax.swing.*
20 import javax.swing.border.* 23 import javax.swing.border.*
21 import javax.swing.text.JTextComponent 24 import javax.swing.text.JTextComponent
25 import javax.swing.text.html.HTMLEditorKit
22 import javax.swing.text.html.StyleSheet 26 import javax.swing.text.html.StyleSheet
23 import javax.swing.text.html.HTMLEditorKit
24 import kotlin.concurrent.thread 27 import kotlin.concurrent.thread
25 import org.jsoup.Jsoup 28 import org.jsoup.Jsoup
26 import org.jsoup.nodes.* 29 import org.jsoup.nodes.*
27 30
28 /* name we call ourselves */ 31 /* name we call ourselves */
181 } 184 }
182 } 185 }
183 186
184 fun main(args: Array<String>) { 187 fun main(args: Array<String>) {
185 LOGGER.log(Level.INFO, "beginning execution") 188 LOGGER.log(Level.INFO, "beginning execution")
189 if (OS.type == OS.MAC) {
190 System.setProperty("apple.laf.useScreenMenuBar", "true")
191 }
186 var frame: JFrame? = null 192 var frame: JFrame? = null
187 var con: JPanel? = null 193 var con: JPanel? = null
188 inSynSwingThread { 194 inSynSwingThread {
189 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 195 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
190 frame = JFrame(MYNAME) 196 frame = JFrame(MYNAME)
192 layout = BoxLayout(this, BoxLayout.Y_AXIS) 198 layout = BoxLayout(this, BoxLayout.Y_AXIS)
193 border = EmptyBorder(PANEL_BORDER, PANEL_BORDER, PANEL_BORDER, PANEL_BORDER) 199 border = EmptyBorder(PANEL_BORDER, PANEL_BORDER, PANEL_BORDER, PANEL_BORDER)
194 background = frame!!.background 200 background = frame!!.background
195 } 201 }
196 frame!!.apply { 202 frame!!.apply {
203 jMenuBar = makeMenuBar()
197 contentPane.add( 204 contentPane.add(
198 JScrollPane(con!!).apply { 205 JScrollPane(con!!).apply {
199 verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS 206 verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
200 horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER 207 horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
201 preferredSize = Dimension(CPWIDTH, CPHEIGHT) 208 preferredSize = Dimension(CPWIDTH, CPHEIGHT)
208 val queue = PasteboardQueue(con!!, 10) 215 val queue = PasteboardQueue(con!!, 10)
209 val updater = UpdateIt(queue, 1000).apply { start() } 216 val updater = UpdateIt(queue, 1000).apply { start() }
210 inSwingThread { frame!!.addWindowListener(KillIt(updater)) } 217 inSwingThread { frame!!.addWindowListener(KillIt(updater)) }
211 } 218 }
212 219
220 class MenuItemListener: ActionListener {
221 override fun actionPerformed(e: ActionEvent) {
222 println(e.actionCommand + " selected")
223 }
224 }
225
226 fun makeMenuBar() = JMenuBar().apply {
227 val al: ActionListener = MenuItemListener()
228 if (OS.type != OS.MAC) {
229 add(JMenu("File").apply {
230 add(JMenuItem("Quit").apply {
231 actionCommand = "File.Quit"
232 addActionListener(al)
233 makeShortcut(KeyEvent.VK_Q)
234 })
235 })
236 }
237 add(JMenu("Edit").apply {
238 add(JMenuItem("Clone").apply {
239 actionCommand = "Edit.Clone"
240 addActionListener(al)
241 makeShortcut(KeyEvent.VK_C)
242 })
243 add(JMenuItem("Coerce…").apply {
244 actionCommand = "Edit.Coerce"
245 addActionListener(al)
246 makeShortcut(KeyEvent.VK_K)
247 })
248 add(JMenuItem("Delete").apply {
249 actionCommand = "Edit.Delete"
250 addActionListener(al)
251 setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0))
252 })
253 add(JMenuItem("Find…").apply {
254 actionCommand = "Edit.Find"
255 addActionListener(al)
256 makeShortcut(KeyEvent.VK_F)
257 })
258 })
259 if (OS.type != OS.MAC) {
260 add(JMenu("Help").apply {
261 add(JMenuItem("About ClipMan…").apply {
262 actionCommand = "Help.About"
263 addActionListener(al)
264 })
265 })
266 }
267 }
268
213 fun inSwingThread(block: () -> Unit) { 269 fun inSwingThread(block: () -> Unit) {
214 SwingUtilities.invokeLater(Runnable(block)) 270 SwingUtilities.invokeLater(Runnable(block))
215 } 271 }
216 272
217 fun inSynSwingThread(block: () -> Unit) { 273 fun inSynSwingThread(block: () -> Unit) {
229 preferredSize = dim 285 preferredSize = dim
230 size = dim 286 size = dim
231 val r = modelToView(document.length) 287 val r = modelToView(document.length)
232 preferredSize = Dimension(width, r.y + r.height + SLOP) 288 preferredSize = Dimension(width, r.y + r.height + SLOP)
233 } 289 }
290
291 val SC_KEY_MASK = Toolkit.getDefaultToolkit().menuShortcutKeyMask
292 fun JMenuItem.makeShortcut(key: Int): Unit {
293 setAccelerator(KeyStroke.getKeyStroke(key, SC_KEY_MASK))
294 }