comparison src/name/blackcap/clipman/Main.kt @ 17:9dd58db4d15a

Only convert RTF to HTML if needed. Much more efficient.
author David Barts <n5jrn@me.com>
date Tue, 21 Jan 2020 16:24:18 -0800
parents 732f92dc3bc6
children 5e0d1fe61da9
comparison
equal deleted inserted replaced
16:88703ca72fc3 17:9dd58db4d15a
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.concurrent.Semaphore 16 import java.util.concurrent.Semaphore
17 import java.util.logging.Level 17 import java.util.logging.Level
18 import java.util.logging.Logger 18 import java.util.logging.Logger
19 import java.util.zip.CRC32
19 import javax.swing.* 20 import javax.swing.*
20 import javax.swing.border.* 21 import javax.swing.border.*
21 import javax.swing.text.JTextComponent 22 import javax.swing.text.JTextComponent
22 import javax.swing.text.html.StyleSheet 23 import javax.swing.text.html.StyleSheet
23 import javax.swing.text.html.HTMLEditorKit 24 import javax.swing.text.html.HTMLEditorKit
76 private val stdBorder = 77 private val stdBorder =
77 CompoundBorder(LineBorder(Color.GRAY, INNER_BORDER), 78 CompoundBorder(LineBorder(Color.GRAY, INNER_BORDER),
78 EmptyBorder(MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER)) 79 EmptyBorder(MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER, MARGIN_BORDER))
79 80
80 override fun run() { 81 override fun run() {
81 var oldContents = "" 82 var oldContents: PasteboardItem? = null
82 var newContents = ""
83 while (true) { 83 while (true) {
84 if (enabled) { 84 if (enabled) {
85 var contents = PasteboardItem.read() 85 var contents = PasteboardItem.read()
86 if (contents != null) { 86 if ((contents != null) && (contents != oldContents)) {
87 newContents = when (contents) { 87 val stdWidth = queue.parent.size.width - 2 * (PANEL_BORDER+OUTER_BORDER+INNER_BORDER+MARGIN_BORDER)
88 is PasteboardItem.Plain -> contents.plain 88 val widget = JPanel().apply {
89 is PasteboardItem.HTML -> contents.html 89 layout = BoxLayout(this, BoxLayout.Y_AXIS)
90 background = queue.parent.background
91 border = outerBorder
90 } 92 }
91 if (oldContents != newContents) { 93 val (plain, html) = when(contents) {
92 var stdWidth: Int? = null 94 is PasteboardItem.Plain -> Pair(contents.plain, null)
93 inSynSwingThread { 95 is PasteboardItem.HTML -> Pair(null, contents.html)
94 stdWidth = queue.parent.size.width - 2 * (PANEL_BORDER+OUTER_BORDER+INNER_BORDER+MARGIN_BORDER) 96 is PasteboardItem.RTF -> Pair(contents.plain, contents.html)
97 }
98 if (html == null) {
99 widget.run {
100 add(stdLabel("Plain text"))
101 add(ClipText().apply {
102 contentType = "text/plain"
103 text = plain
104 font = Font(Font.MONOSPACED, Font.PLAIN, MONO_SIZE)
105 border = stdBorder
106 autoSize(stdWidth)
107 setEditable(false)
108 alignmentX = JTextPane.LEFT_ALIGNMENT
109 })
95 } 110 }
96 var widget = JPanel().apply { 111 } else {
97 layout = BoxLayout(this, BoxLayout.Y_AXIS) 112 widget.run {
98 background = queue.parent.background 113 add(stdLabel("Styled text"))
99 border = outerBorder 114 val (dhtml, style) = preproc(html)
115 val hek = HTMLEditorKit().apply {
116 style.addStyleSheet(styleSheet)
117 styleSheet = style
118 }
119 add(ClipText().apply {
120 editorKit = hek
121 text = dhtml
122 border = stdBorder
123 autoSize(stdWidth)
124 setEditable(false)
125 alignmentX = JTextPane.LEFT_ALIGNMENT
126 })
100 } 127 }
101 when (contents) {
102 is PasteboardItem.Plain -> widget.run {
103 add(stdLabel("Plain text"))
104 add(ClipText().apply {
105 contentType = "text/plain"
106 text = contents.plain
107 font = Font(Font.MONOSPACED, Font.PLAIN, MONO_SIZE)
108 border = stdBorder
109 autoSize(stdWidth!!)
110 setEditable(false)
111 alignmentX = JTextPane.LEFT_ALIGNMENT
112 })
113 }
114 is PasteboardItem.HTML -> widget.run {
115 add(stdLabel("Styled text"))
116 val (html, style) = preproc(contents.html)
117 val hek = HTMLEditorKit().apply {
118 style.addStyleSheet(styleSheet)
119 styleSheet = style
120 }
121 add(ClipText().apply {
122 editorKit = hek
123 text = html
124 border = stdBorder
125 autoSize(stdWidth!!)
126 setEditable(false)
127 alignmentX = JTextPane.LEFT_ALIGNMENT
128 })
129 }
130 }
131 queue.add(QueueItem(widget, contents))
132 oldContents = newContents
133 } 128 }
129 queue.add(QueueItem(widget, contents))
130 oldContents = contents
134 } 131 }
135 } 132 }
136 if (Thread.interrupted()) { 133 if (Thread.interrupted()) {
137 return 134 return
138 } 135 }