annotate src/name/blackcap/clipman/PasteboardQueue.kt @ 9:8fcff14defa2

Stomp out race conditions and set width adaptively.
author David Barts <n5jrn@me.com>
date Sun, 19 Jan 2020 15:43:01 -0800
parents d832c7b2bfd0
children 88703ca72fc3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 /*
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
2 * The queue of pasteboard items we manage. New stuff gets added to the
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
3 * tail, and old stuff truncated off the head.
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 */
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
5 package name.blackcap.clipman
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
6
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
7 import java.awt.Container
1
fb224c3aebdf Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
8 import java.awt.Rectangle
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
9 import java.util.Collections
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
10 import java.util.LinkedList
1
fb224c3aebdf Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
11 import java.util.logging.Level
fb224c3aebdf Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
12 import java.util.logging.Logger
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
13 import javax.swing.*
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
14
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 /**
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 * A queue that tracks the data we display and the widgets used to
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 * display them. We never explicitly remove stuff from the queue,
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 * though items will get silently discarded to prevent the queue from
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
19 * exceeding the specified maximum size.
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 */
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
21 class PasteboardQueue(val parent: Container, maxSize: Int) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 private val queue = LinkedList<QueueItem>()
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 private var _maxSize = maxSize
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
24
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 /**
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 * The maximum allowed size of this queue. Attempts to make the queue
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
27 * larger than this size, or specifying a size smaller than the current
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
28 * size, will result in the oldest item(s) being discarded. A size less
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
29 * than or equal to zero means an unlimited size.
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
30 */
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
31 var maxSize: Int
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
32 get() { return _maxSize }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 @Synchronized set(value) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 _maxSize = value
5
d832c7b2bfd0 Clean out some deadwood.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
35 truncate()
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
36 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
37
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 /**
1
fb224c3aebdf Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
39 * Add a QueueItem to the end of the queue.
fb224c3aebdf Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
40 * @param item QueueItem to add
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 */
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 @Synchronized fun add(item: QueueItem) {
1
fb224c3aebdf Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
43 inSwingThread {
fb224c3aebdf Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
44 parent.add(item.component)
fb224c3aebdf Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
45 /* XXX - assumes there is a JScrollPane above us in the tree */
fb224c3aebdf Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
46 var sp = parent.parent
fb224c3aebdf Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
47 while (sp != null && !(sp is JScrollPane)) {
fb224c3aebdf Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
48 sp = sp.parent
fb224c3aebdf Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
49 }
fb224c3aebdf Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
50 (sp as JScrollPane).run {
2
6fb94eae32fa Maybe this will auto-scroll reliably?
David Barts <n5jrn@me.com>
parents: 1
diff changeset
51 validate()
6fb94eae32fa Maybe this will auto-scroll reliably?
David Barts <n5jrn@me.com>
parents: 1
diff changeset
52 verticalScrollBar.run { value = maximum + 1 }
1
fb224c3aebdf Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
53 }
fb224c3aebdf Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
54 }
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
55 queue.addLast(item)
5
d832c7b2bfd0 Clean out some deadwood.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
56 truncate()
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
58
5
d832c7b2bfd0 Clean out some deadwood.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
59 private fun truncate() {
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 if (_maxSize > 0) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 var size = queue.size
5
d832c7b2bfd0 Clean out some deadwood.
David Barts <n5jrn@me.com>
parents: 2
diff changeset
62 var dirty = false
0
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 while (size > _maxSize) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 var extra = queue.removeFirst().component
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 inSwingThread { parent.remove(extra) }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 dirty = true
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 size -= 1
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
68 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 if (dirty) {
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
70 inSwingThread { parent.validate() }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 }
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
75
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
76 /**
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
77 * An item in the above queue.
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
78 */
be282c48010a Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 data class QueueItem(val component: JComponent, val contents: PasteboardItem)