Mercurial > cgi-bin > hgweb.cgi > ClipMan
annotate src/name/blackcap/clipman/PasteboardQueue.kt @ 23:14049bc97a7c
More package stuff.
author | David Barts <n5jrn@me.com> |
---|---|
date | Thu, 23 Jan 2020 14:29:32 -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 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.* |
21 | 14 import javax.swing.text.JTextComponent |
0
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 /** |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
17 * 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
|
18 * 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
|
19 * 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
|
20 * exceeding the specified maximum size. |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
21 */ |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
22 class PasteboardQueue(val parent: Container, maxSize: Int) { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
23 private val queue = LinkedList<QueueItem>() |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
24 private var _maxSize = maxSize |
16
88703ca72fc3
Make an efficiency improvement: cache the scrollPane.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
25 private var scrollPane: JScrollPane? = null |
88703ca72fc3
Make an efficiency improvement: cache the scrollPane.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
26 init { |
88703ca72fc3
Make an efficiency improvement: cache the scrollPane.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
27 var sp: Container? = parent |
88703ca72fc3
Make an efficiency improvement: cache the scrollPane.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
28 while (sp != null) { |
88703ca72fc3
Make an efficiency improvement: cache the scrollPane.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
29 if (sp is JScrollPane) { |
88703ca72fc3
Make an efficiency improvement: cache the scrollPane.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
30 scrollPane = sp |
88703ca72fc3
Make an efficiency improvement: cache the scrollPane.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
31 break |
88703ca72fc3
Make an efficiency improvement: cache the scrollPane.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
32 } |
88703ca72fc3
Make an efficiency improvement: cache the scrollPane.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
33 sp = sp.parent |
88703ca72fc3
Make an efficiency improvement: cache the scrollPane.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
34 } |
88703ca72fc3
Make an efficiency improvement: cache the scrollPane.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
35 } |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
36 |
21 | 37 data class Offset(var inQueue: Int, var inItem: Int) |
38 enum class Direction { FORWARDS, BACKWARDS } | |
39 | |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
40 /** |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
41 * 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
|
42 * 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
|
43 * 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
|
44 * 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
|
45 */ |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
46 var maxSize: Int |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
47 get() { return _maxSize } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
48 @Synchronized set(value) { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
49 _maxSize = value |
5 | 50 truncate() |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
51 } |
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 /** |
1
fb224c3aebdf
Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
54 * 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
|
55 * @param item QueueItem to add |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
56 */ |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
57 @Synchronized fun add(item: QueueItem) { |
1
fb224c3aebdf
Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
58 inSwingThread { |
fb224c3aebdf
Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
59 parent.add(item.component) |
16
88703ca72fc3
Make an efficiency improvement: cache the scrollPane.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
60 scrollPane?.run { |
2
6fb94eae32fa
Maybe this will auto-scroll reliably?
David Barts <n5jrn@me.com>
parents:
1
diff
changeset
|
61 validate() |
6fb94eae32fa
Maybe this will auto-scroll reliably?
David Barts <n5jrn@me.com>
parents:
1
diff
changeset
|
62 verticalScrollBar.run { value = maximum + 1 } |
1
fb224c3aebdf
Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
63 } |
fb224c3aebdf
Got it auto-scrolling to the bottom.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
64 } |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
65 queue.addLast(item) |
5 | 66 truncate() |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
67 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
68 |
21 | 69 /** |
70 * Find and highlight the next occurrence of the specified string | |
71 * @param string to search | |
72 * @param whether to search backwards (default forwards) | |
73 * @param case-folding flag (default true) | |
74 * @param starting point (0, 0) for forwards, (m, n) for backwards | |
75 * @return position where start of string was found, or null | |
76 */ | |
77 fun find(needle: String, direction: Direction = Direction.FORWARDS, | |
78 foldCase: Boolean = true, origin: Offset? = null): Offset? | |
79 { | |
80 /* canonicalize the origin */ | |
81 val norigin = if (origin == null) { | |
82 if (direction == Direction.FORWARDS) { | |
83 Offset(0, 0) | |
84 } else { | |
85 Offset(queue.size - 1, queue.last.searchable.document.length) | |
86 } | |
87 } else { | |
88 origin | |
89 } | |
90 | |
91 /* XXX - not finished */ | |
92 return null | |
93 } | |
94 | |
5 | 95 private fun truncate() { |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
96 if (_maxSize > 0) { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
97 var size = queue.size |
5 | 98 var dirty = false |
0
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
99 while (size > _maxSize) { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
100 var extra = queue.removeFirst().component |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
101 inSwingThread { parent.remove(extra) } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
102 dirty = true |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
103 size -= 1 |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
104 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
105 if (dirty) { |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
106 inSwingThread { parent.validate() } |
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 } |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
111 |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
112 /** |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
113 * An item in the above queue. |
be282c48010a
Incomplete; checking it in as a backup.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
114 */ |
21 | 115 data class QueueItem( |
116 val component: JComponent, | |
117 val searchable: JTextComponent, | |
118 val contents: PasteboardItem) |