From fbb8459d345f269ffc5397d79832108cfb222981 Mon Sep 17 00:00:00 2001 From: Admir Obralija Date: Sun, 17 Jul 2016 18:40:30 +0200 Subject: [PATCH] Add command executor. --- .../java/net/sf/jabref/JabRefPreferences.java | 6 +++ .../sf/jabref/gui/desktop/JabRefDesktop.java | 12 +++++- .../sf/jabref/gui/preftabs/ExternalTab.java | 42 +++++++++++++++++-- src/main/resources/l10n/JabRef_da.properties | 3 ++ src/main/resources/l10n/JabRef_de.properties | 3 ++ src/main/resources/l10n/JabRef_en.properties | 2 + src/main/resources/l10n/JabRef_es.properties | 3 ++ src/main/resources/l10n/JabRef_fa.properties | 3 ++ src/main/resources/l10n/JabRef_fr.properties | 3 ++ src/main/resources/l10n/JabRef_in.properties | 3 ++ src/main/resources/l10n/JabRef_it.properties | 3 ++ src/main/resources/l10n/JabRef_ja.properties | 3 ++ src/main/resources/l10n/JabRef_nl.properties | 3 ++ src/main/resources/l10n/JabRef_no.properties | 3 ++ .../resources/l10n/JabRef_pt_BR.properties | 3 ++ src/main/resources/l10n/JabRef_ru.properties | 3 ++ src/main/resources/l10n/JabRef_sv.properties | 3 ++ src/main/resources/l10n/JabRef_tr.properties | 3 ++ src/main/resources/l10n/JabRef_vi.properties | 3 ++ src/main/resources/l10n/JabRef_zh.properties | 3 ++ 20 files changed, 105 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/sf/jabref/JabRefPreferences.java b/src/main/java/net/sf/jabref/JabRefPreferences.java index 79d178103f7c..5b70db28a7df 100644 --- a/src/main/java/net/sf/jabref/JabRefPreferences.java +++ b/src/main/java/net/sf/jabref/JabRefPreferences.java @@ -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 @@ -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, ""); diff --git a/src/main/java/net/sf/jabref/gui/desktop/JabRefDesktop.java b/src/main/java/net/sf/jabref/gui/desktop/JabRefDesktop.java index acb7afe6d6b3..0c0f628087fd 100644 --- a/src/main/java/net/sf/jabref/gui/desktop/JabRefDesktop.java +++ b/src/main/java/net/sf/jabref/gui/desktop/JabRefDesktop.java @@ -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(); diff --git a/src/main/java/net/sf/jabref/gui/preftabs/ExternalTab.java b/src/main/java/net/sf/jabref/gui/preftabs/ExternalTab.java index c9e36d0c9905..d2a0ec762dae 100644 --- a/src/main/java/net/sf/jabref/gui/preftabs/ExternalTab.java +++ b/src/main/java/net/sf/jabref/gui/preftabs/ExternalTab.java @@ -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; @@ -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; @@ -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( + "" + + Localization.lang("Note: Use the placeholder {DIR}" + + " for the location of the current database file.") + + ""); 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; @@ -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", ""); @@ -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(); @@ -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 @@ -233,5 +266,6 @@ public void actionPerformed(ActionEvent e) { private void updateEnableStatus() { consoleEmulatorPath.setEnabled(specifiedConsole.isSelected()); browseButton.setEnabled(specifiedConsole.isSelected()); + consoleCommand.setEnabled(executeConsole.isSelected()); } } diff --git a/src/main/resources/l10n/JabRef_da.properties b/src/main/resources/l10n/JabRef_da.properties index 867360e97860..db96de39d262 100644 --- a/src/main/resources/l10n/JabRef_da.properties +++ b/src/main/resources/l10n/JabRef_da.properties @@ -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= +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.= diff --git a/src/main/resources/l10n/JabRef_de.properties b/src/main/resources/l10n/JabRef_de.properties index fc87e0c183f2..e298d7db96d4 100644 --- a/src/main/resources/l10n/JabRef_de.properties +++ b/src/main/resources/l10n/JabRef_de.properties @@ -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 +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.=Hinweis\:_{DIR}_als_Platzhalter_für_den_Speicherort_der_Datenbank_benutzen. diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index cac91d53b67f..62935af2e449 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -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 +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.=Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file. diff --git a/src/main/resources/l10n/JabRef_es.properties b/src/main/resources/l10n/JabRef_es.properties index 08a1c59425be..69b737793322 100644 --- a/src/main/resources/l10n/JabRef_es.properties +++ b/src/main/resources/l10n/JabRef_es.properties @@ -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= +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.= diff --git a/src/main/resources/l10n/JabRef_fa.properties b/src/main/resources/l10n/JabRef_fa.properties index 652710f19923..3823f6455b12 100644 --- a/src/main/resources/l10n/JabRef_fa.properties +++ b/src/main/resources/l10n/JabRef_fa.properties @@ -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= +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.= diff --git a/src/main/resources/l10n/JabRef_fr.properties b/src/main/resources/l10n/JabRef_fr.properties index 27679ad58881..400619437c84 100644 --- a/src/main/resources/l10n/JabRef_fr.properties +++ b/src/main/resources/l10n/JabRef_fr.properties @@ -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= +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.= diff --git a/src/main/resources/l10n/JabRef_in.properties b/src/main/resources/l10n/JabRef_in.properties index 1a45af039370..c84b07d8b1b3 100644 --- a/src/main/resources/l10n/JabRef_in.properties +++ b/src/main/resources/l10n/JabRef_in.properties @@ -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= +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.= diff --git a/src/main/resources/l10n/JabRef_it.properties b/src/main/resources/l10n/JabRef_it.properties index eeaf5106680b..ddd56e1323a4 100644 --- a/src/main/resources/l10n/JabRef_it.properties +++ b/src/main/resources/l10n/JabRef_it.properties @@ -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= +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.= diff --git a/src/main/resources/l10n/JabRef_ja.properties b/src/main/resources/l10n/JabRef_ja.properties index 0c463ec69d41..10eb09c3a6b7 100644 --- a/src/main/resources/l10n/JabRef_ja.properties +++ b/src/main/resources/l10n/JabRef_ja.properties @@ -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= +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.= diff --git a/src/main/resources/l10n/JabRef_nl.properties b/src/main/resources/l10n/JabRef_nl.properties index 5927ccafe6ac..fdfee3e294d1 100644 --- a/src/main/resources/l10n/JabRef_nl.properties +++ b/src/main/resources/l10n/JabRef_nl.properties @@ -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= +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.= diff --git a/src/main/resources/l10n/JabRef_no.properties b/src/main/resources/l10n/JabRef_no.properties index 830dbe878442..c3680da6639d 100644 --- a/src/main/resources/l10n/JabRef_no.properties +++ b/src/main/resources/l10n/JabRef_no.properties @@ -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= +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.= diff --git a/src/main/resources/l10n/JabRef_pt_BR.properties b/src/main/resources/l10n/JabRef_pt_BR.properties index e0869a7aae87..29e35ae1a96d 100644 --- a/src/main/resources/l10n/JabRef_pt_BR.properties +++ b/src/main/resources/l10n/JabRef_pt_BR.properties @@ -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= +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.= diff --git a/src/main/resources/l10n/JabRef_ru.properties b/src/main/resources/l10n/JabRef_ru.properties index e0a42f85309f..d919a533bad3 100644 --- a/src/main/resources/l10n/JabRef_ru.properties +++ b/src/main/resources/l10n/JabRef_ru.properties @@ -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= +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.= diff --git a/src/main/resources/l10n/JabRef_sv.properties b/src/main/resources/l10n/JabRef_sv.properties index 8445e7bb0c45..38a988b27642 100644 --- a/src/main/resources/l10n/JabRef_sv.properties +++ b/src/main/resources/l10n/JabRef_sv.properties @@ -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= +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.= diff --git a/src/main/resources/l10n/JabRef_tr.properties b/src/main/resources/l10n/JabRef_tr.properties index f461cce1c6d9..114117ae28a4 100644 --- a/src/main/resources/l10n/JabRef_tr.properties +++ b/src/main/resources/l10n/JabRef_tr.properties @@ -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= +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.= diff --git a/src/main/resources/l10n/JabRef_vi.properties b/src/main/resources/l10n/JabRef_vi.properties index 89027fd1df28..b0b7a759f8c9 100644 --- a/src/main/resources/l10n/JabRef_vi.properties +++ b/src/main/resources/l10n/JabRef_vi.properties @@ -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= +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.= diff --git a/src/main/resources/l10n/JabRef_zh.properties b/src/main/resources/l10n/JabRef_zh.properties index adfd3d6d67d6..6134e05224da 100644 --- a/src/main/resources/l10n/JabRef_zh.properties +++ b/src/main/resources/l10n/JabRef_zh.properties @@ -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= +Note\:_Use_the_placeholder_{DIR}_for_the_location_of_the_current_database_file.=