From d1d74c7d4d12c81b1b324fc4f973f2f437b2bb83 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Mon, 17 Feb 2020 23:14:23 +0100 Subject: [PATCH] OpenConsole, OpenFolder, OpenExternalFile --- src/main/java/org/jabref/gui/BasePanel.java | 87 ------------------- src/main/java/org/jabref/gui/JabRefFrame.java | 2 +- .../org/jabref/gui/OpenConsoleAction.java | 35 ++++++++ .../jabref/gui/OpenExternalFileAction.java | 53 +++++++++++ .../java/org/jabref/gui/OpenFolderAction.java | 45 ++++++++++ .../org/jabref/gui/maintable/MainTable.java | 2 +- .../jabref/gui/maintable/RightClickMenu.java | 22 ++--- 7 files changed, 142 insertions(+), 104 deletions(-) create mode 100644 src/main/java/org/jabref/gui/OpenConsoleAction.java create mode 100644 src/main/java/org/jabref/gui/OpenExternalFileAction.java create mode 100644 src/main/java/org/jabref/gui/OpenFolderAction.java diff --git a/src/main/java/org/jabref/gui/BasePanel.java b/src/main/java/org/jabref/gui/BasePanel.java index 767e6199dfa..f45506934ff 100644 --- a/src/main/java/org/jabref/gui/BasePanel.java +++ b/src/main/java/org/jabref/gui/BasePanel.java @@ -306,21 +306,6 @@ private void setupActions() { actions.put(Actions.MERGE_DATABASE, new AppendDatabaseAction(frame, this)); - actions.put(Actions.OPEN_EXTERNAL_FILE, this::openExternalFile); - - actions.put(Actions.OPEN_FOLDER, () -> JabRefExecutorService.INSTANCE.execute(() -> { - final List files = FileUtil.getListOfLinkedFiles(mainTable.getSelectedEntries(), bibDatabaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences())); - for (final Path f : files) { - try { - JabRefDesktop.openFolderAndSelectFile(f.toAbsolutePath()); - } catch (IOException e) { - LOGGER.info("Could not open folder", e); - } - } - })); - - actions.put(Actions.OPEN_CONSOLE, () -> JabRefDesktop.openConsole(frame.getCurrentBasePanel().getBibDatabaseContext().getDatabaseFile().orElse(null))); - actions.put(Actions.PULL_CHANGES_FROM_SHARED_DATABASE, () -> { DatabaseSynchronizer dbmsSynchronizer = frame.getCurrentBasePanel().getBibDatabaseContext().getDBMSSynchronizer(); dbmsSynchronizer.pullChanges(); @@ -542,37 +527,6 @@ private void copyKeyAndTitle() { } } - private void openExternalFile() { - final List selectedEntries = mainTable.getSelectedEntries(); - if (selectedEntries.size() != 1) { - output(Localization.lang("This operation requires exactly one item to be selected.")); - return; - } - JabRefExecutorService.INSTANCE.execute(() -> { - final BibEntry entry = selectedEntries.get(0); - if (!entry.hasField(StandardField.FILE)) { - // no bibtex field - new SearchAndOpenFile(entry, BasePanel.this).searchAndOpen(); - return; - } - - List files = new ArrayList<>(); - entry.getField(StandardField.FILE).map(FileFieldParser::parse).ifPresent(files::addAll); - - if (files.isEmpty()) { - // content in BibTeX field is not readable - new SearchAndOpenFile(entry, BasePanel.this).searchAndOpen(); - return; - } - LinkedFile flEntry = files.get(0); - try { - JabRefDesktop.openExternalFileAnyFormat(this.getBibDatabaseContext(), flEntry.getLink(), ExternalFileTypes.getInstance().fromLinkedFile(flEntry, true)); - } catch (IOException ex) { - dialogService.showErrorDialogAndWait(ex); - } - }); - } - /** * This method is called from JabRefFrame if a database specific action is requested by the user. Runs the command * if it is defined, or prints an error message to the standard error stream. @@ -1076,47 +1030,6 @@ public void cut() { mainTable.cut(); } - private static class SearchAndOpenFile { - - private final BibEntry entry; - private final BasePanel basePanel; - - public SearchAndOpenFile(final BibEntry entry, final BasePanel basePanel) { - this.entry = entry; - this.basePanel = basePanel; - } - - public void searchAndOpen() { - if (!Globals.prefs.getBoolean(JabRefPreferences.RUN_AUTOMATIC_FILE_SEARCH)) { - /* The search can lead to an unexpected 100% CPU usage which is perceived - as a bug, if the search incidentally starts at a directory with lots - of stuff below. It is now disabled by default. */ - return; - } - - final Set types = ExternalFileTypes.getInstance().getExternalFileTypeSelection(); - final List dirs = basePanel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences()); - final List extensions = types.stream().map(ExternalFileType::getExtension).collect(Collectors.toList()); - - // Run the search operation: - FileFinder fileFinder = FileFinders.constructFromConfiguration(Globals.prefs.getAutoLinkPreferences()); - try { - List files = fileFinder.findAssociatedFiles(entry, dirs, extensions); - if (!files.isEmpty()) { - Path file = files.get(0); - Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByFile(file); - if (type.isPresent()) { - JabRefDesktop.openExternalFileAnyFormat(file, basePanel.getBibDatabaseContext(), type); - basePanel.output(Localization.lang("External viewer called") + '.'); - } - } - } catch (IOException ex) { - LOGGER.error("Problems with finding/or opening files ", ex); - basePanel.output(Localization.lang("Error") + ": " + ex.getMessage()); - } - } - } - private class GroupTreeListener { @Subscribe diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index 6712bfd9777..e93c8c8896a 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -812,7 +812,7 @@ private MenuBar createMenu() { factory.createMenuItem(StandardActions.SHOW_PDF_VIEWER, new ShowDocumentViewerAction()), factory.createMenuItem(StandardActions.EDIT_ENTRY, new OldDatabaseCommandWrapper(Actions.EDIT, this, stateManager)), - factory.createMenuItem(StandardActions.OPEN_CONSOLE, new OldDatabaseCommandWrapper(Actions.OPEN_CONSOLE, this, stateManager)) + factory.createMenuItem(StandardActions.OPEN_CONSOLE, new OpenConsoleAction(stateManager)) ); }); diff --git a/src/main/java/org/jabref/gui/OpenConsoleAction.java b/src/main/java/org/jabref/gui/OpenConsoleAction.java new file mode 100644 index 00000000000..34cf38b261c --- /dev/null +++ b/src/main/java/org/jabref/gui/OpenConsoleAction.java @@ -0,0 +1,35 @@ +package org.jabref.gui; + +import java.io.IOException; + +import org.jabref.gui.actions.SimpleCommand; +import org.jabref.gui.desktop.JabRefDesktop; +import org.jabref.model.database.BibDatabaseContext; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.jabref.gui.actions.ActionHelper.needsDatabase; + +public class OpenConsoleAction extends SimpleCommand { + + private static final Logger LOGGER = LoggerFactory.getLogger(OpenConsoleAction.class); + private final StateManager stateManager; + + public OpenConsoleAction(StateManager stateManager) { + this.stateManager = stateManager; + + this.executable.bind(needsDatabase(stateManager)); + } + + @Override + public void execute() { + stateManager.getActiveDatabase().flatMap(BibDatabaseContext::getDatabasePath).ifPresent(path -> { + try { + JabRefDesktop.openConsole(path.toFile()); + } catch (IOException e) { + LOGGER.info("Could not open console", e); + } + }); + } +} diff --git a/src/main/java/org/jabref/gui/OpenExternalFileAction.java b/src/main/java/org/jabref/gui/OpenExternalFileAction.java new file mode 100644 index 00000000000..4fd2b376f76 --- /dev/null +++ b/src/main/java/org/jabref/gui/OpenExternalFileAction.java @@ -0,0 +1,53 @@ +package org.jabref.gui; + +import java.util.List; + +import org.jabref.Globals; +import org.jabref.gui.actions.SimpleCommand; +import org.jabref.gui.externalfiletype.ExternalFileTypes; +import org.jabref.gui.fieldeditors.LinkedFileViewModel; +import org.jabref.logic.l10n.Localization; +import org.jabref.model.entry.BibEntry; +import org.jabref.preferences.PreferencesService; + +import static org.jabref.gui.actions.ActionHelper.needsDatabase; + +public class OpenExternalFileAction extends SimpleCommand { + + private final DialogService dialogService; + private final StateManager stateManager; + private final PreferencesService preferencesService; + + public OpenExternalFileAction(DialogService dialogService, StateManager stateManager, PreferencesService preferencesService) { + this.dialogService = dialogService; + this.stateManager = stateManager; + this.preferencesService = preferencesService; + + this.executable.bind(needsDatabase(stateManager)); + } + + @Override + public void execute() { + stateManager.getActiveDatabase().ifPresent(databaseContext -> { + final List selectedEntries = stateManager.getSelectedEntries(); + + if (selectedEntries.size() != 1) { + dialogService.notify(Localization.lang("This operation requires exactly one item to be selected.")); + return; + } + + final BibEntry entry = selectedEntries.get(0); + + LinkedFileViewModel linkedFileViewModel = new LinkedFileViewModel( + entry.getFiles().get(0), + entry, + databaseContext, + Globals.TASK_EXECUTOR, + dialogService, + preferencesService.getXMPPreferences(), + preferencesService.getFilePreferences(), + ExternalFileTypes.getInstance()); + linkedFileViewModel.open(); + }); + } +} diff --git a/src/main/java/org/jabref/gui/OpenFolderAction.java b/src/main/java/org/jabref/gui/OpenFolderAction.java new file mode 100644 index 00000000000..d0512071b41 --- /dev/null +++ b/src/main/java/org/jabref/gui/OpenFolderAction.java @@ -0,0 +1,45 @@ +package org.jabref.gui; + +import org.jabref.Globals; +import org.jabref.gui.actions.SimpleCommand; +import org.jabref.gui.externalfiletype.ExternalFileTypes; +import org.jabref.gui.fieldeditors.LinkedFileViewModel; +import org.jabref.preferences.PreferencesService; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.jabref.gui.actions.ActionHelper.needsDatabase; + +public class OpenFolderAction extends SimpleCommand { + + private static final Logger LOGGER = LoggerFactory.getLogger(OpenFolderAction.class); + private final DialogService dialogService; + private final StateManager stateManager; + private final PreferencesService preferencesService; + + public OpenFolderAction(DialogService dialogService, StateManager stateManager, PreferencesService preferencesService) { + this.dialogService = dialogService; + this.stateManager = stateManager; + this.preferencesService = preferencesService; + + this.executable.bind(needsDatabase(stateManager)); + } + + @Override + public void execute() { + stateManager.getActiveDatabase().ifPresent(databaseContext -> + stateManager.getSelectedEntries().forEach(entry -> { + LinkedFileViewModel linkedFileViewModel = new LinkedFileViewModel( + entry.getFiles().get(0), + entry, + databaseContext, + Globals.TASK_EXECUTOR, + dialogService, + preferencesService.getXMPPreferences(), + preferencesService.getFilePreferences(), + ExternalFileTypes.getInstance()); + linkedFileViewModel.openFolder(); + })); + } +} diff --git a/src/main/java/org/jabref/gui/maintable/MainTable.java b/src/main/java/org/jabref/gui/maintable/MainTable.java index 935470b1599..2293be5e914 100644 --- a/src/main/java/org/jabref/gui/maintable/MainTable.java +++ b/src/main/java/org/jabref/gui/maintable/MainTable.java @@ -85,7 +85,7 @@ public MainTable(MainTableDataModel model, JabRefFrame frame, panel.showAndEdit(entry.getEntry()); } }) - .withContextMenu(entry -> RightClickMenu.create(entry, keyBindingRepository, panel, frame.getDialogService())) + .withContextMenu(entry -> RightClickMenu.create(entry, keyBindingRepository, panel, frame.getDialogService(), Globals.stateManager, Globals.prefs)) .setOnDragDetected(this::handleOnDragDetected) .setOnDragDropped(this::handleOnDragDropped) .setOnDragOver(this::handleOnDragOver) diff --git a/src/main/java/org/jabref/gui/maintable/RightClickMenu.java b/src/main/java/org/jabref/gui/maintable/RightClickMenu.java index 7029810a2fa..1204706c66e 100644 --- a/src/main/java/org/jabref/gui/maintable/RightClickMenu.java +++ b/src/main/java/org/jabref/gui/maintable/RightClickMenu.java @@ -10,6 +10,9 @@ import org.jabref.Globals; import org.jabref.gui.BasePanel; import org.jabref.gui.DialogService; +import org.jabref.gui.OpenExternalFileAction; +import org.jabref.gui.OpenFolderAction; +import org.jabref.gui.StateManager; import org.jabref.gui.actions.ActionFactory; import org.jabref.gui.actions.Actions; import org.jabref.gui.actions.OldCommandWrapper; @@ -27,11 +30,12 @@ import org.jabref.model.entry.field.SpecialField; import org.jabref.model.entry.field.StandardField; import org.jabref.preferences.JabRefPreferences; +import org.jabref.preferences.PreferencesService; import org.jabref.preferences.PreviewPreferences; public class RightClickMenu { - public static ContextMenu create(BibEntryTableViewModel entry, KeyBindingRepository keyBindingRepository, BasePanel panel, DialogService dialogService) { + public static ContextMenu create(BibEntryTableViewModel entry, KeyBindingRepository keyBindingRepository, BasePanel panel, DialogService dialogService, StateManager stateManager, PreferencesService preferencesService) { ContextMenu contextMenu = new ContextMenu(); ActionFactory factory = new ActionFactory(keyBindingRepository); @@ -58,8 +62,8 @@ public static ContextMenu create(BibEntryTableViewModel entry, KeyBindingReposit contextMenu.getItems().add(new SeparatorMenuItem()); - contextMenu.getItems().add(factory.createMenuItem(StandardActions.OPEN_FOLDER, getOpenFolderCommand(panel))); - contextMenu.getItems().add(factory.createMenuItem(StandardActions.OPEN_EXTERNAL_FILE, getOpenExternalFileCommand(panel))); + contextMenu.getItems().add(factory.createMenuItem(StandardActions.OPEN_FOLDER, new OpenFolderAction(dialogService, stateManager, preferencesService))); + contextMenu.getItems().add(factory.createMenuItem(StandardActions.OPEN_EXTERNAL_FILE, new OpenExternalFileAction(dialogService, stateManager, preferencesService))); contextMenu.getItems().add(factory.createMenuItem(StandardActions.OPEN_URL, getOpenUrlCommand(panel))); contextMenu.getItems().add(new SeparatorMenuItem()); @@ -90,18 +94,6 @@ private static OldCommandWrapper getOpenUrlCommand(BasePanel panel) { return command; } - private static OldCommandWrapper getOpenExternalFileCommand(BasePanel panel) { - OldCommandWrapper command = new OldCommandWrapper(Actions.OPEN_EXTERNAL_FILE, panel); - command.setExecutable(isFieldSetForSelectedEntry(StandardField.FILE, panel)); - return command; - } - - private static OldCommandWrapper getOpenFolderCommand(BasePanel panel) { - OldCommandWrapper command = new OldCommandWrapper(Actions.OPEN_FOLDER, panel); - command.setExecutable(isFieldSetForSelectedEntry(StandardField.FILE, panel)); - return command; - } - private static Menu createCopySubMenu(BasePanel panel, ActionFactory factory, DialogService dialogService) { Menu copySpecialMenu = factory.createMenu(StandardActions.COPY_MORE); copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_TITLE, new OldCommandWrapper(Actions.COPY_TITLE, panel)));