view src/name/blackcap/clipman/PasteboardView.kt @ 58:88056f373a94

Set app name, deal with some deprecation.
author David Barts <n5jrn@me.com>
date Sat, 26 Mar 2022 23:45:34 -0700
parents 19d9da731c43
children
line wrap: on
line source

/*
 * View-related pasteboard stuff.
 */
package name.blackcap.clipman

import java.awt.Color
import java.awt.Dimension
import javax.swing.*
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(val basedOn: PasteboardItem): JTextPane() {
    private val normalBorder = BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(Color.GRAY, INNER_BORDER),
            BorderFactory.createEmptyBorder(MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER))
    private val selectedBorder = BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(Color.BLACK, INNER_BORDER+1),
            BorderFactory.createEmptyBorder(MARGIN_BORDER-1, MARGIN_BORDER-1, MARGIN_BORDER-1, MARGIN_BORDER-1))
    init {
        border = normalBorder
        setEditable(false)
        alignmentX = JTextPane.LEFT_ALIGNMENT
    }

    /**
     * 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(Application.queue.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 =
        BorderFactory.createMatteBorder(OUTER_BORDER_TOP, OUTER_BORDER, OUTER_BORDER, OUTER_BORDER,
            Application.queue.parent.background)

    val contents = JPanel().apply {
        layout = BoxLayout(this, BoxLayout.Y_AXIS)
        background = Application.queue.parent.background
        border = outerBorder
        add(JLabel(label).apply {
            horizontalAlignment = JLabel.LEFT
            alignmentX = JLabel.LEFT_ALIGNMENT
        })
        add(searchable)
    }
}