-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.kt
114 lines (103 loc) · 3.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
package example
import java.awt.*
import java.awt.event.ActionEvent
import javax.swing.*
import javax.swing.plaf.basic.BasicSplitPaneUI
fun makeUI(): Component {
val splitPane = JSplitPane(JSplitPane.VERTICAL_SPLIT)
splitPane.topComponent = object : JScrollPane(JTable(5, 3)) {
override fun getMinimumSize() = Dimension(0, 100)
}
splitPane.bottomComponent = object : JScrollPane(JTree()) {
override fun getMinimumSize() = Dimension(0, 100)
}
splitPane.isOneTouchExpandable = true
val north = JPanel(GridLayout(0, 2, 5, 5))
north.border = BorderFactory.createEmptyBorder(5, 5, 5, 5)
val minDividerAction = object : AbstractAction("Min:DividerLocation") {
override fun actionPerformed(e: ActionEvent) {
splitPane.dividerLocation = 0
}
}
north.add(JButton(minDividerAction))
val maxDividerAction = object : AbstractAction("Max:DividerLocation") {
override fun actionPerformed(e: ActionEvent) {
val ins = splitPane.insets
if (splitPane.orientation == JSplitPane.VERTICAL_SPLIT) {
splitPane.dividerLocation = splitPane.height - (ins?.bottom ?: 0)
} else {
splitPane.dividerLocation = splitPane.width - (ins?.right ?: 0)
}
}
}
north.add(JButton(maxDividerAction))
val minAction = object : AbstractAction("selectMin") {
override fun actionPerformed(e: ActionEvent) {
splitPane.requestFocusInWindow()
EventQueue.invokeLater {
val cmd = e.actionCommand
splitPane.actionMap[cmd]?.actionPerformed(ActionEvent(splitPane, e.id, cmd))
}
}
}
north.add(JButton(minAction))
val maxAction = object : AbstractAction("selectMax") {
override fun actionPerformed(e: ActionEvent) {
splitPane.requestFocusInWindow()
EventQueue.invokeLater {
val cmd = e.actionCommand
splitPane.actionMap[cmd]?.actionPerformed(ActionEvent(splitPane, e.id, cmd))
}
}
}
north.add(JButton(maxAction))
val minButton = JButton("Min:keepHidden")
val maxButton = JButton("Max:keepHidden")
(splitPane.ui as? BasicSplitPaneUI)?.divider?.also {
initDividerButtonModel(it, minButton, maxButton)
}
north.add(minButton)
north.add(maxButton)
val p = JPanel(BorderLayout())
p.add(north, BorderLayout.NORTH)
p.add(splitPane)
p.preferredSize = Dimension(320, 240)
return p
}
private fun initDividerButtonModel(
divider: Container,
minButton: JButton,
maxButton: JButton,
) {
var selectMinModel: ButtonModel? = null
var selectMaxModel: ButtonModel? = null
for (c in divider.components) {
if (c is JButton) {
val m = c.model
if (selectMinModel == null) {
selectMinModel = m
} else if (selectMaxModel == null) {
selectMaxModel = m
}
}
}
minButton.model = selectMinModel
maxButton.model = selectMaxModel
}
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
}
}
}