comparison src/name/blackcap/clipman/PasteboardQueue.kt @ 0:be282c48010a

Incomplete; checking it in as a backup.
author David Barts <n5jrn@me.com>
date Tue, 14 Jan 2020 14:07:19 -0800
parents
children fb224c3aebdf
comparison
equal deleted inserted replaced
-1:000000000000 0:be282c48010a
1 /*
2 * The queue of pasteboard items we manage. New stuff gets added to the
3 * tail, and old stuff truncated off the head.
4 */
5 package name.blackcap.clipman
6
7 import java.awt.Container
8 import java.util.Collections
9 import java.util.LinkedList
10 import javax.swing.*
11
12 /**
13 * A queue that tracks the data we display and the widgets used to
14 * display them. We never explicitly remove stuff from the queue,
15 * though items will get silently discarded to prevent the queue from
16 * exceeding the specified maximum size.
17 */
18 class PasteboardQueue(val parent: Container, maxSize: Int) {
19 private val queue = LinkedList<QueueItem>()
20 private var _maxSize = maxSize
21
22 /**
23 * The maximum allowed size of this queue. Attempts to make the queue
24 * larger than this size, or specifying a size smaller than the current
25 * size, will result in the oldest item(s) being discarded. A size less
26 * than or equal to zero means an unlimited size.
27 */
28 var maxSize: Int
29 get() { return _maxSize }
30 @Synchronized set(value) {
31 _maxSize = value
32 truncate(false)
33 }
34
35 /**
36 * Add a JComponent to the end of the queue.
37 * @param item JComponent to add
38 */
39 @Synchronized fun add(item: QueueItem) {
40 inSwingThread { parent.add(item.component) }
41 queue.addLast(item)
42 truncate(true)
43 }
44
45 private fun truncate(forceValidate: Boolean) {
46 if (_maxSize > 0) {
47 var size = queue.size
48 var dirty = forceValidate
49 while (size > _maxSize) {
50 var extra = queue.removeFirst().component
51 inSwingThread { parent.remove(extra) }
52 dirty = true
53 size -= 1
54 }
55 if (dirty) {
56 inSwingThread { parent.validate() }
57 }
58 }
59 }
60 }
61
62 /**
63 * An item in the above queue.
64 */
65 data class QueueItem(val component: JComponent, val contents: PasteboardItem)