comparison src/name/blackcap/clipman/PasteboardQueue.kt @ 21:c10a447b9e1b

Add some searching hooks.
author David Barts <n5jrn@me.com>
date Thu, 23 Jan 2020 00:02:07 -0800
parents 88703ca72fc3
children 8aa2dfac27eb
comparison
equal deleted inserted replaced
20:17296054c103 21:c10a447b9e1b
9 import java.util.Collections 9 import java.util.Collections
10 import java.util.LinkedList 10 import java.util.LinkedList
11 import java.util.logging.Level 11 import java.util.logging.Level
12 import java.util.logging.Logger 12 import java.util.logging.Logger
13 import javax.swing.* 13 import javax.swing.*
14 import javax.swing.text.JTextComponent
14 15
15 /** 16 /**
16 * A queue that tracks the data we display and the widgets used to 17 * A queue that tracks the data we display and the widgets used to
17 * display them. We never explicitly remove stuff from the queue, 18 * display them. We never explicitly remove stuff from the queue,
18 * though items will get silently discarded to prevent the queue from 19 * though items will get silently discarded to prevent the queue from
30 break 31 break
31 } 32 }
32 sp = sp.parent 33 sp = sp.parent
33 } 34 }
34 } 35 }
36
37 data class Offset(var inQueue: Int, var inItem: Int)
38 enum class Direction { FORWARDS, BACKWARDS }
35 39
36 /** 40 /**
37 * The maximum allowed size of this queue. Attempts to make the queue 41 * The maximum allowed size of this queue. Attempts to make the queue
38 * larger than this size, or specifying a size smaller than the current 42 * larger than this size, or specifying a size smaller than the current
39 * size, will result in the oldest item(s) being discarded. A size less 43 * size, will result in the oldest item(s) being discarded. A size less
60 } 64 }
61 queue.addLast(item) 65 queue.addLast(item)
62 truncate() 66 truncate()
63 } 67 }
64 68
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
65 private fun truncate() { 95 private fun truncate() {
66 if (_maxSize > 0) { 96 if (_maxSize > 0) {
67 var size = queue.size 97 var size = queue.size
68 var dirty = false 98 var dirty = false
69 while (size > _maxSize) { 99 while (size > _maxSize) {
80 } 110 }
81 111
82 /** 112 /**
83 * An item in the above queue. 113 * An item in the above queue.
84 */ 114 */
85 data class QueueItem(val component: JComponent, val contents: PasteboardItem) 115 data class QueueItem(
116 val component: JComponent,
117 val searchable: JTextComponent,
118 val contents: PasteboardItem)