Mercurial > cgi-bin > hgweb.cgi > ClipMan
annotate src/name/blackcap/clipman/Main.kt @ 24:dac8dfb4b549
Preliminary menu bar support.
author | David Barts <n5jrn@me.com> |
---|---|
date | Thu, 23 Jan 2020 19:25:17 -0800 |
parents | c10a447b9e1b |
children | 8aa2dfac27eb |
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.* |
24 | 13 import java.awt.event.ActionEvent |
14 import java.awt.event.ActionListener | |
15 import java.awt.event.KeyEvent | |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
16 import java.awt.event.WindowEvent |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
17 import java.awt.event.WindowListener |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
18 import java.util.Date |
13
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
19 import java.util.concurrent.Semaphore |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
20 import java.util.logging.Level |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
21 import java.util.logging.Logger |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
22 import javax.swing.* |
8
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
diff
changeset
|
23 import javax.swing.border.* |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
24 import javax.swing.text.JTextComponent |
24 | 25 import javax.swing.text.html.HTMLEditorKit |
13
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
26 import javax.swing.text.html.StyleSheet |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
27 import kotlin.concurrent.thread |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
28 import org.jsoup.Jsoup |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
29 import org.jsoup.nodes.* |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
30 |
7 | 31 /* name we call ourselves */ |
32 val MYNAME = "ClipMan" | |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
33 |
8
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
diff
changeset
|
34 /* default sizes */ |
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
diff
changeset
|
35 val CPWIDTH = 640 |
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
diff
changeset
|
36 val CPHEIGHT = 480 |
9
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
37 |
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
38 /* border widths */ |
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
39 val PANEL_BORDER = 9 |
10 | 40 val OUTER_BORDER_TOP = 3 |
41 val OUTER_BORDER = 9 | |
9
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
42 val INNER_BORDER = 1 |
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
43 val MARGIN_BORDER = 3 |
8
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
diff
changeset
|
44 |
13
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
45 /* 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
|
46 val MONO_SIZE = 14 |
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
47 val PROP_SIZE = 16 |
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
48 |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
49 /* 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
|
50 class KillIt(val thr: Thread) : WindowListener { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
51 // events we don't care about |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
52 override fun windowActivated(e: WindowEvent) {} |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
53 override fun windowClosed(e: WindowEvent) {} |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
54 override fun windowDeactivated(e: WindowEvent) {} |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
55 override fun windowDeiconified(e: WindowEvent) {} |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
56 override fun windowIconified(e: WindowEvent) {} |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
57 override fun windowOpened(e: WindowEvent) {} |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
58 |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
59 // and the one we do |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
60 override fun windowClosing(e: WindowEvent) { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
61 thr.run { interrupt(); join() } |
6 | 62 LOGGER.log(Level.INFO, "execution complete") |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
63 System.exit(0) |
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 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
66 |
11
5bbb436fbcf7
Fix the glitch of the JTextPane's sometimes being too tall.
David Barts <n5jrn@me.com>
parents:
10
diff
changeset
|
67 class ClipText: JTextPane() { |
5bbb436fbcf7
Fix the glitch of the JTextPane's sometimes being too tall.
David Barts <n5jrn@me.com>
parents:
10
diff
changeset
|
68 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
|
69 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
|
70 } |
5bbb436fbcf7
Fix the glitch of the JTextPane's sometimes being too tall.
David Barts <n5jrn@me.com>
parents:
10
diff
changeset
|
71 } |
5bbb436fbcf7
Fix the glitch of the JTextPane's sometimes being too tall.
David Barts <n5jrn@me.com>
parents:
10
diff
changeset
|
72 |
20
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
73 /* HTMLEditorKit shares all stylesheets. How unbelievably braindamaged. */ |
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
74 class MyEditorKit: HTMLEditorKit() { |
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
75 private var _styleSheet = defaultStyleSheet |
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
76 override fun getStyleSheet() = _styleSheet |
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
77 override fun setStyleSheet(value: StyleSheet) { |
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
78 _styleSheet = value |
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
79 } |
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
80 |
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
81 val defaultStyleSheet: StyleSheet |
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
82 get() { |
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
83 return super.getStyleSheet() |
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
84 } |
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
85 } |
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
86 |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
87 /* the updating thread */ |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
88 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
|
89 @Volatile var enabled = true |
9
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
90 private val outerBorder = |
10 | 91 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
|
92 queue.parent.background) |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
93 private val stdBorder = |
9
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
94 CompoundBorder(LineBorder(Color.GRAY, INNER_BORDER), |
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
95 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
|
96 |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
97 override fun run() { |
17
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
98 var oldContents: PasteboardItem? = null |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
99 while (true) { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
100 if (enabled) { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
101 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
|
102 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
|
103 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
|
104 val widget = JPanel().apply { |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
105 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
|
106 background = queue.parent.background |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
107 border = outerBorder |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
108 } |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
109 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
|
110 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
|
111 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
|
112 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
|
113 } |
21 | 114 var searchable: JTextComponent? = null |
17
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
115 if (html == null) { |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
116 widget.run { |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
117 add(stdLabel("Plain text")) |
21 | 118 searchable = ClipText().apply { |
17
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
119 contentType = "text/plain" |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
120 text = plain |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
121 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
|
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 |
21 | 126 } |
127 add(searchable) | |
8
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
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 } else { |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
130 widget.run { |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
131 add(stdLabel("Styled text")) |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
132 val (dhtml, style) = preproc(html) |
20
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
133 val hek = MyEditorKit().apply { |
17296054c103
HTMLEditorKit shares all stylesheets. How unbelievably braindamaged.
David Barts <n5jrn@me.com>
parents:
19
diff
changeset
|
134 style.addStyleSheet(defaultStyleSheet) |
17
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
135 styleSheet = style |
3
70caa73e32f7
Stop it from spinning on non-textual clipboard data.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
136 } |
21 | 137 searchable = ClipText().apply { |
17
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
138 editorKit = hek |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
139 text = dhtml |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
140 border = stdBorder |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
141 autoSize(stdWidth) |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
142 setEditable(false) |
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
143 alignmentX = JTextPane.LEFT_ALIGNMENT |
21 | 144 } |
145 add(searchable) | |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
146 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
147 } |
21 | 148 queue.add(QueueItem(widget, searchable!!, contents)) |
17
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
149 oldContents = contents |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
150 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
151 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
152 if (Thread.interrupted()) { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
153 return |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
154 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
155 try { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
156 Thread.sleep(interval - System.currentTimeMillis() % interval) |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
157 } catch (e: InterruptedException) { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
158 return |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
159 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
160 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
161 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
162 |
8
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
diff
changeset
|
163 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
|
164 horizontalAlignment = JLabel.LEFT |
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
diff
changeset
|
165 alignmentX = JLabel.LEFT_ALIGNMENT |
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
diff
changeset
|
166 } |
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
diff
changeset
|
167 |
13
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
168 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
|
169 val sty = StyleSheet().apply { |
15 | 170 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
|
171 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
|
172 } |
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
173 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
|
174 select("style").forEach { |
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
175 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
|
176 } |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
177 select(":root>head>meta").remove() |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
178 outputSettings() |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
179 .charset(CHARSET_NAME) |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
180 .syntax(Document.OutputSettings.Syntax.xml) |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
181 outerHtml() |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
182 } |
13
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
183 return Pair(scrubbed, sty) |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
184 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
185 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
186 |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
187 fun main(args: Array<String>) { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
188 LOGGER.log(Level.INFO, "beginning execution") |
24 | 189 if (OS.type == OS.MAC) { |
190 System.setProperty("apple.laf.useScreenMenuBar", "true") | |
191 } | |
21 | 192 var frame: JFrame? = null |
193 var con: JPanel? = null | |
9
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
194 inSynSwingThread { |
21 | 195 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
196 frame = JFrame(MYNAME) | |
197 con = JPanel().apply { | |
198 layout = BoxLayout(this, BoxLayout.Y_AXIS) | |
199 border = EmptyBorder(PANEL_BORDER, PANEL_BORDER, PANEL_BORDER, PANEL_BORDER) | |
200 background = frame!!.background | |
201 } | |
202 frame!!.apply { | |
24 | 203 jMenuBar = makeMenuBar() |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
204 contentPane.add( |
21 | 205 JScrollPane(con!!).apply { |
8
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
diff
changeset
|
206 verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
207 horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER |
8
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
diff
changeset
|
208 preferredSize = Dimension(CPWIDTH, CPHEIGHT) |
21 | 209 background = frame!!.background |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
210 }, BorderLayout.CENTER) |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
211 pack() |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
212 setVisible(true) |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
213 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
214 } |
21 | 215 val queue = PasteboardQueue(con!!, 10) |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
216 val updater = UpdateIt(queue, 1000).apply { start() } |
21 | 217 inSwingThread { frame!!.addWindowListener(KillIt(updater)) } |
7 | 218 } |
219 | |
24 | 220 class MenuItemListener: ActionListener { |
221 override fun actionPerformed(e: ActionEvent) { | |
222 println(e.actionCommand + " selected") | |
223 } | |
224 } | |
225 | |
226 fun makeMenuBar() = JMenuBar().apply { | |
227 val al: ActionListener = MenuItemListener() | |
228 if (OS.type != OS.MAC) { | |
229 add(JMenu("File").apply { | |
230 add(JMenuItem("Quit").apply { | |
231 actionCommand = "File.Quit" | |
232 addActionListener(al) | |
233 makeShortcut(KeyEvent.VK_Q) | |
234 }) | |
235 }) | |
236 } | |
237 add(JMenu("Edit").apply { | |
238 add(JMenuItem("Clone").apply { | |
239 actionCommand = "Edit.Clone" | |
240 addActionListener(al) | |
241 makeShortcut(KeyEvent.VK_C) | |
242 }) | |
243 add(JMenuItem("Coerce…").apply { | |
244 actionCommand = "Edit.Coerce" | |
245 addActionListener(al) | |
246 makeShortcut(KeyEvent.VK_K) | |
247 }) | |
248 add(JMenuItem("Delete").apply { | |
249 actionCommand = "Edit.Delete" | |
250 addActionListener(al) | |
251 setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)) | |
252 }) | |
253 add(JMenuItem("Find…").apply { | |
254 actionCommand = "Edit.Find" | |
255 addActionListener(al) | |
256 makeShortcut(KeyEvent.VK_F) | |
257 }) | |
258 }) | |
259 if (OS.type != OS.MAC) { | |
260 add(JMenu("Help").apply { | |
261 add(JMenuItem("About ClipMan…").apply { | |
262 actionCommand = "Help.About" | |
263 addActionListener(al) | |
264 }) | |
265 }) | |
266 } | |
267 } | |
268 | |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
269 fun inSwingThread(block: () -> Unit) { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
270 SwingUtilities.invokeLater(Runnable(block)) |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
271 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
272 |
9
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
273 fun inSynSwingThread(block: () -> Unit) { |
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
274 val ready = Semaphore(0) |
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
275 inSwingThread { |
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
276 block() |
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
277 ready.release() |
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
278 } |
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
279 ready.acquire() |
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
280 } |
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
281 |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
282 fun JTextComponent.autoSize(width: Int): Unit { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
283 val SLOP = 10 |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
284 val dim = Dimension(width, width) |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
285 preferredSize = dim |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
286 size = dim |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
287 val r = modelToView(document.length) |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
288 preferredSize = Dimension(width, r.y + r.height + SLOP) |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
289 } |
24 | 290 |
291 val SC_KEY_MASK = Toolkit.getDefaultToolkit().menuShortcutKeyMask | |
292 fun JMenuItem.makeShortcut(key: Int): Unit { | |
293 setAccelerator(KeyStroke.getKeyStroke(key, SC_KEY_MASK)) | |
294 } |