feat(fx): add standalone status bar and Java/FXML examples
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package de.neitzel.fx.example.bars;
|
||||
|
||||
import de.neitzel.fx.control.StatusBar;
|
||||
import javafx.application.Application;
|
||||
import javafx.beans.binding.Bindings;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ProgressBar;
|
||||
import javafx.scene.control.TextArea;
|
||||
import javafx.scene.control.ToolBar;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/** An ordinary JavaFX editor demonstrating standalone toolbar and status content. */
|
||||
public final class BarsApp extends Application {
|
||||
@Override
|
||||
public void start(Stage stage) throws IOException {
|
||||
boolean fxml = getParameters().getRaw().contains("fxml");
|
||||
stage.setTitle("Standalone bars — " + (fxml ? "FXML" : "Java"));
|
||||
stage.setScene(new Scene(create(fxml), 640, 360));
|
||||
stage.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the same screen using Java or ordinary FXMLLoader, without MVVM.
|
||||
*
|
||||
* @param fxml whether to load the FXML version
|
||||
* @return a screen ready to place in a Scene
|
||||
* @throws IOException if the example FXML cannot be loaded
|
||||
*/
|
||||
public static Parent create(boolean fxml) throws IOException {
|
||||
if (fxml) {
|
||||
FXMLLoader loader = new FXMLLoader(BarsApp.class.getResource("Bars.fxml"));
|
||||
Parent root = loader.load();
|
||||
var nodes = loader.getNamespace();
|
||||
connect((TextArea) nodes.get("editor"), (Button) nodes.get("clear"),
|
||||
(Button) nodes.get("append"), (Label) nodes.get("status"),
|
||||
(Label) nodes.get("cursor"), (ProgressBar) nodes.get("progress"));
|
||||
return root;
|
||||
}
|
||||
TextArea editor = new TextArea();
|
||||
editor.setId("editor");
|
||||
editor.setPromptText("Type here, or append sample text.");
|
||||
Button clear = new Button("Clear");
|
||||
clear.setId("clear");
|
||||
Button append = new Button("Append");
|
||||
append.setId("append");
|
||||
Label status = new Label();
|
||||
status.setId("status");
|
||||
Label cursor = new Label();
|
||||
cursor.setId("cursor");
|
||||
ProgressBar progress = new ProgressBar(0);
|
||||
progress.setId("progress");
|
||||
progress.setPrefWidth(120);
|
||||
StatusBar bar = new StatusBar();
|
||||
bar.getLeftItems().add(status);
|
||||
bar.getRightItems().addAll(cursor, progress);
|
||||
BorderPane root = new BorderPane(editor);
|
||||
root.setTop(new ToolBar(clear, append));
|
||||
root.setBottom(bar);
|
||||
connect(editor, clear, append, status, cursor, progress);
|
||||
return root;
|
||||
}
|
||||
|
||||
private static void connect(TextArea editor, Button clear, Button append,
|
||||
Label status, Label cursor, ProgressBar progress) {
|
||||
clear.setOnAction(event -> editor.clear());
|
||||
append.setOnAction(event -> editor.appendText("Hello"));
|
||||
status.textProperty().bind(Bindings.when(editor.textProperty().isEmpty())
|
||||
.then("Ready").otherwise("Edited"));
|
||||
cursor.textProperty().bind(editor.caretPositionProperty().asString("Offset %d"));
|
||||
progress.progressProperty().bind(Bindings.min(1.0, editor.lengthProperty().divide(100.0)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package de.neitzel.fx.example.bars;
|
||||
|
||||
import javafx.application.Application;
|
||||
|
||||
/** Plain launcher for the standalone bars example, usable on the classpath. */
|
||||
public final class BarsMain {
|
||||
private BarsMain() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the example.
|
||||
*
|
||||
* @param args include {@code fxml} to use the FXML screen
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Application.launch(BarsApp.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<?import de.neitzel.fx.control.StatusBar?>
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.ProgressBar?>
|
||||
<?import javafx.scene.control.TextArea?>
|
||||
<?import javafx.scene.control.ToolBar?>
|
||||
<?import javafx.scene.layout.BorderPane?>
|
||||
|
||||
<BorderPane xmlns:fx="http://javafx.com/fxml/1">
|
||||
<top>
|
||||
<ToolBar>
|
||||
<items>
|
||||
<Button fx:id="clear" text="Clear"/>
|
||||
<Button fx:id="append" text="Append"/>
|
||||
</items>
|
||||
</ToolBar>
|
||||
</top>
|
||||
<center>
|
||||
<TextArea fx:id="editor" promptText="Type here, or append sample text."/>
|
||||
</center>
|
||||
<bottom>
|
||||
<StatusBar>
|
||||
<leftItems><Label fx:id="status"/></leftItems>
|
||||
<rightItems>
|
||||
<Label fx:id="cursor"/>
|
||||
<ProgressBar fx:id="progress" progress="0" prefWidth="120"/>
|
||||
</rightItems>
|
||||
</StatusBar>
|
||||
</bottom>
|
||||
</BorderPane>
|
||||
@@ -0,0 +1,73 @@
|
||||
package de.neitzel.fx.example.bars;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ProgressBar;
|
||||
import javafx.scene.control.TextArea;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/** Both standalone construction styles keep the editor and its bars connected. */
|
||||
class StandaloneBarsExampleTest {
|
||||
@BeforeAll
|
||||
static void startJavaFx() throws InterruptedException {
|
||||
CountDownLatch started = new CountDownLatch(1);
|
||||
try {
|
||||
Platform.startup(started::countDown);
|
||||
} catch (IllegalStateException alreadyStarted) {
|
||||
started.countDown();
|
||||
}
|
||||
assertTrue(started.await(10, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaAndFxmlScreensKeepActionsAndStatusConnected() throws InterruptedException {
|
||||
CountDownLatch done = new CountDownLatch(1);
|
||||
AtomicReference<Throwable> failure = new AtomicReference<>();
|
||||
Platform.runLater(() -> {
|
||||
try {
|
||||
for (boolean fxml : new boolean[]{false, true}) {
|
||||
var root = BarsApp.create(fxml);
|
||||
new Scene(root, 640, 360);
|
||||
root.applyCss();
|
||||
root.layout();
|
||||
TextArea editor = (TextArea) root.lookup("#editor");
|
||||
Button append = (Button) root.lookup("#append");
|
||||
Button clear = (Button) root.lookup("#clear");
|
||||
Label status = (Label) root.lookup("#status");
|
||||
Label cursor = (Label) root.lookup("#cursor");
|
||||
ProgressBar progress = (ProgressBar) root.lookup("#progress");
|
||||
|
||||
append.fire();
|
||||
assertEquals("Hello", editor.getText());
|
||||
assertEquals("Edited", status.getText());
|
||||
assertEquals("Offset 5", cursor.getText());
|
||||
assertEquals(0.05, progress.getProgress(), 0.001);
|
||||
clear.fire();
|
||||
assertEquals("", editor.getText());
|
||||
assertEquals("Ready", status.getText());
|
||||
assertEquals("Offset 0", cursor.getText());
|
||||
assertEquals(0, progress.getProgress());
|
||||
}
|
||||
} catch (Throwable thrown) {
|
||||
failure.set(thrown);
|
||||
} finally {
|
||||
done.countDown();
|
||||
}
|
||||
});
|
||||
assertTrue(done.await(10, TimeUnit.SECONDS));
|
||||
if (failure.get() != null) {
|
||||
fail("Standalone bars example failed", failure.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user