Skip to content

Commit

Permalink
Merge pull request #562 from jensonwe/560_test_jigsaw_modules
Browse files Browse the repository at this point in the history
560 test jigsaw modules
  • Loading branch information
manuel-mauky authored Dec 7, 2018
2 parents eb6e105 + ea636d9 commit 08fbee9
Show file tree
Hide file tree
Showing 28 changed files with 686 additions and 1 deletion.
88 changes: 88 additions & 0 deletions examples/jigsaw-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# MvvmFX JigSaw Example

This is a simple application to demonstrate that the mvvmFX framework can be used in applications based on a Java version
above Java 8 and using the jigsaw module system.

### The use-case

The application consists on a main view, which contains two separate sub views to calculate the areas of a circle
respectively a rectangle.
The circle view is included via `fx:include`, the rectangle view is loaded via FluentViewLoader when initializing the
main view.

### Important difference to the other examples in the MvvmFX project

The application is not involved in the build process of the main project because the main project is compiled with Java 8
and the jigsaw-example is compiled with Java 11. Because of differences on the project structures, otherwise building
the main project would fail.

### Configuration to run with Java 11

* set JAVA_HOME to an installed jdk-11
* set IDE's runconfiguration to JRE 11
* set properties tag in pom.xml to
```xml
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>

<java.version>11</java.version>
<mvvmfx.version>1.8.0-SNAPSHOT</mvvmfx.version>
</properties>
```
* set the release tag in the maven compiler plugin to
```xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
```

### Interesting parts

#### JavaFX

Since Java 11, JavaFX is no longer part of the JDK. Therefore the packages for javafx-controls, javafx-graphics,
javafx-base and javafx-fxml from org.openjfx must be added as maven `<dependency>`to the main pom.xml.

#### Modules

Since Java 9, java applications need a `module-info.java` file in the top-level-package of every module.
This module descriptor contains informations on which other packages are required or must be exported respectively opened for
other packages.

```Java
module de.saxsys.mvvmfx.examples.jigsaw.app{

requires de.saxsys.mvvmfx.examples.jigsaw.circle;
requires de.saxsys.mvvmfx.examples.jigsaw.rectangle;
requires javafx.base;
requires javafx.fxml;
requires javafx.controls;
requires javafx.graphics;
requires mvvmfx;

exports de.saxsys.mvvmfx.examples.jigsaw.app to javafx.graphics;

opens de.saxsys.mvvmfx.examples.jigsaw.app to mvvmfx, javafx.fxml;
}
```
##### Instructions

- `requires <name of the module>` - indicates from which other modules the current module depends
- `exports <name of the package>` - indicates which packages from the current module are exported outside the module
- `opens <name of the package>` - provides a package for deep reflection at runtime

##### Further Informations

