-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.kt
279 lines (251 loc) · 7.18 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
package example
import java.awt.*
import java.awt.geom.AffineTransform
import java.awt.image.BufferedImage
import javax.swing.*
import javax.swing.table.DefaultTableModel
import javax.swing.table.TableCellRenderer
import javax.swing.table.TableRowSorter
fun makeUI(): Component {
val columnNames = arrayOf("String", "Integer", "Boolean")
val data = arrayOf(
arrayOf("a", 12, true),
arrayOf("b", 5, false),
arrayOf("C", 92, true),
arrayOf("D", 0, false),
)
val model = object : DefaultTableModel(data, columnNames) {
override fun getColumnClass(column: Int) = getValueAt(0, column).javaClass
}
val table = object : JTable(model) {
override fun updateUI() {
super.updateUI()
val hr = VerticalTableHeaderRenderer()
val cm = getColumnModel()
for (i in 0..<cm.columnCount) {
val tc = cm.getColumn(i)
tc.headerRenderer = hr
tc.preferredWidth = 32
}
}
}
val sorter = TableRowSorter(model)
table.rowSorter = sorter
table.autoResizeMode = JTable.AUTO_RESIZE_OFF
val button = JButton("clear SortKeys")
button.addActionListener { sorter.setSortKeys(null) }
val mb = JMenuBar()
mb.add(LookAndFeelUtils.createLookAndFeelMenu())
return JPanel(BorderLayout()).also {
EventQueue.invokeLater { it.rootPane.jMenuBar = mb }
it.add(JScrollPane(table))
it.add(button, BorderLayout.SOUTH)
it.preferredSize = Dimension(320, 240)
}
}
private class VerticalTableHeaderRenderer : TableCellRenderer {
private val ascendingIcon = UIManager.getLookAndFeelDefaults().getIcon(ASCENDING)
private val descendingIcon = UIManager.getLookAndFeelDefaults().getIcon(DESCENDING)
private val emptyIcon = EmptyIcon()
private val intermediate = JPanel()
private val label = JLabel("", null, SwingConstants.LEADING)
override fun getTableCellRendererComponent(
table: JTable,
value: Any?,
isSelected: Boolean,
hasFocus: Boolean,
row: Int,
column: Int,
): Component {
emptyIcon.width = ascendingIcon.iconWidth
emptyIcon.height = ascendingIcon.iconHeight
UIManager.put(ASCENDING, emptyIcon)
UIManager.put(DESCENDING, emptyIcon)
val r = table.tableHeader.defaultRenderer
val c = r.getTableCellRendererComponent(
table,
value,
isSelected,
hasFocus,
row,
column,
)
UIManager.put(ASCENDING, ascendingIcon)
UIManager.put(DESCENDING, descendingIcon)
if (c is JLabel) {
val sortOrder = getColumnSortOrder(table, column)
val sortIcon = when (sortOrder) {
SortOrder.ASCENDING -> ascendingIcon
SortOrder.DESCENDING -> descendingIcon
else -> emptyIcon
}
label.text = c.text
label.icon = RotateIcon(sortIcon, 90)
label.horizontalTextPosition = SwingConstants.LEFT
label.border = BorderFactory.createEmptyBorder(0, 2, 0, 2)
c.setIcon(makeVerticalHeaderIcon(label))
c.setText(null)
}
return c
}
private fun makeVerticalHeaderIcon(c: Component): Icon? {
val d = c.preferredSize
val w = d.height
val h = d.width
val bi = BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB)
val g2 = bi.graphics as? Graphics2D ?: return null
val at = AffineTransform.getTranslateInstance(0.0, h.toDouble())
at.quadrantRotate(-1)
g2.transform = at
SwingUtilities.paintComponent(g2, c, intermediate, 0, 0, h, w)
g2.dispose()
return ImageIcon(bi)
}
companion object {
private const val ASCENDING = "Table.ascendingSortIcon"
private const val DESCENDING = "Table.descendingSortIcon"
fun getColumnSortOrder(
table: JTable?,
column: Int,
): SortOrder {
var rv = SortOrder.UNSORTED
if (table != null && table.rowSorter != null) {
val sortKeys = table.rowSorter.sortKeys
val mi = table.convertColumnIndexToModel(column)
if (sortKeys.isNotEmpty() && sortKeys[0].column == mi) {
rv = sortKeys[0].sortOrder
}
}
return rv
}
}
}
private class EmptyIcon : Icon {
var width = 5
var height = 5
override fun paintIcon(
c: Component?,
g: Graphics,
x: Int,
y: Int,
) {
// do nothing
}
override fun getIconWidth() = width
override fun getIconHeight() = height
}
private class RotateIcon(
icon: Icon,
rotate: Int,
) : Icon {
private val dim = Dimension()
private val image: Image
private var trans: AffineTransform? = null
init {
require(rotate % 90 == 0) { "$rotate: Rotate must be (rotate % 90 == 0)" }
dim.setSize(icon.iconWidth, icon.iconHeight)
image = BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB)
val g = image.getGraphics()
icon.paintIcon(null, g, 0, 0)
g.dispose()
val numQuadrants = rotate / 90 % 4
val tx: Int
val ty: Int
when (numQuadrants) {
3, -1 -> {
tx = 0
ty = dim.width
dim.setSize(icon.iconHeight, icon.iconWidth)
}
1, -3 -> {
tx = dim.height
ty = 0
dim.setSize(icon.iconHeight, icon.iconWidth)
}
2 -> {
tx = dim.width
ty = dim.height
}
else -> {
tx = 0
ty = 0
}
}
trans = AffineTransform.getTranslateInstance(tx.toDouble(), ty.toDouble()).also {
it.quadrantRotate(numQuadrants)
}
}
override fun paintIcon(
c: Component?,
g: Graphics,
x: Int,
y: Int,
) {
val g2 = g.create() as? Graphics2D ?: return
g2.translate(x, y)
g2.drawImage(image, trans, c)
g2.dispose()
}
override fun getIconWidth() = dim.width
override fun getIconHeight() = dim.height
}
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
}
}
}