84
MVVM mit JavaFX Manuel Mauky @manuel_mauky 07.04.2016 Max Wielsch @simawiel &

MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

  • Upload
    others

  • View
    29

  • Download
    0

Embed Size (px)

Citation preview

Page 1: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

MVVM mit JavaFXManuel Mauky

@manuel_mauky

07.04.2016

Max Wielsch@simawiel

&

Page 2: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

● Individual-Softwareentwicklung

● Dresden, München, Berlin, Hamburg, Leipzig, Görlitz

● 200+ Employees

Page 3: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

JavaFX

Page 4: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

JavaFX

● Toolkit für Java UIs

● Teil der JRE

● Offizieller Ersatz für Swing

Windows Mac OS Linux

Page 5: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

JavaFX Features

Properties

Charts

Effects & Animations

Media Engine

FXML + CSS

HardwareAcceleration

Multi Touch

Timelines

Packaging

Web View

Page 6: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Scene Graph

Root Node: Parent(StackPane, HBox, ...)

Branch Node(can have children)

Leaf Node(cannot have children)

Page 7: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Scene Graph

AnchorPane

StackPane, Group, Label

VBox, Rectangle, Button

CustomComponent

Nodes

Page 8: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Scene Graph

Stage

SceneScene Scene

Scene Graph

Page 9: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Bsp.: Transitions

Rectangle

0 s 10 s

rotateProperty = 0 rotateProperty = 45Rectangle

...

RotateTransition rotateTransition = new RotateTransition( Duration.seconds(10), reactangle);rotateTransition.setByAngle(45);rotateTransition.play();

Page 10: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Trennung von Darstellung und Verhalten<BorderPane id=“border“>

<top> <Label text=“Page Title“/></top>

<center> <Label text = “Some Data Here“/></center>

</BorderPane>

Test.fxml Test.java

#border { -fx-background-color:white;}

.Label { -fx-text-fill: black;}

Test.css

@FXMLprivate final BorderPane border;

public MainView() { border.setDisabled(true);}

Saubere Trennung dieser Aspekte

Structure

Darstellung

Verhalten

Page 11: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Gluon Scene Builder

<BorderPane id=“border“><top> <Label text=“Page Title“/></top>

<center> <Label text = “Some Data Here“/></center>

</BorderPane>

<BorderPane id=“border“><top> <Label text=“Page Title“/></top>

<center> <Label text = “Some Data Here“/></center>

</BorderPane>

Page 12: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation
Page 13: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

JavaFX @ saxsys

Page 14: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation
Page 15: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation
Page 16: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation
Page 17: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation
Page 18: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

APPLIKATIONSSTRUKTUR

Page 19: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Applikation

Page 20: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

ApplikationsModell

Präsentation

Page 21: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

ApplikationsModell

Aussehen

Verhalten

Page 22: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

MODEL VIEW VIEWMODEL

Page 23: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

● Basiert auf Presentation Model Pattern

● 2005 von Microsoft publiziert

● WPF (.NET), JavaScript (knockout.js), ...

Model View ViewModel

Page 24: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Model

● Applikationsmodell

● Unabhängig von UI

● Backend-System usw.

Model

Page 25: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

ViewModel

● UI-Zustand

● Präsentationslogik

● Kommunikation mit Backend

● Aufbereitung von Modell-Daten

Model

ViewModel

Page 26: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

View

● Daten aus ViewModel anzeigen

● Nutzereingaben an ViewModel weitergeben

● UI-Zustand im ViewModel aktualisieren

Model

ViewModel

View

Page 27: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

VIEW UND VIEWMODEL SYNCHRON

HALTEN?

Page 28: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Model

ViewModel

View

Page 29: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Data Binding und Properties

String

Page 30: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Data Binding und Properties

StringPropertyString

Page 31: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Data Binding und Properties

StringPropertyString

notifications about changes (events)

Page 32: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Data Binding und Properties

StringPropertyString

StringPropertyString

Page 33: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Data Binding und Properties

StringPropertyString Binding

= StringPropertyString

Page 34: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

StringProperty a = new SimpleStringProperty();StringProperty b = new SimpleStringProperty();

a.bindBidirectional(b);

a.setValue(“Hallo”);System.out.println(b.getValue()); // “Hallo”b.setValue(“World”);System.out.println(a.getValue()); // “World”

Page 35: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

IntegerPropertyDoublePropertyStringPropertyBooleanPropertyListProperty<T>ObjectProperty<T>MapProperty<K,V>

Page 36: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Data Binding in MVVM

welcomeLabel.textProperty().bind( viewModel.welcomeMessageProperty());

Model

ViewModel

View

welcomeMessageProperty.set( “Hallo “

+ user.getFirstName() + “ ” + user.getLastName());

firstName lastName

“Hallo Max Wielsch”

“Max” “Wielsch”

Page 37: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Nutzen

● gesamte Präsentationslogik ist nur im ViewModel

● ViewModel ist mit Unit-Tests testbar

→ Hohe Testbarkeit von Frontend-Code

→ Test-Driven-Development im der UI

UI Tests

System Tests

Integration Tests

