/
App.kt
133 lines (120 loc) · 3.68 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
package example
import java.awt.*
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.ComponentAdapter
import java.awt.event.ComponentEvent
import javax.swing.*
import javax.swing.colorchooser.AbstractColorChooserPanel
import javax.swing.plaf.basic.BasicColorChooserUI
private val defaultRadio = JRadioButton("Default")
private val enabledRadio = JRadioButton("setEnabled(false)", true)
fun makeUI(): Component {
val label = JLabel()
label.isOpaque = true
label.background = Color.WHITE
val button = JButton("open JColorChooser")
button.addActionListener {
val color = getColor(button.rootPane)
if (color != null) {
label.background = color
}
}
return JPanel(BorderLayout(10, 10)).also {
it.add(makeBox(), BorderLayout.NORTH)
it.add(label)
it.add(button, BorderLayout.SOUTH)
it.border = BorderFactory.createEmptyBorder(10, 10, 10, 10)
it.preferredSize = Dimension(320, 240)
}
}
private fun getColor(parent: Container): Color? {
val rgbName = UIManager.getString("ColorChooser.rgbNameText", parent.locale)
val cc = object : JColorChooser() {
override fun updateUI() {
super.updateUI()
if ("GTK".equals(UIManager.getLookAndFeel().id)) {
setUI(BasicColorChooserUI())
}
}
}
for (ccPanel in cc.chooserPanels) {
// Java 9: ccPanel.setColorTransparencySelectionEnabled(colorTransparency)
if (rgbName == ccPanel.displayName) {
if (!defaultRadio.isSelected) {
EventQueue.invokeLater { setTransparencySelectionEnabled(ccPanel) }
}
} else {
cc.removeChooserPanel(ccPanel)
}
}
val ok = ColorTracker(cc)
val dialog = JColorChooser.createDialog(parent, "title", true, cc, ok, null)
dialog.addComponentListener(object : ComponentAdapter() {
override fun componentHidden(e: ComponentEvent) {
(e.component as? Window)?.dispose()
}
})
dialog.isVisible = true // blocks until user brings dialog down...
val color = ok.color
return color
}
private fun setTransparencySelectionEnabled(p: AbstractColorChooserPanel) {
val alphaName = UIManager.getString("ColorChooser.rgbAlphaText", p.locale)
var idx0 = 0
var idx1 = 0
for (c in SwingUtils.descendants(p)) {
when {
c is JLabel && alphaName == c.text -> setEnabledOrVisible(c)
c is JSlider && idx0++ == 3 -> setEnabledOrVisible(c)
c is JSpinner && idx1++ == 3 -> setEnabledOrVisible(c)
}
}
}
private fun setEnabledOrVisible(c: Component) {
if (enabledRadio.isSelected) {
c.isEnabled = false
} else {
c.isVisible = false
}
}
private fun makeBox(): Box {
val box = Box.createVerticalBox()
box.border = BorderFactory.createTitledBorder("ColorTransparencySelectionEnabled")
val bg = ButtonGroup()
val visibleRadio = JRadioButton("setVisible(false)")
for (r in listOf(defaultRadio, enabledRadio, visibleRadio)) {
bg.add(r)
box.add(r)
}
return box
}
private class ColorTracker(private val chooser: JColorChooser) : ActionListener {
var color: Color? = null
private set
override fun actionPerformed(e: ActionEvent) {
color = chooser.color
}
}
object SwingUtils {
fun descendants(parent: Container): List<Component> = parent.components
.filterIsInstance<Container>()
.flatMap { listOf(it) + descendants(it) }
}
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
}
}
}