Skip to content

Commit

Permalink
OpenConsole, OpenFolder, OpenExternalFile
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Feb 17, 2020
1 parent 0d2efc4 commit d1d74c7
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 104 deletions.
87 changes: 0 additions & 87 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path> 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();
Expand Down Expand Up @@ -542,37 +527,6 @@ private void copyKeyAndTitle() {
}
}

private void openExternalFile() {
final List<BibEntry> 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<LinkedFile> 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.
Expand Down Expand Up @@ -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<ExternalFileType> types = ExternalFileTypes.getInstance().getExternalFileTypeSelection();
final List<Path> dirs = basePanel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences());
final List<String> extensions = types.stream().map(ExternalFileType::getExtension).collect(Collectors.toList());

// Run the search operation:
FileFinder fileFinder = FileFinders.constructFromConfiguration(Globals.prefs.getAutoLinkPreferences());
try {
List<Path> files = fileFinder.findAssociatedFiles(entry, dirs, extensions);
if (!files.isEmpty()) {
Path file = files.get(0);
Optional<ExternalFileType> 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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
});

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/org/jabref/gui/OpenConsoleAction.java
Original file line number Diff line number Diff line change
@@ -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);
}
});
}
}
53 changes: 53 additions & 0 deletions src/main/java/org/jabref/gui/OpenExternalFileAction.java
Original file line number Diff line number Diff line change
@@ -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<BibEntry> 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();
});
}
}
45 changes: 45 additions & 0 deletions src/main/java/org/jabref/gui/OpenFolderAction.java
Original file line number Diff line number Diff line change
@@ -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();
}));
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 7 additions & 15 deletions src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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());
Expand Down Expand Up @@ -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)));
Expand Down

0 comments on commit d1d74c7

Please sign in to comment.