Unit Tests

Page 38: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

LIVE CODINGTDD

Example: Todo List

Page 39: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

View Hierarchien

View View

View

ViewModel ViewModel ViewModel

? ?

Page 40: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Herausvorderungen mit MVVM

● Kommunikation zwischen VMs in komplexeren Anwendungen

● Erfordert Disziplin, Sichtbarkeiten nicht zu verletzen

● Mehr Code für Indirektionsschicht nötig

Page 41: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation
Page 42: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

… ist ein Application-Framework, das benötigte Komponenten für die Verwendung des Model-View-ViewModel-Patterns mit JavaFX zur Verfügung stellt.

https://github.com/sialcasa/mvvmFX

Page 43: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Basis Klassen für MVVMErweiterter FXML Loader

Dependency-Injection-UnterstützungResourceBundlesModelWrapper

NotificationsCommandsValidation

Scopes

Page 44: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Base Classes / Interfaces

class TodolistViewModel implements ViewModel {…

}

class TodolistView implements FxmlView<TodolistViewModel> {

@InjectViewModelprivate TodolistViewModel viewModel;

}

Page 45: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

URL url = getClass().getResource(“/de/saxsys/MyView.fxml”);FXMLLoader loader = new FXMLLoader(url);loader.load();loader.getRoot(); // loaded nodeloader.getController(); // controller class

Page 46: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

URL url = getClass().getResource(“/de/saxsys/MyView.fxml”);FXMLLoader loader = new FXMLLoader(url);loader.load();loader.getRoot(); // loaded nodeloader.getController(); // controller class

ViewTuple tuple = FluentViewLoader.fxmlView(MyView.class).load();tuple.getView(); // loaded node (View)tuple.getCodeBehind(); // controller class (View)tuple.getViewModel(); // ViewModel

Page 47: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

DEPENDENCYINJECTION

Page 48: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

class MyViewModel implements ViewModel {

private Service service = new ServiceImpl(); // ?

}

Page 49: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

class MyViewModel implements ViewModel {

private Service service = new ServiceImpl(); // ?

@Injectprivate Service service;

}

Page 50: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Dependency Injection

● Inversion of Control

● Keine statische Abhängigkeit zu einer spezifischen Implementierung,

nur zu Interfaces

● Mock-Implementierungen für Unit-Tests nutzen

● Lebenszyklus von Instanzen konfigurieren

Page 51: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Dependency Injection Support

MvvmFX.setCustomDependencyInjector(...);

oder

<dependency>

<groupId>de.saxsys</groupId>

<artifactId>mvvmfx-cdi</artifactId>

</dependency>v

<dependency>

<groupId>de.saxsys</groupId>

<artifactId>mvvmfx-guice</artifactId>

</dependency>

Page 52: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Dependency Injection Support

public class MyFxApp extends Application { … }

public class MyFxApp extends MvvmfxCdiApplication { … }

public class MyFxApp extends MvvmfxGuiceApplication { … }

oder

Page 53: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

NOTIFICATIONS

Page 54: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Notifications

...

ViewModel

ViewModel

ViewModel

ViewModel

ViewModel

ViewModel

ViewModel

Page 55: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Notifications

notificationCenter.subscribe(“messageKey”, (k,v) -> …);

notificationCenter.publish(“messageKey”);

Page 56: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Notifications

ViewModel

View

Page 57: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Notifications

public class MyView implements FxmlView<MyViewModel> { @InjectViewModel private MyViewModel viewModel; … viewModel.subscribe(“messageKey”, (k,v) -> doSomething()); …}public class MyViewModel implements ViewModel { … publish(“messageKey”); …}

Page 58: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

VALIDATION

Page 59: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Validation

Validation logic

boolean isPhoneNumberValid(String input) {

return Pattern.compile("\\+?[0-9\\s]{3,20}")

.matcher(input).matches();

}

Visualization

Page 60: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Validation

ControlsFX ValidationSupport

TextField textField = ...;

ValidationSupport validationSupport = new ValidationSupport();

validationSupport.registerValidator(textField,

Validator.createRegexValidator("Wrong Number", "\\+?[0-9\\s]{3,20}", Severity.ERROR));

Page 61: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Validation

// ViewModel

private final Validator phoneValidator = ...

public ValidationStatus phoneValidation() {

return phoneValidator.getValidationStatus();

}

// View

ValidationVisualizer validVisualizer= new ControlsFxVisualizer();

validVisualizer.initVisualization(viewModel.phoneValidation(), textField);

Page 62: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Validation

StringProperty phoneNumber = new SimpleStringProperty();

Predicate<String> predicate = input -> Pattern.compile("\\+?[0-9\\s]{3,20}")

.matcher(input).matches();

