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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
# Standalone toolbar and status bar
|
||||
|
||||
Use JavaFX's `ToolBar` for toolbar content and NeitzelLib's
|
||||
`de.neitzel.fx.control.StatusBar` for left/right status content. Both accept
|
||||
ordinary application Nodes. They work in Java and ordinary FXML without a
|
||||
DockingManager, ViewLoader, ViewModel or initialized injection context.
|
||||
|
||||
The toolbar deliberately reuses JavaFX's existing public control: its item
|
||||
list, orientation, overflow handling, accessibility and CSS already provide
|
||||
the required behavior. There is no second library toolbar API to learn.
|
||||
|
||||
## Java
|
||||
|
||||
```java
|
||||
var save = new Button("Save");
|
||||
var toolbar = new ToolBar(save);
|
||||
|
||||
var status = new Label("Ready");
|
||||
var cursor = new Label();
|
||||
cursor.textProperty().bind(editor.caretPositionProperty().asString("Offset %d"));
|
||||
var progress = new ProgressBar();
|
||||
progress.progressProperty().bind(completedFraction);
|
||||
|
||||
var statusBar = new StatusBar();
|
||||
statusBar.getLeftItems().add(status);
|
||||
statusBar.getRightItems().addAll(cursor, progress);
|
||||
|
||||
shell.setTop(toolbar);
|
||||
shell.setBottom(statusBar);
|
||||
```
|
||||
|
||||
`getLeftItems()` and `getRightItems()` return live observable lists. Items
|
||||
retain their identity, properties and event handlers. Add, remove and reorder
|
||||
items on the JavaFX Application Thread. Ordinary JavaFX scene-graph ownership
|
||||
rules apply: a Node can appear in only one place at a time. Removing an item
|
||||
from its list detaches it; the application owns its resources and bindings.
|
||||
|
||||
The left group stays at the left edge and the right group at the right edge
|
||||
while there is available space between them. A status bar's preferred size
|
||||
comes from its content. Parent layouts can stretch it to the window width.
|
||||
|
||||
## FXML
|
||||
|
||||
```xml
|
||||
<?import de.neitzel.fx.control.StatusBar?>
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.ToolBar?>
|
||||
<?import javafx.scene.layout.BorderPane?>
|
||||
|
||||
<BorderPane xmlns:fx="http://javafx.com/fxml/1">
|
||||
<top>
|
||||
<ToolBar>
|
||||
<items><Button text="Save"/></items>
|
||||
</ToolBar>
|
||||
</top>
|
||||
<bottom>
|
||||
<StatusBar>
|
||||
<leftItems><Label text="Ready"/></leftItems>
|
||||
<rightItems><Label text="Line 1, column 1"/></rightItems>
|
||||
</StatusBar>
|
||||
</bottom>
|
||||
</BorderPane>
|
||||
```
|
||||
|
||||
Load this with ordinary `FXMLLoader`. Bind application state after loading,
|
||||
or use an application controller. No NeitzelLib loading mechanism is required.
|
||||
|
||||
## Styling
|
||||
|
||||
The status container has CSS class `status-bar`; its item groups have classes
|
||||
`status-bar-left` and `status-bar-right`. Use ordinary Region CSS for padding
|
||||
and background, and HBox CSS for item spacing. JavaFX ToolBar retains its
|
||||
standard `tool-bar` CSS class and skin.
|
||||
|
||||
## Example
|
||||
|
||||
The standalone bars example in `fx-example` provides a Java-built screen and
|
||||
an equivalent FXML-built screen. Both have an editor, Clear and Append actions,
|
||||
status text, a bound cursor position and a demonstration progress value derived
|
||||
from the editor's current length. Switch the startup parameter to `fxml` to
|
||||
choose the FXML screen. The example performs no file writes and uses no MVVM.
|
||||
|
||||
From the repository root with JDK 25, package the reactor and run either screen
|
||||
without installing changed artifacts into the local Maven repository:
|
||||
|
||||
```bash
|
||||
./mvnw -pl fx-example -am package
|
||||
java --enable-native-access=ALL-UNNAMED \
|
||||
-cp 'fx-example/target/classes:fx/target/classes:fx-example/target/libs/*' \
|
||||
de.neitzel.fx.example.bars.BarsMain
|
||||
```
|
||||
|
||||
Append `fxml` to the launch command for the FXML screen. The classpath separator
|
||||
shown is for macOS/Linux; use `;` on Windows. A graphical desktop session is
|
||||
required. The launch class is ordinary Java and delegates to the Application.
|
||||
|
||||
Dockable-owned bars and optional MVVM bindings are a later integration step;
|
||||
the standalone controls already support ordinary JavaFX property bindings.
|
||||
@@ -0,0 +1,83 @@
|
||||
package de.neitzel.fx.control;
|
||||
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.layout.Priority;
|
||||
import javafx.scene.layout.Region;
|
||||
|
||||
/**
|
||||
* Standalone status content aligned to the left and right edges of a window.
|
||||
* Items are ordinary JavaFX Nodes; no docking or MVVM runtime is required.
|
||||
* Modify the item lists on the JavaFX Application Thread.
|
||||
*/
|
||||
public final class StatusBar extends Region {
|
||||
private final HBox left = new HBox(8);
|
||||
private final HBox right = new HBox(8);
|
||||
private final HBox row;
|
||||
|
||||
/** Creates an empty status bar with independently populated left and right sides. */
|
||||
public StatusBar() {
|
||||
getStyleClass().add("status-bar");
|
||||
setPadding(new Insets(4, 8, 4, 8));
|
||||
left.getStyleClass().add("status-bar-left");
|
||||
right.getStyleClass().add("status-bar-right");
|
||||
left.setAlignment(Pos.CENTER_LEFT);
|
||||
right.setAlignment(Pos.CENTER_RIGHT);
|
||||
Region gap = new Region();
|
||||
HBox.setHgrow(gap, Priority.ALWAYS);
|
||||
row = new HBox(8, left, gap, right);
|
||||
row.setAlignment(Pos.CENTER_LEFT);
|
||||
getChildren().add(row);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the live list of content aligned at the left edge.
|
||||
*
|
||||
* @return ordered application Nodes
|
||||
*/
|
||||
public ObservableList<Node> getLeftItems() {
|
||||
return left.getChildren();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the live list of content aligned at the right edge.
|
||||
*
|
||||
* @return ordered application Nodes
|
||||
*/
|
||||
public ObservableList<Node> getRightItems() {
|
||||
return right.getChildren();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected double computeMinWidth(double height) {
|
||||
return snappedLeftInset() + row.minWidth(height) + snappedRightInset();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected double computePrefWidth(double height) {
|
||||
return snappedLeftInset() + row.prefWidth(height) + snappedRightInset();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected double computeMinHeight(double width) {
|
||||
return snappedTopInset() + row.minHeight(contentWidth(width)) + snappedBottomInset();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected double computePrefHeight(double width) {
|
||||
return snappedTopInset() + row.prefHeight(contentWidth(width)) + snappedBottomInset();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void layoutChildren() {
|
||||
row.resizeRelocate(snappedLeftInset(), snappedTopInset(), contentWidth(getWidth()),
|
||||
Math.max(0, getHeight() - snappedTopInset() - snappedBottomInset()));
|
||||
}
|
||||
|
||||
private double contentWidth(double width) {
|
||||
return width < 0 ? width : Math.max(0, width - snappedLeftInset() - snappedRightInset());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package de.neitzel.fx.view;
|
||||
|
||||
import de.neitzel.fx.control.StatusBar;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ProgressBar;
|
||||
import javafx.scene.control.ToolBar;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/** Standalone bars display application content without a docking or MVVM runtime. */
|
||||
class StandaloneBarsTest {
|
||||
@BeforeAll
|
||||
static void startJavaFx() throws InterruptedException {
|
||||
FxThread.start();
|
||||
}
|
||||
|
||||
@Test
|
||||
void statusContentStaysAtOppositeEdgesAndFollowsBindings() throws Exception {
|
||||
FxThread.onFx(() -> {
|
||||
var text = new SimpleStringProperty("Ready");
|
||||
Label status = new Label();
|
||||
status.textProperty().bind(text);
|
||||
Label cursor = new Label("Line 4, column 7");
|
||||
StatusBar bar = new StatusBar();
|
||||
bar.getLeftItems().add(status);
|
||||
bar.getRightItems().add(cursor);
|
||||
BorderPane root = new BorderPane();
|
||||
root.setBottom(bar);
|
||||
Scene scene = new Scene(root, 640, 240);
|
||||
root.applyCss();
|
||||
root.layout();
|
||||
|
||||
assertSame(scene, status.getScene());
|
||||
assertSame(scene, cursor.getScene());
|
||||
assertTrue(status.localToScene(status.getBoundsInLocal()).getMinX() < 20);
|
||||
assertTrue(cursor.localToScene(cursor.getBoundsInLocal()).getMaxX() > 620);
|
||||
assertTrue(status.localToScene(status.getBoundsInLocal()).getMaxX()
|
||||
< cursor.localToScene(cursor.getBoundsInLocal()).getMinX());
|
||||
text.set("Saved");
|
||||
assertEquals("Saved", status.getText());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void plainFxmlLoadsBothBarsAndTheirApplicationNodes() throws Exception {
|
||||
FxThread.onFx(() -> {
|
||||
var resource = getClass().getResource("standalone-bars.fxml");
|
||||
assertNotNull(resource);
|
||||
FXMLLoader loader = new FXMLLoader(resource);
|
||||
BorderPane root = loader.load();
|
||||
ToolBar toolbar = (ToolBar) root.getTop();
|
||||
StatusBar statusBar = (StatusBar) root.getBottom();
|
||||
Button action = (Button) toolbar.getItems().getFirst();
|
||||
Label status = (Label) statusBar.getLeftItems().getFirst();
|
||||
ProgressBar progress = (ProgressBar) statusBar.getRightItems().getFirst();
|
||||
action.setOnAction(event -> {
|
||||
status.setText("Complete");
|
||||
progress.setProgress(1);
|
||||
});
|
||||
new Scene(root, 640, 240);
|
||||
root.applyCss();
|
||||
root.layout();
|
||||
|
||||
action.fire();
|
||||
assertEquals("Complete", status.getText());
|
||||
assertEquals(1, progress.getProgress());
|
||||
assertNotNull(progress.getScene());
|
||||
statusBar.getRightItems().remove(progress);
|
||||
assertNull(progress.getParent());
|
||||
assertNull(progress.getScene());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?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.ToolBar?>
|
||||
<?import javafx.scene.layout.BorderPane?>
|
||||
|
||||
<BorderPane xmlns:fx="http://javafx.com/fxml/1">
|
||||
<top>
|
||||
<ToolBar>
|
||||
<items><Button text="Complete"/></items>
|
||||
</ToolBar>
|
||||
</top>
|
||||
<bottom>
|
||||
<StatusBar>
|
||||
<leftItems><Label text="Ready"/></leftItems>
|
||||
<rightItems><ProgressBar progress="0"/></rightItems>
|
||||
</StatusBar>
|
||||
</bottom>
|
||||
</BorderPane>
|
||||
Reference in New Issue
Block a user