-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement date editor in JavaFX (#2781)
* Reimplement date editor in JavaFX * Fix checkstyle issue
- Loading branch information
1 parent
9af8052
commit 059f805
Showing
23 changed files
with
155 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/org/jabref/gui/fieldeditors/AbstractEditorViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.jabref.gui.fieldeditors; | ||
|
||
import javafx.beans.property.SimpleStringProperty; | ||
import javafx.beans.property.StringProperty; | ||
|
||
import org.jabref.gui.AbstractViewModel; | ||
import org.jabref.gui.util.BindingsHelper; | ||
import org.jabref.model.entry.BibEntry; | ||
|
||
public class AbstractEditorViewModel extends AbstractViewModel { | ||
protected StringProperty text = new SimpleStringProperty(""); | ||
|
||
public StringProperty textProperty() { | ||
return text; | ||
} | ||
|
||
public void bindToEntry(String fieldName, BibEntry entry) { | ||
BindingsHelper.bindBidirectional( | ||
this.textProperty(), | ||
entry.getFieldBinding(fieldName), | ||
newValue -> { | ||
if (newValue != null) { | ||
entry.setField(fieldName, newValue); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<?import javafx.scene.control.DatePicker?> | ||
<?import javafx.scene.layout.HBox?> | ||
<fx:root xmlns:fx="http://javafx.com/fxml/1" type="HBox" xmlns="http://javafx.com/javafx/8.0.112"> | ||
<DatePicker fx:id="datePicker" prefHeight="0" HBox.hgrow="ALWAYS"/> | ||
</fx:root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.jabref.gui.fieldeditors; | ||
|
||
import java.time.format.DateTimeFormatter; | ||
|
||
import javafx.fxml.FXML; | ||
import javafx.scene.Parent; | ||
import javafx.scene.control.DatePicker; | ||
import javafx.scene.layout.HBox; | ||
|
||
import org.jabref.gui.util.ControlHelper; | ||
import org.jabref.model.entry.BibEntry; | ||
|
||
public class DateEditor extends HBox implements FieldEditorFX { | ||
|
||
private final String fieldName; | ||
@FXML private DateEditorViewModel viewModel; | ||
@FXML private DatePicker datePicker; | ||
|
||
public DateEditor(String fieldName, DateTimeFormatter dateFormatter) { | ||
this.fieldName = fieldName; | ||
this.viewModel = new DateEditorViewModel(dateFormatter); | ||
|
||
ControlHelper.loadFXMLForControl(this); | ||
|
||
datePicker.setConverter(viewModel.getDateToStringConverter()); | ||
datePicker.getEditor().textProperty().bindBidirectional(viewModel.textProperty()); | ||
} | ||
|
||
public DateEditorViewModel getViewModel() { | ||
return viewModel; | ||
} | ||
|
||
@Override | ||
public void bindToEntry(BibEntry entry) { | ||
viewModel.bindToEntry(fieldName, entry); | ||
} | ||
|
||
@Override | ||
public Parent getNode() { | ||
return this; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/org/jabref/gui/fieldeditors/DateEditorViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.jabref.gui.fieldeditors; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import javafx.util.StringConverter; | ||
|
||
import org.jabref.model.entry.Date; | ||
import org.jabref.model.strings.StringUtil; | ||
|
||
public class DateEditorViewModel extends AbstractEditorViewModel { | ||
private final DateTimeFormatter dateFormatter; | ||
|
||
public DateEditorViewModel(DateTimeFormatter dateFormatter) { | ||
this.dateFormatter = dateFormatter; | ||
} | ||
|
||
public StringConverter<LocalDate> getDateToStringConverter() { | ||
return new StringConverter<LocalDate>() { | ||
|
||
@Override | ||
public String toString(LocalDate date) { | ||
if (date != null) { | ||
return dateFormatter.format(date); | ||
} else { | ||
return ""; | ||
} | ||
} | ||
|
||
@Override | ||
public LocalDate fromString(String string) { | ||
if (StringUtil.isNotBlank(string)) { | ||
// We accept all kinds of dates (not just in the format specified) | ||
return Date.parse(string).map(Date::toLocalDate).orElse(null); | ||
} else { | ||
return null; | ||
} | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.