Skip to content

Commit

Permalink
Reimplement date editor in JavaFX (#2781)
Browse files Browse the repository at this point in the history
* Reimplement date editor in JavaFX

* Fix checkstyle issue
  • Loading branch information
tobiasdiez authored Apr 22, 2017
1 parent 9af8052 commit 059f805
Show file tree
Hide file tree
Showing 23 changed files with 155 additions and 241 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
## [Unreleased]

### Changed
- Continued to redesign the user interface: this time the editor got a fresh coat of paint:
- The buttons were changed to icons.
- Removed the hidden feature that a double click in the editor inserted the current date.


### Fixed

Expand Down
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ dependencies {
compile 'org.citationstyles:locales:1.0.1-SNAPSHOT'
compile 'de.undercouch:citeproc-java:1.0.1'

compile 'com.github.lgooddatepicker:LGoodDatePicker:8.3.0'

compile 'com.github.tomtung:latex2unicode_2.12:0.2'

compile group: 'com.microsoft.azure', name: 'applicationinsights-core', version: '1.0.+'
Expand Down
5 changes: 0 additions & 5 deletions external-libraries.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,6 @@ Project: SwingX
URL: https://swingx.java.net/
License: LGPL-3.0

Id: com.github.lgooddatepicker:LGoodDatePicker
Project: LGoodDatePicker
URL: https://github.com/LGoodDatePicker/LGoodDatePicker
License: MIT

Id: spin
Path: lib/spin.jar
Project: Spin
Expand Down
96 changes: 0 additions & 96 deletions src/main/java/org/jabref/gui/date/DatePickerButton.java

This file was deleted.

8 changes: 1 addition & 7 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -610,13 +610,7 @@ public Optional<JComponent> getExtra(final FieldEditor editor) {

final Set<FieldProperty> fieldExtras = InternalBibtexFields.getFieldProperties(fieldName);

// timestamp or a other field with datepicker command
if (Globals.prefs.get(JabRefPreferences.TIME_STAMP_FIELD).equals(fieldName)
|| fieldExtras.contains(FieldProperty.DATE)) {
// double click AND datefield => insert the current date (today)
return FieldExtraComponents.getDateTimeExtraComponent(editor,
fieldExtras.contains(FieldProperty.DATE), fieldExtras.contains(FieldProperty.ISO_DATE));
} else if (!panel.getBibDatabaseContext().getMetaData().getContentSelectorValuesForField(fieldName).isEmpty()) {
if (!panel.getBibDatabaseContext().getMetaData().getContentSelectorValuesForField(fieldName).isEmpty()) {
return FieldExtraComponents.getSelectorExtraComponent(frame, panel, editor, contentSelectors,
storeFieldAction);
} else if (fieldExtras.contains(FieldProperty.YES_NO)) {
Expand Down
59 changes: 0 additions & 59 deletions src/main/java/org/jabref/gui/entryeditor/FieldExtraComponents.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.jabref.gui.BasePanel;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.contentselector.FieldContentSelector;
import org.jabref.gui.date.DatePickerButton;
import org.jabref.gui.entryeditor.EntryEditor.StoreFieldAction;
import org.jabref.gui.fieldeditors.FieldEditor;
import org.jabref.logic.l10n.Localization;
Expand Down Expand Up @@ -103,64 +102,6 @@ public static Optional<JComponent> getSelectorExtraComponent(JabRefFrame frame,
return Optional.of(ws);
}

/**
* Set up field such that double click inserts the current date
* If isDataPicker is True, a button with a data picker is returned
*
* @param editor reference to the FieldEditor to display the date value
* @param useDatePicker shows a DatePickerButton if true
* @param useIsoFormat if true ISO format is always used
* @return
*/
public static Optional<JComponent> getDateTimeExtraComponent(FieldEditor editor, boolean useDatePicker,
boolean useIsoFormat) {
/*
((JTextArea) editor).addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {// double click
String date = "";
if(useIsoFormat) {
date = DateTimeFormatter.ISO_DATE.format(LocalDate.now());
} else {
date = DateTimeFormatter.ofPattern(Globals.prefs.get(JabRefPreferences.TIME_STAMP_FORMAT)).format(
LocalDateTime.now());
}
editor.setText(date);
}
}
});
*/

// insert a datepicker, if the extras field contains this command
if (useDatePicker) {
DatePickerButton datePicker = new DatePickerButton(editor, useIsoFormat);

/*
// register a DocumentListener on the underlying text document which notifies the DatePicker which date is currently set
((JTextArea) editor).getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
datePicker.updateDatePickerDate(editor.getText());
}
@Override
public void removeUpdate(DocumentEvent e) {
datePicker.updateDatePickerDate(editor.getText());
}
@Override
public void changedUpdate(DocumentEvent e) {
datePicker.updateDatePickerDate(editor.getText());
}
});
*/

return Optional.of(datePicker.getDatePicker());
} else {
return Optional.empty();
}

}

/**
* Return a dropdown list with the alternatives for editor type fields
*
Expand Down
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);
}
});
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/DateEditor.fxml
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>
42 changes: 42 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/DateEditor.java
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 src/main/java/org/jabref/gui/fieldeditors/DateEditorViewModel.java
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;
}
}
};
}
}
16 changes: 0 additions & 16 deletions src/main/java/org/jabref/gui/fieldeditors/EditorTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
import javafx.scene.text.Font;

import org.jabref.gui.GUIGlobals;
import org.jabref.gui.util.BindingsHelper;
import org.jabref.model.entry.BibEntry;

public class EditorTextArea extends javafx.scene.control.TextArea implements Initializable {

private String fieldName;

public EditorTextArea() {
this("");
}
Expand Down Expand Up @@ -48,16 +44,4 @@ private String convertToHex(java.awt.Color color) {
public void initialize(URL location, ResourceBundle resources) {

}

public void bindToEntry(String fieldName, BibEntry entry) {
this.fieldName = fieldName;
BindingsHelper.bindBidirectional(
this.textProperty(),
entry.getFieldBinding(fieldName),
newValue -> {
if (newValue != null) {
entry.setField(fieldName, newValue);
}
});
}
}
12 changes: 7 additions & 5 deletions src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.gui.fieldeditors;

import java.time.format.DateTimeFormatter;
import java.util.Set;

import org.jabref.Globals;
Expand All @@ -16,12 +17,13 @@ public class FieldEditors {
public static FieldEditorFX getForField(String fieldName, TaskExecutor taskExecutor, DialogService dialogService, JournalAbbreviationLoader journalAbbreviationLoader, JournalAbbreviationPreferences journalAbbreviationPreferences, JabRefPreferences preferences) {
final Set<FieldProperty> fieldExtras = InternalBibtexFields.getFieldProperties(fieldName);

// TODO: Implement this
// TODO: Implement all of them
if (Globals.prefs.get(JabRefPreferences.TIME_STAMP_FIELD).equals(fieldName) || fieldExtras.contains(FieldProperty.DATE)) {
// timestamp or a other field with datepicker command
// double click AND datefield => insert the current date (today)
//return FieldExtraComponents.getDateTimeExtraComponent(editor,
// fieldExtras.contains(FieldProperty.DATE), fieldExtras.contains(FieldProperty.ISO_DATE));
if (fieldExtras.contains(FieldProperty.ISO_DATE)) {
return new DateEditor(fieldName, DateTimeFormatter.ISO_DATE);
} else {
return new DateEditor(fieldName, DateTimeFormatter.ofPattern(Globals.prefs.get(JabRefPreferences.TIME_STAMP_FORMAT)));
}
} else if (fieldExtras.contains(FieldProperty.EXTERNAL)) {
return new UrlEditor(fieldName, dialogService);
} else if (fieldExtras.contains(FieldProperty.JOURNAL_NAME)) {
Expand Down
Loading

0 comments on commit 059f805

Please sign in to comment.