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

Fix right clicking on any entry and selecting "Open folder" results in the NullPointer exception #4797

Merged
merged 6 commits into from
Apr 13, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,5 @@ Johannes Manner <johannes.manner@web.de>
Dominik Traczyk <dominik646@gmail.com>
Cerrianne Santos <cerrianne.santos@gmail.com>
Stefan Scheffel <st155160@stud.uni-stuttgart.de>
Stefan Gerzmann <steppery@gmx.de>
Stefan Gerzmann <steppery@gmx.de>
Deepak Kumar <deepakkumar24196@gmail.com>
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We changed the title of Group Dialog to "Add subgroup" from "Edit group" when we select Add subgroup option.
- We enable import button only if entries are selected. [#4755](https://github.com/JabRef/jabref/issues/4755)
- We made modifications to improve contrast of UI elements. [#4583](https://github.com/JabRef/jabref/issues/4583)
- We add an option in preference settings to set what action to be taken by JabRef when right clicking on any entry in any database and selecting "Open folder". [#4763](https://github.com/JabRef/jabref/issues/4763)

### Fixed
- We fixed an issue where corresponding groups are sometimes not highlighted when clicking on entries [#3112](https://github.com/JabRef/jabref/issues/3112)
Expand Down Expand Up @@ -99,8 +100,8 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We fixed an issue where only one PDF file could be imported [#4422](https://github.com/JabRef/jabref/issues/4422)
- We fixed an issue where "Move to group" would always move the first entry in the library and not the selected [#4414](https://github.com/JabRef/jabref/issues/4414)
- We fixed an issue where an older dialog appears when downloading full texts from the quality menu. [#4489](https://github.com/JabRef/jabref/issues/4489)


- We fixed an issue where right clicking on any entry in any database and selecting "Open folder" results in the NullPointer exception. [#4763](https://github.com/JabRef/jabref/issues/4763)
- We fixed an issue where option 'open terminal here' with custom command was passing wrong argument. [#4802](https://github.com/JabRef/jabref/issues/4802)



Expand Down
43 changes: 31 additions & 12 deletions src/main/java/org/jabref/gui/desktop/JabRefDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,33 @@ private static void openExternalFilePlatformIndependent(Optional<ExternalFileTyp
* @throws IOException
*/
public static void openFolderAndSelectFile(Path fileLink) throws IOException {
NATIVE_DESKTOP.openFolderAndSelectFile(fileLink);
if (fileLink == null) {
return;
}
boolean usingDefault = Globals.prefs.getBoolean(JabRefPreferences.USE_DEFAULT_FILE_BROWSER_APPLICATION);

if (usingDefault) {
NATIVE_DESKTOP.openFolderAndSelectFile(fileLink);
} else {
String absolutePath = fileLink.toAbsolutePath().getParent().toString();
String command = Globals.prefs.get(JabRefPreferences.FILE_BROWSER_COMMAND);
if (!command.isEmpty()) {
command = command.replaceAll("\\s+", " "); // normalize white spaces

// replace the placeholder if used
command = command.replace("%DIR", absolutePath);
String[] subcommands = command.split(" ");

LOGGER.info("Executing command \"" + command + "\"...");

try {
new ProcessBuilder(subcommands).start();
} catch (IOException exception) {
LOGGER.error("Open File Browser", exception);
JabRefGUI.getMainFrame().getDialogService().notify(Localization.lang("Error occured while executing the command \"%0\".", command));
}
}
}
}

/**
Expand Down Expand Up @@ -234,24 +260,17 @@ public static void openConsole(File file) throws IOException {

if (!command.isEmpty()) {
command = command.replaceAll("\\s+", " "); // normalize white spaces
String[] subcommands = command.split(" ");
command = command.replace("%DIR", absolutePath); // replace the placeholder if used

// replace the placeholder if used
String commandLoggingText = command.replace("%DIR", absolutePath);
String[] subcommands = command.split(" ");

JabRefGUI.getMainFrame().output(Localization.lang("Executing command \"%0\"...", commandLoggingText));
LOGGER.info("Executing command \"" + commandLoggingText + "\"...");
LOGGER.info("Executing command \"" + command + "\"...");

try {
new ProcessBuilder(subcommands).start();
} catch (IOException exception) {
LOGGER.error("Open console", exception);

JOptionPane.showMessageDialog(null,
Localization.lang("Error occured while executing the command \"%0\".", commandLoggingText),
Localization.lang("Open console") + " - " + Localization.lang("Error"),
JOptionPane.ERROR_MESSAGE);
JabRefGUI.getMainFrame().output(null);
JabRefGUI.getMainFrame().getDialogService().notify(Localization.lang("Error occured while executing the command \"%0\".", command));
}
}
}
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/org/jabref/gui/desktop/os/Linux.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ public void openFileWithApplication(String filePath, String application) throws

@Override
public void openFolderAndSelectFile(Path filePath) throws IOException {
String desktopSession = System.getenv("DESKTOP_SESSION").toLowerCase(Locale.ROOT);
String desktopSession = System.getenv("DESKTOP_SESSION");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this an Optional.ofNullable(System.get...)
and then later you can check with ...isPresent()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thanks, I'll make changes here.


String cmd;
String cmd = "xdg-open " + filePath.toAbsolutePath().getParent().toString(); //default command

if (desktopSession.contains("gnome")) {
cmd = "nautilus" + filePath.toString().replace(" ", "\\ ");
} else if (desktopSession.contains("kde")) {
cmd = "dolphin --select " + filePath.toString().replace(" ", "\\ ");
} else {
cmd = "xdg-open " + filePath.toAbsolutePath().getParent().toString();
if (desktopSession != null) {
desktopSession = desktopSession.toLowerCase(Locale.ROOT);
if (desktopSession.contains("gnome")) {
cmd = "nautilus" + filePath.toString().replace(" ", "\\ ");
} else if (desktopSession.contains("kde")) {
cmd = "dolphin --select " + filePath.toString().replace(" ", "\\ ");
}
}

Runtime.getRuntime().exec(cmd);
}

Expand All @@ -73,7 +73,6 @@ public void openConsole(String absolutePath) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

String emulatorName = reader.readLine();

if (emulatorName != null) {
emulatorName = emulatorName.substring(emulatorName.lastIndexOf(File.separator) + 1);

Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/jabref/gui/preferences/ExternalTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.OS;
import org.jabref.model.strings.StringUtil;
import org.jabref.preferences.JabRefPreferences;

class ExternalTab implements PrefsTab {
Expand All @@ -38,6 +39,12 @@ class ExternalTab implements PrefsTab {
private final RadioButton sumatraReader;
private final TextField adobeAcrobatReaderPath;
private final TextField sumatraReaderPath;

private final RadioButton defaultFileBrowser;
private final RadioButton executeFileBrowser;
private final TextField fileBrowserCommand;
private final Button fileBrowserButton;

private final GridPane builder = new GridPane();
private final DialogService dialogService;
private final FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder().build();
Expand All @@ -59,11 +66,20 @@ public ExternalTab(JabRefFrame frame, PreferencesDialog prefsDiag, JabRefPrefere
sumatraReaderPath = new TextField();
Button browseSumatraReader = new Button(Localization.lang("Browse"));

defaultFileBrowser = new RadioButton(Localization.lang("Use default file browser"));
executeFileBrowser = new RadioButton(Localization.lang("Execute Command"));
fileBrowserCommand = new TextField();
fileBrowserButton = new Button(Localization.lang("Browser"));

Label commandDescription = new Label(Localization.lang("Note: Use the placeholder %0 for the location of the opened library file.", "%DIR"));
defaultConsole.setOnAction(e -> updateExecuteConsoleButtonAndFieldEnabledState());
executeConsole.setOnAction(e -> updateExecuteConsoleButtonAndFieldEnabledState());
browseButton.setOnAction(e -> showConsoleChooser());

fileBrowserButton.disableProperty().bind(executeFileBrowser.selectedProperty().not());
fileBrowserCommand.disableProperty().bind(executeFileBrowser.selectedProperty().not());
fileBrowserButton.setOnAction(e -> showFileBrowserCommandChooser());

browseAdobeAcrobatReader.setOnAction(e -> showAdobeChooser());

GridPane consoleOptionPanel = new GridPane();
Expand All @@ -83,6 +99,17 @@ public ExternalTab(JabRefFrame frame, PreferencesDialog prefsDiag, JabRefPrefere
adobeAcrobatReader.setToggleGroup(pdfReaderGroup);
pdfOptionPanel.add(browseAdobeAcrobatReader, 3, 1);

Label fileBrowserCommandDescription = new Label(Localization.lang("Note: Use the placeholder %0 for the location of the opened library file.", "%DIR"));
GridPane fileBrowserOptionPanel = new GridPane();
final ToggleGroup fileBrowserGroup = new ToggleGroup();
defaultFileBrowser.setToggleGroup(fileBrowserGroup);
executeFileBrowser.setToggleGroup(fileBrowserGroup);
fileBrowserOptionPanel.add(defaultFileBrowser, 1, 1);
fileBrowserOptionPanel.add(executeFileBrowser, 1, 2);
fileBrowserOptionPanel.add(fileBrowserCommand, 2, 2);
fileBrowserOptionPanel.add(fileBrowserButton, 3, 2);
fileBrowserOptionPanel.add(fileBrowserCommandDescription, 2, 3);

if (OS.WINDOWS) {
browseSumatraReader.setOnAction(e -> showSumatraChooser());
pdfOptionPanel.add(sumatraReader, 1, 2);
Expand Down Expand Up @@ -133,6 +160,12 @@ public ExternalTab(JabRefFrame frame, PreferencesDialog prefsDiag, JabRefPrefere

builder.add(pdfOptionPanel, 1, 13);

Label openFileBrowser = new Label(Localization.lang("Open File Browser"));
openFileBrowser.getStyleClass().add("sectionHeader");
builder.add(openFileBrowser, 1, 14);

builder.add(fileBrowserOptionPanel, 1, 15);

}

@Override
Expand Down Expand Up @@ -165,6 +198,10 @@ public void setValues() {

consoleCommand.setText(Globals.prefs.get(JabRefPreferences.CONSOLE_COMMAND));

defaultFileBrowser.setSelected(Globals.prefs.getBoolean(JabRefPreferences.USE_DEFAULT_FILE_BROWSER_APPLICATION));
executeFileBrowser.setSelected(!Globals.prefs.getBoolean(JabRefPreferences.USE_DEFAULT_FILE_BROWSER_APPLICATION));
fileBrowserCommand.setText(Globals.prefs.get(JabRefPreferences.FILE_BROWSER_COMMAND));

adobeAcrobatReaderPath.setText(Globals.prefs.get(JabRefPreferences.ADOBE_ACROBAT_COMMAND));
if (OS.WINDOWS) {
sumatraReaderPath.setText(Globals.prefs.get(JabRefPreferences.SUMATRA_PDF_COMMAND));
Expand All @@ -187,6 +224,13 @@ public void storeSettings() {
prefs.putBoolean(JabRefPreferences.USE_DEFAULT_CONSOLE_APPLICATION, defaultConsole.isSelected());
prefs.put(JabRefPreferences.CONSOLE_COMMAND, consoleCommand.getText());
prefs.put(JabRefPreferences.ADOBE_ACROBAT_COMMAND, adobeAcrobatReaderPath.getText());

prefs.putBoolean(JabRefPreferences.USE_DEFAULT_FILE_BROWSER_APPLICATION, defaultFileBrowser.isSelected());
if (StringUtil.isNotBlank(fileBrowserCommand.getText())) {
prefs.put(JabRefPreferences.FILE_BROWSER_COMMAND, fileBrowserCommand.getText());
} else {
prefs.putBoolean(JabRefPreferences.USE_DEFAULT_FILE_BROWSER_APPLICATION, true); //default if no command specified
}
if (OS.WINDOWS) {
prefs.put(JabRefPreferences.SUMATRA_PDF_COMMAND, sumatraReaderPath.getText());
}
Expand Down Expand Up @@ -220,6 +264,10 @@ private void showSumatraChooser() {
dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(file -> sumatraReaderPath.setText(file.toAbsolutePath().toString()));
}

private void showFileBrowserCommandChooser() {
dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(file -> fileBrowserCommand.setText(file.toAbsolutePath().toString()));
}

private void readerSelected() {
if (adobeAcrobatReader.isSelected()) {
prefs.put(JabRefPreferences.USE_PDF_READER, adobeAcrobatReaderPath.getText());
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ public class JabRefPreferences implements PreferencesService {
public static final String ADOBE_ACROBAT_COMMAND = "adobeAcrobatCommand";
public static final String SUMATRA_PDF_COMMAND = "sumatraCommand";
public static final String USE_PDF_READER = "usePDFReader";
public static final String USE_DEFAULT_FILE_BROWSER_APPLICATION = "userDefaultFileBrowserApplication";
public static final String FILE_BROWSER_COMMAND = "fileBrowserCommand";

// Currently, it is not possible to specify defaults for specific entry types
// When this should be made possible, the code to inspect is org.jabref.gui.preferences.BibtexKeyPatternPrefTab.storeSettings() -> LabelPattern keypatterns = getCiteKeyPattern(); etc
public static final String DEFAULT_BIBTEX_KEY_PATTERN = "defaultBibtexKeyPattern";
Expand Down Expand Up @@ -729,16 +732,19 @@ private JabRefPreferences() {
defaults.put(USE_UNIT_FORMATTER_ON_SEARCH, Boolean.TRUE);

defaults.put(USE_DEFAULT_CONSOLE_APPLICATION, Boolean.TRUE);
defaults.put(USE_DEFAULT_FILE_BROWSER_APPLICATION, Boolean.TRUE);
if (OS.WINDOWS) {
defaults.put(CONSOLE_COMMAND, "C:\\Program Files\\ConEmu\\ConEmu64.exe /single /dir \"%DIR\"");
defaults.put(ADOBE_ACROBAT_COMMAND, "C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader");
defaults.put(SUMATRA_PDF_COMMAND, "C:\\Program Files\\SumatraPDF");
defaults.put(USE_PDF_READER, ADOBE_ACROBAT_COMMAND);
defaults.put(FILE_BROWSER_COMMAND, "explorer.exe /select, \"%DIR\"");
} else {
defaults.put(CONSOLE_COMMAND, "");
defaults.put(ADOBE_ACROBAT_COMMAND, "");
defaults.put(SUMATRA_PDF_COMMAND, "");
defaults.put(USE_PDF_READER, "");
defaults.put(FILE_BROWSER_COMMAND, "");
}

//versioncheck defaults
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1737,7 +1737,6 @@ Open\ console=Open console
Use\ default\ terminal\ emulator=Use default terminal emulator
Execute\ command=Execute command
Note\:\ Use\ the\ placeholder\ %0\ for\ the\ location\ of\ the\ opened\ library\ file.=Note: Use the placeholder %0 for the location of the opened library file.
Executing\ command\ \"%0\"...=Executing command \"%0\"...
Error\ occured\ while\ executing\ the\ command\ \"%0\".=Error occured while executing the command \"%0\".
Reformat\ ISSN=Reformat ISSN

Expand Down Expand Up @@ -2129,3 +2128,8 @@ Use\ selected\ document=Use selected document
Accept\ changes=Accept changes
Dismiss\ changes=Dismiss changes
The\ library\ has\ been\ modified\ by\ another\ program.=The library has been modified by another program.

Browser=Browser
Execute\ Command=Execute Command
Open\ File\ Browser=Open File Browser
Use\ default\ file\ browser=Use default file browser