annotate src/name/blackcap/clipman/Main.kt @ 18:96cc73ae2904

Make it more fail-safe.
author David Barts <n5jrn@me.com>
date Tue, 21 Jan 2020 16:39:02 -0800
parents 9dd58db4d15a
children 5e0d1fe61da9
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
13
fe0fcfc8b2aa Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
16 import java.util.concurrent.Semaphore
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 import java.util.logging.Level
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 import java.util.logging.Logger
17
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
19 import java.util.zip.CRC32
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 import javax.swing.*
8
7715ff59f053 Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
21 import javax.swing.border.*
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 import javax.swing.text.JTextComponent
13
fe0fcfc8b2aa Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
23 import javax.swing.text.html.StyleSheet
fe0fcfc8b2aa Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
24 import javax.swing.text.html.HTMLEditorKit
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 import kotlin.concurrent.thread
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 import org.jsoup.Jsoup
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
27 import org.jsoup.nodes.*
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
28
7
fbb3a6999590 More border tweaks, etc.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
29 /* name we call ourselves */
fbb3a6999590 More border tweaks, etc.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
30 val MYNAME = "ClipMan"
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
31
8
7715ff59f053 Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
32 /* default sizes */
7715ff59f053 Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
33 val CPWIDTH = 640
7715ff59f053 Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
34 val CPHEIGHT = 480
9
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
35
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
36 /* border widths */
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
37 val PANEL_BORDER = 9
10
e7e067f5b649 Make border sizes more explicit.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
38 val OUTER_BORDER_TOP = 3
e7e067f5b649 Make border sizes more explicit.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
39 val OUTER_BORDER = 9
9
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
40 val INNER_BORDER = 1
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
41 val MARGIN_BORDER = 3
8
7715ff59f053 Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
42
13
fe0fcfc8b2aa Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
43 /* default font sizes in the text-display panes */
fe0fcfc8b2aa Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
44 val MONO_SIZE = 14
fe0fcfc8b2aa Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
45 val PROP_SIZE = 16
fe0fcfc8b2aa Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
46
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 /* 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
48 class KillIt(val thr: Thread) : WindowListener {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 // events we don't care about
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
50 override fun windowActivated(e: WindowEvent) {}
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
51 override fun windowClosed(e: WindowEvent) {}
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
52 override fun windowDeactivated(e: WindowEvent) {}
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 override fun windowDeiconified(e: WindowEvent) {}
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 override fun windowIconified(e: WindowEvent) {}
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
55 override fun windowOpened(e: WindowEvent) {}
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
56
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 // and the one we do
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 override fun windowClosing(e: WindowEvent) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
59 thr.run { interrupt(); join() }
6
0640efd6b54a Tweak borders (incomplete).
David Barts <n5jrn@me.com>
parents: 4
diff changeset
60 LOGGER.log(Level.INFO, "execution complete")
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 System.exit(0)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
62 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
64
11
5bbb436fbcf7 Fix the glitch of the JTextPane's sometimes being too tall.
David Barts <n5jrn@me.com>
parents: 10
diff changeset
65 class ClipText: JTextPane() {
5bbb436fbcf7 Fix the glitch of the JTextPane's sometimes being too tall.
David Barts <n5jrn@me.com>
parents: 10
diff changeset
66 override fun getMaximumSize(): Dimension {
5bbb436fbcf7 Fix the glitch of the JTextPane's sometimes being too tall.
David Barts <n5jrn@me.com>
parents: 10
diff changeset
67 return Dimension(Int.MAX_VALUE, preferredSize.height)
5bbb436fbcf7 Fix the glitch of the JTextPane's sometimes being too tall.
David Barts <n5jrn@me.com>
parents: 10
diff changeset
68 }
5bbb436fbcf7 Fix the glitch of the JTextPane's sometimes being too tall.
David Barts <n5jrn@me.com>
parents: 10
diff changeset
69 }
5bbb436fbcf7 Fix the glitch of the JTextPane's sometimes being too tall.
David Barts <n5jrn@me.com>
parents: 10
diff changeset
70
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 /* the updating thread */
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 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
73 @Volatile var enabled = true
9
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
74 private val outerBorder =
10
e7e067f5b649 Make border sizes more explicit.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
75 MatteBorder(OUTER_BORDER_TOP, OUTER_BORDER, OUTER_BORDER, OUTER_BORDER,
9
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
76 queue.parent.background)
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
77 private val stdBorder =
9
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
78 CompoundBorder(LineBorder(Color.GRAY, INNER_BORDER),
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
79 EmptyBorder(MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER))
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
80
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
81 override fun run() {
17
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
82 var oldContents: PasteboardItem? = null
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
83 while (true) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
84 if (enabled) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
85 var contents = PasteboardItem.read()
17
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
86 if ((contents != null) && (contents != oldContents)) {
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
87 val stdWidth = queue.parent.size.width - 2 * (PANEL_BORDER+OUTER_BORDER+INNER_BORDER+MARGIN_BORDER)
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
88 val widget = JPanel().apply {
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
89 layout = BoxLayout(this, BoxLayout.Y_AXIS)
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
90 background = queue.parent.background
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
91 border = outerBorder
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
92 }
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
93 val (plain, html) = when(contents) {
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
94 is PasteboardItem.Plain -> Pair(contents.plain, null)
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
95 is PasteboardItem.HTML -> Pair(null, contents.html)
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
96 is PasteboardItem.RTF -> Pair(contents.plain, contents.html)
3
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
97 }
17
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
98 if (html == null) {
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
99 widget.run {
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
100 add(stdLabel("Plain text"))
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
101 add(ClipText().apply {
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
102 contentType = "text/plain"
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
103 text = plain
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
104 font = Font(Font.MONOSPACED, Font.PLAIN, MONO_SIZE)
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
105 border = stdBorder
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
106 autoSize(stdWidth)
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
107 setEditable(false)
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
108 alignmentX = JTextPane.LEFT_ALIGNMENT
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
109 })
8
7715ff59f053 Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
110 }
17
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
111 } else {
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
112 widget.run {
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
113 add(stdLabel("Styled text"))
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
114 val (dhtml, style) = preproc(html)
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
115 val hek = HTMLEditorKit().apply {
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
116 style.addStyleSheet(styleSheet)
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
117 styleSheet = style
3
70caa73e32f7 Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
118 }
17
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
119 add(ClipText().apply {
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
120 editorKit = hek
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
121 text = dhtml
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
122 border = stdBorder
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
123 autoSize(stdWidth)
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
124 setEditable(false)
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
125 alignmentX = JTextPane.LEFT_ALIGNMENT
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
126 })
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
127 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
128 }
17
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
129 queue.add(QueueItem(widget, contents))
9dd58db4d15a Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
130 oldContents = contents
0
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 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
133 if (Thread.interrupted()) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
134 return
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 try {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
137 Thread.sleep(interval - System.currentTimeMillis() % interval)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
138 } catch (e: InterruptedException) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
139 return
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 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
142 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
143
8
7715ff59f053 Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
144 private fun stdLabel(text: String) = JLabel(text).apply {
7715ff59f053 Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
145 horizontalAlignment = JLabel.LEFT
7715ff59f053 Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
146 alignmentX = JLabel.LEFT_ALIGNMENT
7715ff59f053 Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
147 }
7715ff59f053 Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
148
13
fe0fcfc8b2aa Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
149 private fun preproc(html: String): Pair<String, StyleSheet> {
fe0fcfc8b2aa Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
150 val sty = StyleSheet().apply {
15
732f92dc3bc6 Don't need head.
David Barts <n5jrn@me.com>
parents: 14
diff changeset
151 addRule("body { font-family: serif; font-size: %d; }".format(PROP_SIZE))
14
0cd912d29184 Better cope with Swing's idiosyncrasies.
David Barts <n5jrn@me.com>
parents: 13
diff changeset
152 addRule("code, kbd, pre, samp, tt { font-family: monospace; font-size: %d; }".format(MONO_SIZE))
13
fe0fcfc8b2aa Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
153 }
fe0fcfc8b2aa Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
154 val scrubbed = Jsoup.parse(html).run {
fe0fcfc8b2aa Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
155 select("style").forEach {
fe0fcfc8b2aa Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
156 it.dataNodes().forEach { sty.addRule(it.wholeData) }
fe0fcfc8b2aa Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
157 }
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
158 select(":root>head>meta").remove()
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
159 outputSettings()
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
160 .charset(CHARSET_NAME)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
161 .syntax(Document.OutputSettings.Syntax.xml)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
162 outerHtml()
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
163 }
13
fe0fcfc8b2aa Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
164 return Pair(scrubbed, sty)
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
165 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
166 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
167
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
168 fun main(args: Array<String>) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
169 LOGGER.log(Level.INFO, "beginning execution")
7
fbb3a6999590 More border tweaks, etc.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
170 setLookFeel()
8
7715ff59f053 Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
171 val frame = JFrame(MYNAME)
4
debe0413280f Use JPanel.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
172 val con = JPanel().apply {
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
173 layout = BoxLayout(this, BoxLayout.Y_AXIS)
9
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
174 border = EmptyBorder(PANEL_BORDER, PANEL_BORDER, PANEL_BORDER, PANEL_BORDER)
6
0640efd6b54a Tweak borders (incomplete).
David Barts <n5jrn@me.com>
parents: 4
diff changeset
175 background = frame.background
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
176 }
9
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
177 inSynSwingThread {
6
0640efd6b54a Tweak borders (incomplete).
David Barts <n5jrn@me.com>
parents: 4
diff changeset
178 frame.apply {
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
179 contentPane.add(
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
180 JScrollPane(con).apply {
8
7715ff59f053 Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
181 verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
182 horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
8
7715ff59f053 Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
183 preferredSize = Dimension(CPWIDTH, CPHEIGHT)
6
0640efd6b54a Tweak borders (incomplete).
David Barts <n5jrn@me.com>
parents: 4
diff changeset
184 background = frame.background
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
185 }, BorderLayout.CENTER)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
186 pack()
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
187 setVisible(true)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
188 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
189 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
190 val queue = PasteboardQueue(con, 10)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
191 val updater = UpdateIt(queue, 1000).apply { start() }
6
0640efd6b54a Tweak borders (incomplete).
David Barts <n5jrn@me.com>
parents: 4
diff changeset
192 inSwingThread { frame.addWindowListener(KillIt(updater)) }
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
193 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
194
7
fbb3a6999590 More border tweaks, etc.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
195 fun setLookFeel() {
fbb3a6999590 More border tweaks, etc.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
196 inSwingThread {
fbb3a6999590 More border tweaks, etc.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
197 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
fbb3a6999590 More border tweaks, etc.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
198 }
fbb3a6999590 More border tweaks, etc.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
199 }
fbb3a6999590 More border tweaks, etc.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
200
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
201 fun inSwingThread(block: () -> Unit) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
202 SwingUtilities.invokeLater(Runnable(block))
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
203 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
204
9
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
205 fun inSynSwingThread(block: () -> Unit) {
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
206 val ready = Semaphore(0)
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
207 inSwingThread {
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
208 block()
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
209 ready.release()
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
210 }
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
211 ready.acquire()
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
212 }
8fcff14defa2 Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
213
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
214 fun JTextComponent.autoSize(width: Int): Unit {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
215 val SLOP = 10
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
216 val dim = Dimension(width, width)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
217 preferredSize = dim
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
218 size = dim
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
219 val r = modelToView(document.length)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
220 preferredSize = Dimension(width, r.y + r.height + SLOP)
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
221 }