Skip to content

Commit

Permalink
Add command executor.
Browse files Browse the repository at this point in the history
  • Loading branch information
obraliar committed Jul 17, 2016
1 parent c5c95f7 commit fbb8459
Show file tree
Hide file tree
Showing 20 changed files with 105 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/main/java/net/sf/jabref/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ public class JabRefPreferences {
public static final String KEY_PATTERN_REPLACEMENT = "KeyPatternReplacement";

public static final String CONSOLE_APPLICATION = "consoleApplication";
public static final String CONSOLE_COMMAND = "consoleCommand";
public static final String USE_DEFAULT_CONSOLE_APPLICATION = "useDefaultConsoleApplication";
public static final String USE_SPECIFIED_CONSOLE_APPLICATION = "useSpecifiedConsoleApplication";
public static final String USE_CONSOLE_COMMAND = "useConsoleCommand";

// 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
Expand Down Expand Up @@ -868,7 +871,10 @@ private JabRefPreferences() {
defaults.put(INDENT, 4);

defaults.put(USE_DEFAULT_CONSOLE_APPLICATION, Boolean.TRUE);
defaults.put(USE_SPECIFIED_CONSOLE_APPLICATION, Boolean.FALSE);
defaults.put(USE_CONSOLE_COMMAND, Boolean.FALSE);
defaults.put(CONSOLE_APPLICATION, "");
defaults.put(CONSOLE_COMMAND, "");

//versioncheck defaults
defaults.put(VersionPreferences.VERSION_IGNORED_UPDATE, "");
Expand Down
12 changes: 11 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 @@ -323,10 +323,20 @@ public static void openConsole(File file) throws IOException {
String absolutePath = file.toPath().toAbsolutePath().getParent().toString();
Path path = Paths.get(Globals.prefs.get(JabRefPreferences.CONSOLE_APPLICATION));
boolean usingDefault = Globals.prefs.getBoolean(JabRefPreferences.USE_DEFAULT_CONSOLE_APPLICATION);
boolean usingCommand = Globals.prefs.getBoolean(JabRefPreferences.USE_CONSOLE_COMMAND);
boolean usingSpecified = Globals.prefs.getBoolean(JabRefPreferences.USE_SPECIFIED_CONSOLE_APPLICATION);

if (usingDefault) {
NATIVE_DESKTOP.openConsole(absolutePath);
} else if (Files.exists(path) && !Files.isDirectory(path) && path.isAbsolute()) {
} else if (usingCommand) {
String command = Globals.prefs.get(JabRefPreferences.CONSOLE_COMMAND);
if (!command.isEmpty()) {
command = command.replaceAll("\\{DIR\\}", absolutePath); // replace the placeholder if used
command = command.replaceAll("\\s+", " "); // normalize white spaces
String[] subcommands = command.split(" ");
new ProcessBuilder(subcommands).start();
}
} else if (usingSpecified && Files.exists(path) && !Files.isDirectory(path) && path.isAbsolute()) {
ProcessBuilder process = new ProcessBuilder(path.toString());
process.directory(new File(absolutePath));
process.start();
Expand Down
42 changes: 38 additions & 4 deletions src/main/java/net/sf/jabref/gui/preftabs/ExternalTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
Expand Down Expand Up @@ -60,8 +59,11 @@ class ExternalTab extends JPanel implements PrefsTab {

private final JRadioButton defaultConsole;
private final JRadioButton specifiedConsole;
private final JRadioButton executeConsole;
private final JTextField consoleEmulatorPath;
private final JTextField consoleCommand;
private final JFileChooser consoleChooser;
private final JLabel commandDescription;
private final JButton browseButton;


Expand All @@ -74,21 +76,31 @@ 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"));
specifiedConsole = new JRadioButton(Localization.lang("Specify terminal emulator") + ":");
consoleEmulatorPath = new JTextField(25);
executeConsole = new JRadioButton(Localization.lang("Execute command") + ":");
consoleEmulatorPath = new JTextField();
consoleCommand = new JTextField();
consoleChooser = new JFileChooser();
commandDescription = new JLabel(
"<html>" +
Localization.lang("<u>Note</u>: Use the placeholder <i>{DIR}</i>" +
" for the location of the current database file.") +
"</html>");
browseButton = new JButton(Localization.lang("Browse"));

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

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

defaultConsole.addActionListener(new ConsoleRadioButtonActionListener());
specifiedConsole.addActionListener(new ConsoleRadioButtonActionListener());
executeConsole.addActionListener(new ConsoleRadioButtonActionListener());
browseButton.addActionListener(new BrowseButtonActionListener());

layoutConstraints.fill = GridBagConstraints.HORIZONTAL;
Expand All @@ -107,6 +119,17 @@ public ExternalTab(JabRefFrame frame, PreferencesDialog prefsDiag, JabRefPrefere
layoutConstraints.insets = new Insets(0, 4, 6, 0);
consoleOptionPanel.add(browseButton, layoutConstraints);

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

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

layoutConstraints.gridy = 3;
consoleOptionPanel.add(commandDescription, layoutConstraints);

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

Expand Down Expand Up @@ -173,8 +196,15 @@ public void setValues() {

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

defaultConsole.setSelected(Globals.prefs.getBoolean(JabRefPreferences.USE_DEFAULT_CONSOLE_APPLICATION));
specifiedConsole.setSelected(!Globals.prefs.getBoolean(JabRefPreferences.USE_DEFAULT_CONSOLE_APPLICATION));
if (Globals.prefs.getBoolean(JabRefPreferences.USE_CONSOLE_COMMAND)) {
executeConsole.setSelected(true);
} else if (Globals.prefs.getBoolean(JabRefPreferences.USE_SPECIFIED_CONSOLE_APPLICATION)) {
specifiedConsole.setSelected(true);
} else {
defaultConsole.setSelected(true);
}

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

updateEnableStatus();
Expand All @@ -186,7 +216,10 @@ public void storeSettings() {
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.putBoolean(JabRefPreferences.USE_SPECIFIED_CONSOLE_APPLICATION, specifiedConsole.isSelected());
prefs.putBoolean(JabRefPreferences.USE_CONSOLE_COMMAND, executeConsole.isSelected());
prefs.put(JabRefPreferences.CONSOLE_APPLICATION, consoleEmulatorPath.getText());
prefs.put(JabRefPreferences.CONSOLE_COMMAND, consoleCommand.getText());
}

@Override
Expand Down Expand Up @@ -233,5 +266,6 @@ public void actionPerformed(ActionEvent e) {
private void updateEnableStatus() {
consoleEmulatorPath.setEnabled(specifiedConsole.isSelected());
browseButton.setEnabled(specifiedConsole.isSelected());
consoleCommand.setEnabled(executeConsole.isSelected());
}
}
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_da.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1711,3 +1711,6 @@ Open_console=
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=
Specify_terminal_emulator=
Use_default_terminal_emulator=

Execute_command=
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2429,3 +2429,6 @@ Open_console=Terminal_öffnen
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=Bitte_geben_Sie_den_absoluten_Pfad_zu_einem_exsistierenden_Terminal-Emulator_ein.
Specify_terminal_emulator=Terminal-Emaultor_spezifizieren
Use_default_terminal_emulator=Standard_Terminal-Emulator_verwenden

Execute_command=Kommando_ausführen
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=<u>Hinweis</u>\:_<i>{DIR}</i>_als_Platzhalter_für_den_Speicherort_der_Datenbank_benutzen.
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2278,3 +2278,5 @@ Open_console=Open_console
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=Please_enter_the_absolute_path_to_an_existing_terminal_emulator.
Specify_terminal_emulator=Specify_terminal_emulator
Use_default_terminal_emulator=Use_default_terminal_emulator
Execute_command=Execute_command
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1613,3 +1613,6 @@ Open_console=
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=
Specify_terminal_emulator=
Use_default_terminal_emulator=

Execute_command=
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_fa.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2399,3 +2399,6 @@ Open_console=
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=
Specify_terminal_emulator=
Use_default_terminal_emulator=

Execute_command=
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1657,3 +1657,6 @@ Open_console=
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=
Specify_terminal_emulator=
Use_default_terminal_emulator=

Execute_command=
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_in.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1632,3 +1632,6 @@ Open_console=
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=
Specify_terminal_emulator=
Use_default_terminal_emulator=

Execute_command=
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_it.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1732,3 +1732,6 @@ Open_console=
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=
Specify_terminal_emulator=
Use_default_terminal_emulator=

Execute_command=
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2377,3 +2377,6 @@ Open_console=
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=
Specify_terminal_emulator=
Use_default_terminal_emulator=

Execute_command=
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_nl.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2408,3 +2408,6 @@ Open_console=
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=
Specify_terminal_emulator=
Use_default_terminal_emulator=

Execute_command=
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_no.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2804,3 +2804,6 @@ Open_console=
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=
Specify_terminal_emulator=
Use_default_terminal_emulator=

Execute_command=
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_pt_BR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1626,3 +1626,6 @@ Open_console=
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=
Specify_terminal_emulator=
Use_default_terminal_emulator=

Execute_command=
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_ru.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2376,3 +2376,6 @@ Open_console=
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=
Specify_terminal_emulator=
Use_default_terminal_emulator=

Execute_command=
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_sv.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1569,3 +1569,6 @@ Open_console=
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=
Specify_terminal_emulator=
Use_default_terminal_emulator=

Execute_command=
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_tr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1645,3 +1645,6 @@ Open_console=
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=
Specify_terminal_emulator=
Use_default_terminal_emulator=

Execute_command=
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_vi.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2400,3 +2400,6 @@ Open_console=
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=
Specify_terminal_emulator=
Use_default_terminal_emulator=

Execute_command=
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_zh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1639,3 +1639,6 @@ Open_console=
Please_enter_the_absolute_path_to_an_existing_terminal_emulator.=
Specify_terminal_emulator=
Use_default_terminal_emulator=

Execute_command=
<u>Note</u>\:_Use_the_placeholder_<i>{DIR}</i>_for_the_location_of_the_current_database_file.=

0 comments on commit fbb8459

Please sign in to comment.