-
Notifications
You must be signed in to change notification settings - Fork 4
/
App.kt
234 lines (211 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
223
224
225
226
227
228
229
230
231
232
233
234
package example
import java.awt.*
import java.awt.event.HierarchyEvent
import java.awt.event.HierarchyListener
import java.awt.geom.AffineTransform
import java.awt.geom.Ellipse2D
import java.beans.PropertyChangeEvent
import java.beans.PropertyChangeListener
import javax.swing.*
import javax.swing.Timer
import kotlin.math.PI
private val area = JTextArea()
private val bar = JProgressBar()
private val statusPanel = JPanel(BorderLayout())
private val runButton = JButton("run")
private val cancelButton = JButton("cancel")
private val loadingLabel = LoadingLabel()
private var worker: BackgroundTask? = null
private fun executeWorker() {
runButton.isEnabled = false
cancelButton.isEnabled = true
loadingLabel.startAnimation()
statusPanel.removeAll()
statusPanel.add(bar)
statusPanel.revalidate()
bar.isIndeterminate = true
val w = object : BackgroundTask() {
override fun process(chunks: List<String?>) {
if (runButton.isDisplayable && !isCancelled) {
chunks.forEach { appendLine(it) }
} else {
cancel(true)
}
}
override fun done() {
if (!runButton.isDisplayable) {
cancel(true)
return
}
loadingLabel.stopAnimation()
runButton.isEnabled = true
cancelButton.isEnabled = false
statusPanel.remove(bar)
statusPanel.revalidate()
appendLine("\n")
runCatching {
appendLine(if (isCancelled) "Cancelled" else get())
}.onFailure {
if (it is InterruptedException) {
Thread.currentThread().interrupt()
}
appendLine("Interrupted")
}
appendLine("\n\n")
}
}
w.addPropertyChangeListener(ProgressListener(bar))
w.execute()
worker = w
}
private fun appendLine(str: String?) {
area.append(str)
area.caretPosition = area.document.length
}
fun makeUI(): Component {
area.isEditable = false
area.lineWrap = true
runButton.addActionListener { executeWorker() }
cancelButton.addActionListener {
worker?.takeUnless { it.isDone }?.cancel(true)
worker = null
}
val box = Box.createHorizontalBox()
box.add(loadingLabel)
box.add(Box.createHorizontalGlue())
box.add(runButton)
box.add(cancelButton)
return JPanel(BorderLayout()).also {
it.add(box, BorderLayout.NORTH)
it.add(statusPanel, BorderLayout.SOUTH)
it.add(JScrollPane(area))
it.preferredSize = Dimension(320, 240)
}
}
private open class BackgroundTask : SwingWorker<String, String?>() {
@Throws(InterruptedException::class)
override fun doInBackground(): String {
Thread.sleep(1000)
var current = 0
val lengthOfTask = 120 // list.size()
publish("Length Of Task: $lengthOfTask")
publish("\n------------------------------\n")
while (current < lengthOfTask && !isCancelled) {
publish(".")
progress = 100 * current / lengthOfTask
Thread.sleep(50)
current++
}
return "Done"
}
}
private class ProgressListener(
private val progressBar: JProgressBar,
) : PropertyChangeListener {
init {
progressBar.value = 0
}
override fun propertyChange(e: PropertyChangeEvent) {
if ("progress" == e.propertyName) {
progressBar.isIndeterminate = false
progressBar.value = e.newValue as? Int ?: 0
}
}
}
private class LoadingLabel : JLabel(AnimeIcon()) {
private var listener: HierarchyListener? = null
private val animator = Timer(100) {
(icon as? AnimeIcon)?.next()
repaint()
}
override fun updateUI() {
removeHierarchyListener(listener)
super.updateUI()
isOpaque = true
listener = HierarchyListener { e ->
val b = e.changeFlags and HierarchyEvent.DISPLAYABILITY_CHANGED.toLong() != 0L
if (b && !e.component.isDisplayable) {
animator.stop()
}
}
addHierarchyListener(listener)
}
fun startAnimation() {
(icon as? AnimeIcon)?.setRunning(true)
animator.start()
}
fun stopAnimation() {
(icon as? AnimeIcon)?.setRunning(false)
animator.stop()
}
}
private class AnimeIcon : Icon {
private val list = mutableListOf<Shape>()
private val dim: Dimension
private var running = false
private var rotate = 45.0
init {
val r = 4.0
val s = Ellipse2D.Double(0.0, 0.0, 2.0 * r, 2.0 * r)
for (i in 0..7) {
val at = AffineTransform.getRotateInstance(i * 2 * PI / 8)
at.concatenate(AffineTransform.getTranslateInstance(r, r))
list.add(at.createTransformedShape(s))
}
val d = (r * 2 * (1 + 3)).toInt()
dim = Dimension(d, d)
}
override fun paintIcon(
c: Component?,
g: Graphics,
x: Int,
y: Int,
) {
val g2 = g.create() as? Graphics2D ?: return
g2.paint = c?.background ?: Color.WHITE
g2.fillRect(x, y, iconWidth, iconHeight)
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
g2.paint = ELLIPSE_COLOR
val xx = x + dim.width / 2.0
val yy = y + dim.height / 2.0
val at = AffineTransform.getRotateInstance(Math.toRadians(rotate), xx, yy)
at.concatenate(AffineTransform.getTranslateInstance(xx, yy))
val size = list.size
for (i in 0..<size) {
val alpha = if (running) (i + 1f) / size else .5f
g2.composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)
g2.fill(at.createTransformedShape(list[i]))
}
g2.dispose()
}
override fun getIconWidth() = dim.width
override fun getIconHeight() = dim.height
operator fun next() {
if (running) {
rotate = (rotate + 45.0) % 360.0 // 45 = 360 / 8
}
}
fun setRunning(running: Boolean) {
this.running = running
}
companion object {
private val ELLIPSE_COLOR = Color(0xE6_B3_B3)
}
}
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
}
}
}