-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.kt
185 lines (168 loc) · 4.1 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
package example
import java.awt.*
import java.awt.event.MouseEvent
import java.util.EventObject
import javax.swing.*
import javax.swing.tree.DefaultTreeCellRenderer
import javax.swing.tree.TreeCellEditor
import javax.swing.tree.TreeCellRenderer
import javax.swing.tree.TreeNode
fun makeUI(): Component {
val tree = object : JTree() {
override fun updateUI() {
setCellRenderer(null)
setCellEditor(null)
super.updateUI()
setCellRenderer(ButtonCellRenderer())
setCellEditor(ButtonCellEditor())
setRowHeight(0)
}
}
tree.isEditable = true
return JPanel(BorderLayout()).also {
it.add(JScrollPane(tree))
it.preferredSize = Dimension(320, 240)
}
}
private class ButtonPanel : JPanel() {
val renderer = DefaultTreeCellRenderer()
val b1 = ColorButton(ColorIcon(Color.RED))
val b2 = ColorButton(ColorIcon(Color.GREEN))
val b3 = ColorButton(ColorIcon(Color.BLUE))
init {
isOpaque = false
}
fun remakePanel(c: Component): Component {
removeAll()
listOf(b1, b2, b3, c).forEach { add(it) }
return this
}
}
private class ButtonCellRenderer : TreeCellRenderer {
private val panel = ButtonPanel()
override fun getTreeCellRendererComponent(
tree: JTree,
value: Any?,
selected: Boolean,
expanded: Boolean,
leaf: Boolean,
row: Int,
hasFocus: Boolean,
): Component {
val c = panel.renderer.getTreeCellRendererComponent(
tree,
value,
selected,
expanded,
leaf,
row,
hasFocus,
)
return panel.remakePanel(c)
}
}
private class ButtonCellEditor :
AbstractCellEditor(),
TreeCellEditor {
private val panel = ButtonPanel()
init {
panel.b1.addActionListener { stopCellEditing() }
panel.b2.addActionListener { stopCellEditing() }
panel.b3.addActionListener { stopCellEditing() }
}
override fun getTreeCellEditorComponent(
tree: JTree,
value: Any?,
isSelected: Boolean,
expanded: Boolean,
leaf: Boolean,
row: Int,
): Component {
val c = panel.renderer.getTreeCellRendererComponent(
tree,
value,
true,
expanded,
leaf,
row,
true,
)
return panel.remakePanel(c)
}
override fun getCellEditorValue() = panel.renderer.text
override fun isCellEditable(e: EventObject?): Boolean {
val tree = (e as? MouseEvent)?.component as? JTree ?: return false
val pt = e.point
val path = tree.getPathForLocation(pt.x, pt.y)
val r = tree.getPathBounds(path)
val node = path?.lastPathComponent
return if (node is TreeNode && r != null && r.contains(pt)) {
val row = tree.getRowForLocation(pt.x, pt.y)
val renderer = tree.cellRenderer
val c = renderer.getTreeCellRendererComponent(
tree,
" ",
true,
true,
node.isLeaf,
row,
true,
)
c.bounds = r
c.setLocation(0, 0)
// tree.doLayout()
tree.revalidate()
pt.translate(-r.x, -r.y)
SwingUtilities.getDeepestComponentAt(c, pt.x, pt.y) is JButton
} else {
false
}
}
}
private class ColorButton(
icon: ColorIcon,
) : JButton(icon) {
init {
pressedIcon = ColorIcon(icon.color.darker())
isFocusable = false
isFocusPainted = false
isBorderPainted = false
isContentAreaFilled = false
border = BorderFactory.createEmptyBorder()
}
}
private class ColorIcon(
val color: Color,
) : Icon {
override fun paintIcon(
c: Component,
g: Graphics,
x: Int,
y: Int,
) {
val g2 = g.create() as? Graphics2D ?: return
g2.translate(x, y)
g2.paint = color
g2.fillRect(0, 0, iconWidth, iconHeight)
g2.dispose()
}
override fun getIconWidth() = 8
override fun getIconHeight() = 8
}
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
}
}
}