Skip to content

Commit

Permalink
Fetcher timestamp (#3092)
Browse files Browse the repository at this point in the history
* Remove redundancy

* Update timestamp when entry is fetched #3074

* Unused import

* Move to own preferences class

* Checkstyle is a pain in the ***
  • Loading branch information
stefan-kolb authored and tobiasdiez committed Aug 10, 2017
1 parent 2971f29 commit 451d829
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/JabRefMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/jabref/gui/EntryTypeDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
54 changes: 15 additions & 39 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<FieldChange> 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() {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
9 changes: 8 additions & 1 deletion src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 451d829

Please sign in to comment.