comparison src/name/blackcap/clipman/Pasteboard.kt @ 37:9890445e4cc4

Didn't like eating its own dog food. Fixed.
author David Barts <n5jrn@me.com>
date Thu, 30 Jan 2020 21:03:56 -0800
parents 0c6c18a733b7
children 2a5808156f99
comparison
equal deleted inserted replaced
36:fcf82e3b7e31 37:9890445e4cc4
15 import java.io.ByteArrayInputStream 15 import java.io.ByteArrayInputStream
16 import java.nio.charset.Charset 16 import java.nio.charset.Charset
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 kotlin.collections.HashMap 19 import kotlin.collections.HashMap
20
21 /* Kotlin bug: compaion class fails to see these unless they are out here. */
22 private val CHARSET = Charset.forName(CHARSET_NAME)
23 private val HTML_FLAVOR = DataFlavor("text/html; document=all; class=\"[B\"; charset=" + CHARSET_NAME)
20 24
21 /** 25 /**
22 * Represents an item of data in the clipboard and how to read and 26 * Represents an item of data in the clipboard and how to read and
23 * write it. 27 * write it.
24 */ 28 */
58 62
59 /* we use this when writing data back to the clipboard */ 63 /* we use this when writing data back to the clipboard */
60 64
61 private class PasteboardData(val item: PasteboardItem): 65 private class PasteboardData(val item: PasteboardItem):
62 Transferable, ClipboardOwner { 66 Transferable, ClipboardOwner {
63 private val CHARSET = Charset.forName(CHARSET_NAME)
64 private val HTML_FLAVOR = DataFlavor("text/html; document=all; class=\"[B\"; charset=" + CHARSET_NAME)
65 private val _data: HashMap<DataFlavor, Any> 67 private val _data: HashMap<DataFlavor, Any>
66 private val flavors: Array<DataFlavor> 68 private val flavors: Array<DataFlavor>
67 69
68 init { 70 init {
69 _data = HashMap<DataFlavor, Any>().apply { 71 _data = HashMap<DataFlavor, Any>().apply {
115 /** 117 /**
116 * Read the item in the pasteboard. 118 * Read the item in the pasteboard.
117 * @return a PasteboardItem? object, null if nothing could be read 119 * @return a PasteboardItem? object, null if nothing could be read
118 */ 120 */
119 fun read() : PasteboardItem? { 121 fun read() : PasteboardItem? {
122 /* println("-- BEGIN FLAVORS --")
123 CLIPBOARD.availableDataFlavors.forEach {
124 println(it.mimeType)
125 }
126 println("-- END FLAVORS --") */
120 check() 127 check()
121 val plain = getClipboardData(DataFlavor.stringFlavor) as String? 128 val plain = getClipboardData(DataFlavor.stringFlavor) as String?
122 if (plain == null) { 129 if (plain == null) {
123 return null 130 return null
124 } 131 }
125 val html = getClipboardData(DataFlavor.allHtmlFlavor) as String? 132 val html = getClipboardData(HTML_FLAVOR) as ByteArray?
126 if (html == null) { 133 if (html == null) {
127 val rtf = getClipboardData(RTF_FLAVOR) as ByteArray? 134 val rtf = getClipboardData(RTF_FLAVOR) as ByteArray?
128 return if (rtf == null) { Plain(plain) } else { RTF(plain, rtf) } 135 return if (rtf == null) { Plain(plain) } else { RTF(plain, rtf) }
129 } else { 136 } else {
130 return HTML(plain, html) 137 return HTML(plain, html.toString(CHARSET))
131 } 138 }
132 } 139 }
133 140
134 /** 141 /**
135 * Write an item to the pasteboard. 142 * Write an item to the pasteboard.