annotate src/name/blackcap/clipman/Main.kt @ 6:0640efd6b54a

Tweak borders (incomplete).
author David Barts <n5jrn@me.com>
date Sun, 19 Jan 2020 10:53:38 -0800
parents debe0413280f
children fbb3a6999590
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() }
6
0640efd6b54a Tweak borders (incomplete).
David Barts <n5jrn@me.com>
parents: 4
diff changeset
41 LOGGER.log(Level.INFO, "execution complete")
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 System.exit(0)
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
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 /* the updating thread */
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 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
48 @Volatile var enabled = true
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 private val stdBorder =
6
0640efd6b54a Tweak borders (incomplete).
David Barts <n5jrn@me.com>
parents: 4
diff changeset
50 CompoundBorder(LineBorder(Color(0xeeeeee), 9), LineBorder(Color.GRAY, 1))
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
51
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
52 override fun run() {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 var oldContents = ""
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 var newContents = ""
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
55 while (true) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
56 if (enabled) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 var contents = PasteboardItem.read()
3
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
58 if (contents != null) {
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
59 newContents = when (contents) {
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
60 is PasteboardItem.Plain -> contents.plain
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
61 is PasteboardItem.HTML -> contents.plain
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
62 }
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
63 if (oldContents != newContents) {
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
64 var widget: JComponent = when(contents) {
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
65 is PasteboardItem.Plain -> JTextPane().apply {
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
66 contentType = "text/plain"
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
67 toolTipText = "Plain text"
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
68 text = contents.plain
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
69 font = Font(Font.MONOSPACED, Font.PLAIN, 14)
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
70 border = stdBorder
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
71 autoSize(600)
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
72 setEditable(false)
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
73 }
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
74 is PasteboardItem.HTML -> JTextPane().apply {
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
75 contentType = "text/html"
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
76 toolTipText = "Styled text"
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
77 text = scrub(contents.html)
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
78 border = stdBorder
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
79 autoSize(600)
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
80 setEditable(false)
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
81 }
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 }
3
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
83 queue.add(QueueItem(widget, contents))
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
84 oldContents = newContents
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
85 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
86 }
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 if (Thread.interrupted()) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
89 return
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
90 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
91 try {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
92 Thread.sleep(interval - System.currentTimeMillis() % interval)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
93 } catch (e: InterruptedException) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
94 return
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
95 }
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 private fun scrub(html: String): String {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
100 return Jsoup.parse(html).run {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
101 select(":root>head>meta").remove()
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
102 outputSettings()
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
103 .charset(CHARSET_NAME)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
104 .syntax(Document.OutputSettings.Syntax.xml)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
105 outerHtml()
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
106 }
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 fun main(args: Array<String>) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
111 LOGGER.log(Level.INFO, "beginning execution")
6
0640efd6b54a Tweak borders (incomplete).
David Barts <n5jrn@me.com>
parents: 4
diff changeset
112 val frame = JFrame("ClipMan").apply { preferredSize = Dimension(640, 480) }
4
debe0413280f Use JPanel.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
113 val con = JPanel().apply {
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
114 layout = BoxLayout(this, BoxLayout.Y_AXIS)
6
0640efd6b54a Tweak borders (incomplete).
David Barts <n5jrn@me.com>
parents: 4
diff changeset
115 border = EmptyBorder(9, 9, 9, 9)
0640efd6b54a Tweak borders (incomplete).
David Barts <n5jrn@me.com>
parents: 4
diff changeset
116 background = frame.background
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
117 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
118 inSwingThread {
6
0640efd6b54a Tweak borders (incomplete).
David Barts <n5jrn@me.com>
parents: 4
diff changeset
119 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
0640efd6b54a Tweak borders (incomplete).
David Barts <n5jrn@me.com>
parents: 4
diff changeset
120 frame.apply {
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
121 contentPane.add(
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
122 JScrollPane(con).apply {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
123 verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
124 horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
125 preferredSize = Dimension(640, 480)
6
0640efd6b54a Tweak borders (incomplete).
David Barts <n5jrn@me.com>
parents: 4
diff changeset
126 background = frame.background
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
127 }, BorderLayout.CENTER)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
128 pack()
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
129 setVisible(true)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
130 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
131 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
132 val queue = PasteboardQueue(con, 10)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
133 val updater = UpdateIt(queue, 1000).apply { start() }
6
0640efd6b54a Tweak borders (incomplete).
David Barts <n5jrn@me.com>
parents: 4
diff changeset
134 inSwingThread { frame.addWindowListener(KillIt(updater)) }
0
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
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
137 fun inSwingThread(block: () -> Unit) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
138 SwingUtilities.invokeLater(Runnable(block))
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
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
141 fun JTextComponent.autoSize(width: Int): Unit {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
142 val SLOP = 10
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
143 val dim = Dimension(width, width)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
144 preferredSize = dim
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
145 size = dim
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
146 val r = modelToView(document.length)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
147 preferredSize = Dimension(width, r.y + r.height + SLOP)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
148 }