annotate src/name/blackcap/clipman/Main.kt @ 2:6fb94eae32fa

Maybe this will auto-scroll reliably?
author David Barts <n5jrn@me.com>
date Sat, 18 Jan 2020 10:58:45 -0800
parents be282c48010a
children 70caa73e32f7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 /*
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
2 * The entry point and most of the view logic is here.
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
3 */
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 package name.blackcap.clipman
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
5
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 import java.awt.BorderLayout
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
7 import java.awt.Color
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
8 import java.awt.Container
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
9 import java.awt.Dimension
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
10 import java.awt.Font
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
11 import java.awt.Toolkit;
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
12 import java.awt.datatransfer.*
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
13 import java.awt.event.WindowEvent
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
14 import java.awt.event.WindowListener
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 import java.util.Date
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 import java.util.logging.Level
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 import java.util.logging.Logger
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 import javax.swing.*
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
19 import javax.swing.border.CompoundBorder
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 import javax.swing.border.EmptyBorder
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
21 import javax.swing.border.LineBorder
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 import javax.swing.text.JTextComponent
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 import kotlin.concurrent.thread
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 import org.jsoup.Jsoup
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 import org.jsoup.nodes.*
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
26
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
27
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
28 /* kills the updating thread (and does a system exit) when needed */
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
29 class KillIt(val thr: Thread) : WindowListener {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
30 // events we don't care about
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
31 override fun windowActivated(e: WindowEvent) {}
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
32 override fun windowClosed(e: WindowEvent) {}
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 override fun windowDeactivated(e: WindowEvent) {}
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 override fun windowDeiconified(e: WindowEvent) {}
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
35 override fun windowIconified(e: WindowEvent) {}
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
36 override fun windowOpened(e: WindowEvent) {}
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
37
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 // and the one we do
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
39 override fun windowClosing(e: WindowEvent) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
40 thr.run { interrupt(); join() }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 System.exit(0)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
44
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 /* the updating thread */
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 class UpdateIt(val queue: PasteboardQueue, val interval: Int): Thread() {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 @Volatile var enabled = true
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
48 private val stdBorder =
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 CompoundBorder(EmptyBorder(5, 10, 5, 10), LineBorder(Color.GRAY, 1))
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
50
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
51 override fun run() {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
52 var oldContents = ""
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 var newContents = ""
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 while (true) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
55 if (enabled) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
56 var contents = PasteboardItem.read()
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 if (contents == null) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 LOGGER.log(Level.WARNING, "unable to read clipboard")
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
59 continue
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 newContents = when (contents) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
62 is PasteboardItem.Plain -> contents.plain
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 is PasteboardItem.HTML -> contents.plain
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 if (oldContents != newContents) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 var widget: JComponent = when(contents) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 is PasteboardItem.Plain -> JTextPane().apply {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
68 contentType = "text/plain"
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 toolTipText = "Plain text"
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
70 text = contents.plain
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 font = Font(Font.MONOSPACED, Font.PLAIN, 14)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 border = stdBorder
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 autoSize(600)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 setEditable(false)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
76 is PasteboardItem.HTML -> JTextPane().apply {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
77 contentType = "text/html"
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
78 toolTipText = "Styled text"
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 text = scrub(contents.html)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 border = stdBorder
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
81 autoSize(600)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 setEditable(false)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
83 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
84 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
85 queue.add(QueueItem(widget, contents))
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
86 oldContents = newContents
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
87 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
88 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
89 if (Thread.interrupted()) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
90 return
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
91 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
92 try {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
93 Thread.sleep(interval - System.currentTimeMillis() % interval)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
94 } catch (e: InterruptedException) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
95 return
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
96 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
97 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
98 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
99
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
100 private fun scrub(html: String): String {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
101 return Jsoup.parse(html).run {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
102 select(":root>head>meta").remove()
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
103 outputSettings()
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
104 .charset(CHARSET_NAME)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
105 .syntax(Document.OutputSettings.Syntax.xml)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
106 outerHtml()
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
107 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
108 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
109 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
110
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
111 fun main(args: Array<String>) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
112 LOGGER.log(Level.INFO, "beginning execution")
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
113 val con = Container().apply {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
114 layout = BoxLayout(this, BoxLayout.Y_AXIS)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
115 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
116 var frame: JFrame? = null
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
117 inSwingThread {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
118 frame = JFrame("ClipMan").apply {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
119 preferredSize = Dimension(640, 480)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
120 contentPane.add(
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
121 JScrollPane(con).apply {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
122 verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
123 horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
124 preferredSize = Dimension(640, 480)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
125 }, BorderLayout.CENTER)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
126 pack()
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
127 setVisible(true)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
128 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
129 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
130 val queue = PasteboardQueue(con, 10)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
131 val updater = UpdateIt(queue, 1000).apply { start() }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
132 inSwingThread { frame?.addWindowListener(KillIt(updater)) }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
133 LOGGER.log(Level.INFO, "execution complete")
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
134 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
135
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
136 fun inSwingThread(block: () -> Unit) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
137 SwingUtilities.invokeLater(Runnable(block))
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
138 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
139
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
140 fun JTextComponent.autoSize(width: Int): Unit {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
141 val SLOP = 10
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
142 val dim = Dimension(width, width)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
143 preferredSize = dim
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
144 size = dim
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
145 val r = modelToView(document.length)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
146 preferredSize = Dimension(width, r.y + r.height + SLOP)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
147 }