Skip to content

Commit

Permalink
Fix arch flaw
Browse files Browse the repository at this point in the history
Co-authored-by: Christoph <siedlerkiller@gmail.com>
Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 8, 2024
1 parent b1cd984 commit 1d38f85
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 35 deletions.
14 changes: 8 additions & 6 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.prefs.BackingStoreException;

import org.jabref.gui.externalfiles.AutoSetFileLinksUtil;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.util.CurrentThreadTaskExecutor;
import org.jabref.logic.JabRefException;
import org.jabref.logic.UiCommand;
Expand Down Expand Up @@ -123,7 +122,8 @@ private Optional<ParserResult> importBibtexToOpenBase(String argument, ImportFor
}

/**
* @param importArguments: <code>fileName[,format]</code>
*
* @param importArguments Format: <code>fileName[,format]</code>
*/
private Optional<ParserResult> importFile(String importArguments) {
LOGGER.debug("Importing file {}", importArguments);
Expand Down Expand Up @@ -730,15 +730,17 @@ private void resetPreferences(String value) {
private void automaticallySetFileLinks(List<ParserResult> loaded) {
for (ParserResult parserResult : loaded) {
BibDatabase database = parserResult.getDatabase();
LOGGER.info(Localization.lang("Automatically setting file links"));

// this needs the named compound and it will execute stuff on the javafx thread
LOGGER.info(Localization.lang("Automatically setting file links for {}", parserResult.getDatabaseContext().getDatabasePath()
.map(Path::getFileName)
.map(Path::toString)
.orElse("UNKNOWN")));

AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(
parserResult.getDatabaseContext(),
preferencesService.getFilePreferences(),
preferencesService.getAutoLinkPreferences());
util.linkAssociatedFiles(database.getEntries(), new NamedCompound(""));

util.linkAssociatedFiles(database.getEntries(), (linkedFile, bibEntry) -> bibEntry.addFile(linkedFile));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.externalfiles;

import java.util.List;
import java.util.function.BiConsumer;

import javax.swing.undo.UndoManager;

Expand All @@ -10,11 +11,15 @@
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableFieldChange;
import org.jabref.gui.util.BindingsHelper;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.bibtex.FileFieldWriter;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.entry.field.StandardField;
import org.jabref.preferences.PreferencesService;

import static org.jabref.gui.actions.ActionHelper.needsDatabase;
Expand Down Expand Up @@ -47,16 +52,26 @@ public AutoLinkFilesAction(DialogService dialogService, PreferencesService prefe
public void execute() {
final BibDatabaseContext database = stateManager.getActiveDatabase().orElseThrow(() -> new NullPointerException("Database null"));
final List<BibEntry> entries = stateManager.getSelectedEntries();
final AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(

AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(
database,
preferences.getFilePreferences(),
preferences.getAutoLinkPreferences());
final NamedCompound nc = new NamedCompound(Localization.lang("Automatically set file links"));

Task<AutoSetFileLinksUtil.LinkFilesResult> linkFilesTask = new Task<>() {
final BiConsumer<LinkedFile, BibEntry> onLinkedFile = (linkedFile, entry) -> {
// lambda for gui actions that are relevant when setting the linked file entry when ui is opened
String newVal = FileFieldWriter.getStringRepresentation(linkedFile);
String oldVal = entry.getField(StandardField.FILE).orElse(null);
UndoableFieldChange fieldChange = new UndoableFieldChange(entry, StandardField.FILE, oldVal, newVal);
nc.addEdit(fieldChange); // push to undo manager is in succeeded
entry.addFile(linkedFile);
};

@Override
protected AutoSetFileLinksUtil.LinkFilesResult call() {
return util.linkAssociatedFiles(entries, nc);
return util.linkAssociatedFiles(entries, onLinkedFile);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;

import org.jabref.gui.externalfiletype.ExternalFileType;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.externalfiletype.UnknownExternalFileType;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableFieldChange;
import org.jabref.gui.util.UiTaskExecutor;
import org.jabref.logic.bibtex.FileFieldWriter;
import org.jabref.logic.util.io.AutoLinkPreferences;
import org.jabref.logic.util.io.FileFinder;
import org.jabref.logic.util.io.FileFinders;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.entry.field.StandardField;
import org.jabref.preferences.FilePreferences;

import org.slf4j.Logger;
Expand Down Expand Up @@ -66,7 +62,7 @@ private AutoSetFileLinksUtil(List<Path> directories, FilePreferences filePrefere
this.filePreferences = filePreferences;
}

public LinkFilesResult linkAssociatedFiles(List<BibEntry> entries, NamedCompound ce) {
public LinkFilesResult linkAssociatedFiles(List<BibEntry> entries, BiConsumer<LinkedFile, BibEntry> onAddLinkedFile) {
LinkFilesResult result = new LinkFilesResult();

for (BibEntry entry : entries) {
Expand All @@ -79,26 +75,12 @@ public LinkFilesResult linkAssociatedFiles(List<BibEntry> entries, NamedCompound
LOGGER.error("Problem finding files", e);
}

if (ce != null) {
boolean changed = false;

for (LinkedFile linkedFile : linkedFiles) {
// store undo information
String newVal = FileFieldWriter.getStringRepresentation(linkedFile);
String oldVal = entry.getField(StandardField.FILE).orElse(null);
UndoableFieldChange fieldChange = new UndoableFieldChange(entry, StandardField.FILE, oldVal, newVal);
ce.addEdit(fieldChange);
changed = true;

UiTaskExecutor.runInJavaFXThread(() -> {
entry.addFile(linkedFile);
});
}

if (changed) {
result.addBibEntry(entry);
}
for (LinkedFile linkedFile : linkedFiles) {
// store undo information
onAddLinkedFile.accept(linkedFile, entry);
}

result.addBibEntry(entry);
}
return result;
}
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/org/jabref/gui/frame/JabRefFrameViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jabref.gui.LibraryTab;
import org.jabref.gui.LibraryTabContainer;
import org.jabref.gui.StateManager;
import org.jabref.gui.externalfiles.AutoLinkFilesAction;
import org.jabref.gui.importer.ImportEntriesDialog;
import org.jabref.gui.importer.ParserResultWarningDialog;
import org.jabref.gui.importer.actions.OpenDatabaseAction;
Expand Down Expand Up @@ -158,7 +159,14 @@ public void handleUiCommands(List<UiCommand> uiCommands) {
.forEach(command -> openDatabases(command.parserResults()));
}

// Handle automatically setting file links
uiCommands.stream()
.filter(UiCommand.AutoSetFileLinks.class::isInstance).findAny()
.map(UiCommand.AutoSetFileLinks.class::cast)
.ifPresent(autoSetFileLinks -> autoSetFileLinks(autoSetFileLinks.parserResults()));

// Handle jumpToEntry
// Needs to go last, because it requires all libraries opened
uiCommands.stream()
.filter(UiCommand.JumpToEntryKey.class::isInstance)
.map(UiCommand.JumpToEntryKey.class::cast)
Expand All @@ -172,14 +180,13 @@ public void handleUiCommands(List<UiCommand> uiCommands) {
}

private void openDatabases(List<ParserResult> parserResults) {
final List<ParserResult> failed = new ArrayList<>();
final List<ParserResult> toOpenTab = new ArrayList<>();

// Remove invalid databases
List<ParserResult> invalidDatabases = parserResults.stream()
.filter(ParserResult::isInvalid)
.toList();
failed.addAll(invalidDatabases);
final List<ParserResult> failed = new ArrayList<>(invalidDatabases);
parserResults.removeAll(invalidDatabases);

// passed file (we take the first one) should be focused
Expand Down Expand Up @@ -397,4 +404,10 @@ void addImportedEntries(final LibraryTab tab, final ParserResult parserResult) {
dialog.setTitle(Localization.lang("Import"));
dialogService.showCustomDialogAndWait(dialog);
}

void autoSetFileLinks(List<ParserResult> loaded) {
for (ParserResult parserResult : loaded) {
new AutoLinkFilesAction(dialogService, preferences, stateManager, undoManager, taskExecutor).execute();
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/logic/UiCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ record BlankWorkspace() implements UiCommand { }
record JumpToEntryKey(String citationKey) implements UiCommand { }

record OpenDatabases(List<ParserResult> parserResults) implements UiCommand { }

record AutoSetFileLinks(List<ParserResult> parserResults) implements UiCommand { }
}

0 comments on commit 1d38f85

Please sign in to comment.