Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending the demo application with data objects #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
= Supernaut.FX Sample Maven Project

== Setup
This sample project contains a few @Singleton-objects to demonstrate how you can separate for example a few data objects and your visual components.

. Install JDK 11 or later. (JDK 15 or 16 is recommended)
. Install Maven. (Maven 3.6 or later is recommended)
== Setup

NOTE:: Until Supernaut.FX v0.1.1 is released, you'll have to build Supernaut locally and install into your local Maven Repo.
. Install JDK 11 or later.
(JDK 15 or 16 is recommended)
. Install Maven.
(Maven 3.6 or later is recommended)

== Build Instructions

Expand All @@ -17,12 +19,13 @@ or, to run the app:

. `mvn javafx:run`


== Graal VM Setup

. Install Graal VM 21.0.0.r11 (Latest GraalVM JDK 11)
. `gu install native-image`
. Set `GRAALVM_HOME` environment variable
. `gu install native-image` or `sdk use java 21.0.0.r11-grl`
. Set `GRAALVM_HOME` environment variable, e.g. on Mac:

`export GRAALVM_HOME="$HOME/.sdkman/candidates/java/21.0.0.r11-grl"`

== Graal VM Native Image

Expand Down
40 changes: 26 additions & 14 deletions src/main/java/app/supernaut/fx/sample/maven/HelloFX.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

import app.supernaut.fx.FxForegroundApp;
import app.supernaut.fx.FxLauncher;
import app.supernaut.services.BrowserService;
import app.supernaut.fx.sample.maven.demo.DataObject;
import app.supernaut.fx.sample.maven.demo.SystemInfo;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import javax.inject.Singleton;
import java.net.URI;

@Singleton
public class HelloFX implements FxForegroundApp.FxApplicationCompat {
private static final URI githubRepoUri = URI.create("https://github.com/SupernautApp/SupernautFX");
private final BrowserService browserService;

public HelloFX(BrowserService browserService) {
this.browserService = browserService;
private final DataObject dataObject;
private final SystemInfo systemInfo;

public HelloFX(DataObject dataObject, SystemInfo systemInfo) {
this.dataObject = dataObject;
this.systemInfo = systemInfo;
}

@Override
Expand All @@ -30,14 +31,25 @@ public void start(Stage stage) {
}

private Scene buildScene() {
var javaVersion = System.getProperty("java.version");
var javafxVersion = System.getProperty("javafx.version");
var label = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
//var hyperlink = new Hyperlink("Powered by Supernaut.FX"); // Doesn't work in Graal native-image yet
var vbox = new VBox(label /*, hyperlink */);
var lblJavaVersion = new Label();
lblJavaVersion.textProperty().bind(dataObject.javaVersion);
var lblJavaFxVersion = new Label();
lblJavaFxVersion.textProperty().bind(dataObject.javaFxVersion);
var lblCounter = new Label();
lblCounter.textProperty().bind(dataObject.counter.asString());

var vbox = new VBox(
new Label("Hello World!"),
new Label("Running on Java:"),
lblJavaVersion,
new Label("With JavaFX:"),
lblJavaFxVersion,
new Label("Counter:"),
lblCounter
);
vbox.setAlignment(Pos.CENTER);
//hyperlink.setOnAction(e -> browserService.showDocument(githubRepoUri));
return new Scene(vbox, 350, 100);

return new Scene(vbox, 350, 400);
}

public static void main(String[] args) {
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/app/supernaut/fx/sample/maven/demo/DataObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package app.supernaut.fx.sample.maven.demo;

import javafx.application.Platform;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import javax.inject.Singleton;

@Singleton
public class DataObject {

public final StringProperty javaVersion;
public final StringProperty javaFxVersion;
public final IntegerProperty counter;

public DataObject() {
javaVersion = new SimpleStringProperty();
javaFxVersion = new SimpleStringProperty();
counter = new SimpleIntegerProperty(0);
}

public synchronized void incrementCounter() {
Platform.runLater(() -> counter.set(counter.get() + 1));
}
}
33 changes: 33 additions & 0 deletions src/main/java/app/supernaut/fx/sample/maven/demo/SystemInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package app.supernaut.fx.sample.maven.demo;

import javax.inject.Singleton;
import java.util.Timer;
import java.util.TimerTask;

@Singleton
public class SystemInfo {

private final DataObject dataObject;

public SystemInfo(DataObject dataObject) {
this.dataObject = dataObject;

init();

Timer timer = new Timer(true);
timer.scheduleAtFixedRate(new CountIncrementer(), 0, 1000);
}

private void init() {
dataObject.javaVersion.set(System.getProperty("java.version"));
dataObject.javaFxVersion.set(System.getProperty("javafx.version"));
}

private class CountIncrementer extends TimerTask {
@Override
public void run() {
dataObject.incrementCounter();
System.out.println("Incremented to " + dataObject.counter.get());
}
}
}
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

opens app.supernaut.fx.sample.maven to javafx.graphics, java.base;
exports app.supernaut.fx.sample.maven;
exports app.supernaut.fx.sample.maven.demo;

uses app.supernaut.fx.FxLauncher;
}