# HG changeset patch # User David Barts # Date 1579471217 28800 # Node ID 7715ff59f0536a6b07083c48ab4160771c5b6ac8 # Parent fbb3a699959066b1f4fcc035f009e4bf4aedb4da Get layout of main display non-ugly. diff -r fbb3a6999590 -r 7715ff59f053 src/name/blackcap/clipman/Main.kt --- a/src/name/blackcap/clipman/Main.kt Sun Jan 19 12:46:42 2020 -0800 +++ b/src/name/blackcap/clipman/Main.kt Sun Jan 19 14:00:17 2020 -0800 @@ -16,9 +16,7 @@ import java.util.logging.Level import java.util.logging.Logger import javax.swing.* -import javax.swing.border.CompoundBorder -import javax.swing.border.EmptyBorder -import javax.swing.border.LineBorder +import javax.swing.border.* import javax.swing.text.JTextComponent import kotlin.concurrent.thread import org.jsoup.Jsoup @@ -27,6 +25,11 @@ /* name we call ourselves */ val MYNAME = "ClipMan" +/* default sizes */ +val CPWIDTH = 640 +val CPHEIGHT = 480 +val TPWIDTH = CPWIDTH - 60 + /* kills the updating thread (and does a system exit) when needed */ class KillIt(val thr: Thread) : WindowListener { // events we don't care about @@ -48,9 +51,9 @@ /* the updating thread */ class UpdateIt(val queue: PasteboardQueue, val interval: Int): Thread() { @Volatile var enabled = true + private val outerBorder = MatteBorder(3, 9, 9, 9, queue.parent.background) private val stdBorder = - CompoundBorder(LineBorder(queue.parent.background, 9), - CompoundBorder(LineBorder(Color.GRAY, 1), EmptyBorder(3, 3, 3, 3))) + CompoundBorder(LineBorder(Color.GRAY, 1), EmptyBorder(3, 3, 3, 3)) override fun run() { var oldContents = "" @@ -64,23 +67,35 @@ is PasteboardItem.HTML -> contents.plain } if (oldContents != newContents) { - var widget: JComponent = when(contents) { - is PasteboardItem.Plain -> JTextPane().apply { - contentType = "text/plain" - toolTipText = "Plain text" - text = contents.plain - font = Font(Font.MONOSPACED, Font.PLAIN, 14) - border = stdBorder - autoSize(600) - setEditable(false) + var widget = JPanel().apply { + layout = BoxLayout(this, BoxLayout.Y_AXIS) + background = queue.parent.background + border = outerBorder + } + when (contents) { + is PasteboardItem.Plain -> widget.run { + add(stdLabel("Plain text")) + add(JTextPane().apply { + contentType = "text/plain" + text = contents.plain + font = Font(Font.MONOSPACED, Font.PLAIN, 14) + border = stdBorder + autoSize(TPWIDTH) + setEditable(false) + alignmentX = JTextPane.LEFT_ALIGNMENT + }) } - is PasteboardItem.HTML -> JTextPane().apply { - contentType = "text/html" - toolTipText = "Styled text" - text = scrub(contents.html) - border = stdBorder - autoSize(600) - setEditable(false) + is PasteboardItem.HTML -> widget.run { + add(stdLabel("Styled text")) + add(JTextPane().apply { + contentType = "text/html" + toolTipText = "Styled text" + text = scrub(contents.html) + border = stdBorder + autoSize(TPWIDTH) + setEditable(false) + alignmentX = JTextPane.LEFT_ALIGNMENT + }) } } queue.add(QueueItem(widget, contents)) @@ -99,6 +114,11 @@ } } + private fun stdLabel(text: String) = JLabel(text).apply { + horizontalAlignment = JLabel.LEFT + alignmentX = JLabel.LEFT_ALIGNMENT + } + private fun scrub(html: String): String { return Jsoup.parse(html).run { select(":root>head>meta").remove() @@ -113,7 +133,7 @@ fun main(args: Array) { LOGGER.log(Level.INFO, "beginning execution") setLookFeel() - val frame = JFrame(MYNAME).apply { preferredSize = Dimension(640, 480) } + val frame = JFrame(MYNAME) val con = JPanel().apply { layout = BoxLayout(this, BoxLayout.Y_AXIS) border = EmptyBorder(9, 9, 9, 9) @@ -123,9 +143,9 @@ frame.apply { contentPane.add( JScrollPane(con).apply { - verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED + verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER - preferredSize = Dimension(640, 480) + preferredSize = Dimension(CPWIDTH, CPHEIGHT) background = frame.background }, BorderLayout.CENTER) pack()