-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.kt
222 lines (204 loc) · 5.97 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
package example
import java.awt.*
import java.awt.image.BufferedImage
import java.io.File
import java.net.URI
import javax.imageio.ImageIO
import javax.swing.*
import javax.swing.table.DefaultTableCellRenderer
import javax.swing.table.DefaultTableModel
import javax.swing.table.TableCellRenderer
fun makeUI(): Component {
UIManager.put("TableHeader.rightAlignSortArrow", false)
val columnNames = arrayOf("Name", "CPU", "Memory", "Disk")
val data = arrayOf(
arrayOf("aaa", "1%", "1.6MB", "0MB/S"),
arrayOf("bbb", "1%", "2.4MB", "3MB/S"),
arrayOf("ccc", "2%", "0.3MB", "1MB/S"),
arrayOf("ddd", "3%", "0.5MB", "2MB/S"),
)
val model = object : DefaultTableModel(data, columnNames) {
override fun getColumnClass(column: Int) = String::class.java
}
val table = object : JTable(model) {
override fun updateUI() {
super.updateUI()
val cr = DefaultTableCellRenderer()
cr.horizontalAlignment = SwingConstants.RIGHT
val hr = SortIconLayoutHeaderRenderer(this)
val cm = getColumnModel()
for (i in 1..<cm.columnCount) {
val tc = cm.getColumn(i)
tc.headerRenderer = hr
tc.cellRenderer = cr
}
}
}
table.autoCreateRowSorter = true
val mb = JMenuBar()
mb.add(LookAndFeelUtils.createLookAndFeelMenu())
return JPanel(BorderLayout()).also {
EventQueue.invokeLater { it.rootPane.jMenuBar = mb }
it.add(JScrollPane(table))
it.preferredSize = Dimension(320, 240)
}
}
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 SortIconLayoutHeaderRenderer(
component: Component,
) : TableCellRenderer {
private val ascendingIcon = UIManager.getLookAndFeelDefaults().getIcon(ASCENDING)
private val ascendingUri = getIconUri(ascendingIcon, component)
private val descendingIcon = UIManager.getLookAndFeelDefaults().getIcon(DESCENDING)
private val descendingUri = getIconUri(descendingIcon, component)
private val emptyIcon = EmptyIcon()
private val naturalUri = getIconUri(emptyIcon, component)
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 sortUri = when (sortOrder) {
SortOrder.ASCENDING -> ascendingUri
SortOrder.DESCENDING -> descendingUri
else -> naturalUri
}
val v = 10
val img = "<img src='%s'>".format(sortUri)
val pct = "<td align='right'>%d%%".format(v)
val fmt = "<html><table><tr><td>%s%s<tr><td><td align='right'>%s"
c.text = fmt.format(img, pct, value)
}
return c
}
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
}
}
}
fun getIconUri(
icon: Icon,
c: Component,
): URI? {
val w = icon.iconWidth
val h = icon.iconHeight
val img = BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB)
val g2 = img.createGraphics()
icon.paintIcon(c, g2, 0, 0)
g2.dispose()
return runCatching {
val tmp = File.createTempFile("icon", ".png")
tmp.deleteOnExit()
ImageIO.write(img, "png", tmp)
tmp.toURI()
}.onFailure {
UIManager.getLookAndFeel().provideErrorFeedback(c)
}.getOrNull()
}
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
}
}
}