Commit 23b2d885 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Updated JavaFX/Xml prototype

parent ed48bc03
package de.hdm_stuttgart.mi.sd2;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Driver class for a simple JavaFX demonstration.
*
*/
public class FxmlGuiDriver extends Application {
private static final Logger log = LogManager.getLogger(FxmlGuiDriver.class);
/**
* @param args unused
*/
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage) throws Exception {
log.info("Starting Hello JavaFX and Maven demonstration application");
final String fxmlFile = "/fxml/hello.fxml";
log.debug("Loading FXML for main view from: {}", fxmlFile);
final FXMLLoader loader = new FXMLLoader();
final Parent rootNode = (Parent) loader.load(getClass().getResourceAsStream(fxmlFile));
log.debug("Showing JFX scene");
final Scene scene = new Scene(rootNode, 400, 200);
//scene.getStylesheets().add("/styles/styles.css");
stage.setTitle("Hello JavaFX and Maven");
stage.setScene(scene);
stage.show();
}
}
......@@ -2,6 +2,7 @@ package de.hdm_stuttgart.mi.sd2;
import de.hdm_stuttgart.mi.sd2.GuiHelper.Dialog;
import de.hdm_stuttgart.mi.sd2.GuiHelper.NumberField;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
......@@ -19,78 +20,78 @@ import org.apache.logging.log4j.Logger;
public class GuiDriver extends Application {
private static Logger log = LogManager.getLogger(GuiDriver.class);
private final static int
colIndex_0 = 0,
colIndex_1 = 1;
private final Button resetBtn = new Button(Conf.get("resetButton.text"));
private final TextField nameField = new TextField();
private final NumberField ageField = new NumberField();
public static void main( String[] args ) {
launch(args);
}
public GuiDriver() {
resetBtn.setDisable(true);
nameField.textProperty().addListener(event -> {
log.info("Name value '" + nameField.getText() + "' has been entered");
resetBtn.setDisable(false);
});
ageField.textProperty().addListener(event -> {
log.info("Age value '" + ageField.getText() + "' has been entered");
resetBtn.setDisable(false);
});
resetBtn.setOnAction(event-> {
nameField.setText("");
ageField.setText("");
resetBtn.setDisable(true);
Dialog.showInfo(Conf.get("infoFieldReset"));
log.info("re-setting name field");
});
}
@Override
public void start(final Stage primaryStage) {
primaryStage.setTitle("Sample GUI");
final GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
int currentRowIndex = 0;
final Text scenetitle = new Text("Simple GUI example");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
grid.add(scenetitle, colIndex_0, currentRowIndex, 2, 1);
currentRowIndex++;
grid.add(new Label("Your name:"), colIndex_0, currentRowIndex);
grid.add(nameField, colIndex_1, currentRowIndex);
currentRowIndex++;
grid.add(new Label("Your age:"), colIndex_0, currentRowIndex);
grid.add(ageField, colIndex_1, currentRowIndex);
currentRowIndex++;
grid.add(resetBtn, colIndex_0, currentRowIndex);
Scene scene = new Scene(grid, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void stop() throws Exception {
super.stop();
log.info("Terminating application");
}
private static Logger log = LogManager.getLogger(GuiDriver.class);
private final static int
colIndex_0 = 0,
colIndex_1 = 1;
private final Button resetBtn = new Button(Conf.get("resetButton.text"));
private final TextField nameField = new TextField();
private final NumberField ageField = new NumberField();
public static void main(String[] args) {
launch(args);
}
public GuiDriver() {
resetBtn.setDisable(true);
nameField.textProperty().addListener(event -> {
log.info("Name value '" + nameField.getText() + "' has been entered");
resetBtn.setDisable(false);
});
ageField.textProperty().addListener(event -> {
log.info("Age value '" + ageField.getText() + "' has been entered");
resetBtn.setDisable(false);
});
resetBtn.setOnAction(event -> {
nameField.setText("");
ageField.setText("");
resetBtn.setDisable(true);
Dialog.showInfo(Conf.get("infoFieldReset"));
log.info("re-setting name field");
});
}
@Override
public void start(final Stage primaryStage) {
primaryStage.setTitle("Sample GUI");
final GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
int currentRowIndex = 0;
final Text scenetitle = new Text("Simple GUI example");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
grid.add(scenetitle, colIndex_0, currentRowIndex, 2, 1);
currentRowIndex++;
grid.add(new Label("Your name:"), colIndex_0, currentRowIndex);
grid.add(nameField, colIndex_1, currentRowIndex);
currentRowIndex++;
grid.add(new Label("Your age:"), colIndex_0, currentRowIndex);
grid.add(ageField, colIndex_1, currentRowIndex);
currentRowIndex++;
grid.add(resetBtn, colIndex_0, currentRowIndex);
final Scene scene = new Scene(grid, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void stop() throws Exception {
super.stop();
log.info("Terminating application");
}
}
\ No newline at end of file
......@@ -13,67 +13,65 @@ import java.io.StringWriter;
/**
* See http://code.makery.ch/blog/javafx-dialogs-official
* for further explanations.
*
*/
public class Dialog {
/**
* Show a modal info box
* @param msg A message to be displayed
*/
public static void showInfo(final String msg) {
final Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Info");
alert.setContentText(msg);
alert.showAndWait();
}
/**
* Showing an error box and terminating without any further error processing
*
* @param msg An informative message
* @param ex The exception to be interpreted by an expert
* @param exitCode The exit code to be used by e.g. the calling process.
*
*/
public static void showExceptionAndExit(final String msg, final Exception ex, int exitCode) {
final Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Unrecoverable error");
alert.setHeaderText("Application will be terminated!");
alert.setContentText(msg);
/**
* Show a modal info box
*
* @param msg A message to be displayed
*/
public static void showInfo(final String msg) {
final Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Info");
alert.setContentText(msg);
alert.showAndWait();
}
/**
* Showing an error box and terminating without any further error processing
*
* @param msg An informative message
* @param ex The exception to be interpreted by an expert
* @param exitCode The exit code to be used by e.g. the calling process.
*/
public static void showExceptionAndExit(final String msg, final Exception ex, int exitCode) {
// Create expandable Exception.
final Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Unrecoverable error");
alert.setHeaderText("Application will be terminated!");
alert.setContentText(msg);
if (null != ex) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
String exceptionText = sw.toString();
Label label = new Label("You may copy and forward this exception stacktrace to an expert:");
// Create expandable Exception.
TextArea textArea = new TextArea(exceptionText);
textArea.setEditable(false);
textArea.setWrapText(true);
if (null != ex) {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
final String exceptionText = sw.toString();
textArea.setMaxWidth(Double.MAX_VALUE);
textArea.setMaxHeight(Double.MAX_VALUE);
GridPane.setVgrow(textArea, Priority.ALWAYS);
GridPane.setHgrow(textArea, Priority.ALWAYS);
final Label label = new Label("You may copy and forward this exception stacktrace to an expert:");
GridPane expContent = new GridPane();
expContent.setMaxWidth(Double.MAX_VALUE);
expContent.add(label, 0, 0);
expContent.add(textArea, 0, 1);
final TextArea textArea = new TextArea(exceptionText);
textArea.setEditable(false);
textArea.setWrapText(true);
// Set expandable Exception into the dialog pane.
alert.getDialogPane().setExpandableContent(expContent);
}
textArea.setMaxWidth(Double.MAX_VALUE);
textArea.setMaxHeight(Double.MAX_VALUE);
GridPane.setVgrow(textArea, Priority.ALWAYS);
GridPane.setHgrow(textArea, Priority.ALWAYS);
alert.showAndWait();
System.exit(exitCode);
}
}
final GridPane expContent = new GridPane();
expContent.setMaxWidth(Double.MAX_VALUE);
expContent.add(label, 0, 0);
expContent.add(textArea, 0, 1);
// Set expandable Exception into the dialog pane.
alert.getDialogPane().setExpandableContent(expContent);
}
alert.showAndWait();
System.exit(exitCode);
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sd2.GuiHelper;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.TextField;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class NumberField extends TextField {
private int value = 0;
public NumberField() {
setText("0");
textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable,
String oldValue, String newValue) {
switch (newValue) {
case "":
case "-":
return;
}
try {
value = Integer.parseInt(newValue);
} catch (NumberFormatException e) {
setText(oldValue);
}
}
});
}
public int getValue() {
return value;
}
}
private static Logger log = LogManager.getLogger(NumberField.class);
private int value = 0;
public NumberField() {
setText("0");
textProperty().addListener((event, oldValue, newValue) -> {
switch (newValue.trim()) {
case "":
case "-":
return;
}
try {
value = Integer.parseInt(newValue);
} catch (final NumberFormatException e) {
log.warn("Rejecting illegal age input: «" + newValue + "»");
setText(oldValue);
}
});
}
public int getValue() {
return value;
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sd2;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* A simple controller providing a callback method {@link #sayHello()}
*
*/
public class HelloController {
private final static Logger log = LogManager.getLogger(HelloController.class);
@FXML private TextField firstNameField;
@FXML private TextField lastNameField;
@FXML private Label helloLabel;
/**
* deriving a greeting message from firstName and lastName
*/
@FXML public void sayHello() {
final String firstName = firstNameField.getText();
final String lastName = lastNameField.getText();
final StringBuilder builder = new StringBuilder();
if (0 < firstName.trim().length()) {
builder.append(firstName);
if (0 < lastName.trim().length()) {
builder.append(' ').append(lastName);
}
} else if (0 < lastName.trim().length()) {
builder.append(lastName);
}
if (0 < builder.length()) {
final String name = builder.toString();
log.debug("Saying hello to " + name);
helloLabel.setText("Hello " + name);
} else {
log.debug("Neither first name nor last name was set, saying hello to anonymous person");
helloLabel.setText("Hello mysterious person");
}
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<!--
Adding stylesheets="/styles/styles.css" yet does not work, see FxmlGuiDriver as well.
-->
<GridPane fx:controller="de.hdm_stuttgart.mi.sd2.HelloController"
hgap="5.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="402.0" prefWidth="600.0" styleClass="main-panel" vgap="5.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" >
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="319.0" minWidth="10.0" prefWidth="279.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="470.0" minWidth="10.0" prefWidth="321.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="First name:" />
<Label text="Last name:" GridPane.rowIndex="1" />
<Button mnemonicParsing="false" onAction="#sayHello" text="Say hello" GridPane.rowIndex="2" />
<TextField fx:id="firstNameField" GridPane.columnIndex="1" />
<TextField fx:id="lastNameField" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label fx:id="helloLabel" styleClass="hello-message" text=" "
GridPane.columnIndex="1" GridPane.rowIndex="2" />
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
</GridPane>
\ No newline at end of file
/* Application wide styles */
.label {
-fx-font-size: 12px;
-fx-font-weight: bold;
-fx-text-fill: #333333;
-fx-effect: dropshadow( gaussian , rgba(255,255,255,0.5) , 0,0,0,1 );
}
.button {
-fx-text-fill: white;
-fx-font-family: "Arial Narrow";
-fx-font-weight: bold;
-fx-background-color: linear-gradient(#61a2b1, #2A5058);
-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );
}
.button:hover{
-fx-background-color: #395bae;
}
/* Component specific styles */
.main-panel {
-fx-background-image: url("../images/background.jpg");
}
.hello-message {
-fx-text-fill: #AA0000;
-fx-font-weight: bold;
-fx-effect: dropshadow( gaussian , rgba(255,255,255,0.5) , 0,0,0,1 );
}
......@@ -11,7 +11,7 @@
</includes>
</fileSet>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<fileSet filtered="true" packaged="true">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
......@@ -21,28 +21,41 @@
</excludes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<fileSet filtered="false" packaged="false" >
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>config.properties</exclude>
<exclude>log4j2.xml</exclude>
<exclude>fxml/hello.fxml</exclude>
</excludes>
</fileSet>
<fileSet filtered="true" packaged="false">
<directory>src/main/resources</directory>
<includes>
<include>fxml/hello.fxml</include>
<include>log4j2.xml</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8" packaged="true">
<fileSet filtered="true" packaged="true">
<directory>src/main/resources</directory>
<includes>
<include>config.properties</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<fileSet filtered="true" packaged="true">
<directory>src/test/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="false" packaged="false" encoding="UTF-8">
<fileSet filtered="false" packaged="false">
<directory/>
<includes>
<include>.gitignore</include>
......
package ${package};
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Driver class for a simple JavaFX demonstration.
*
*/
public class FxmlGuiDriver extends Application {
private static final Logger log = LogManager.getLogger(FxmlGuiDriver.class);
/**
* @param args unused
*/
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage) throws Exception {
log.info("Starting Hello JavaFX and Maven demonstration application");
final String fxmlFile = "/fxml/hello.fxml";
log.debug("Loading FXML for main view from: {}", fxmlFile);
final FXMLLoader loader = new FXMLLoader();
final Parent rootNode = (Parent) loader.load(getClass().getResourceAsStream(fxmlFile));
log.debug("Showing JFX scene");
final Scene scene = new Scene(rootNode, 400, 200);
//scene.getStylesheets().add("/styles/styles.css");
stage.setTitle("Hello JavaFX and Maven");
stage.setScene(scene);
stage.show();
}
}
......@@ -2,6 +2,7 @@ package ${package};
import ${package}.GuiHelper.Dialog;
import ${package}.GuiHelper.NumberField;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
......@@ -19,78 +20,78 @@ import org.apache.logging.log4j.Logger;
public class GuiDriver extends Application {
private static Logger log = LogManager.getLogger(GuiDriver.class);
private final static int
colIndex_0 = 0,
colIndex_1 = 1;
private final Button resetBtn = new Button(Conf.get("resetButton.text"));
private final TextField nameField = new TextField();
private final NumberField ageField = new NumberField();
public static void main( String[] args ) {
launch(args);
}