changeset 21:c10a447b9e1b

Add some searching hooks.
author David Barts <n5jrn@me.com>
date Thu, 23 Jan 2020 00:02:07 -0800
parents 17296054c103
children 829769cb1c13
files src/name/blackcap/clipman/Main.kt src/name/blackcap/clipman/PasteboardQueue.kt
diffstat 2 files changed, 56 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/src/name/blackcap/clipman/Main.kt	Tue Jan 21 23:38:10 2020 -0800
+++ b/src/name/blackcap/clipman/Main.kt	Thu Jan 23 00:02:07 2020 -0800
@@ -81,7 +81,6 @@
     }
 }
 
-
 /* the updating thread */
 class UpdateIt(val queue: PasteboardQueue, val interval: Int): Thread() {
     @Volatile var enabled = true
@@ -109,10 +108,11 @@
                         is PasteboardItem.HTML -> Pair(null, contents.html)
                         is PasteboardItem.RTF -> Pair(contents.plain, contents.html)
                     }
+                    var searchable: JTextComponent? = null
                     if (html == null) {
                         widget.run {
                             add(stdLabel("Plain text"))
-                            add(ClipText().apply {
+                            searchable = ClipText().apply {
                                 contentType = "text/plain"
                                 text = plain
                                 font = Font(Font.MONOSPACED, Font.PLAIN, MONO_SIZE)
@@ -120,7 +120,8 @@
                                 autoSize(stdWidth)
                                 setEditable(false)
                                 alignmentX = JTextPane.LEFT_ALIGNMENT
-                            })
+                            }
+                            add(searchable)
                         }
                     } else {
                         widget.run {
@@ -130,17 +131,18 @@
                                 style.addStyleSheet(defaultStyleSheet)
                                 styleSheet = style
                             }
-                            add(ClipText().apply {
+                            searchable = ClipText().apply {
                                 editorKit = hek
                                 text = dhtml
                                 border = stdBorder
                                 autoSize(stdWidth)
                                 setEditable(false)
                                 alignmentX = JTextPane.LEFT_ALIGNMENT
-                            })
+                            }
+                            add(searchable)
                         }
                     }
-                    queue.add(QueueItem(widget, contents))
+                    queue.add(QueueItem(widget, searchable!!, contents))
                     oldContents = contents
                 }
             }
@@ -181,35 +183,31 @@
 
 fun main(args: Array<String>) {
     LOGGER.log(Level.INFO, "beginning execution")
-    setLookFeel()
-    val frame = JFrame(MYNAME)
-    val con = JPanel().apply {
-        layout = BoxLayout(this, BoxLayout.Y_AXIS)
-        border = EmptyBorder(PANEL_BORDER, PANEL_BORDER, PANEL_BORDER, PANEL_BORDER)
-        background = frame.background
-    }
+    var frame: JFrame? = null
+    var con: JPanel? = null
     inSynSwingThread {
-        frame.apply {
+        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
+        frame = JFrame(MYNAME)
+        con = JPanel().apply {
+            layout = BoxLayout(this, BoxLayout.Y_AXIS)
+            border = EmptyBorder(PANEL_BORDER, PANEL_BORDER, PANEL_BORDER, PANEL_BORDER)
+            background = frame!!.background
+        }
+        frame!!.apply {
             contentPane.add(
-                JScrollPane(con).apply {
+                JScrollPane(con!!).apply {
                     verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
                     horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
                     preferredSize = Dimension(CPWIDTH, CPHEIGHT)
-                    background = frame.background
+                    background = frame!!.background
                 }, BorderLayout.CENTER)
             pack()
             setVisible(true)
         }
     }
-    val queue = PasteboardQueue(con, 10)
+    val queue = PasteboardQueue(con!!, 10)
     val updater = UpdateIt(queue, 1000).apply { start() }
-    inSwingThread { frame.addWindowListener(KillIt(updater)) }
-}
-
-fun setLookFeel() {
-    inSwingThread {
-        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
-    }
+    inSwingThread { frame!!.addWindowListener(KillIt(updater)) }
 }
 
 fun inSwingThread(block: () -> Unit) {
--- 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)