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