Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an integration test for ObservableValue.asFlow
- Loading branch information
1 parent
897fefa
commit 485e7c3
Showing
1 changed file
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package examples | ||
|
||
import javafx.application.Application | ||
import javafx.scene.Scene | ||
import javafx.scene.control.* | ||
import javafx.scene.layout.GridPane | ||
import javafx.stage.Stage | ||
import javafx.beans.property.SimpleStringProperty | ||
import javafx.event.EventHandler | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.flow.* | ||
import kotlinx.coroutines.javafx.* | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
fun main(args: Array<String>) { | ||
Application.launch(FxAsFlowApp::class.java, *args) | ||
} | ||
|
||
/** | ||
* Adapted from | ||
* https://github.com/ReactiveX/RxJavaFX/blob/a78ca7d15f7d82d201df8fafb6eba732ec17e327/src/test/java/io/reactivex/rxjavafx/RxJavaFXTest.java | ||
*/ | ||
class FxAsFlowApp: Application(), CoroutineScope { | ||
|
||
private var job = Job() | ||
override val coroutineContext: CoroutineContext | ||
get() = JavaFx + job | ||
|
||
private val incrementBttn = Button("Increment") | ||
private val incrementLabel = Label("") | ||
private val textInput = TextField() | ||
private val flippedTextLabel = Label() | ||
private val spinner = Spinner<Int>() | ||
private val spinnerChangesLabel = Label() | ||
|
||
public override fun start( primaryStage: Stage) { | ||
val gridPane = GridPane() | ||
gridPane.apply { | ||
hgap = 10.0 | ||
vgap = 10.0 | ||
add(incrementBttn, 0, 0) | ||
add(incrementLabel, 1, 0) | ||
add(textInput, 0, 1) | ||
add(flippedTextLabel, 1, 1) | ||
add(spinner, 0, 2) | ||
add(spinnerChangesLabel, 1, 2) | ||
} | ||
val scene = Scene(gridPane) | ||
primaryStage.apply { | ||
width = 275.0 | ||
setScene(scene) | ||
show() | ||
} | ||
} | ||
|
||
public override fun stop() { | ||
super.stop() | ||
job.cancel() | ||
job = Job() | ||
} | ||
|
||
init { | ||
// Initializing the "Increment" button | ||
val stringProperty = SimpleStringProperty("") | ||
var i = 0 | ||
incrementBttn.onAction = EventHandler { | ||
i += 1 | ||
stringProperty.set(i.toString()) | ||
} | ||
launch { | ||
stringProperty.asFlow().collect { | ||
if (it != null) { | ||
stringProperty.set(it) | ||
} | ||
} | ||
} | ||
incrementLabel.textProperty().bind(stringProperty) | ||
// Initializing the reversed text field | ||
val stringProperty2 = SimpleStringProperty("") | ||
launch { | ||
textInput.textProperty().asFlow().collect { | ||
if (it != null) { | ||
stringProperty2.set(it.reversed()) | ||
} | ||
} | ||
} | ||
flippedTextLabel.textProperty().bind(stringProperty2) | ||
// Initializing the spinner | ||
spinner.valueFactory = SpinnerValueFactory.IntegerSpinnerValueFactory(0, 100) | ||
spinner.isEditable = true | ||
val stringProperty3 = SimpleStringProperty("") | ||
launch { | ||
spinner.valueProperty().asFlow().collect { | ||
if (it != null) { | ||
stringProperty3.set("NEW: $it") | ||
} | ||
} | ||
} | ||
spinnerChangesLabel.textProperty().bind(stringProperty3) | ||
} | ||
} |