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

Extend the OpenConsoleFeature #1582

Merged
merged 15 commits into from
Jul 19, 2016
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
## [Unreleased]

### Changed
- [#462](https://github.com/JabRef/jabref/issues/462) Extend the OpenConsoleFeature by offering a selection between default terminal emulator and configurable command execution.
- [#1516](https://github.com/JabRef/jabref/issues/1516) Selected field names are written in uppercase in the entry editor
- For developers: Moved the bst package into logic. This requires the regeneration of antlr sources, execute: gradlew generateSource
- [#1026](https://github.com/JabRef/jabref/issues/1026) JabRef does no longer delete user comments outside of BibTeX entries and strings
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/net/sf/jabref/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ public class JabRefPreferences {
public static final String KEY_PATTERN_REGEX = "KeyPatternRegex";
public static final String KEY_PATTERN_REPLACEMENT = "KeyPatternReplacement";

public static final String CONSOLE_COMMAND = "consoleCommand";
public static final String USE_DEFAULT_CONSOLE_APPLICATION = "useDefaultConsoleApplication";

// Currently, it is not possible to specify defaults for specific entry types
// When this should be made possible, the code to inspect is net.sf.jabref.gui.preftabs.LabelPatternPrefTab.storeSettings() -> LabelPattern keypatterns = getLabelPattern(); etc
public static final String DEFAULT_LABEL_PATTERN = "defaultLabelPattern";
Expand Down Expand Up @@ -864,6 +867,13 @@ private JabRefPreferences() {
defaults.put(LINE_LENGTH, 65);
defaults.put(INDENT, 4);

defaults.put(USE_DEFAULT_CONSOLE_APPLICATION, Boolean.TRUE);
if (OS.WINDOWS) {
defaults.put(CONSOLE_COMMAND, "C:\\Program Files\\ConEmu\\ConEmu64.exe /single /dir \"%DIR\"");
} else {
defaults.put(CONSOLE_COMMAND, "");
}

//versioncheck defaults
defaults.put(VersionPreferences.VERSION_IGNORED_UPDATE, "");
}
Expand Down
47 changes: 46 additions & 1 deletion src/main/java/net/sf/jabref/gui/desktop/JabRefDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.sf.jabref.BibDatabaseContext;
import net.sf.jabref.Globals;
import net.sf.jabref.JabRefGUI;
import net.sf.jabref.JabRefPreferences;
import net.sf.jabref.external.ExternalFileType;
import net.sf.jabref.external.ExternalFileTypeEntryEditor;
import net.sf.jabref.external.ExternalFileTypes;
Expand Down Expand Up @@ -303,13 +304,57 @@ public static void openBrowserShowPopup(String url) {
}
}

/**
* Opens a new console starting on the given file location
*
* If no command is specified in {@link Globals},
* the default system console will be executed.
*
* @param file Location the console should be opened at.
*/
public static void openConsole(File file) throws IOException {
if (file == null) {
return;
}

String absolutePath = file.toPath().toAbsolutePath().getParent().toString();
NATIVE_DESKTOP.openConsole(absolutePath);
boolean usingDefault = Globals.prefs.getBoolean(JabRefPreferences.USE_DEFAULT_CONSOLE_APPLICATION);

if (usingDefault) {
NATIVE_DESKTOP.openConsole(absolutePath);
} else {
String command = Globals.prefs.get(JabRefPreferences.CONSOLE_COMMAND);
command = command.trim();

if (!command.isEmpty()) {
command = command.replaceAll("\\s+", " "); // normalize white spaces
String[] subcommands = command.split(" ");
StringBuilder commandLoggingText = new StringBuilder();

for (int i = 0; i < subcommands.length; i++) {
subcommands[i] = subcommands[i].replace("%DIR", absolutePath); // replace the placeholder if used
commandLoggingText.append(subcommands[i]);
if (i < (subcommands.length - 1)) {
commandLoggingText.append(" ");
}
}

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

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

JOptionPane.showMessageDialog(JabRefGUI.getMainFrame(),
Localization.lang("Error_occured_while_executing_the_command_\"%0\".", commandLoggingText.toString()),
Localization.lang("Open console") + " - " + Localization.lang("Error"),
JOptionPane.ERROR_MESSAGE);
JabRefGUI.getMainFrame().output(null);
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a java doc comment. Also mention that this depends on the user settings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}

// TODO: Move to OS.java
Expand Down
82 changes: 82 additions & 0 deletions src/main/java/net/sf/jabref/gui/preftabs/ExternalTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@
package net.sf.jabref.gui.preftabs;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import net.sf.jabref.Globals;
import net.sf.jabref.JabRefPreferences;
import net.sf.jabref.external.ExternalFileTypeEditor;
import net.sf.jabref.external.push.PushToApplication;
Expand All @@ -45,6 +52,12 @@ class ExternalTab extends JPanel implements PrefsTab {
private final JTextField citeCommand;
private final JCheckBox openFoldersOfAttachedFiles;

private final JRadioButton defaultConsole;
private final JRadioButton executeConsole;
private final JTextField consoleCommand;
private final JButton browseButton;


public ExternalTab(JabRefFrame frame, PreferencesDialog prefsDiag, JabRefPreferences prefs) {
this.prefs = prefs;
this.frame = frame;
Expand All @@ -54,6 +67,47 @@ public ExternalTab(JabRefFrame frame, PreferencesDialog prefsDiag, JabRefPrefere
citeCommand = new JTextField(25);
editFileTypes.addActionListener(ExternalFileTypeEditor.getAction(prefsDiag));


defaultConsole = new JRadioButton(Localization.lang("Use default terminal emulator"));
executeConsole = new JRadioButton(Localization.lang("Execute command") + ":");
consoleCommand = new JTextField();
browseButton = new JButton(Localization.lang("Browse"));

JLabel commandDescription = new JLabel(Localization.lang(
"Note: Use the placeholder %0 for the location of the opened database file.", "%DIR"));

ButtonGroup consoleOptions = new ButtonGroup();
consoleOptions.add(defaultConsole);
consoleOptions.add(executeConsole);

JPanel consoleOptionPanel = new JPanel(new GridBagLayout());
GridBagConstraints layoutConstraints = new GridBagConstraints();

defaultConsole.addActionListener(e -> updateExecuteConsoleButtonAndFieldEnabledState());
executeConsole.addActionListener(e -> updateExecuteConsoleButtonAndFieldEnabledState());
browseButton.addActionListener(e -> showConsoleChooser());

layoutConstraints.fill = GridBagConstraints.HORIZONTAL;

layoutConstraints.gridx = 0;
layoutConstraints.gridy = 0;
layoutConstraints.insets = new Insets(0, 0, 6, 0);
consoleOptionPanel.add(defaultConsole, layoutConstraints);

layoutConstraints.gridy = 1;
consoleOptionPanel.add(executeConsole, layoutConstraints);

layoutConstraints.gridx = 1;
consoleOptionPanel.add(consoleCommand, layoutConstraints);

layoutConstraints.gridx = 2;
layoutConstraints.insets = new Insets(0, 4, 6, 0);
consoleOptionPanel.add(browseButton, layoutConstraints);

layoutConstraints.gridx = 1;
layoutConstraints.gridy = 2;
consoleOptionPanel.add(commandDescription, layoutConstraints);

FormLayout layout = new FormLayout(
"1dlu, 8dlu, left:pref, 4dlu, fill:150dlu, 4dlu, fill:pref", "");

Expand Down Expand Up @@ -92,6 +146,12 @@ public ExternalTab(JabRefFrame frame, PreferencesDialog prefsDiag, JabRefPrefere
builder.nextLine();
builder.append(pan);
builder.append(editFileTypes);
builder.nextLine();

builder.appendSeparator(Localization.lang("Open console"));
builder.nextLine();
builder.append(new JPanel());
builder.append(consoleOptionPanel);

pan = builder.getPanel();
pan.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
Expand All @@ -113,13 +173,22 @@ public void setValues() {
openFoldersOfAttachedFiles.setSelected(prefs.getBoolean(JabRefPreferences.OPEN_FOLDERS_OF_ATTACHED_FILES));

citeCommand.setText(prefs.get(JabRefPreferences.CITE_COMMAND));

defaultConsole.setSelected(Globals.prefs.getBoolean(JabRefPreferences.USE_DEFAULT_CONSOLE_APPLICATION));
executeConsole.setSelected(!Globals.prefs.getBoolean(JabRefPreferences.USE_DEFAULT_CONSOLE_APPLICATION));

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

updateExecuteConsoleButtonAndFieldEnabledState();
}

@Override
public void storeSettings() {
prefs.put(JabRefPreferences.EMAIL_SUBJECT, emailSubject.getText());
prefs.putBoolean(JabRefPreferences.OPEN_FOLDERS_OF_ATTACHED_FILES, openFoldersOfAttachedFiles.isSelected());
prefs.put(JabRefPreferences.CITE_COMMAND, citeCommand.getText());
prefs.putBoolean(JabRefPreferences.USE_DEFAULT_CONSOLE_APPLICATION, defaultConsole.isSelected());
prefs.put(JabRefPreferences.CONSOLE_COMMAND, consoleCommand.getText());
}

@Override
Expand All @@ -131,4 +200,17 @@ public boolean validateSettings() {
public String getTabName() {
return Localization.lang("External programs");
}

private void updateExecuteConsoleButtonAndFieldEnabledState() {
browseButton.setEnabled(executeConsole.isSelected());
consoleCommand.setEnabled(executeConsole.isSelected());
}

private void showConsoleChooser() {
JFileChooser consoleChooser = new JFileChooser();
int answer = consoleChooser.showOpenDialog(ExternalTab.this);
if (answer == JFileChooser.APPROVE_OPTION) {
consoleCommand.setText(consoleChooser.getSelectedFile().getAbsolutePath());
}
}
}
7 changes: 7 additions & 0 deletions src/main/resources/l10n/JabRef_da.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1706,3 +1706,10 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Open_console=
Use_default_terminal_emulator=
Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=
7 changes: 6 additions & 1 deletion src/main/resources/l10n/JabRef_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2423,4 +2423,9 @@ Online_help_forum=Online-Hilfeforum

Custom=


Open_console=Terminal_öffnen
Use_default_terminal_emulator=Standard_Terminal-Emulator_verwenden
Execute_command=Befehl_ausführen
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=Hinweis\:_%0_als_Platzhalter_für_den_Speicherort_der_Datenbank_benutzen.
Executing_command_\"%0\"...=Ausführung_des_Kommandos_\"%0\"...
Error_occured_while_executing_the_command_\"%0\".=Während_der_Ausführung_des_Befehls_\"%0\"_ist_ein_Fehler_aufgetreten.
9 changes: 8 additions & 1 deletion src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2272,4 +2272,11 @@ A_new_version_of_JabRef_has_been_released.=A_new_version_of_JabRef_has_been_rele
JabRef_is_up-to-date.=JabRef_is_up-to-date.
Latest_version=Latest_version
Online_help_forum=Online_help_forum
Custom=Custom
Custom=Custom

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_database_file.=Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.
Executing_command_\"%0\"...=Executing_command_\"%0\"...
Error_occured_while_executing_the_command_\"%0\".=Error_occured_while_executing_the_command_\"%0\".
7 changes: 7 additions & 0 deletions src/main/resources/l10n/JabRef_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1608,3 +1608,10 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Open_console=
Use_default_terminal_emulator=
Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=
7 changes: 7 additions & 0 deletions src/main/resources/l10n/JabRef_fa.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2394,3 +2394,10 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Open_console=
Use_default_terminal_emulator=
Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=
7 changes: 7 additions & 0 deletions src/main/resources/l10n/JabRef_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1652,3 +1652,10 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Open_console=
Use_default_terminal_emulator=
Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=
7 changes: 7 additions & 0 deletions src/main/resources/l10n/JabRef_in.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1627,3 +1627,10 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Open_console=
Use_default_terminal_emulator=
Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=
7 changes: 7 additions & 0 deletions src/main/resources/l10n/JabRef_it.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1727,3 +1727,10 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Open_console=
Use_default_terminal_emulator=
Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=
7 changes: 7 additions & 0 deletions src/main/resources/l10n/JabRef_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2372,3 +2372,10 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Open_console=
Use_default_terminal_emulator=
Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=
7 changes: 7 additions & 0 deletions src/main/resources/l10n/JabRef_nl.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2403,3 +2403,10 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Open_console=
Use_default_terminal_emulator=
Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=
7 changes: 7 additions & 0 deletions src/main/resources/l10n/JabRef_no.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2799,3 +2799,10 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Open_console=
Use_default_terminal_emulator=
Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=
7 changes: 7 additions & 0 deletions src/main/resources/l10n/JabRef_pt_BR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1621,3 +1621,10 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Open_console=
Use_default_terminal_emulator=
Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=
7 changes: 7 additions & 0 deletions src/main/resources/l10n/JabRef_ru.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2371,3 +2371,10 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Open_console=
Use_default_terminal_emulator=
Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=
7 changes: 7 additions & 0 deletions src/main/resources/l10n/JabRef_sv.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1564,3 +1564,10 @@ Online_help_forum=Onlineforum
Custom=Egna



Open_console=
Use_default_terminal_emulator=
Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=
Loading