Skip to content

Commit

Permalink
Merge pull request #42 from dlemmermann/master
Browse files Browse the repository at this point in the history
New DateField support.
  • Loading branch information
dlemmermann authored Jan 9, 2019
2 parents 207c288 + 3afab1c commit f1b4f67
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ To use this framework as part of your Maven build simply add the jcentral reposi
<dependency>
<groupId>com.dlsc.formsfx</groupId>
<artifactId>formsfx-core</artifactId>
<version>11.3.0</version>
<version>11.3.1</version>
</dependency>
```

Expand Down
6 changes: 5 additions & 1 deletion formsfx-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.dlsc.formsfx</groupId>
<artifactId>formsfx-core</artifactId>
<packaging>jar</packaging>
<version>11.3.0</version>
<version>11.3.1</version>

<name>FormsFX Core</name>
<description>A framework for quickly creating form-based UIs.</description>
Expand All @@ -21,6 +21,10 @@
<url>https://github.com/dlemmermann/formsfx</url>
</scm>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<distributionManagement>
<repository>
<id>bintray-dlsc-oss-repository</id>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.dlsc.formsfx.model.structure;

/*-
* ========================LICENSE_START=================================
* FormsFX
* %%
* Copyright (C) 2017 - 2018 DLSC Software & Consulting
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================LICENSE_END==================================
*/

import com.dlsc.formsfx.view.controls.SimpleDateControl;
import javafx.beans.property.ObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.util.converter.LocalDateStringConverter;

import java.time.LocalDate;
import java.time.chrono.Chronology;
import java.time.format.FormatStyle;
import java.util.Locale;

/**
* This class provides an implementation of a {@link Field} containing a {@code LocalDate} value.
*
* @author Tomasz Krzemiński
*/
public class DateField extends DataField<ObjectProperty<LocalDate>, LocalDate, DateField> {
/**
* Internal constructor for the {@code DataField} class. To create new
* elements, see the static factory methods in {@code Field}.
*
* @param valueProperty The property that is used to store the current valid value
* of the field.
* @param persistentValueProperty The property that is used to store the latest persisted
* @see Field ::ofStringType
* @see Field ::ofIntegerType
* @see Field ::ofDoubleType
* @see Field ::ofBooleanType
*/
public DateField(ObjectProperty<LocalDate> valueProperty, ObjectProperty<LocalDate> persistentValueProperty) {
super(valueProperty, persistentValueProperty);

Chronology chronology = Chronology.ofLocale(Locale.getDefault(Locale.Category.FORMAT));
stringConverter = new LocalDateStringConverter(FormatStyle.SHORT, null, chronology);
renderer = new SimpleDateControl();
userInput.setValue(null);
userInput.setValue(stringConverter.toString((LocalDate) persistentValue.getValue()));
}

@Override
public DateField bind(ObjectProperty<LocalDate> binding) {
return super.bind(binding);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,10 @@
import com.dlsc.formsfx.model.util.TranslationService;
import com.dlsc.formsfx.view.controls.SimpleControl;
import javafx.beans.InvalidationListener;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.property.*;
import javafx.collections.FXCollections;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -412,6 +403,26 @@ public static <T> SingleSelectionField<T> ofSingleSelectionType(ListProperty<T>
return new SingleSelectionField<>(new SimpleListProperty<>(itemsBinding.getValue()), itemsBinding.indexOf(selectionBinding.getValue())).bind(itemsBinding, selectionBinding);
}

/**
* Creates a new {@link DateField} with given default value
*
* @param defaultValue The initial value and persistent value of the field.
* @return Returns a new {@link DateField}.
*/
public static DateField ofDate(LocalDate defaultValue) {
return new DateField(new SimpleObjectProperty<>(defaultValue), new SimpleObjectProperty<>(defaultValue));
}

/**
* Creates a new {@link DateField} with given property
*
* @param binding The property from the model to be bound with.
* @return Returns a new {@link DateField}.
*/
public static DateField ofDate(ObjectProperty<LocalDate> binding) {
return new DateField(new SimpleObjectProperty<>(binding.getValue()), new SimpleObjectProperty<>(binding.getValue())).bind(binding);
}

/**
* Sets the required property to for the current field without providing an
* error message.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.dlsc.formsfx.view.controls;

/*-
* ========================LICENSE_START=================================
* FormsFX
* %%
* Copyright (C) 2017 - 2018 DLSC Software & Consulting
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================LICENSE_END==================================
*/

import com.dlsc.formsfx.model.structure.DateField;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;

/**
* This class provides a specific implementation to edit date values.
*
* @author Tomasz Krzemiński
*/
public class SimpleDateControl extends SimpleControl<DateField> {

protected Label fieldLabel;
protected DatePicker picker;
protected Label readOnlyLabel;
protected StackPane stack;

@Override
public void initializeParts() {
super.initializeParts();

stack = new StackPane();

fieldLabel = new Label();
readOnlyLabel = new Label();
picker = new DatePicker();
picker.setEditable(true);
}

@Override
public void layoutParts() {
super.layoutParts();

int columns = field.getSpan();
readOnlyLabel.getStyleClass().add("read-only-label");

picker.setMaxWidth(Double.MAX_VALUE);

stack.setAlignment(Pos.CENTER_LEFT);
stack.getChildren().addAll(picker, readOnlyLabel);

Node labelDescription = field.getLabelDescription();
Node valueDescription = field.getValueDescription();

add(fieldLabel, 0, 0, 2, 1);
if (labelDescription != null) {
GridPane.setValignment(labelDescription, VPos.TOP);
add(labelDescription, 0, 1, 2, 1);
}
add(stack, 2, 0, columns - 2, 1);
if (valueDescription != null) {
GridPane.setValignment(valueDescription, VPos.TOP);
add(valueDescription, 2, 1, columns - 2, 1);
}
}

@Override
public void setupBindings() {
super.setupBindings();

picker.disableProperty().bind(field.editableProperty().not());
readOnlyLabel.visibleProperty().bind(field.editableProperty().not());

picker.getEditor().textProperty().bindBidirectional(field.userInputProperty());
fieldLabel.textProperty().bind(field.labelProperty());
picker.promptTextProperty().bind(field.placeholderProperty());
picker.managedProperty().bind(picker.visibleProperty());
}

/**
* {@inheritDoc}
*/
@Override
public void setupEventHandlers() {
picker.getEditor().textProperty().addListener((observable, oldValue, newValue) -> field.userInputProperty().setValue(String.valueOf(newValue)));
}

}
4 changes: 2 additions & 2 deletions formsfx-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.dlsc.formsfx</groupId>
<artifactId>formsfx</artifactId>
<version>11.3.0</version>
<version>11.3.1</version>
</parent>

<name>FormsFX Demo</name>
Expand All @@ -19,7 +19,7 @@
<dependency>
<groupId>com.dlsc.formsfx</groupId>
<artifactId>formsfx-core</artifactId>
<version>11.3.0</version>
<version>11.3.1</version>
</dependency>
</dependencies>

Expand Down
25 changes: 13 additions & 12 deletions formsfx-demo/src/main/java/com/dlsc/formsfx/demo/model/Country.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
package com.dlsc.formsfx.demo.model;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.time.LocalDate;
import java.time.Month;

/**
* This class serves as the model for the demo application.
*
Expand All @@ -26,6 +18,7 @@ public class Country {
private StringProperty name = new SimpleStringProperty("Switzerland");
private StringProperty iso = new SimpleStringProperty("CH");
private BooleanProperty independence = new SimpleBooleanProperty(true);
private ObjectProperty<LocalDate> independenceDay = new SimpleObjectProperty<>(LocalDate.of(1648, Month.OCTOBER, 24));

private StringProperty currencyShort = new SimpleStringProperty("CHF");
private StringProperty currencyLong = new SimpleStringProperty("Swiss Franc");
Expand Down Expand Up @@ -73,6 +66,14 @@ public BooleanProperty independenceProperty() {
return independence;
}

public LocalDate getIndependenceDay() {
return independenceDay.get();
}

public ObjectProperty<LocalDate> independenceDayProperty() {
return independenceDay;
}

public String getCurrencyShort() {
return currencyShort.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ private void createForm() {
.validate(StringLengthValidator.exactly(2, "ISO_3166_error_message")),
Field.ofBooleanType(country.independenceProperty())
.label("independent_label")
.required("required_error_message"),
Field.ofDate(country.getIndependenceDay())
.label("independent_since_label")
.required("required_error_message")
.placeholder("independent_since_placeholder")
),
Section.of(
Field.ofStringType(country.currencyShortProperty())
Expand Down
4 changes: 3 additions & 1 deletion formsfx-demo/src/main/resources/demo-locale_de_CH.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ time_zone_label = Zeitzone
summer_time_zone_label = Sommer-Zeitzone
other_information_label = Weitere Informationen
country_label = Land
independent_since_label = Independence Day
independent_since_placeholder = Independent Since
ISO_3166_label = ISO 3166
independent_label = Unabh\u00e4ngig
form_label = L\u00e4nder Formular
Expand Down Expand Up @@ -43,4 +45,4 @@ country_error_message = Landesname muss l\u00e4nger als 2 Zeichen sein
ISO_3166_error_message = ISO 3166 code muss genau 2 Zeichen lang sein
secret_error_message = Passwortl\u00e4nge muss zwischen 1 und 10 Zeichen sein

capital_tooltip = Die Hauptstadt des Landes ist oftmals nicht die gr\u00f6sste Stadt.
capital_tooltip = Die Hauptstadt des Landes ist oftmals nicht die gr\u00f6sste Stadt.
4 changes: 3 additions & 1 deletion formsfx-demo/src/main/resources/demo-locale_en_UK.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ other_information_label = Other Information
country_label = Country
ISO_3166_label = ISO 3166
independent_label = Independent
independent_since_label = Independence Day
independent_since_placeholder = Independent Since
form_label = Country Form
driving_label = Driving on the

Expand Down Expand Up @@ -43,4 +45,4 @@ country_error_message = Name of country has to be longer than 2 characters
ISO_3166_error_message = ISO 3166 code has to be exactly 2 characters long
secret_error_message = Password length must be between 1 and 10 characters

capital_tooltip = The capital city is often not the largest city.
capital_tooltip = The capital city is often not the largest city.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<groupId>com.dlsc.formsfx</groupId>
<artifactId>formsfx</artifactId>
<packaging>pom</packaging>
<version>11.3.0</version>
<version>11.3.1</version>

<modules>
<module>formsfx-core</module>
Expand Down

0 comments on commit f1b4f67

Please sign in to comment.