view src/name/blackcap/clipman/PasteboardQueue.kt @ 16:88703ca72fc3

Make an efficiency improvement: cache the scrollPane.
author David Barts <n5jrn@me.com>
date Tue, 21 Jan 2020 13:07:35 -0800
parents d832c7b2bfd0
children c10a447b9e1b
line wrap: on
line source

/*
 * The queue of pasteboard items we manage. New stuff gets added to the
 * tail, and old stuff truncated off the head.
 */
package name.blackcap.clipman

import java.awt.Container
import java.awt.Rectangle
import java.util.Collections
import java.util.LinkedList
import java.util.logging.Level
import java.util.logging.Logger
import javax.swing.*

/**
 * A queue that tracks the data we display and the widgets used to
 * display them. We never explicitly remove stuff from the queue,
 * though items will get silently discarded to prevent the queue from
 * exceeding the specified maximum size.
 */
class PasteboardQueue(val parent: Container, maxSize: Int) {
    private val queue = LinkedList<QueueItem>()
    private var _maxSize = maxSize
    private var scrollPane: JScrollPane? = null
    init {
        var sp: Container? = parent
        while (sp != null) {
            if (sp is JScrollPane) {
                scrollPane = sp
                break
            }
            sp = sp.parent
        }
    }

    /**
     * The maximum allowed size of this queue. Attempts to make the queue
     * larger than this size, or specifying a size smaller than the current
     * size, will result in the oldest item(s) being discarded. A size less
     * than or equal to zero means an unlimited size.
     */
    var maxSize: Int
        get() { return _maxSize }
        @Synchronized set(value) {
            _maxSize = value
            truncate()
        }

    /**
     * Add a QueueItem to the end of the queue.
     * @param item QueueItem to add
     */
    @Synchronized fun add(item: QueueItem) {
        inSwingThread {
            parent.add(item.component)
            scrollPane?.run {
                validate()
                verticalScrollBar.run { value = maximum + 1 }
            }
        }
        queue.addLast(item)
        truncate()
    }

    private fun truncate() {
        if (_maxSize > 0) {
            var size = queue.size
            var dirty = false
            while (size > _maxSize) {
                var extra = queue.removeFirst().component
                inSwingThread { parent.remove(extra) }
                dirty = true
                size -= 1
            }
            if (dirty) {
                inSwingThread { parent.validate() }
            }
        }
    }
}

/**
 * An item in the above queue.
 */
data class QueueItem(val component: JComponent, val contents: PasteboardItem)