-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.kt
277 lines (250 loc) · 6.73 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
package example
import java.awt.*
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.FocusEvent
import java.awt.event.FocusListener
import java.util.Date
import javax.swing.*
import javax.swing.plaf.ColorUIResource
import javax.swing.table.DefaultTableCellRenderer
import javax.swing.table.DefaultTableModel
import javax.swing.table.TableCellEditor
import javax.swing.table.TableCellRenderer
import javax.swing.table.TableModel
fun makeUI(): Component {
val columnNames = arrayOf("Type", "Value")
val data = arrayOf(
arrayOf("String", "text"),
arrayOf("Date", Date()),
arrayOf("Integer", 12),
arrayOf("Double", 3.45),
arrayOf("Boolean", true),
arrayOf("Color", Color.RED),
)
val model = object : DefaultTableModel(data, columnNames) {
override fun getColumnClass(column: Int) = getValueAt(0, column).javaClass
}
return JPanel(BorderLayout()).also {
it.add(JScrollPane(PropertyTable(model)))
it.preferredSize = Dimension(320, 240)
}
}
class PropertyTable(
model: TableModel,
) : JTable(model) {
private var editingClass: Class<*>? = null
private fun getClassAt(
row: Int,
column: Int,
): Class<*> {
val mc = convertColumnIndexToModel(column)
val mr = convertRowIndexToModel(row)
return model.getValueAt(mr, mc).javaClass
}
override fun updateUI() {
setSelectionForeground(ColorUIResource(Color.RED))
setSelectionBackground(ColorUIResource(Color.RED))
super.updateUI()
setDefaultRenderer(Color::class.java, ColorRenderer())
setDefaultEditor(Color::class.java, ColorEditor())
setDefaultEditor(Date::class.java, DateEditor())
}
override fun getCellRenderer(
row: Int,
column: Int,
): TableCellRenderer = if (convertColumnIndexToModel(column) == TARGET_COLUMN) {
getDefaultRenderer(getClassAt(row, column))
} else {
super.getCellRenderer(row, column)
}
override fun getCellEditor(
row: Int,
column: Int,
): TableCellEditor = if (convertColumnIndexToModel(column) == TARGET_COLUMN) {
editingClass = getClassAt(row, column)
getDefaultEditor(editingClass)
} else {
editingClass = null
super.getCellEditor(row, column)
}
override fun getColumnClass(
column: Int,
) = if (convertColumnIndexToModel(column) == TARGET_COLUMN) {
editingClass
} else {
super.getColumnClass(column)
}
companion object {
private const val TARGET_COLUMN = 1
}
}
class DateEditor :
AbstractCellEditor(),
TableCellEditor {
private val spinner = JSpinner(SpinnerDateModel())
init {
val editor = JSpinner.DateEditor(spinner, "yyyy/MM/dd")
spinner.editor = editor
setArrowButtonEnabled(false)
editor.textField.horizontalAlignment = SwingConstants.LEFT
val fl = object : FocusListener {
override fun focusLost(e: FocusEvent) {
setArrowButtonEnabled(false)
}
override fun focusGained(e: FocusEvent) {
setArrowButtonEnabled(true)
EventQueue.invokeLater {
(e.component as? JTextField)?.also {
it.caretPosition = 8
it.selectionStart = 8
it.selectionEnd = 10
}
}
}
}
editor.textField.addFocusListener(fl)
spinner.border = BorderFactory.createEmptyBorder()
}
private fun setArrowButtonEnabled(flag: Boolean) {
for (c in spinner.components) {
(c as? JButton)?.isEnabled = flag
}
}
override fun getTableCellEditorComponent(
table: JTable,
value: Any?,
isSelected: Boolean,
row: Int,
column: Int,
): Component {
spinner.value = value
return spinner
}
override fun getCellEditorValue(): Any = spinner.value
override fun stopCellEditing(): Boolean {
val stopEditing = runCatching {
spinner.commitEdit()
}.onFailure {
UIManager.getLookAndFeel().provideErrorFeedback(spinner)
}.isSuccess
return stopEditing && super.stopCellEditing()
}
}
class ColorRenderer : DefaultTableCellRenderer() {
override fun getTableCellRendererComponent(
table: JTable,
value: Any?,
isSelected: Boolean,
hasFocus: Boolean,
row: Int,
column: Int,
): Component {
val c = super.getTableCellRendererComponent(
table,
value,
isSelected,
hasFocus,
row,
column,
)
if (c is JLabel && value is Color) {
c.icon = ColorIcon(value)
c.text = "(${value.red}, ${value.green}, ${value.blue})"
}
return c
}
}
class ColorEditor :
AbstractCellEditor(),
TableCellEditor,
ActionListener {
private val button = JButton()
private val colorChooser: JColorChooser
private val dialog: JDialog
private var currentColor = Color.WHITE
init {
button.actionCommand = EDIT
button.addActionListener(this)
button.isContentAreaFilled = false
button.isFocusPainted = false
button.border = BorderFactory.createEmptyBorder(1, 1, 1, 1)
button.isOpaque = false
button.horizontalAlignment = SwingConstants.LEFT
button.horizontalTextPosition = SwingConstants.RIGHT
colorChooser = JColorChooser()
dialog = JColorChooser.createDialog(
button,
"Pick a Color",
true,
colorChooser,
this,
null,
)
}
override fun actionPerformed(e: ActionEvent) {
if (EDIT == e.actionCommand) {
button.background = currentColor
button.icon = ColorIcon(currentColor)
colorChooser.color = currentColor
dialog.isVisible = true
fireEditingStopped()
} else {
currentColor = colorChooser.color
}
}
override fun getCellEditorValue(): Color = currentColor
override fun getTableCellEditorComponent(
table: JTable,
value: Any?,
isSelected: Boolean,
row: Int,
column: Int,
): Component {
currentColor = value as? Color
button.icon = ColorIcon(currentColor)
val r = currentColor.red
val g = currentColor.green
val b = currentColor.blue
button.text = "($r, $g, $b)"
return button
}
companion object {
private const val EDIT = "edit"
}
}
class ColorIcon(
private 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() = 10
override fun getIconHeight() = 10
}
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
}
}
}