Mercurial > cgi-bin > hgweb.cgi > ClipMan
annotate src/name/blackcap/clipman/Main.kt @ 65:ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sun, 12 Jan 2025 11:32:17 -0800 |
parents | 8c6d6ad92aa1 |
children |
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.Container |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
8 import java.awt.Dimension |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
9 import java.awt.Font |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
10 import java.awt.datatransfer.* |
27
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
11 import java.awt.event.MouseEvent |
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
12 import java.awt.event.MouseListener |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
13 import java.awt.event.WindowEvent |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
14 import java.awt.event.WindowListener |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
15 import java.util.Date |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
16 import java.util.logging.Level |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
17 import java.util.logging.Logger |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
18 import javax.swing.* |
13
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
19 import javax.swing.text.html.StyleSheet |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
20 import kotlin.concurrent.thread |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
21 import org.jsoup.Jsoup |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
22 import org.jsoup.nodes.* |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
23 |
7 | 24 /* name we call ourselves */ |
25 val MYNAME = "ClipMan" | |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
26 |
8
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
diff
changeset
|
27 /* default sizes */ |
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
diff
changeset
|
28 val CPWIDTH = 640 |
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
diff
changeset
|
29 val CPHEIGHT = 480 |
9
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
30 |
27
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
31 /* width of main panel border */ |
9
8fcff14defa2
Stomp out race conditions and set width adaptively.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
32 val PANEL_BORDER = 9 |
8
7715ff59f053
Get layout of main display non-ugly.
David Barts <n5jrn@me.com>
parents:
7
diff
changeset
|
33 |
13
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
34 /* 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
|
35 val MONO_SIZE = 14 |
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
36 val PROP_SIZE = 16 |
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
37 |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
38 /* kills the updating thread (and does a system exit) when needed */ |
27
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
39 class KillIt() : WindowListener { |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
40 // events we don't care about |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
41 override fun windowActivated(e: WindowEvent) {} |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
42 override fun windowClosed(e: WindowEvent) {} |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
43 override fun windowDeactivated(e: WindowEvent) {} |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
44 override fun windowDeiconified(e: WindowEvent) {} |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
45 override fun windowIconified(e: WindowEvent) {} |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
46 override fun windowOpened(e: WindowEvent) {} |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
47 |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
48 // and the one we do |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
49 override fun windowClosing(e: WindowEvent) { |
6 | 50 LOGGER.log(Level.INFO, "execution complete") |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
51 System.exit(0) |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
52 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
53 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
54 |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
55 /* the updating thread */ |
27
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
56 class UpdateIt(val interval: Int): Thread(), MouseListener { |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
57 @Volatile var enabled = true |
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 override fun run() { |
17
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
60 var oldContents: PasteboardItem? = null |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
61 while (true) { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
62 if (enabled) { |
65
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
63 var kontents: PasteboardItem? = null |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
64 inSynSwingThread { |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
65 kontents = PasteboardItem.read() |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
66 } |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
67 val contents = kontents |
17
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
68 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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 } |
65
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
74 inSynSwingThread { |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
75 val piv = if (html == null) { |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
76 PasteboardItemView("Plain text", ClipText(contents).apply { |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
77 contentType = "text/plain" |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
78 text = plain |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
79 font = Font(Font.MONOSPACED, Font.PLAIN, MONO_SIZE) |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
80 resize() |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
81 }) |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
82 } else { |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
83 val (dhtml, style) = preproc(html) |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
84 val hek = MyEditorKit().apply { |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
85 style.addStyleSheet(defaultStyleSheet) |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
86 styleSheet = style |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
87 } |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
88 PasteboardItemView("Styled text", ClipText(contents).apply { |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
89 editorKit = hek |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
90 text = dhtml |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
91 resize() |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
92 }) |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
93 } |
65
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
94 piv.searchable.addMouseListener(this) |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
95 Application.queue.add(QueueItem(contents, piv)) |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
96 } |
17
9dd58db4d15a
Only convert RTF to HTML if needed. Much more efficient.
David Barts <n5jrn@me.com>
parents:
15
diff
changeset
|
97 oldContents = contents |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
98 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
99 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
100 if (Thread.interrupted()) { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
101 return |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
102 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
103 try { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
104 Thread.sleep(interval - System.currentTimeMillis() % interval) |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
105 } catch (e: InterruptedException) { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
106 return |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
107 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
108 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
109 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
110 |
13
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
111 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
|
112 val sty = StyleSheet().apply { |
43
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
113 addRule("body { font-family: serif; font-size: ${PROP_SIZE}; }") |
339e2da5bf83
Simplified the settings. Compiles, not tested.
David Barts <n5jrn@me.com>
parents:
41
diff
changeset
|
114 addRule("code, kbd, pre, samp, tt { font-family: monospace; font-size: ${MONO_SIZE}; }") |
13
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
115 } |
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
116 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
|
117 select("style").forEach { |
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
118 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
|
119 } |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
120 select(":root>head>meta").remove() |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
121 outputSettings() |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
122 .charset(CHARSET_NAME) |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
123 .syntax(Document.OutputSettings.Syntax.xml) |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
124 outerHtml() |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
125 } |
13
fe0fcfc8b2aa
Braindead POS wasn't honoring <style> tags; fixed that.
David Barts <n5jrn@me.com>
parents:
12
diff
changeset
|
126 return Pair(scrubbed, sty) |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
127 } |
27
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
128 |
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
129 /* MouseListener methods */ |
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
130 |
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
131 override fun mouseClicked(e: MouseEvent) { |
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
132 val source = e.getSource() as? ClipText |
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
133 if (source == null) { |
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
134 return |
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
135 } |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
136 Application.queue.deselectAll() |
27
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
137 source.selected = true |
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
138 source.validate() |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
139 Application.anyRequired.enable() |
31 | 140 source.basedOn.let { |
141 if (it is PasteboardItem.HTML || it is PasteboardItem.RTF) { | |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
142 Application.styledRequired.enable() |
35
5f8475b37e23
Got it correctly enabling and disabling menu items.
David Barts <n5jrn@me.com>
parents:
31
diff
changeset
|
143 } else { |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
144 Application.styledRequired.disable() |
31 | 145 } |
146 } | |
27
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
147 } |
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
148 |
65
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
149 override fun mousePressed(e: MouseEvent) { |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
150 maybeShowPopup(e) |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
151 } |
27
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
152 |
65
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
153 override fun mouseReleased(e: MouseEvent) { |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
154 maybeShowPopup(e) |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
155 } |
27
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
156 |
65
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
157 private fun maybeShowPopup(e: MouseEvent) { |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
158 if (e.isPopupTrigger()) { |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
159 Application.popupMenu.show(e.component, e.x, e.y) |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
160 } |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
161 } |
27
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
162 |
65
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
163 override fun mouseEntered(e: MouseEvent) { } |
ca0fab758ff9
Hopefully fix the race conditions that sometimes make it crash.
David Barts <n5jrn@me.com>
parents:
63
diff
changeset
|
164 override fun mouseExited(e: MouseEvent) { } |
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 |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
167 object Application { |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
168 /* name we call ourselves */ |
48 | 169 val MYNAME = "ClipMan" |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
170 |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
171 /* global UI objects, must be created on the Swing thread */ |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
172 var queue: PasteboardQueue by setOnce() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
173 var frame: JFrame by setOnce() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
174 var coerceDialog: CoerceDialog by setOnce() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
175 var menuItemListener: MenuItemListener by setOnce() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
176 var popupMenu: MyPopupMenu by setOnce() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
177 var searchDialog: SearchDialog by setOnce() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
178 var settingsDialog: SettingsDialog by setOnce() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
179 |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
180 /* used by the menus, but not themselves Swing objects */ |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
181 val anyRequired = SelectionRequired() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
182 val styledRequired = SelectionRequired() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
183 |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
184 fun initialize() { |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
185 /* make ourselves look more native */ |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
186 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
187 |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
188 /* initialize reusable GUI objects */ |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
189 frame = JFrame(MYNAME) /* must init this gui object first */ |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
190 coerceDialog = CoerceDialog() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
191 menuItemListener = MenuItemListener() /* must init before menus */ |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
192 popupMenu = MyPopupMenu() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
193 searchDialog = SearchDialog() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
194 settingsDialog = SettingsDialog() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
195 |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
196 /* set up the main frame */ |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
197 val con = JPanel().apply { |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
198 layout = BoxLayout(this, BoxLayout.Y_AXIS) |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
199 border = BorderFactory.createEmptyBorder(PANEL_BORDER, PANEL_BORDER, PANEL_BORDER, PANEL_BORDER) |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
200 background = frame.background |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
201 } |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
202 frame.apply { |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
203 jMenuBar = MyMenuBar() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
204 contentPane.add( |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
205 JScrollPane(con).apply { |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
206 verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
207 horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
208 preferredSize = Dimension(CPWIDTH, CPHEIGHT) |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
209 background = frame.background |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
210 }, BorderLayout.CENTER) |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
211 pack() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
212 setVisible(true) |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
213 addWindowListener(KillIt()) |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
214 } |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
215 setMacMenus() |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
216 |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
217 /* launch the updating thread */ |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
218 queue = PasteboardQueue(con, settingsDialog.qLength) |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
219 UpdateIt(1000).apply { start() } |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
220 } |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
221 } |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
222 |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
223 |
27
8aa2dfac27eb
Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
24
diff
changeset
|
224 /* entry point */ |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
225 fun main(args: Array<String>) { |
63 | 226 setUpErrors() |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
227 LOGGER.log(Level.INFO, "beginning execution") |
24 | 228 if (OS.type == OS.MAC) { |
229 System.setProperty("apple.laf.useScreenMenuBar", "true") | |
58
88056f373a94
Set app name, deal with some deprecation.
David Barts <n5jrn@me.com>
parents:
48
diff
changeset
|
230 System.setProperty("apple.awt.application.name", MYNAME) |
24 | 231 } |
47
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
232 inSwingThread { |
19d9da731c43
Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents:
43
diff
changeset
|
233 Application.initialize() |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
234 } |
24 | 235 } |
63 | 236 |
237 private fun setUpErrors() { | |
238 val ps = java.io.PrintStream( | |
239 java.io.FileOutputStream(ERR_FILE), true, CHARSET) | |
240 System.setOut(ps) | |
241 System.setErr(ps) | |
242 } |