Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some enhancements and cleanups related to dates #1575

Merged
merged 14 commits into from
Jul 26, 2016
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Fixed [#1527](https://github.com/JabRef/jabref/issues/1527): 'Get BibTeX data from DOI' Removes Marking
- Fixed [#1592](https://github.com/JabRef/jabref/issues/1592): LibreOffice: wrong numbers in citation labels
- Fixed [#1321](https://github.com/JabRef/jabref/issues/1321): LaTeX commands in fields not displayed in the list of references
- Date fields in the BibLatex standard are now always formatted in the correct way, independent of the preferences

### Removed
- It is not longer possible to choose to convert HTML sub- and superscripts to equations
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ public void update() {
tidialog.setVisible(true);

if (tidialog.okPressed()) {
UpdateField.setAutomaticFields(Collections.singletonList(bibEntry), false, false);
UpdateField.setAutomaticFields(Collections.singletonList(bibEntry), false, false, Globals.prefs);
insertEntry(bibEntry);
}
});
Expand Down Expand Up @@ -898,7 +898,7 @@ private void paste() {
firstBE = be;
}
UpdateField.setAutomaticFields(be, Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_OWNER),
Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP));
Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP), Globals.prefs);

// We have to clone the
// entries, since the pasted
Expand Down Expand Up @@ -1223,7 +1223,7 @@ public BibEntry newEntry(EntryType type) {
// Set owner/timestamp if options are enabled:
List<BibEntry> list = new ArrayList<>();
list.add(be);
UpdateField.setAutomaticFields(list, true, true);
UpdateField.setAutomaticFields(list, true, true, Globals.prefs);

// Create an UndoableInsertEntry object.
getUndoManager().addEdit(new UndoableInsertEntry(database, be, BasePanel.this));
Expand Down Expand Up @@ -1372,7 +1372,7 @@ public void insertEntry(final BibEntry bibEntry) {
database.insertEntry(bibEntry);
if (Globals.prefs.getBoolean(JabRefPreferences.USE_OWNER)) {
// Set owner field to default value
UpdateField.setAutomaticFields(bibEntry, true, true);
UpdateField.setAutomaticFields(bibEntry, true, true, Globals.prefs);
}
// Create an UndoableInsertEntry object.
getUndoManager().addEdit(new UndoableInsertEntry(database, bibEntry, BasePanel.this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ private void addSelectedEntries(NamedCompound ce, final List<BibEntry> selected)

// Set owner/timestamp if options are enabled:
UpdateField.setAutomaticFields(selected, Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_OWNER),
Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP));
Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP), Globals.prefs);

