/
App.kt
366 lines (332 loc) · 10 KB
/
App.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package example
import java.awt.*
import java.awt.event.ActionListener
import java.awt.event.MouseEvent
import java.beans.DefaultPersistenceDelegate
import java.beans.XMLDecoder
import java.beans.XMLEncoder
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.ByteArrayInputStream
import java.io.File
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.util.EventObject
import javax.swing.*
import javax.swing.event.TreeModelEvent
import javax.swing.event.TreeModelListener
import javax.swing.tree.DefaultMutableTreeNode
import javax.swing.tree.DefaultTreeCellRenderer
import javax.swing.tree.DefaultTreeModel
import javax.swing.tree.TreeCellEditor
import javax.swing.tree.TreeCellRenderer
private val textArea = JTextArea()
private fun makeUI(): Component {
val tree = makeTree()
val save = JButton("save")
save.addActionListener {
runCatching {
val file = File.createTempFile("output", ".xml")
val d = DefaultPersistenceDelegate(arrayOf("label", "status"))
XMLEncoder(BufferedOutputStream(Files.newOutputStream(file.toPath()))).use { xe ->
xe.setPersistenceDelegate(CheckBoxNode::class.java, d)
xe.writeObject(tree.model)
}
Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8).use {
textArea.read(it, "temp")
}
}.onFailure {
textArea.text = it.message
}
}
val load = JButton("load")
load.addActionListener {
val text = textArea.text
if (text.isNotEmpty()) {
val bytes = text.toByteArray(StandardCharsets.UTF_8)
XMLDecoder(BufferedInputStream(ByteArrayInputStream(bytes))).use { xd ->
(xd.readObject() as? DefaultTreeModel)?.also {
it.addTreeModelListener(CheckBoxStatusUpdateListener())
tree.model = it
}
}
}
}
val box = Box.createHorizontalBox().also {
it.add(Box.createHorizontalGlue())
it.add(save)
it.add(Box.createHorizontalStrut(4))
it.add(load)
}
val sp = JSplitPane(JSplitPane.VERTICAL_SPLIT).also {
it.resizeWeight = .5
it.topComponent = JScrollPane(tree)
it.bottomComponent = JScrollPane(textArea)
}
return JPanel(BorderLayout()).also {
it.add(sp)
it.add(box, BorderLayout.SOUTH)
it.border = BorderFactory.createEmptyBorder(5, 5, 5, 5)
it.preferredSize = Dimension(320, 240)
}
}
private fun makeTree(): JTree {
val tree = object : JTree() {
override fun updateUI() {
setCellRenderer(null)
setCellEditor(null)
super.updateUI()
setCellRenderer(CheckBoxNodeRenderer())
setCellEditor(CheckBoxNodeEditor())
}
}
tree.isEditable = true
tree.border = BorderFactory.createEmptyBorder(4, 4, 4, 4)
val model = tree.model
(tree.model.root as? DefaultMutableTreeNode)?.also { root ->
root.breadthFirstEnumeration().toList()
.filterIsInstance<DefaultMutableTreeNode>()
.forEach {
it.userObject = CheckBoxNode(it.userObject?.toString() ?: "", Status.DESELECTED)
}
}
model.addTreeModelListener(CheckBoxStatusUpdateListener())
tree.expandRow(0)
return tree
}
private open class TriStateCheckBox : JCheckBox() {
override fun updateUI() {
val currentIcon = icon
icon = null
super.updateUI()
if (currentIcon != null) {
icon = IndeterminateIcon()
}
isOpaque = false
}
}
private class IndeterminateIcon : Icon {
private val icon = UIManager.getIcon("CheckBox.icon")
override fun paintIcon(
c: Component?,
g: Graphics,
x: Int,
y: Int,
) {
val g2 = g.create() as? Graphics2D ?: return
g2.translate(x, y)
icon.paintIcon(c, g2, 0, 0)
g2.paint = FOREGROUND
g2.fillRect(MARGIN, (iconHeight - HEIGHT) / 2, iconWidth - MARGIN - MARGIN, HEIGHT)
g2.dispose()
}
override fun getIconWidth() = icon.iconWidth
override fun getIconHeight() = icon.iconHeight
companion object {
private val FOREGROUND = Color(0xC8_32_14_FF.toInt(), true)
private const val MARGIN = 4
private const val HEIGHT = 2
}
}
enum class Status {
SELECTED,
DESELECTED,
INDETERMINATE,
}
private data class CheckBoxNode(
val label: String,
val status: Status,
) {
override fun toString() = label
}
private class CheckBoxStatusUpdateListener : TreeModelListener {
private var adjusting = false
override fun treeNodesChanged(e: TreeModelEvent) {
val children = e.children
val model = e.source
if (adjusting || model !is DefaultTreeModel) {
return
}
adjusting = true
val node: DefaultMutableTreeNode?
val c: CheckBoxNode?
val isNotRoot = children != null && children.size == 1
if (isNotRoot) {
node = children[0] as? DefaultMutableTreeNode
c = node?.userObject as? CheckBoxNode
val parent = e.treePath
var n = parent.lastPathComponent as? DefaultMutableTreeNode
while (n != null) {
updateParentUserObject(n)
n = n.parent as? DefaultMutableTreeNode ?: break
}
model.nodeChanged(n)
} else {
node = model.root as? DefaultMutableTreeNode
c = node?.userObject as? CheckBoxNode
}
updateAllChildrenUserObject(node, c?.status)
model.nodeChanged(node)
adjusting = false
}
private fun updateParentUserObject(pn: DefaultMutableTreeNode) {
val list = pn.children().toList()
.filterIsInstance<DefaultMutableTreeNode>()
.map { it.userObject }
.filterIsInstance<CheckBoxNode>()
.map { it.status }
(pn.userObject as? CheckBoxNode)?.label?.also { l ->
when {
list.all { it == Status.DESELECTED } -> pn.userObject = CheckBoxNode(l, Status.DESELECTED)
list.all { it == Status.SELECTED } -> pn.userObject = CheckBoxNode(l, Status.SELECTED)
else -> pn.userObject = CheckBoxNode(l, Status.INDETERMINATE)
}
}
}
private fun updateAllChildrenUserObject(
parent: DefaultMutableTreeNode?,
status: Status?,
) {
if (parent == null || status == null) {
return
}
parent.breadthFirstEnumeration().toList()
.filterIsInstance<DefaultMutableTreeNode>()
.filter { parent != it }
.forEach {
it.userObject = CheckBoxNode((it.userObject as? CheckBoxNode)?.label ?: "", status)
}
}
override fun treeNodesInserted(e: TreeModelEvent) {
// not needed
}
override fun treeNodesRemoved(e: TreeModelEvent) {
// not needed
}
override fun treeStructureChanged(e: TreeModelEvent) {
// not needed
}
}
private class CheckBoxNodeRenderer : TreeCellRenderer {
private val panel = JPanel(BorderLayout())
private val checkBox = TriStateCheckBox()
private val tcr = DefaultTreeCellRenderer()
override fun getTreeCellRendererComponent(
tree: JTree,
value: Any?,
selected: Boolean,
expanded: Boolean,
leaf: Boolean,
row: Int,
hasFocus: Boolean,
): Component {
val c = tcr.getTreeCellRendererComponent(
tree,
value,
selected,
expanded,
leaf,
row,
hasFocus,
)
c.font = tree.font
if (value is DefaultMutableTreeNode) {
panel.isFocusable = false
panel.isRequestFocusEnabled = false
panel.isOpaque = false
checkBox.isEnabled = tree.isEnabled
checkBox.font = tree.font
checkBox.isFocusable = false
checkBox.isOpaque = false
val userObject = value.userObject
if (userObject is CheckBoxNode) {
if (userObject.status == Status.INDETERMINATE) {
checkBox.icon = IndeterminateIcon()
} else {
checkBox.icon = null
}
(c as? JLabel)?.text = userObject.label
checkBox.isSelected = userObject.status == Status.SELECTED
}
panel.add(checkBox, BorderLayout.WEST)
panel.add(c)
return panel
}
return c
}
}
private class CheckBoxNodeEditor : AbstractCellEditor(), TreeCellEditor {
private val panel = JPanel(BorderLayout())
private val checkBox = object : TriStateCheckBox() {
private var handler: ActionListener? = null
override fun updateUI() {
removeActionListener(handler)
super.updateUI()
isOpaque = false
isFocusable = false
handler = ActionListener { stopCellEditing() }
addActionListener(handler)
}
}
private val tcr = DefaultTreeCellRenderer()
private var str: String? = null
override fun getTreeCellEditorComponent(
tree: JTree,
value: Any?,
selected: Boolean,
expanded: Boolean,
leaf: Boolean,
row: Int,
): Component {
val c = tcr.getTreeCellRendererComponent(tree, value, true, expanded, leaf, row, true)
c.font = tree.font
if (value is DefaultMutableTreeNode) {
panel.isFocusable = false
panel.isRequestFocusEnabled = false
panel.isOpaque = false
checkBox.isEnabled = tree.isEnabled
checkBox.font = tree.font
val userObject = value.userObject
if (userObject is CheckBoxNode) {
if (userObject.status == Status.INDETERMINATE) {
checkBox.icon = IndeterminateIcon()
} else {
checkBox.icon = null
}
(c as? JLabel)?.text = userObject.label
checkBox.isSelected = userObject.status == Status.SELECTED
str = userObject.label
}
panel.add(checkBox, BorderLayout.WEST)
panel.add(c)
return panel
}
return c
}
override fun getCellEditorValue() =
CheckBoxNode(str ?: "", if (checkBox.isSelected) Status.SELECTED else Status.DESELECTED)
override fun isCellEditable(e: EventObject?) =
((e as? MouseEvent)?.component as? JTree)?.let { pathContainsPoint(it, e.point) } ?: false
private fun pathContainsPoint(tree: JTree, p: Point) =
tree.getPathBounds(tree.getPathForLocation(p.x, p.y))?.let {
it.width = checkBox.preferredSize.width
it.contains(p)
} ?: false
}
fun main() {
EventQueue.invokeLater {
runCatching {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
}.onFailure {
it.printStackTrace()
Toolkit.getDefaultToolkit().beep()
}
JFrame().apply {
defaultCloseOperation = WindowConstants.EXIT_ON_CLOSE
contentPane.add(makeUI())
pack()
setLocationRelativeTo(null)
isVisible = true
}
}
}