-
Notifications
You must be signed in to change notification settings - Fork 4
/
App.kt
338 lines (308 loc) · 8.96 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
package example
import java.awt.*
import java.awt.geom.Area
import java.awt.geom.Path2D
import java.awt.geom.PathIterator
import java.awt.geom.Point2D
import javax.swing.*
import javax.swing.plaf.nimbus.AbstractRegionPainter
import javax.swing.tree.DefaultTreeCellRenderer
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.sign
import kotlin.math.sqrt
fun makeUI(): Component {
val tree = JTree()
tree.rowHeight = 20
val mb = JMenuBar()
mb.add(LookAndFeelUtils.createLookAndFeelMenu())
return JPanel(GridLayout(1, 0, 2, 2)).also {
EventQueue.invokeLater { it.rootPane.jMenuBar = mb }
it.add(makeScrollPane(tree))
it.add(makeScrollPane(RoundedSelectionTree0()))
it.add(makeScrollPane(RoundedSelectionTree()))
it.preferredSize = Dimension(320, 240)
}
}
private fun makeScrollPane(view: Component): JScrollPane {
val scroll = JScrollPane(view)
scroll.background = Color.WHITE
scroll.viewport.background = Color.WHITE
scroll.border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
return scroll
}
private class RoundedSelectionTree : JTree() {
override fun paintComponent(g: Graphics) {
val sr = selectionRows
if (sr?.isNotEmpty() == true) {
val g2 = g.create() as? Graphics2D ?: return
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
g2.paint = SELECTED_COLOR
val area = Area()
sr.map { getRowBounds(it) }.forEach { area.add(Area(it)) }
val arc = 4.0
for (a in GeomUtils.singularization(area)) {
val lst = GeomUtils.convertAreaToPoint2DList(a)
GeomUtils.flatteningStepsOnRightSide(lst, arc * 2.0)
g2.fill(GeomUtils.convertRoundedPath(lst, arc))
}
g2.dispose()
}
super.paintComponent(g)
}
override fun updateUI() {
super.updateUI()
setCellRenderer(TransparentTreeCellRenderer())
isOpaque = false
setRowHeight(20)
val d = UIDefaults()
d["Tree:TreeCell[Enabled+Selected].backgroundPainter"] = TransparentTreeCellPainter()
putClientProperty("Nimbus.Overrides", d)
putClientProperty("Nimbus.Overrides.InheritDefaults", false)
addTreeSelectionListener { repaint() }
}
companion object {
private val SELECTED_COLOR = Color(0xC8_00_78_D7.toInt(), true)
}
}
private class RoundedSelectionTree0 : JTree() {
override fun paintComponent(g: Graphics) {
val sr = selectionRows
if (sr?.isNotEmpty() == true) {
val g2 = g.create() as? Graphics2D ?: return
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
g2.paint = SELECTED_COLOR
val area = Area()
sr.map { getRowBounds(it) }.forEach { area.add(Area(it)) }
val arc = 4.0
for (a in GeomUtils.singularization(area)) {
g2.fill(GeomUtils.convertRoundedPath(GeomUtils.convertAreaToPoint2DList(a), arc))
}
g2.dispose()
}
super.paintComponent(g)
}
override fun updateUI() {
super.updateUI()
setCellRenderer(TransparentTreeCellRenderer())
isOpaque = false
setRowHeight(20)
val d = UIDefaults()
d["Tree:TreeCell[Enabled+Selected].backgroundPainter"] = TransparentTreeCellPainter()
putClientProperty("Nimbus.Overrides", d)
putClientProperty("Nimbus.Overrides.InheritDefaults", false)
addTreeSelectionListener { repaint() }
}
companion object {
private val SELECTED_COLOR = Color(0xC8_00_78_D7.toInt(), true)
}
}
private class TransparentTreeCellRenderer : DefaultTreeCellRenderer() {
override fun getTreeCellRendererComponent(
tree: JTree,
value: Any,
selected: Boolean,
expanded: Boolean,
leaf: Boolean,
row: Int,
hasFocus: Boolean,
): Component {
val c = super.getTreeCellRendererComponent(
tree,
value,
selected,
expanded,
leaf,
row,
false,
)
(c as? JComponent)?.isOpaque = false
return c
}
override fun getBackgroundNonSelectionColor() = ALPHA_OF_ZERO
override fun getBackgroundSelectionColor() = ALPHA_OF_ZERO
companion object {
private val ALPHA_OF_ZERO = Color(0x0, true)
}
}
private class TransparentTreeCellPainter : AbstractRegionPainter() {
override fun doPaint(
g: Graphics2D,
c: JComponent,
width: Int,
height: Int,
extendedCacheKeys: Array<Any>?,
) {
// Do nothing
}
override fun getPaintContext() = null
}
private object LookAndFeelUtils {
private var lookAndFeel = UIManager.getLookAndFeel().javaClass.name
fun createLookAndFeelMenu(): JMenu {
val menu = JMenu("LookAndFeel")
val buttonGroup = ButtonGroup()
for (info in UIManager.getInstalledLookAndFeels()) {
val b = JRadioButtonMenuItem(info.name, info.className == lookAndFeel)
initLookAndFeelAction(info, b)
menu.add(b)
buttonGroup.add(b)
}
return menu
}
fun initLookAndFeelAction(
info: UIManager.LookAndFeelInfo,
b: AbstractButton,
) {
val cmd = info.className
b.text = info.name
b.actionCommand = cmd
b.hideActionText = true
b.addActionListener { setLookAndFeel(cmd) }
}
@Throws(
ClassNotFoundException::class,
InstantiationException::class,
IllegalAccessException::class,
UnsupportedLookAndFeelException::class,
)
private fun setLookAndFeel(newLookAndFeel: String) {
val oldLookAndFeel = lookAndFeel
if (oldLookAndFeel != newLookAndFeel) {
UIManager.setLookAndFeel(newLookAndFeel)
lookAndFeel = newLookAndFeel
updateLookAndFeel()
}
}
private fun updateLookAndFeel() {
for (window in Window.getWindows()) {
SwingUtilities.updateComponentTreeUI(window)
}
}
}
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
}
}
}
private object GeomUtils {
fun convertAreaToPoint2DList(area: Area): MutableList<Point2D> {
val list = mutableListOf<Point2D>()
val pi = area.getPathIterator(null)
val coords = DoubleArray(6)
while (!pi.isDone) {
val pathSegmentType = pi.currentSegment(coords)
when (pathSegmentType) {
PathIterator.SEG_MOVETO, PathIterator.SEG_LINETO ->
list.add(Point2D.Double(coords[0], coords[1]))
}
pi.next()
}
return list
}
fun flatteningStepsOnRightSide(list: MutableList<Point2D>, arc: Double): List<Point2D> {
val sz = list.size
for (i in 0..<sz) {
val i1 = (i + 1) % sz
val i2 = (i + 2) % sz
val i3 = (i + 3) % sz
val pt0 = list[i]
val pt1 = list[i1]
val pt2 = list[i2]
val pt3 = list[i3]
val dx1 = pt2.x - pt1.x
if (abs(dx1) > 1.0e-1 && abs(dx1) < arc) {
val max = max(pt0.x, pt2.x)
replace(list, i, max, pt0.y)
replace(list, i1, max, pt1.y)
replace(list, i2, max, pt2.y)
replace(list, i3, max, pt3.y)
}
}
return list
}
private fun replace(list: MutableList<Point2D>, i: Int, x: Double, y: Double) {
list.removeAt(i)
list.add(i, Point2D.Double(x, y))
}
fun convertRoundedPath(list: List<Point2D>, arc: Double): Path2D {
val kappa = 4.0 * (sqrt(2.0) - 1.0) / 3.0
val akv = arc - arc * kappa
val pt0 = list[0]
val path = Path2D.Double()
val sz = list.size
path.moveTo(pt0.x + arc, pt0.y)
for (i in 0..<sz) {
val prv = list[(i - 1 + sz) % sz]
val cur = list[i]
val nxt = list[(i + 1) % sz]
val dx0 = sign(cur.x - prv.x)
val dy0 = sign(cur.y - prv.y)
val dx1 = sign(nxt.x - cur.x)
val dy1 = sign(nxt.y - cur.y)
path.curveTo(
cur.x - dx0 * akv,
cur.y - dy0 * akv,
cur.x + dx1 * akv,
cur.y + dy1 * akv,
cur.x + dx1 * arc,
cur.y + dy1 * arc,
)
path.lineTo(nxt.x - dx1 * arc, nxt.y - dy1 * arc)
}
path.closePath()
return path
}
fun singularization(rect: Area): List<Area> {
val list = mutableListOf<Area>()
val path = Path2D.Double()
val pi = rect.getPathIterator(null)
val coords = DoubleArray(6)
while (!pi.isDone) {
val pathSegmentType = pi.currentSegment(coords)
when (pathSegmentType) {
PathIterator.SEG_MOVETO -> path.moveTo(
coords[0],
coords[1],
)
PathIterator.SEG_LINETO -> path.lineTo(
coords[0],
coords[1],
)
PathIterator.SEG_QUADTO -> path.quadTo(
coords[0],
coords[1],
coords[2],
coords[3],
)
PathIterator.SEG_CUBICTO -> path.curveTo(
coords[0],
coords[1],
coords[2],
coords[3],
coords[4],
coords[5],
)
PathIterator.SEG_CLOSE -> {
path.closePath()
list.add(Area(path))
path.reset()
}
}
pi.next()
}
return list
}
}