// Mark entries if we should
if (EntryMarker.shouldMarkEntries()) {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/net/sf/jabref/gui/date/DatePickerButton.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright (C) 2003-2011 Raik Nagel
* 2016 JabRef Contributors
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
Expand Down Expand Up @@ -26,6 +27,7 @@
import javax.swing.JComponent;
import javax.swing.JPanel;

import net.sf.jabref.Globals;
import net.sf.jabref.gui.fieldeditors.FieldEditor;
import net.sf.jabref.gui.util.FocusRequester;
import net.sf.jabref.logic.util.date.EasyDateFormat;
Expand All @@ -37,9 +39,11 @@ public class DatePickerButton implements ActionListener {
private final DatePicker datePicker = new DatePicker();
private final JPanel panel = new JPanel();
private final FieldEditor editor;
private final boolean isoFormat;


public DatePickerButton(FieldEditor pEditor) {
public DatePickerButton(FieldEditor pEditor, Boolean isoFormat) {
this.isoFormat = isoFormat;
datePicker.showButtonOnly(true);
datePicker.addActionListener(this);
datePicker.setShowTodayButton(true);
Expand All @@ -52,7 +56,11 @@ public DatePickerButton(FieldEditor pEditor) {
public void actionPerformed(ActionEvent e) {
Date date = datePicker.getDate();
if (date != null) {
editor.setText(new EasyDateFormat().getDateAt(date));
if (isoFormat) {
editor.setText(EasyDateFormat.isoDateFormat().getDateAt(date));
} else {
editor.setText(EasyDateFormat.fromPreferences(Globals.prefs).getDateAt(date));
}
// Set focus to editor component after changing its text:
new FocusRequester(editor.getTextComponent());
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ public Optional<JComponent> getExtra(final FieldEditor editor) {
|| fieldExtras.contains(FieldProperties.DATE)) {
// double click AND datefield => insert the current date (today)
return FieldExtraComponents.getDateTimeExtraComponent(editor,
fieldExtras.contains(FieldProperties.DATE));
fieldExtras.contains(FieldProperties.DATE), fieldExtras.contains(FieldProperties.ISO_DATE));
} else if (fieldExtras.contains(FieldProperties.EXTERNAL)) {
return FieldExtraComponents.getExternalExtraComponent(panel, editor);
} else if (fieldExtras.contains(FieldProperties.JOURNAL_NAME)) {
Expand Down Expand Up @@ -1126,10 +1126,10 @@ public void actionPerformed(ActionEvent event) {

// Add an UndoableKeyChange to the baseframe's undoManager.
UndoableKeyChange undoableKeyChange = new UndoableKeyChange(panel.getDatabase(), entry, oldValue, newValue);
if (TimeStamp.updateTimeStampIsSet()) {
if (TimeStamp.updateTimeStampIsSet(Globals.prefs)) {
NamedCompound ce = new NamedCompound(undoableKeyChange.getPresentationName());
ce.addEdit(undoableKeyChange);
TimeStamp.doUpdateTimeStamp(entry)
TimeStamp.doUpdateTimeStamp(entry, Globals.prefs)
.ifPresent(fieldChange -> ce.addEdit(new UndoableFieldChange(fieldChange)));
ce.end();
panel.getUndoManager().addEdit(ce);
Expand Down Expand Up @@ -1194,11 +1194,11 @@ public void actionPerformed(ActionEvent event) {

// Add an UndoableFieldChange to the baseframe's undoManager.
UndoableFieldChange undoableFieldChange = new UndoableFieldChange(entry, fieldEditor.getFieldName(), oldValue, toSet);
if (TimeStamp.updateTimeStampIsSet()) {
if (TimeStamp.updateTimeStampIsSet(Globals.prefs)) {
NamedCompound ce = new NamedCompound(undoableFieldChange.getPresentationName());
ce.addEdit(undoableFieldChange);

TimeStamp.doUpdateTimeStamp(entry)
TimeStamp.doUpdateTimeStamp(entry, Globals.prefs)
.ifPresent(fieldChange -> ce.addEdit(new UndoableFieldChange(fieldChange)));
ce.end();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,21 +349,22 @@ public static Optional<JComponent> getSelectorExtraComponent(JabRefFrame frame,
* @param isDatePicker
* @return
*/
public static Optional<JComponent> getDateTimeExtraComponent(FieldEditor editor, Boolean isDatePicker) {
public static Optional<JComponent> getDateTimeExtraComponent(FieldEditor editor, Boolean isDatePicker,
Boolean isoFormat) {
((JTextArea) editor).addMouseListener(new MouseAdapter() {

@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {// double click
String date = new EasyDateFormat().getCurrentDate();
String date = EasyDateFormat.isoDateFormat().getCurrentDate();
editor.setText(date);
}
}
});

// insert a datepicker, if the extras field contains this command
if (isDatePicker) {
DatePickerButton datePicker = new DatePickerButton(editor);
DatePickerButton datePicker = new DatePickerButton(editor, isoFormat);
return Optional.of(datePicker.getDatePicker());
} else {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ private boolean parseWithFreeCiteAndAddEntries() {
if (importedEntries.isEmpty()) {
return false;
} else {
UpdateField.setAutomaticFields(importedEntries, false, false);
UpdateField.setAutomaticFields(importedEntries, false, false, Globals.prefs);
boolean markEntries = EntryMarker.shouldMarkEntries();

for (BibEntry e : importedEntries) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/gui/preftabs/GeneralTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.awt.BorderLayout;
import java.awt.Component;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
Expand Down Expand Up @@ -278,7 +278,7 @@ public void storeSettings() {
public boolean validateSettings() {
try {
// Test if date format is legal:
new SimpleDateFormat(timeStampFormat.getText());
DateTimeFormatter.ofPattern(timeStampFormat.getText());

} catch (IllegalArgumentException ex2) {
JOptionPane.showMessageDialog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private static void mergeFromBibtex(JabRefFrame frame, BasePanel panel, ParserRe
for (BibEntry originalEntry : fromDatabase.getEntries()) {
BibEntry be = (BibEntry) originalEntry.clone();
be.setId(IdGenerator.next());
UpdateField.setAutomaticFields(be, overwriteOwner, overwriteTimeStamp);
UpdateField.setAutomaticFields(be, overwriteOwner, overwriteTimeStamp, Globals.prefs);
database.insertEntry(be);
appendedEntries.add(be);
originalEntries.add(originalEntry);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/sf/jabref/importer/EntryFromPDFCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -95,8 +96,8 @@ private void addEntryDataFromPDDocumentInformation(File pdfFile, BibEntry entry)
Calendar creationDate = pdfDocInfo.getCreationDate();
if (creationDate != null) {
// default time stamp follows ISO-8601. Reason: https://xkcd.com/1179/
String date = new SimpleDateFormat("yyyy-MM-dd")
.format(creationDate.getTime());
String date = LocalDate.of(creationDate.YEAR, creationDate.MONTH + 1, creationDate.DAY_OF_MONTH)
.format(DateTimeFormatter.ISO_LOCAL_DATE);
appendToField(entry, Globals.prefs.get(JabRefPreferences.TIME_STAMP_FIELD), date);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/importer/ImportMenuItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ private ParserResult mergeImportResults(List<ImportFormatReader.UnknownFormatImp

// set timestamp and owner
UpdateField.setAutomaticFields(entries, Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_OWNER),
Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP)); // set timestamp and owner
Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP), Globals.prefs); // set timestamp and owner

boolean markEntries = !openInNew && EntryMarker.shouldMarkEntries();
for (BibEntry entry : entries) {
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/net/sf/jabref/importer/ZipFileChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
Expand Down Expand Up @@ -150,15 +154,15 @@ public ZipFileChooser(ImportCustomizationDialog importCustomizationDialog, ZipFi
* @param zipFile Zip-File
* @return entries that can be selected
*/
private static ZipEntry[] getSelectableZipEntries(ZipFile zipFile) {
private static List<ZipEntry> getSelectableZipEntries(ZipFile zipFile) {
List<ZipEntry> entries = new ArrayList<>();
Enumeration<? extends ZipEntry> e = zipFile.entries();
for (ZipEntry entry : Collections.list(e)) {
if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
entries.add(entry);
}
}
return entries.toArray(new ZipEntry[entries.size()]);
return entries;
}

/*
Expand Down Expand Up @@ -188,13 +192,13 @@ public Dimension getSize() {
*/
private static class ZipFileChooserTableModel extends AbstractTableModel {

private final String[] columnNames = new String[] {Localization.lang("Name"),
Localization.lang("Last modified"), Localization.lang("Size")};
private final ZipEntry[] rows;
private final List<String> columnNames = Arrays.asList(Localization.lang("Name"),
Localization.lang("Last modified"), Localization.lang("Size"));
private final List<ZipEntry> rows;
private final ZipFile zipFile;


ZipFileChooserTableModel(ZipFile zipFile, ZipEntry[] rows) {
ZipFileChooserTableModel(ZipFile zipFile, List<ZipEntry> rows) {
super();
this.rows = rows;
this.zipFile = zipFile;
Expand All @@ -206,7 +210,7 @@ private static class ZipFileChooserTableModel extends AbstractTableModel {
*/
@Override
public int getColumnCount() {
return columnNames.length;
return columnNames.size();
}

/*
Expand All @@ -215,7 +219,7 @@ public int getColumnCount() {
*/
@Override
public int getRowCount() {
return this.rows.length;
return this.rows.size();
}

/*
Expand All @@ -224,7 +228,7 @@ public int getRowCount() {
*/
@Override
public String getColumnName(int col) {
return columnNames[col];
return columnNames.get(col);
}

/**
Expand All @@ -234,7 +238,7 @@ public String getColumnName(int col) {
* @return Zip file entry
*/
public ZipEntry getZipEntry(int rowIndex) {
return this.rows[rowIndex];
return this.rows.get(rowIndex);
}

/**
Expand All @@ -257,7 +261,9 @@ public Object getValueAt(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
value = entry.getName();
} else if (columnIndex == 1) {
value = SimpleDateFormat.getDateTimeInstance().format(new Date(entry.getTime()));
value = ZonedDateTime.ofInstant(new Date(entry.getTime()).toInstant(),
ZoneId.systemDefault())
.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
} else if (columnIndex == 2) {
value = entry.getSize();
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/sf/jabref/logic/layout/format/CurrentDate.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2005-2015 Andreas Rudert, Oscar Gustafsson
Copyright (C) 2005-2016 Andreas Rudert, Oscar Gustafsson

All programs in this directory and
subdirectories are published under the GNU General Public License as
Expand All @@ -25,15 +25,15 @@
*/
package net.sf.jabref.logic.layout.format;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import net.sf.jabref.logic.layout.LayoutFormatter;

/**
* Inserts the current date (the time a database is being exported).
*
* <p>If a fieldText is given, it must be a valid {@link SimpleDateFormat} pattern.
* <p>If a fieldText is given, it must be a valid {@link DateTimeFormatter} pattern.
* If none is given, the format pattern will be <code>yyyy-MM-dd hh:mm:ss z</code>.
* This follows ISO-8601. Reason: <a href="https://xkcd.com/1179/">https://xkcd.com/1179/</a>.</p>
*
Expand All @@ -55,6 +55,6 @@ public String format(String fieldText) {
if ((fieldText != null) && (fieldText.trim() != null) && !fieldText.trim().isEmpty()) {
format = fieldText;
}
return new SimpleDateFormat(format).format(new Date());
return ZonedDateTime.now().format(DateTimeFormatter.ofPattern(format));
}
}
Loading