Coverage Report - net.sf.jabref.external.PushToEmacs
 
Classes in this File Line Coverage Branch Coverage Complexity
PushToEmacs
45%
19/42
25%
2/8
1.833
PushToEmacs$1
0%
0/13
0%
0/4
1.833
 
 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  
  * Created by IntelliJ IDEA.
 14  
  * User: alver
 15  
  * Date: Jan 14, 2006
 16  
  * Time: 4:55:23 PM
 17  
  * To change this template use File | Settings | File Templates.
 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  
                 // Windows gnuclient escaping:
 72  
                 // java string: "(insert \\\"\\\\cite{Blah2001}\\\")";
 73  
                 // so cmd receives: (insert \"\\cite{Blah2001}\")
 74  
                 // so emacs receives: (insert "\cite{Blah2001}")
 75  
                 new String[] {"gnuclient", "-qe",
 76  
                 "(insert \\\"\\\\" + Globals.prefs.get("citeCommandEmacs").replaceAll("\\\\", "\\\\\\\\") +
 77  
                         "{" + keys + "}\\\")"}
 78  
             :
 79  
                 // Linux gnuclient escaping:
 80  
                 // java string: "(insert \"\\\\cite{Blah2001}\")"
 81  
                 // so sh receives: (insert "\\cite{Blah2001}")
 82  
                 // so emacs receives: (insert "\cite{Blah2001}")
 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  
                     // Error stream has been closed. See if there were any errors:
 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