Validator phoneValidator = new FunctionBasedValidator<>(

phoneNumber, predicate, ValidationMessage.error("Not a valid phone number");

Page 63: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Validation

Validator phoneValidator = new FunctionBasedValidator(...);Validator emailValidator = new ObservableRuleBasedValidator(...);

Validator formValidator = new CompositeValidator();formValidator.addValidators(phoneValidator, emailValidator);

Page 64: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

SCOPES

Page 65: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Scopes

Detail ViewMaster View

Page 66: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Scopes

public class MasterDetailScope implements Scope { private ObjectProperty<Person> selectedPerson; // getter, setter...}

Page 67: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Scopes

public class MasterViewModel implements ViewModel { @InjectScope private MasterDetailScope scope; // access the scope to set the selected person}

public class DetailViewModel implements ViewModel { @InjectScope private MasterDetailScope scope; // access the scope to get the selected person}

Page 68: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Source Code, Wiki, Tutorials: https://github.com/sialcasa/mvvmFX

Feedback, Bug Reports, Feature Requests erwünscht :-)

www.mvvmfx.de

Page 69: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Max Wielsch

[email protected]://max-wielsch.blogspot.com@simawiel

Manuel Mauky

[email protected]://lestard.eu@manuel_mauky

Q & A

Page 70: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation
Page 71: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation
Page 72: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

MODELWRAPPER

Page 73: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

ModelWrapper

Der ModelWrapper optimiert das Lesen und Schreiben von Modell-

Daten.

Page 74: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Negative Examplepublic class ContactFormViewModel implements ViewModel {

private StringProperty firstname = new SimpleStringProperty(); private StringProperty lastname = new SimpleStringProperty(); private StringProperty emailAddress = new SimpleStringProperty(); private StringProperty phoneNumber = new SimpleStringProperty();

public StringProperty firstnameProperty() { return firstname; } public StringProperty lastnameProperty() { return lastname; } public StringProperty emailAddressProperty() { return emailAddress; } public StringProperty phoneNumberProperty() { return phoneNumber; }}

Page 75: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

public class ContactFormViewModel implements ViewModel { // Properties and Property-Getter …

@Inject private Repository repository; private person;

public void showPerson(String id) { person = repository.find(id);

firstname.setValue(person.getFirstName()); lastname.setValue(person.getLastName()); emailAddress.setValue(person.getEmailAddress()); phoneNumber.setValue(person.getPhoneNumber()); } public void save() { person.setFirstName(firstname.get()); person.setLastName(lastname.get()); person.setEmailAddress(emailAddress.get()); person.setPhoneNumber(phoneNumber.get());

repository.persist(person); }}

Page 76: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

public class ContactFormViewModel implements ViewModel {

private ModelWrapper<Person> modelWrapper = new ModelWrapper<>(); @Inject private Repository repository;

public void showPerson(String id) { Person person = repository.find(id);

modelWrapper.set(person);modelWrapper.reload();

} public void save() {

modelWrapper.commit();repository.persist(modelWrapper.get());

}

public StringProperty firstnameProperty() {return modelWrapper.field(Person::getFirstName, Person::setFirstName);

} public StringProperty lastnameProperty() {

return modelWrapper.field(Person::getLastName, Person::setLastName); } public StringProperty emailAddressProperty() {

return modelWrapper.field(Person::getEmailAddress, Person::setEmailAddress); } public StringProperty phoneNumberProperty() {

return modelWrapper.field(Person::getPhoneNumber, Person::setPhoneNumber); }}

Page 77: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

ADVANCED RESOURCE

BUNDLES

Page 78: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Advanced (Cascading) ResourceBundles

title=”Other Window”menu_help=”Help” title=”Settings Dialog”

title=”My Cool App”ok_button=”OK”cancel_button=”Cancel”

title=”Other Window”menu_help=”Help”ok_button=”OK”cancel_button=”Cancel”

title=”Settings Dialog”ok_button=”OK”cancel_button=”Cancel”

View1 ResourceBundle View2 ResourceBundle

Resources in View1 Resources in View2

Global ResourceBundle

Page 79: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Advanced (Cascading) ResourceBundles

ResourceBundle bundle = ResourceBundle.getBundle("myapp");

MvvmFX.setGlobalResourceBundle(bundle);

Page 80: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Advanced (Cascading) ResourceBundles

FluentViewLoader .fxmlView(MyView.class) .resourceBundle(bundle) .load();

Page 81: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Advanced (Cascading) ResourceBundles

<fx:include source="../menu/MenuView.fxml" resources="menu"/>

Page 82: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

RenderingJavaFX Application Thread

Quantum Toolkit

Prism ThreadGlass Windowing Toolkit

Direct3D / ... TimerEvent

PulseEvent

SynchronizeScene Graph

being translated (max 60 FPS)

active AnimationCSS change

Page 83: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Model View Controller

Model Controller

View

Page 84: MVVM mit JavaFX - JUG Saxony...2016/04/07  · Model View ViewModel Model Applikationsmodell Unabhängig von UI Backend-System usw. Model ViewModel UI-Zustand Präsentationslogik Kommunikation

Problems

● Kopplung zwischen Controller und View

● Schwer mit Unit-Tests zu testen

● Stattdessen notwendig: Integrationstests mit UI-Automatisierung

○ langsam

○ komplex

○ wartungsintensiv

○ mehr Anforderungen an Build-Infrastruktur (z. B. Jenkins benötigt

grafische Oberfläche)

● Vermischung von verschiedenen Aspekten