[More information about the the Java Platform Module System](https://dzone.com/articles/java-9-modules-introduction-part-1)


43 changes: 43 additions & 0 deletions examples/jigsaw-example/app/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>de.saxsys.mvvmfx.jigsaw-example</groupId>
<artifactId>jigsaw-example</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>jigsaw-example-app</artifactId>

<dependencies>
<!-- https://mvnrepository.com/artifact/de.saxsys/mvvmfx -->
<dependency>
<groupId>de.saxsys</groupId>
<artifactId>mvvmfx</artifactId>
<version>${mvvmfx.version}</version>
</dependency>

<dependency>
<groupId>de.saxsys.mvvmfx.jigsaw-example</groupId>
<artifactId>jigsaw-example-circle</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>de.saxsys.mvvmfx.jigsaw-example</groupId>
<artifactId>jigsaw-example-rectangle</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package de.saxsys.mvvmfx.examples.jigsaw.app;

import de.saxsys.mvvmfx.FluentViewLoader;
import de.saxsys.mvvmfx.FxmlView;
import de.saxsys.mvvmfx.InjectViewModel;
import de.saxsys.mvvmfx.ViewTuple;
import de.saxsys.mvvmfx.examples.jigsaw.rectangle.RectangleView;
import de.saxsys.mvvmfx.examples.jigsaw.rectangle.RectangleViewModel;
import javafx.fxml.FXML;
import javafx.scene.layout.VBox;
import java.util.ResourceBundle;

public class AppView implements FxmlView<AppViewModel> {

@FXML
VBox appMainBox;

@FXML
VBox circle;

@InjectViewModel
private AppViewModel appViewModel;

public void initialize() {
loadRectangleView();
}

private void loadRectangleView() {
ResourceBundle rectangleBundle = ResourceBundle.getBundle("rectangle");

ViewTuple<RectangleView, RectangleViewModel> viewTuple = FluentViewLoader.fxmlView(
RectangleView.class).resourceBundle(rectangleBundle).load();

appMainBox.getChildren().add(viewTuple.getView());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package de.saxsys.mvvmfx.examples.jigsaw.app;

import de.saxsys.mvvmfx.ViewModel;

public class AppViewModel implements ViewModel {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.saxsys.mvvmfx.examples.jigsaw.app;

import de.saxsys.mvvmfx.FluentViewLoader;
import de.saxsys.mvvmfx.ViewTuple;
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.util.ResourceBundle;

public class Main extends Application {

public static void main(String... args) {
Application.launch(args);
}

@Override
public void start(Stage stage) {
ResourceBundle resourceBundle = ResourceBundle.getBundle("app");
stage.setTitle(resourceBundle.getString("app.stage.title"));

ViewTuple<AppView, AppViewModel> viewTuple = FluentViewLoader.fxmlView(AppView.class).load();
Parent root = viewTuple.getView();
stage.setScene(new Scene(root));
stage.show();
}
}
15 changes: 15 additions & 0 deletions examples/jigsaw-example/app/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

module de.saxsys.mvvmfx.examples.jigsaw.app{

requires de.saxsys.mvvmfx.examples.jigsaw.circle;
requires de.saxsys.mvvmfx.examples.jigsaw.rectangle;
requires javafx.base;
requires javafx.fxml;
requires javafx.controls;
requires javafx.graphics;
requires mvvmfx;

exports de.saxsys.mvvmfx.examples.jigsaw.app to javafx.graphics;

opens de.saxsys.mvvmfx.examples.jigsaw.app to mvvmfx, javafx.fxml;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
app.stage.title=Jigsaw Testanwendung
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
app.stage.title=Jigsaw test application
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.VBox?>
<VBox xmlns="http://javafx.com/javafx"
fx:id="appMainBox"
xmlns:fx="http://javafx.com/fxml"
fx:controller="de.saxsys.mvvmfx.examples.jigsaw.app.AppView"
prefHeight="400.0" prefWidth="600.0">
<fx:include
source="../../../../../../../../../circle/src/main/resources/de/saxsys/mvvmfx/examples/jigsaw/circle/CircleView.fxml" resources="circle" />
</VBox>
15 changes: 15 additions & 0 deletions examples/jigsaw-example/circle/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>de.saxsys.mvvmfx.jigsaw-example</groupId>
<artifactId>jigsaw-example</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>jigsaw-example-circle</artifactId>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package de.saxsys.mvvmfx.examples.jigsaw.circle;

import de.saxsys.mvvmfx.FxmlView;
import de.saxsys.mvvmfx.InjectViewModel;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.util.converter.DoubleStringConverter;

public class CircleView implements FxmlView<CircleViewModel> {

@FXML
private TextField radiusTextField;

@FXML
private Label circleAreaLabel;

@InjectViewModel
private CircleViewModel circleViewModel;


public void initialize() {
setupValidation();
bindViewValues();
}

private void setupValidation() {
TextFormatter<Double> textFormatter = new TextFormatter<>(new DoubleStringConverter(), 0.0,
change -> {
String newText = change.getControlNewText();
if (circleViewModel.validateDouble(newText)) {
return change;
} else {
return null;
}
});

radiusTextField.setTextFormatter(textFormatter);
}

private void bindViewValues() {
radiusTextField.textProperty().bindBidirectional(circleViewModel.radiusProperty());
circleAreaLabel.textProperty().bind(circleViewModel.circularAreaProperty());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package de.saxsys.mvvmfx.examples.jigsaw.circle;

import de.saxsys.mvvmfx.ViewModel;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import java.util.regex.Pattern;

public class CircleViewModel implements ViewModel {

private static final Pattern VALID_DOUBLE_PATTERN = Pattern.compile("((\\d*)|(\\d+\\.\\d*))");

private final StringProperty radius = new SimpleStringProperty();
private final ReadOnlyStringWrapper circularArea = new ReadOnlyStringWrapper("");


public void initialize() {
radius.addListener(observable -> calculateCircleArea());
}

private void calculateCircleArea() {
if (radius.get() != null && !radius.get().isEmpty()) {
double radiusAsDouble = Double.valueOf(radius.get());
double roundedResult = Math.round(Math.pow(radiusAsDouble, 2) * Math.PI);
circularArea.set(Double.toString(roundedResult));
} else {
circularArea.set("");
}
}

boolean validateDouble(String newText) {
return VALID_DOUBLE_PATTERN.matcher(newText).matches();
}

StringProperty radiusProperty() {
return radius;
}

ReadOnlyStringWrapper circularAreaProperty() {
return circularArea;
}
}
13 changes: 13 additions & 0 deletions examples/jigsaw-example/circle/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

module de.saxsys.mvvmfx.examples.jigsaw.circle {

requires javafx.base;
requires javafx.fxml;
requires javafx.controls;
requires javafx.graphics;
requires mvvmfx;

exports de.saxsys.mvvmfx.examples.jigsaw.circle to javafx.graphics, de.saxsys.mvvmfx.examples.jigsaw.app;

opens de.saxsys.mvvmfx.examples.jigsaw.circle to mvvmfx, javafx.fxml;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
circle.label.circlearea=Kreisfläche:
circle.label.radius=Radius
circle.label.title=Berechnen einer Kreisfläche
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
circle.label.circlearea=Circle area:\

circle.label.radius=Radius
circle.label.title=Calculate a circular area
Loading

0 comments on commit 08fbee9

Please sign in to comment.