Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Roman Elizarov | fbb36d3 | 2017-01-23 16:03:54 +0300 | [diff] [blame] | 17 | package examples |
| 18 | |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.future.await |
| 20 | import kotlinx.coroutines.experimental.launch |
| 21 | import kotlinx.coroutines.experimental.swing.Swing |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 22 | import java.awt.Insets |
| 23 | import java.util.concurrent.CompletableFuture |
| 24 | import javax.swing.* |
| 25 | |
| 26 | private fun createAndShowGUI() { |
| 27 | val frame = JFrame("Async UI example") |
| 28 | frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE |
| 29 | |
| 30 | val jProgressBar = JProgressBar(0, 100).apply { |
| 31 | value = 0 |
| 32 | isStringPainted = true |
| 33 | } |
| 34 | |
| 35 | val jTextArea = JTextArea(11, 10) |
| 36 | jTextArea.margin = Insets(5, 5, 5, 5) |
| 37 | jTextArea.isEditable = false |
| 38 | |
| 39 | val panel = JPanel() |
| 40 | |
| 41 | panel.add(jProgressBar) |
| 42 | panel.add(jTextArea) |
| 43 | |
| 44 | frame.contentPane.add(panel) |
| 45 | frame.pack() |
| 46 | frame.isVisible = true |
| 47 | |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 48 | launch(Swing) { |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 49 | for (i in 1..10) { |
| 50 | // 'append' method and consequent 'jProgressBar.setValue' are called |
| 51 | // within Swing event dispatch thread |
| 52 | jTextArea.append( |
Denis Zharkov | b483312 | 2016-12-15 11:49:03 +0300 | [diff] [blame] | 53 | startLongAsyncOperation(i).await() |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 54 | ) |
| 55 | jProgressBar.value = i * 10 |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
Denis Zharkov | 64970a4 | 2016-06-22 19:25:22 +0300 | [diff] [blame] | 60 | private fun startLongAsyncOperation(v: Int) = |
| 61 | CompletableFuture.supplyAsync { |
| 62 | Thread.sleep(1000) |
| 63 | "Message: $v\n" |
| 64 | } |
Denis Zharkov | 8e4e0e4 | 2016-06-22 18:27:19 +0300 | [diff] [blame] | 65 | |
| 66 | fun main(args: Array<String>) { |
| 67 | SwingUtilities.invokeLater(::createAndShowGUI) |
| 68 | } |