-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.kt
108 lines (96 loc) · 2.69 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
package example
import java.awt.*
import java.beans.PropertyChangeEvent
import java.beans.PropertyChangeListener
import javax.swing.*
fun makeUI(): Component {
val area = JTextArea()
area.isEditable = false
val monitor = ProgressMonitor(area, "message", "note", 0, 100)
val runButton = JButton("run")
runButton.addActionListener {
runButton.isEnabled = false
monitor.setProgress(0)
val worker = object : BackgroundTask() {
override fun process(chunks: List<String>) {
chunks.forEach(monitor::setNote)
}
override fun done() {
runButton.isEnabled = true
monitor.close()
runCatching {
val msg = if (isCancelled) "Cancelled" else get()
area.append("$msg\n")
}.onFailure {
if (it is InterruptedException) {
Thread.currentThread().interrupt()
}
area.append("Error: ${it.message}\n")
}
area.caretPosition = area.document.length
}
}
worker.addPropertyChangeListener(ProgressListener(monitor))
worker.execute()
}
val box = Box.createHorizontalBox()
box.add(Box.createHorizontalGlue())
box.add(runButton)
return JPanel(BorderLayout(5, 5)).also {
it.add(JScrollPane(area))
it.add(box, BorderLayout.NORTH)
it.border = BorderFactory.createEmptyBorder(5, 5, 5, 5)
it.preferredSize = Dimension(320, 240)
}
}
private open class BackgroundTask : SwingWorker<String, String>() {
@Throws(InterruptedException::class)
override fun doInBackground(): String {
var current = 0
val lengthOfTask = 120
while (current < lengthOfTask && !isCancelled) {
doSomething()
current++
progress = 100 * current / lengthOfTask
publish("$current/$lengthOfTask")
}
return "Done"
}
@Throws(InterruptedException::class)
protected fun doSomething() {
Thread.sleep(50)
}
}
private class ProgressListener(
private val monitor: ProgressMonitor,
) : PropertyChangeListener {
init {
monitor.setProgress(0)
}
override fun propertyChange(e: PropertyChangeEvent) {
if ("progress" == e.propertyName) {
monitor.setProgress(e.newValue as? Int ?: 0)
val o = e.source
if (o is SwingWorker<*, *> && (o.isDone || monitor.isCanceled)) {
o.cancel(true)
}
}
}
}
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
}
}
}