From 451d829524e916da3be266f7729b1678189b4744 Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Thu, 10 Aug 2017 16:09:04 +0200 Subject: [PATCH] Fetcher timestamp (#3092) * Remove redundancy * Update timestamp when entry is fetched #3074 * Unused import * Move to own preferences class * Checkstyle is a pain in the *** --- src/main/java/org/jabref/JabRefMain.java | 2 +- .../java/org/jabref/gui/EntryTypeDialog.java | 6 ++- .../jabref/gui/entryeditor/EntryEditor.java | 54 ++++++------------- .../jabref/gui/fieldeditors/FieldEditors.java | 4 +- .../gui/importer/EntryFromPDFCreator.java | 3 +- .../actions/AppendDatabaseAction.java | 2 +- .../preferences/TimestampPreferences.java | 49 +++++++++++++++++ .../jabref/preferences/JabRefPreferences.java | 9 +++- 8 files changed, 82 insertions(+), 47 deletions(-) create mode 100644 src/main/java/org/jabref/logic/preferences/TimestampPreferences.java diff --git a/src/main/java/org/jabref/JabRefMain.java b/src/main/java/org/jabref/JabRefMain.java index 0f98ba7e7d4..5a8f429c5fe 100644 --- a/src/main/java/org/jabref/JabRefMain.java +++ b/src/main/java/org/jabref/JabRefMain.java @@ -82,7 +82,7 @@ private static void start(String[] args) { InternalBibtexFields .updateSpecialFields(Globals.prefs.getBoolean(JabRefPreferences.SERIALIZESPECIALFIELDS)); // Update name of the time stamp field based on preferences - InternalBibtexFields.updateTimeStampField(Globals.prefs.get(JabRefPreferences.TIME_STAMP_FIELD)); + InternalBibtexFields.updateTimeStampField(Globals.prefs.getTimestampPreferences().getTimestampField()); // Update which fields should be treated as numeric, based on preferences: InternalBibtexFields.setNumericFields(Globals.prefs.getStringList(JabRefPreferences.NUMERIC_FIELDS)); diff --git a/src/main/java/org/jabref/gui/EntryTypeDialog.java b/src/main/java/org/jabref/gui/EntryTypeDialog.java index f9b09fea886..13b711f284d 100644 --- a/src/main/java/org/jabref/gui/EntryTypeDialog.java +++ b/src/main/java/org/jabref/gui/EntryTypeDialog.java @@ -316,10 +316,14 @@ protected void done() { diag.entryListComplete(); diag.setLocationRelativeTo(frame); diag.setVisible(true); - diag.toFront(); + diag.toFront(); } else { // Regenerate CiteKey of imported BibEntry BibtexKeyPatternUtil.makeAndSetLabel(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern(), frame.getCurrentBasePanel().getDatabase(), bibEntry, Globals.prefs.getBibtexKeyPatternPreferences()); + // Update Timestamps + if (Globals.prefs.getTimestampPreferences().includeCreatedTimestamp()) { + bibEntry.setField(Globals.prefs.getTimestampPreferences().getTimestampField(), Globals.prefs.getTimestampPreferences().now()); + } frame.getCurrentBasePanel().insertEntry(bibEntry); } diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java index a13a8fd002e..c85e6faa8db 100644 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java +++ b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java @@ -11,8 +11,6 @@ import java.awt.event.KeyAdapter; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -32,6 +30,7 @@ import javax.swing.JToolBar; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; +import javax.swing.undo.UndoableEdit; import javafx.embed.swing.JFXPanel; import javafx.scene.Scene; @@ -72,7 +71,6 @@ import org.jabref.logic.search.SearchQueryHighlightListener; import org.jabref.logic.util.UpdateField; import org.jabref.model.EntryTypes; -import org.jabref.model.FieldChange; import org.jabref.model.database.BibDatabase; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.EntryType; @@ -482,21 +480,6 @@ private void warnEmptyBibtexkey() { + Localization.lang("Grouping may not work for this entry.")); } - private boolean updateTimeStampIsSet() { - return Globals.prefs.getBoolean(JabRefPreferences.USE_TIME_STAMP) - && Globals.prefs.getBoolean(JabRefPreferences.UPDATE_TIMESTAMP); - } - - /** - * Updates the timestamp of the given entry and returns the FieldChange - */ - private Optional doUpdateTimeStamp() { - String timeStampField = Globals.prefs.get(JabRefPreferences.TIME_STAMP_FIELD); - String timeStampFormat = Globals.prefs.get(JabRefPreferences.TIME_STAMP_FORMAT); - String timestamp = DateTimeFormatter.ofPattern(timeStampFormat).format(LocalDateTime.now()); - return UpdateField.updateField(entry, timeStampField, timestamp); - } - private class TypeButton extends JButton { private TypeButton() { @@ -639,15 +622,7 @@ public void actionPerformed(ActionEvent event) { // Add an UndoableKeyChange to the baseframe's undoManager. UndoableKeyChange undoableKeyChange = new UndoableKeyChange(entry, oldValue, newValue); - if (updateTimeStampIsSet()) { - NamedCompound ce = new NamedCompound(undoableKeyChange.getPresentationName()); - ce.addEdit(undoableKeyChange); - doUpdateTimeStamp().ifPresent(fieldChange -> ce.addEdit(new UndoableFieldChange(fieldChange))); - ce.end(); - panel.getUndoManager().addEdit(ce); - } else { - panel.getUndoManager().addEdit(undoableKeyChange); - } + updateTimestamp(undoableKeyChange); textField.setValidBackgroundColor(); @@ -709,18 +684,7 @@ public void actionPerformed(ActionEvent event) { // Add an UndoableFieldChange to the baseframe's undoManager. UndoableFieldChange undoableFieldChange = new UndoableFieldChange(entry, fieldEditor.getFieldName(), oldValue, toSet); - if (updateTimeStampIsSet()) { - NamedCompound ce = new NamedCompound(undoableFieldChange.getPresentationName()); - ce.addEdit(undoableFieldChange); - - doUpdateTimeStamp() - .ifPresent(fieldChange -> ce.addEdit(new UndoableFieldChange(fieldChange))); - ce.end(); - - panel.getUndoManager().addEdit(ce); - } else { - panel.getUndoManager().addEdit(undoableFieldChange); - } + updateTimestamp(undoableFieldChange); panel.markBaseChanged(); } catch (InvalidFieldValueException ex) { @@ -747,6 +711,18 @@ public void actionPerformed(ActionEvent event) { }); } } + + private void updateTimestamp(UndoableEdit undoableEdit) { + if (Globals.prefs.getTimestampPreferences().includeTimestamps()) { + NamedCompound compound = new NamedCompound(undoableEdit.getPresentationName()); + compound.addEdit(undoableEdit); + UpdateField.updateField(entry, Globals.prefs.getTimestampPreferences().getTimestampField(), Globals.prefs.getTimestampPreferences().now()).ifPresent(fieldChange -> compound.addEdit(new UndoableFieldChange(fieldChange))); + compound.end(); + panel.getUndoManager().addEdit(compound); + } else { + panel.getUndoManager().addEdit(undoableEdit); + } + } } private class NextEntryAction extends AbstractAction { diff --git a/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java b/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java index fbc9207d07c..089a71ab3f2 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java +++ b/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java @@ -30,11 +30,11 @@ public static FieldEditorFX getForField(String fieldName, TaskExecutor taskExecu AutoCompleteSuggestionProvider suggestionProvider = getSuggestionProvider(fieldName, suggestionProviders, databaseContext.getMetaData()); - if (Globals.prefs.get(JabRefPreferences.TIME_STAMP_FIELD).equals(fieldName) || fieldExtras.contains(FieldProperty.DATE)) { + if (Globals.prefs.getTimestampPreferences().getTimestampField().equals(fieldName) || fieldExtras.contains(FieldProperty.DATE)) { if (fieldExtras.contains(FieldProperty.ISO_DATE)) { return new DateEditor(fieldName, DateTimeFormatter.ofPattern("[uuuu][-MM][-dd]"), suggestionProvider); } else { - return new DateEditor(fieldName, DateTimeFormatter.ofPattern(Globals.prefs.get(JabRefPreferences.TIME_STAMP_FORMAT)), suggestionProvider); + return new DateEditor(fieldName, DateTimeFormatter.ofPattern(Globals.prefs.getTimestampPreferences().getTimestampFormat()), suggestionProvider); } } else if (fieldExtras.contains(FieldProperty.EXTERNAL)) { return new UrlEditor(fieldName, dialogService, suggestionProvider); diff --git a/src/main/java/org/jabref/gui/importer/EntryFromPDFCreator.java b/src/main/java/org/jabref/gui/importer/EntryFromPDFCreator.java index b51cec5860a..11538571332 100644 --- a/src/main/java/org/jabref/gui/importer/EntryFromPDFCreator.java +++ b/src/main/java/org/jabref/gui/importer/EntryFromPDFCreator.java @@ -19,7 +19,6 @@ import org.jabref.model.entry.BibEntry; import org.jabref.pdfimport.PdfImporter; import org.jabref.pdfimport.PdfImporter.ImportPdfFilesResult; -import org.jabref.preferences.JabRefPreferences; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocumentInformation; @@ -99,7 +98,7 @@ private void addEntryDataFromPDDocumentInformation(File pdfFile, BibEntry entry) // default time stamp follows ISO-8601. Reason: https://xkcd.com/1179/ String date = LocalDate.of(Calendar.YEAR, Calendar.MONTH + 1, Calendar.DAY_OF_MONTH) .format(DateTimeFormatter.ISO_LOCAL_DATE); - appendToField(entry, Globals.prefs.get(JabRefPreferences.TIME_STAMP_FIELD), date); + appendToField(entry, Globals.prefs.getTimestampPreferences().getTimestampField(), date); } if (pdfDocInfo.getCustomMetadataValue("bibtex/bibtexkey") != null) { diff --git a/src/main/java/org/jabref/gui/importer/actions/AppendDatabaseAction.java b/src/main/java/org/jabref/gui/importer/actions/AppendDatabaseAction.java index baae05a96f7..76b7350198a 100644 --- a/src/main/java/org/jabref/gui/importer/actions/AppendDatabaseAction.java +++ b/src/main/java/org/jabref/gui/importer/actions/AppendDatabaseAction.java @@ -69,7 +69,7 @@ private static void mergeFromBibtex(BasePanel panel, ParserResult parserResult, if (importEntries) { // Add entries boolean overwriteOwner = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_OWNER); - boolean overwriteTimeStamp = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP); + boolean overwriteTimeStamp = Globals.prefs.getTimestampPreferences().overwriteTimestamp(); for (BibEntry originalEntry : fromDatabase.getEntries()) { BibEntry entry = (BibEntry) originalEntry.clone(); diff --git a/src/main/java/org/jabref/logic/preferences/TimestampPreferences.java b/src/main/java/org/jabref/logic/preferences/TimestampPreferences.java new file mode 100644 index 00000000000..9ed0d0b9ca2 --- /dev/null +++ b/src/main/java/org/jabref/logic/preferences/TimestampPreferences.java @@ -0,0 +1,49 @@ +package org.jabref.logic.preferences; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class TimestampPreferences { + private final boolean useTimestamps; + private final boolean useModifiedTimestamp; + private final String timestampField; + private final String timestampFormat; + private final boolean overwriteTimestamp; + + public TimestampPreferences(boolean useTimestamps, boolean useModifiedTimestamp, String timestampField, String timestampFormat, boolean overwriteTimestamp) { + this.useTimestamps = useTimestamps; + this.useModifiedTimestamp = useModifiedTimestamp; + this.timestampField = timestampField; + this.timestampFormat = timestampFormat; + this.overwriteTimestamp = overwriteTimestamp; + } + + public boolean includeCreatedTimestamp() { + return useTimestamps; + } + + public boolean includeModifiedTimestamp() { + return useModifiedTimestamp; + } + + public String getTimestampField() { + return timestampField; + } + + public String getTimestampFormat() { + return timestampFormat; + } + + public boolean overwriteTimestamp() { + return overwriteTimestamp; + } + + public boolean includeTimestamps() { + return useTimestamps && useModifiedTimestamp; + } + + public String now() { + String timeStampFormat = timestampFormat; + return DateTimeFormatter.ofPattern(timeStampFormat).format(LocalDateTime.now()); + } +} diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 16caeb95a31..78ad2e63395 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -59,6 +59,7 @@ import org.jabref.logic.net.ProxyPreferences; import org.jabref.logic.openoffice.OpenOfficePreferences; import org.jabref.logic.openoffice.StyleLoader; +import org.jabref.logic.preferences.TimestampPreferences; import org.jabref.logic.protectedterms.ProtectedTermsList; import org.jabref.logic.protectedterms.ProtectedTermsLoader; import org.jabref.logic.protectedterms.ProtectedTermsPreferences; @@ -235,11 +236,13 @@ public class JabRefPreferences implements PreferencesService { public static final String DEFAULT_OWNER = "defaultOwner"; public static final String DEFAULT_ENCODING = "defaultEncoding"; public static final String TOOLBAR_VISIBLE = "toolbarVisible"; + // Timestamp preferences + public static final String USE_TIME_STAMP = "useTimeStamp"; public static final String UPDATE_TIMESTAMP = "updateTimestamp"; public static final String TIME_STAMP_FIELD = "timeStampField"; public static final String TIME_STAMP_FORMAT = "timeStampFormat"; public static final String OVERWRITE_TIME_STAMP = "overwriteTimeStamp"; - public static final String USE_TIME_STAMP = "useTimeStamp"; + public static final String WARN_ABOUT_DUPLICATES_IN_INSPECTION = "warnAboutDuplicatesInInspection"; public static final String UNMARK_ALL_ENTRIES_BEFORE_IMPORTING = "unmarkAllEntriesBeforeImporting"; public static final String MARK_IMPORTED_ENTRIES = "markImportedEntries"; @@ -1400,6 +1403,10 @@ public BibtexKeyPatternPreferences getBibtexKeyPatternPreferences() { getBoolean(ENFORCE_LEGAL_BIBTEX_KEY), getKeyPattern(), getKeywordDelimiter()); } + public TimestampPreferences getTimestampPreferences() { + return new TimestampPreferences(getBoolean(USE_TIME_STAMP), getBoolean(UPDATE_TIMESTAMP), get(TIME_STAMP_FIELD), get(TIME_STAMP_FORMAT), getBoolean(OVERWRITE_TIME_STAMP)); + } + public LayoutFormatterPreferences getLayoutFormatterPreferences( JournalAbbreviationLoader journalAbbreviationLoader) { Objects.requireNonNull(journalAbbreviationLoader);