comparison src/name/blackcap/clipman/Main.kt @ 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
comparison
equal deleted inserted replaced
7:fbb3a6999590 8:7715ff59f053
14 import java.awt.event.WindowListener 14 import java.awt.event.WindowListener
15 import java.util.Date 15 import java.util.Date
16 import java.util.logging.Level 16 import java.util.logging.Level
17 import java.util.logging.Logger 17 import java.util.logging.Logger
18 import javax.swing.* 18 import javax.swing.*
19 import javax.swing.border.CompoundBorder 19 import javax.swing.border.*
20 import javax.swing.border.EmptyBorder
21 import javax.swing.border.LineBorder
22 import javax.swing.text.JTextComponent 20 import javax.swing.text.JTextComponent
23 import kotlin.concurrent.thread 21 import kotlin.concurrent.thread
24 import org.jsoup.Jsoup 22 import org.jsoup.Jsoup
25 import org.jsoup.nodes.* 23 import org.jsoup.nodes.*
26 24
27 /* name we call ourselves */ 25 /* name we call ourselves */
28 val MYNAME = "ClipMan" 26 val MYNAME = "ClipMan"
27
28 /* default sizes */
29 val CPWIDTH = 640
30 val CPHEIGHT = 480
31 val TPWIDTH = CPWIDTH - 60
29 32
30 /* kills the updating thread (and does a system exit) when needed */ 33 /* kills the updating thread (and does a system exit) when needed */
31 class KillIt(val thr: Thread) : WindowListener { 34 class KillIt(val thr: Thread) : WindowListener {
32 // events we don't care about 35 // events we don't care about
33 override fun windowActivated(e: WindowEvent) {} 36 override fun windowActivated(e: WindowEvent) {}
46 } 49 }
47 50
48 /* the updating thread */ 51 /* the updating thread */
49 class UpdateIt(val queue: PasteboardQueue, val interval: Int): Thread() { 52 class UpdateIt(val queue: PasteboardQueue, val interval: Int): Thread() {
50 @Volatile var enabled = true 53 @Volatile var enabled = true
54 private val outerBorder = MatteBorder(3, 9, 9, 9, queue.parent.background)
51 private val stdBorder = 55 private val stdBorder =
52 CompoundBorder(LineBorder(queue.parent.background, 9), 56 CompoundBorder(LineBorder(Color.GRAY, 1), EmptyBorder(3, 3, 3, 3))
53 CompoundBorder(LineBorder(Color.GRAY, 1), EmptyBorder(3, 3, 3, 3)))
54 57
55 override fun run() { 58 override fun run() {
56 var oldContents = "" 59 var oldContents = ""
57 var newContents = "" 60 var newContents = ""
58 while (true) { 61 while (true) {
62 newContents = when (contents) { 65 newContents = when (contents) {
63 is PasteboardItem.Plain -> contents.plain 66 is PasteboardItem.Plain -> contents.plain
64 is PasteboardItem.HTML -> contents.plain 67 is PasteboardItem.HTML -> contents.plain
65 } 68 }
66 if (oldContents != newContents) { 69 if (oldContents != newContents) {
67 var widget: JComponent = when(contents) { 70 var widget = JPanel().apply {
68 is PasteboardItem.Plain -> JTextPane().apply { 71 layout = BoxLayout(this, BoxLayout.Y_AXIS)
69 contentType = "text/plain" 72 background = queue.parent.background
70 toolTipText = "Plain text" 73 border = outerBorder
71 text = contents.plain 74 }
72 font = Font(Font.MONOSPACED, Font.PLAIN, 14) 75 when (contents) {
73 border = stdBorder 76 is PasteboardItem.Plain -> widget.run {
74 autoSize(600) 77 add(stdLabel("Plain text"))
75 setEditable(false) 78 add(JTextPane().apply {
79 contentType = "text/plain"
80 text = contents.plain
81 font = Font(Font.MONOSPACED, Font.PLAIN, 14)
82 border = stdBorder
83 autoSize(TPWIDTH)
84 setEditable(false)
85 alignmentX = JTextPane.LEFT_ALIGNMENT
86 })
76 } 87 }
77 is PasteboardItem.HTML -> JTextPane().apply { 88 is PasteboardItem.HTML -> widget.run {
78 contentType = "text/html" 89 add(stdLabel("Styled text"))
79 toolTipText = "Styled text" 90 add(JTextPane().apply {
80 text = scrub(contents.html) 91 contentType = "text/html"
81 border = stdBorder 92 toolTipText = "Styled text"
82 autoSize(600) 93 text = scrub(contents.html)
83 setEditable(false) 94 border = stdBorder
95 autoSize(TPWIDTH)
96 setEditable(false)
97 alignmentX = JTextPane.LEFT_ALIGNMENT
98 })
84 } 99 }
85 } 100 }
86 queue.add(QueueItem(widget, contents)) 101 queue.add(QueueItem(widget, contents))
87 oldContents = newContents 102 oldContents = newContents
88 } 103 }
97 return 112 return
98 } 113 }
99 } 114 }
100 } 115 }
101 116
117 private fun stdLabel(text: String) = JLabel(text).apply {
118 horizontalAlignment = JLabel.LEFT
119 alignmentX = JLabel.LEFT_ALIGNMENT
120 }
121
102 private fun scrub(html: String): String { 122 private fun scrub(html: String): String {
103 return Jsoup.parse(html).run { 123 return Jsoup.parse(html).run {
104 select(":root>head>meta").remove() 124 select(":root>head>meta").remove()
105 outputSettings() 125 outputSettings()
106 .charset(CHARSET_NAME) 126 .charset(CHARSET_NAME)
111 } 131 }
112 132
113 fun main(args: Array<String>) { 133 fun main(args: Array<String>) {
114 LOGGER.log(Level.INFO, "beginning execution") 134 LOGGER.log(Level.INFO, "beginning execution")
115 setLookFeel() 135 setLookFeel()
116 val frame = JFrame(MYNAME).apply { preferredSize = Dimension(640, 480) } 136 val frame = JFrame(MYNAME)
117 val con = JPanel().apply { 137 val con = JPanel().apply {
118 layout = BoxLayout(this, BoxLayout.Y_AXIS) 138 layout = BoxLayout(this, BoxLayout.Y_AXIS)
119 border = EmptyBorder(9, 9, 9, 9) 139 border = EmptyBorder(9, 9, 9, 9)
120 background = frame.background 140 background = frame.background
121 } 141 }
122 inSwingThread { 142 inSwingThread {
123 frame.apply { 143 frame.apply {
124 contentPane.add( 144 contentPane.add(
125 JScrollPane(con).apply { 145 JScrollPane(con).apply {
126 verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED 146 verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
127 horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER 147 horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
128 preferredSize = Dimension(640, 480) 148 preferredSize = Dimension(CPWIDTH, CPHEIGHT)
129 background = frame.background 149 background = frame.background
130 }, BorderLayout.CENTER) 150 }, BorderLayout.CENTER)
131 pack() 151 pack()
132 setVisible(true) 152 setVisible(true)
133 } 153 }