Skip to content

Commit

Permalink
Removed JabRefPreferences dependency in FileUtil and removed TimeStam…
Browse files Browse the repository at this point in the history
…p class (#1791)
  • Loading branch information
oscargus authored Aug 22, 2016
1 parent 3f16e05 commit a746c76
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import net.sf.jabref.gui.JabRefFrame;
import net.sf.jabref.gui.net.MonitoredURLDownload;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.layout.LayoutFormatterPreferences;
import net.sf.jabref.logic.net.URLDownload;
import net.sf.jabref.logic.util.OS;
import net.sf.jabref.logic.util.io.FileUtil;
import net.sf.jabref.preferences.JabRefPreferences;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -255,8 +257,9 @@ private void downloadFinished() {
// FIXME: will break download if no bibtexkey is present!
private String getSuggestedFileName(String suffix) {
String plannedName = FileUtil.createFileNameFromPattern(databaseContext.getDatabase(),
frame.getCurrentBasePanel().getSelectedEntries().get(0), Globals.journalAbbreviationLoader,
Globals.prefs);
frame.getCurrentBasePanel().getSelectedEntries().get(0),
Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN),
LayoutFormatterPreferences.fromPreferences(Globals.prefs, Globals.journalAbbreviationLoader));

if (!suffix.isEmpty()) {
plannedName += "." + suffix;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/sf/jabref/external/DroppedFileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.sf.jabref.gui.undo.UndoableFieldChange;
import net.sf.jabref.gui.undo.UndoableInsertEntry;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.layout.LayoutFormatterPreferences;
import net.sf.jabref.logic.util.OS;
import net.sf.jabref.logic.util.io.FileUtil;
import net.sf.jabref.logic.xmp.XMPPreferences;
Expand Down Expand Up @@ -334,8 +335,9 @@ private boolean showLinkMoveCopyRenameDialog(String linkFileName, ExternalFileTy
renameCheckBox.setText(Localization.lang("Rename file to").concat(": "));

// Determine which name to suggest:
String targetName = FileUtil.createFileNameFromPattern(database, entry, Globals.journalAbbreviationLoader,
Globals.prefs);
String targetName = FileUtil.createFileNameFromPattern(database, entry,
Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN),
LayoutFormatterPreferences.fromPreferences(Globals.prefs, Globals.journalAbbreviationLoader));

renameToTextBox.setText(targetName.concat(".").concat(fileType.getExtension()));

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/net/sf/jabref/external/MoveFileAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.sf.jabref.gui.fieldeditors.FileListEditor;
import net.sf.jabref.gui.util.component.CheckBoxMessage;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.layout.LayoutFormatterPreferences;
import net.sf.jabref.logic.util.io.FileUtil;
import net.sf.jabref.preferences.JabRefPreferences;

Expand Down Expand Up @@ -97,7 +98,9 @@ public void actionPerformed(ActionEvent event) {
// Determine which name to suggest:
String suggName = FileUtil
.createFileNameFromPattern(eEditor.getDatabase(), eEditor.getEntry(),
Globals.journalAbbreviationLoader, Globals.prefs)
Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN),
LayoutFormatterPreferences.fromPreferences(Globals.prefs,
Globals.journalAbbreviationLoader))
.concat(entry.type.isPresent() ? "." + entry.type.get().getExtension() : "");
CheckBoxMessage cbm = new CheckBoxMessage(Localization.lang("Move file to file directory?"),
Localization.lang("Rename to '%0'", suggName),
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/net/sf/jabref/gui/actions/CleanupAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.sf.jabref.logic.cleanup.CleanupPreset;
import net.sf.jabref.logic.cleanup.CleanupWorker;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.layout.LayoutFormatterPreferences;
import net.sf.jabref.model.FieldChange;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.preferences.JabRefPreferences;
Expand Down Expand Up @@ -146,7 +147,9 @@ private int showDialog(CleanupPresetPanel presetPanel) {
private void doCleanup(CleanupPreset preset, BibEntry entry, NamedCompound ce) {
// Create and run cleaner
BibDatabaseContext bibDatabaseContext = panel.getBibDatabaseContext();
CleanupWorker cleaner = new CleanupWorker(bibDatabaseContext, Globals.journalAbbreviationLoader, Globals.prefs);
CleanupWorker cleaner = new CleanupWorker(bibDatabaseContext,
Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN),
LayoutFormatterPreferences.fromPreferences(Globals.prefs, Globals.journalAbbreviationLoader));
List<FieldChange> changes = cleaner.cleanup(preset, entry);

unsuccessfulRenames = cleaner.getUnsuccessfulRenames();
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/net/sf/jabref/gui/date/DatePickerButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.sf.jabref.gui.fieldeditors.FieldEditor;
import net.sf.jabref.gui.util.FocusRequester;
import net.sf.jabref.logic.util.date.EasyDateFormat;
import net.sf.jabref.preferences.JabRefPreferences;

import com.michaelbaranov.microba.calendar.DatePicker;

Expand Down Expand Up @@ -43,7 +44,9 @@ public void actionPerformed(ActionEvent e) {
if (isoFormat) {
editor.setText(EasyDateFormat.isoDateFormat().getDateAt(date));
} else {
editor.setText(EasyDateFormat.fromPreferences(Globals.prefs).getDateAt(date));
editor.setText(EasyDateFormat
.fromTimeStampFormat(Globals.prefs.get(JabRefPreferences.TIME_STAMP_FORMAT))
.getDateAt(date));
}
// Set focus to editor component after changing its text:
new FocusRequester(editor.getTextComponent());
Expand Down
29 changes: 23 additions & 6 deletions src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@
import net.sf.jabref.logic.importer.fileformat.BibtexParser;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.search.SearchQueryHighlightListener;
import net.sf.jabref.logic.util.date.TimeStamp;
import net.sf.jabref.logic.util.UpdateField;
import net.sf.jabref.logic.util.date.EasyDateFormat;
import net.sf.jabref.model.EntryTypes;
import net.sf.jabref.model.FieldChange;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -1129,11 +1131,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(Globals.prefs)) {
if (updateTimeStampIsSet()) {
NamedCompound ce = new NamedCompound(undoableKeyChange.getPresentationName());
ce.addEdit(undoableKeyChange);
TimeStamp.doUpdateTimeStamp(entry, Globals.prefs)
.ifPresent(fieldChange -> ce.addEdit(new UndoableFieldChange(fieldChange)));
doUpdateTimeStamp().ifPresent(fieldChange -> ce.addEdit(new UndoableFieldChange(fieldChange)));
ce.end();
panel.getUndoManager().addEdit(ce);
} else {
Expand Down Expand Up @@ -1196,11 +1197,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(Globals.prefs)) {
if (updateTimeStampIsSet()) {
NamedCompound ce = new NamedCompound(undoableFieldChange.getPresentationName());
ce.addEdit(undoableFieldChange);

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

Expand Down Expand Up @@ -1503,4 +1504,20 @@ public void actionPerformed(ActionEvent event) {
}
}


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 = EasyDateFormat.fromTimeStampFormat(timeStampFormat).getCurrentDate();
return UpdateField.updateField(entry, timeStampField, timestamp);
}

}
15 changes: 7 additions & 8 deletions src/main/java/net/sf/jabref/logic/cleanup/CleanupWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@
import java.util.Objects;

import net.sf.jabref.BibDatabaseContext;
import net.sf.jabref.logic.journals.JournalAbbreviationLoader;
import net.sf.jabref.logic.layout.LayoutFormatterPreferences;
import net.sf.jabref.model.FieldChange;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.preferences.JabRefPreferences;

public class CleanupWorker {

private final BibDatabaseContext databaseContext;
private final JournalAbbreviationLoader repositoryLoader;
private final JabRefPreferences prefs;
private final String fileNamePattern;
private final LayoutFormatterPreferences prefs;
private int unsuccessfulRenames;


public CleanupWorker(BibDatabaseContext databaseContext, JournalAbbreviationLoader repositoryLoader,
JabRefPreferences prefs) {
public CleanupWorker(BibDatabaseContext databaseContext, String fileNamePattern,
LayoutFormatterPreferences prefs) {
this.databaseContext = databaseContext;
this.repositoryLoader = repositoryLoader;
this.fileNamePattern = fileNamePattern;
this.prefs = prefs;
}

Expand Down Expand Up @@ -68,7 +67,7 @@ private List<CleanupJob> determineCleanupActions(CleanupPreset preset) {
}
if (preset.isRenamePDF()) {
RenamePdfCleanup cleaner = new RenamePdfCleanup(preset.isRenamePdfOnlyRelativePaths(), databaseContext,
repositoryLoader, prefs);
fileNamePattern, prefs);
jobs.add(cleaner);
unsuccessfulRenames += cleaner.getUnsuccessfulRenames();
}
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/net/sf/jabref/logic/cleanup/RenamePdfCleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,28 @@

import net.sf.jabref.BibDatabaseContext;
import net.sf.jabref.logic.TypedBibEntry;
import net.sf.jabref.logic.journals.JournalAbbreviationLoader;
import net.sf.jabref.logic.layout.LayoutFormatterPreferences;
import net.sf.jabref.logic.util.OS;
import net.sf.jabref.logic.util.io.FileUtil;
import net.sf.jabref.model.FieldChange;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.ParsedFileField;
import net.sf.jabref.preferences.JabRefPreferences;

public class RenamePdfCleanup implements CleanupJob {

private final BibDatabaseContext databaseContext;
private final boolean onlyRelativePaths;
private final JournalAbbreviationLoader repositoryLoader;
private final JabRefPreferences prefs;
private final String fileNamePattern;
private final LayoutFormatterPreferences prefs;

private int unsuccessfulRenames;


public RenamePdfCleanup(boolean onlyRelativePaths, BibDatabaseContext databaseContext,
JournalAbbreviationLoader repositoryLoader, JabRefPreferences prefs) {
String fileNamePattern, LayoutFormatterPreferences prefs) {
this.databaseContext = Objects.requireNonNull(databaseContext);
this.onlyRelativePaths = onlyRelativePaths;
this.repositoryLoader = Objects.requireNonNull(repositoryLoader);
this.fileNamePattern = Objects.requireNonNull(fileNamePattern);
this.prefs = Objects.requireNonNull(prefs);
}

Expand All @@ -50,9 +49,8 @@ public List<FieldChange> cleanup(BibEntry entry) {
continue;
}

StringBuilder newFilename = new StringBuilder(
FileUtil.createFileNameFromPattern(databaseContext.getDatabase(), entry, repositoryLoader, prefs)
.trim());
StringBuilder newFilename = new StringBuilder(FileUtil
.createFileNameFromPattern(databaseContext.getDatabase(), entry, fileNamePattern, prefs).trim());

//Add extension to newFilename
newFilename.append('.').append(FileUtil.getFileExtension(realOldFilename).orElse("pdf"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.time.format.DateTimeFormatter;
import java.util.Date;

import net.sf.jabref.preferences.JabRefPreferences;

public class EasyDateFormat {

/**
Expand Down Expand Up @@ -59,10 +57,6 @@ public static EasyDateFormat fromTimeStampFormat(String timeStampFormat) {
return new EasyDateFormat(timeStampFormat);
}

public static EasyDateFormat fromPreferences(JabRefPreferences preferences) {
return new EasyDateFormat(preferences.get(JabRefPreferences.TIME_STAMP_FORMAT));
}

public static EasyDateFormat isoDateFormat() {
return new EasyDateFormat(DateTimeFormatter.ISO_LOCAL_DATE);
}
Expand Down
27 changes: 0 additions & 27 deletions src/main/java/net/sf/jabref/logic/util/date/TimeStamp.java

This file was deleted.

16 changes: 7 additions & 9 deletions src/main/java/net/sf/jabref/logic/util/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.regex.Pattern;

import net.sf.jabref.BibDatabaseContext;
import net.sf.jabref.logic.journals.JournalAbbreviationLoader;
import net.sf.jabref.logic.layout.Layout;
import net.sf.jabref.logic.layout.LayoutFormatterPreferences;
import net.sf.jabref.logic.layout.LayoutHelper;
Expand All @@ -31,7 +30,6 @@
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.model.entry.FileField;
import net.sf.jabref.model.entry.ParsedFileField;
import net.sf.jabref.preferences.JabRefPreferences;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -371,19 +369,19 @@ public static List<File> getListOfLinkedFiles(List<BibEntry> bes, List<String> f
/**
* Determines filename provided by an entry in a database
*
* @param database the database, where the entry is located
* @param entry the entry to which the file should be linked to
* @param repositoryLoader
* @param database the database, where the entry is located
* @param entry the entry to which the file should be linked to
* @param fileNamePattern the filename pattern
* @param prefs the layout preferences
* @return a suggested fileName
*/
public static String createFileNameFromPattern(BibDatabase database, BibEntry entry,
JournalAbbreviationLoader repositoryLoader, JabRefPreferences prefs) {
String fileNamePattern, LayoutFormatterPreferences prefs) {
String targetName = entry.getCiteKeyOptional().orElse("default");
StringReader sr = new StringReader(prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN));
StringReader sr = new StringReader(fileNamePattern);
Layout layout = null;
try {
layout = new LayoutHelper(sr, LayoutFormatterPreferences.fromPreferences(prefs, repositoryLoader))
.getLayoutFromText();
layout = new LayoutHelper(sr, prefs).getLayoutFromText();
} catch (IOException e) {
LOGGER.info("Wrong format " + e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import net.sf.jabref.logic.formatter.bibtexfields.NormalizePagesFormatter;
import net.sf.jabref.logic.formatter.bibtexfields.UnitsToLatexFormatter;
import net.sf.jabref.logic.formatter.casechanger.ProtectTermsFormatter;
import net.sf.jabref.logic.journals.JournalAbbreviationLoader;
import net.sf.jabref.logic.layout.LayoutFormatterPreferences;
import net.sf.jabref.logic.protectedterms.ProtectedTermsLoader;
import net.sf.jabref.logic.protectedterms.ProtectedTermsPreferences;
import net.sf.jabref.model.FieldChange;
Expand Down Expand Up @@ -52,16 +52,14 @@ public void setUp() throws IOException {
if (Globals.prefs == null) {
Globals.prefs = JabRefPreferences.getInstance();
}
if (Globals.journalAbbreviationLoader == null) {
Globals.journalAbbreviationLoader = mock(JournalAbbreviationLoader.class);
}

pdfFolder = bibFolder.newFolder();

MetaData metaData = new MetaData();
metaData.setDefaultFileDirectory(pdfFolder.getAbsolutePath());
BibDatabaseContext context = new BibDatabaseContext(new BibDatabase(), metaData, bibFolder.newFile("test.bib"));
worker = new CleanupWorker(context, mock(JournalAbbreviationLoader.class), Globals.prefs);
worker = new CleanupWorker(context, Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN),
mock(LayoutFormatterPreferences.class));
}

@Test(expected = NullPointerException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import java.util.Optional;

import net.sf.jabref.BibDatabaseContext;
import net.sf.jabref.logic.journals.JournalAbbreviationLoader;
import net.sf.jabref.logic.layout.LayoutFormatterPreferences;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.preferences.JabRefPreferences;

import org.junit.Assert;
import org.junit.Before;
Expand All @@ -20,8 +19,7 @@ public class ISSNCleanupTest {

@Before
public void setUp() {
worker = new CleanupWorker(mock(BibDatabaseContext.class), mock(JournalAbbreviationLoader.class),
JabRefPreferences.getInstance());
worker = new CleanupWorker(mock(BibDatabaseContext.class), "", mock(LayoutFormatterPreferences.class));
}

@Test
Expand Down
Loading

0 comments on commit a746c76

Please sign in to comment.