| 1 | |
package net.sf.jabref.external; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.InputStream; |
| 5 | |
|
| 6 | |
import javax.swing.*; |
| 7 | |
|
| 8 | |
import net.sf.jabref.*; |
| 9 | |
import com.jgoodies.forms.builder.DefaultFormBuilder; |
| 10 | |
import com.jgoodies.forms.layout.FormLayout; |
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | 3088196515 | public class PushToEmacs implements PushToApplication { |
| 20 | |
|
| 21 | 3088196515 | private JPanel settings = null; |
| 22 | 3088196515 | private JTextField citeCommand = new JTextField(30); |
| 23 | |
|
| 24 | 3088196515 | private boolean couldNotConnect=false, couldNotRunClient=false; |
| 25 | |
|
| 26 | |
public String getName() { |
| 27 | 122301037 | return Globals.menuTitle("Insert selected citations into Emacs") ; |
| 28 | |
} |
| 29 | |
|
| 30 | |
public String getApplicationName() { |
| 31 | 2965989648 | return "Emacs"; |
| 32 | |
} |
| 33 | |
|
| 34 | |
public String getTooltip() { |
| 35 | 2965989648 | return Globals.lang("Push selection to Emacs"); |
| 36 | |
} |
| 37 | |
|
| 38 | |
public Icon getIcon() { |
| 39 | 3088243600 | return GUIGlobals.getImage("emacs"); |
| 40 | |
} |
| 41 | |
|
| 42 | |
public String getKeyStrokeName() { |
| 43 | 0 | return "Push to Emacs"; |
| 44 | |
} |
| 45 | |
|
| 46 | |
public JPanel getSettingsPanel() { |
| 47 | 790815 | if (settings == null) |
| 48 | 789490 | initSettingsPanel(); |
| 49 | 790815 | citeCommand.setText(Globals.prefs.get("citeCommandEmacs")); |
| 50 | 790815 | return settings; |
| 51 | |
} |
| 52 | |
|
| 53 | |
public void storeSettings() { |
| 54 | 187846 | Globals.prefs.put("citeCommandEmacs", citeCommand.getText()); |
| 55 | 187846 | } |
| 56 | |
|
| 57 | |
private void initSettingsPanel() { |
| 58 | 789490 | DefaultFormBuilder builder = new DefaultFormBuilder( |
| 59 | |
new FormLayout("left:pref, 4dlu, fill:pref", "")); |
| 60 | 789490 | builder.append(Globals.lang("Cite command") + ":"); |
| 61 | 789490 | builder.append(citeCommand); |
| 62 | 789490 | settings = builder.getPanel(); |
| 63 | 789490 | } |
| 64 | |
|
| 65 | |
public void pushEntries(BibtexDatabase database, BibtexEntry[] entries, String keys, MetaData metaData) { |
| 66 | |
|
| 67 | 0 | couldNotConnect=false; |
| 68 | 0 | couldNotRunClient=false; |
| 69 | |
try { |
| 70 | 0 | String[] com = Globals.ON_WIN ? |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
new String[] {"gnuclient", "-qe", |
| 76 | |
"(insert \\\"\\\\" + Globals.prefs.get("citeCommandEmacs").replaceAll("\\\\", "\\\\\\\\") + |
| 77 | |
"{" + keys + "}\\\")"} |
| 78 | |
: |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
new String[] {"gnuclient", "-batch", "-eval", |
| 84 | |
"(insert \"" + Globals.prefs.get("citeCommandEmacs").replaceAll("\\\\", "\\\\\\\\") + |
| 85 | |
"{" + keys + "}\")"}; |
| 86 | |
|
| 87 | 0 | final Process p = Runtime.getRuntime().exec(com); |
| 88 | |
|
| 89 | 0 | Runnable errorListener = new Runnable() { |
| 90 | |
public void run() { |
| 91 | 0 | InputStream out = p.getErrorStream(); |
| 92 | |
int c; |
| 93 | 0 | StringBuffer sb = new StringBuffer(); |
| 94 | |
try { |
| 95 | 0 | while ((c = out.read()) != -1) |
| 96 | 0 | sb.append((char) c); |
| 97 | 0 | } catch (IOException e) { |
| 98 | 0 | e.printStackTrace(); |
| 99 | 0 | } |
| 100 | |
|
| 101 | 0 | if (sb.toString().trim().length() > 0) { |
| 102 | 0 | System.out.println(sb.toString()); |
| 103 | 0 | couldNotConnect = true; |
| 104 | 0 | return; |
| 105 | |
} |
| 106 | 0 | } |
| 107 | |
}; |
| 108 | 0 | Thread t = new Thread(errorListener); |
| 109 | 0 | t.start(); |
| 110 | 0 | t.join(); |
| 111 | |
} |
| 112 | 0 | catch (IOException excep) { |
| 113 | 0 | couldNotRunClient = true; |
| 114 | 0 | return; |
| 115 | 0 | } catch (InterruptedException e) { |
| 116 | 0 | e.printStackTrace(); |
| 117 | 0 | } |
| 118 | |
|
| 119 | 0 | } |
| 120 | |
|
| 121 | |
public void operationCompleted(BasePanel panel) { |
| 122 | 0 | if (couldNotConnect) |
| 123 | 0 | JOptionPane.showMessageDialog( |
| 124 | |
panel.frame(), |
| 125 | |
"<HTML>"+ |
| 126 | |
Globals.lang("Could not connect to a running gnuserv process. Make sure that " |
| 127 | |
+"Emacs or XEmacs is running,<BR>and that the server has been started " |
| 128 | |
+"(by running the command 'gnuserv-start').") |
| 129 | |
+"</HTML>", |
| 130 | |
Globals.lang("Error"), JOptionPane.ERROR_MESSAGE); |
| 131 | 0 | else if (couldNotRunClient) |
| 132 | 0 | JOptionPane.showMessageDialog( |
| 133 | |
panel.frame(), |
| 134 | |
Globals.lang("Could not run the 'gnuclient' program. Make sure you have " |
| 135 | |
+"the gnuserv/gnuclient programs installed."), |
| 136 | |
Globals.lang("Error"), JOptionPane.ERROR_MESSAGE); |
| 137 | |
else { |
| 138 | 0 | panel.output(Globals.lang("Pushed citations to Emacs")); |
| 139 | |
} |
| 140 | 0 | } |
| 141 | |
|
| 142 | |
public boolean requiresBibtexKeys() { |
| 143 | 0 | return true; |
| 144 | |
} |
| 145 | |
} |
| 146 | |
|