changeset 8:7715ff59f053

Get layout of main display non-ugly.
author David Barts <n5jrn@me.com>
date Sun, 19 Jan 2020 14:00:17 -0800
parents fbb3a6999590
children 8fcff14defa2
files src/name/blackcap/clipman/Main.kt
diffstat 1 files changed, 44 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- 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<String>) {
     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()