diff 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
line wrap: on
line diff
--- a/src/name/blackcap/clipman/PasteboardQueue.kt	Tue Jan 21 23:38:10 2020 -0800
+++ b/src/name/blackcap/clipman/PasteboardQueue.kt	Thu Jan 23 00:02:07 2020 -0800
@@ -11,6 +11,7 @@
 import java.util.logging.Level
 import java.util.logging.Logger
 import javax.swing.*
+import javax.swing.text.JTextComponent
 
 /**
  * A queue that tracks the data we display and the widgets used to
@@ -33,6 +34,9 @@
         }
     }
 
+    data class Offset(var inQueue: Int, var inItem: Int)
+    enum class Direction { FORWARDS, BACKWARDS }
+
     /**
      * The maximum allowed size of this queue. Attempts to make the queue
      * larger than this size, or specifying a size smaller than the current
@@ -62,6 +66,32 @@
         truncate()
     }
 
+    /**
+     * Find and highlight the next occurrence of the specified string
+     * @param string to search
+     * @param whether to search backwards (default forwards)
+     * @param case-folding flag (default true)
+     * @param starting point (0, 0) for forwards, (m, n) for backwards
+     * @return position where start of string was found, or null
+     */
+    fun find(needle: String, direction: Direction = Direction.FORWARDS,
+        foldCase: Boolean = true, origin: Offset? = null): Offset?
+    {
+        /* canonicalize the origin */
+        val norigin = if (origin == null) {
+            if (direction == Direction.FORWARDS) {
+                Offset(0, 0)
+            } else {
+                Offset(queue.size - 1, queue.last.searchable.document.length)
+            }
+        } else {
+            origin
+        }
+
+        /* XXX - not finished */
+        return null
+    }
+
     private fun truncate() {
         if (_maxSize > 0) {
             var size = queue.size
@@ -82,4 +112,7 @@
 /**
  * An item in the above queue.
  */
-data class QueueItem(val component: JComponent, val contents: PasteboardItem)
+data class QueueItem(
+    val component: JComponent,
+    val searchable: JTextComponent,
+    val contents: PasteboardItem)