diff src/name/blackcap/clipman/PasteboardView.kt @ 27:8aa2dfac27eb

Big reorg; compiled but untested.
author David Barts <n5jrn@me.com>
date Wed, 29 Jan 2020 10:50:07 -0800
parents
children f1fcc1281dad
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/name/blackcap/clipman/PasteboardView.kt	Wed Jan 29 10:50:07 2020 -0800
@@ -0,0 +1,104 @@
+/*
+ * View-related pasteboard stuff.
+ */
+package name.blackcap.clipman
+
+import java.awt.Color
+import java.awt.Dimension
+import javax.swing.*
+import javax.swing.border.*
+import javax.swing.text.html.StyleSheet
+import javax.swing.text.html.HTMLEditorKit
+
+/* border widths */
+val OUTER_BORDER_TOP = 3
+val OUTER_BORDER = 9
+val INNER_BORDER = 1
+val MARGIN_BORDER = 3
+
+/**
+ * What we use to display the text that is or was in the clipboard.
+ */
+class ClipText: JTextPane() {
+    private val normalBorder = CompoundBorder(LineBorder(Color.GRAY, INNER_BORDER),
+            EmptyBorder(MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER))
+    private val selectedBorder = CompoundBorder(LineBorder(Color.BLACK, INNER_BORDER+1),
+            EmptyBorder(MARGIN_BORDER-1, MARGIN_BORDER-1, MARGIN_BORDER-1, MARGIN_BORDER-1))
+    init {
+        border = normalBorder
+        setEditable(false)
+        alignmentX = JTextPane.LEFT_ALIGNMENT
+        resize()
+    }
+
+    /**
+     * We allow the text to be considered "selected;" such text is used
+     * as the target for any subsequent editing operation.
+     */
+    var selected: Boolean
+    get() {
+        return border === selectedBorder
+    }
+    set(value) {
+        border = if (value) { selectedBorder } else { normalBorder }
+    }
+
+    /**
+     * Refuse to fill the window in the vertical dimension, because doing
+     * so leads to a misleading display of the clipboard contents, falsely
+     * implying the existence of a bunch of trailing whitespace.
+     */
+    override fun getMaximumSize(): Dimension {
+        return Dimension(Int.MAX_VALUE, preferredSize.height)
+    }
+
+    /**
+     * Dynamically size or resize this view.
+     */
+    fun resize() {
+        autoSize(queue.v.parent.size.width -
+            2 * (PANEL_BORDER + OUTER_BORDER + INNER_BORDER + MARGIN_BORDER))
+    }
+}
+
+
+/**
+ * The stock HTMLEditorKit shares all style sheet data between all its
+ * instances. How unbelievably braindamaged. Correct that.
+ */
+class MyEditorKit: HTMLEditorKit() {
+    private var _styleSheet = defaultStyleSheet
+    override fun getStyleSheet() = _styleSheet
+    override fun setStyleSheet(value: StyleSheet) {
+        _styleSheet = value
+    }
+
+    /**
+     * Return the default style sheet that all HTMLEditorKit's come with.
+     */
+    val defaultStyleSheet: StyleSheet
+    get() {
+        return super.getStyleSheet()
+    }
+}
+
+/**
+ * Views are based on a JLabel and a ClipText (which we use for searching),
+ * wrapped in a JPanel that we use to display both.
+ */
+class PasteboardItemView(label: String, val searchable: ClipText) {
+    private val outerBorder =
+        MatteBorder(OUTER_BORDER_TOP, OUTER_BORDER, OUTER_BORDER, OUTER_BORDER,
+            queue.v.parent.background)
+
+    val contents = JPanel().apply {
+        layout = BoxLayout(this, BoxLayout.Y_AXIS)
+        background = queue.v.parent.background
+        border = outerBorder
+        add(JLabel(label).apply {
+            horizontalAlignment = JLabel.LEFT
+            alignmentX = JLabel.LEFT_ALIGNMENT
+        })
+        add(searchable)
+    }
+}