13
13
with this program; if not, write to the Free Software Foundation, Inc.,
14
14
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15
15
*/
16
- package net .sf .jabref .external ;
16
+ package net .sf .jabref .external . push ;
17
17
18
18
import java .io .IOException ;
19
19
import java .io .InputStream ;
22
22
23
23
import net .sf .jabref .*;
24
24
25
- import com .jgoodies .forms .builder .FormBuilder ;
26
- import com .jgoodies .forms .layout .FormLayout ;
27
25
import net .sf .jabref .gui .BasePanel ;
28
26
import net .sf .jabref .gui .IconTheme ;
29
- import net .sf .jabref .gui .actions .BrowseAction ;
30
27
import net .sf .jabref .logic .l10n .Localization ;
31
28
import net .sf .jabref .logic .util .OS ;
32
29
import net .sf .jabref .model .database .BibtexDatabase ;
33
30
import net .sf .jabref .model .entry .BibtexEntry ;
34
31
32
+ import org .apache .commons .logging .Log ;
33
+ import org .apache .commons .logging .LogFactory ;
34
+
35
35
/**
36
- * Created by IntelliJ IDEA.
37
- * User: alver
38
- * Date: Jan 14, 2006
39
- * Time: 4:55:23 PM
36
+ * Created by IntelliJ IDEA. User: alver Date: Jan 14, 2006 Time: 4:55:23 PM
40
37
*/
41
- public class PushToEmacs implements PushToApplication {
38
+ public class PushToEmacs extends AbstractPushToApplication implements PushToApplication {
39
+
40
+ private static final Log LOGGER = LogFactory .getLog (PushToEmacs .class );
42
41
43
- private JPanel settings ;
44
- private final JTextField citeCommand = new JTextField (30 );
45
- private final JTextField emacsPath = new JTextField (30 );
46
42
private final JTextField additionalParams = new JTextField (30 );
47
43
private final JCheckBox useEmacs23 = new JCheckBox ();
48
44
49
- private boolean couldNotConnect ;
50
- private boolean couldNotRunClient ;
51
-
52
-
53
- @ Override
54
- public String getName () {
55
- return Localization .lang ("Insert selected citations into %0" ,getApplicationName ());
56
- }
57
45
58
46
@ Override
59
47
public String getApplicationName () {
60
48
return "Emacs" ;
61
49
}
62
50
63
- @ Override
64
- public String getTooltip () {
65
- return Localization .lang ("Push to %0" , getApplicationName ());
66
- }
67
-
68
51
@ Override
69
52
public Icon getIcon () {
70
53
return IconTheme .getImage ("emacs" );
71
54
}
72
55
73
- @ Override
74
- public String getKeyStrokeName () {
75
- return "Push to Emacs" ;
76
- }
77
-
78
56
@ Override
79
57
public JPanel getSettingsPanel () {
80
- if (settings == null ) {
81
- initSettingsPanel ();
82
- }
83
- citeCommand .setText (Globals .prefs .get (JabRefPreferences .CITE_COMMAND_EMACS ));
84
- emacsPath .setText (Globals .prefs .get (JabRefPreferences .EMACS_PATH ));
85
58
additionalParams .setText (Globals .prefs .get (JabRefPreferences .EMACS_ADDITIONAL_PARAMETERS ));
86
59
useEmacs23 .setSelected (Globals .prefs .getBoolean (JabRefPreferences .EMACS_23 ));
87
- return settings ;
60
+ return super . getSettingsPanel () ;
88
61
}
89
62
90
63
@ Override
91
64
public void storeSettings () {
92
- Globals .prefs .put (JabRefPreferences .CITE_COMMAND_EMACS , citeCommand .getText ());
93
- Globals .prefs .put (JabRefPreferences .EMACS_PATH , emacsPath .getText ());
65
+ super .storeSettings ();
94
66
Globals .prefs .put (JabRefPreferences .EMACS_ADDITIONAL_PARAMETERS , additionalParams .getText ());
95
67
Globals .prefs .putBoolean (JabRefPreferences .EMACS_23 , useEmacs23 .isSelected ());
96
68
}
97
69
98
- private void initSettingsPanel () {
99
- FormBuilder builder = FormBuilder .create ();
100
- builder .layout (new FormLayout ("left:pref, 4dlu, fill:pref:grow, 4dlu, fill:pref" , "p, 2dlu, p, 2dlu, p, 2dlu, p" ));
101
- builder .add (Localization .lang ("Path to gnuclient or emacsclient" ) + ":" ).xy (1 , 1 );
102
- builder .add (emacsPath ).xy (3 ,1 );
103
- BrowseAction action = BrowseAction .buildForFile (emacsPath );
104
- JButton browse = new JButton (Localization .lang ("Browse" ));
105
- browse .addActionListener (action );
106
- builder .add (browse ).xy (5 ,1 );
70
+ @ Override
71
+ protected void initSettingsPanel () {
72
+ super .initSettingsPanel ();
73
+ builder .appendRows ("2dlu, p, 2dlu, p" );
107
74
builder .add (Localization .lang ("Additional parameters" ) + ":" ).xy (1 , 3 );
108
- builder .add (additionalParams ).xy (3 ,3 );
75
+ builder .add (additionalParams ).xy (3 , 3 );
109
76
builder .add (Localization .lang ("Use EMACS 23 insertion string" ) + ":" ).xy (1 , 5 );
110
- builder .add (useEmacs23 ).xy (3 ,5 );
111
- builder .add (Localization .lang ("Cite command" ) + ":" ).xy (1 , 7 );
112
- builder .add (citeCommand ).xy (3 ,7 );
77
+ builder .add (useEmacs23 ).xy (3 , 5 );
113
78
settings = builder .build ();
114
79
}
115
80
116
81
@ Override
117
82
public void pushEntries (BibtexDatabase database , BibtexEntry [] entries , String keys , MetaData metaData ) {
118
83
119
84
couldNotConnect = false ;
120
- couldNotRunClient = false ;
121
- String command = Globals .prefs .get (JabRefPreferences .EMACS_PATH );
85
+ couldNotCall = false ;
86
+ notDefined = false ;
87
+
88
+ initParameters ();
89
+ commandPath = Globals .prefs .get (commandPathPreferenceKey );
90
+
91
+ if ((commandPath == null ) || commandPath .trim ().isEmpty ()) {
92
+ notDefined = true ;
93
+ return ;
94
+ }
95
+
96
+ commandPath = Globals .prefs .get (commandPathPreferenceKey );
122
97
String [] addParams = Globals .prefs .get (JabRefPreferences .EMACS_ADDITIONAL_PARAMETERS ).split (" " );
123
98
try {
124
99
String [] com = new String [addParams .length + 2 ];
125
- com [0 ] = command ;
100
+ com [0 ] = commandPath ;
126
101
System .arraycopy (addParams , 0 , com , 1 , addParams .length );
127
102
String prefix ;
128
103
String suffix ;
@@ -135,19 +110,16 @@ public void pushEntries(BibtexDatabase database, BibtexEntry[] entries, String k
135
110
}
136
111
137
112
com [com .length - 1 ] = OS .WINDOWS ?
138
- // Windows gnuclient escaping:
139
- // java string: "(insert \\\"\\\\cite{Blah2001}\\\")";
140
- // so cmd receives: (insert \"\\cite{Blah2001}\")
141
- // so emacs receives: (insert "\cite{Blah2001}")
142
- prefix .concat ("\\ \" \\ " + Globals .prefs .get (JabRefPreferences .CITE_COMMAND_EMACS ).replaceAll ("\\ \\ " , "\\ \\ \\ \\ " ) +
143
- "{" + keys + "}\\ \" " ).concat (suffix )
144
- :
145
- // Linux gnuclient escaping:
146
- // java string: "(insert \"\\\\cite{Blah2001}\")"
147
- // so sh receives: (insert "\\cite{Blah2001}")
148
- // so emacs receives: (insert "\cite{Blah2001}")
149
- prefix .concat ("\" " + Globals .prefs .get (JabRefPreferences .CITE_COMMAND_EMACS ).replaceAll ("\\ \\ " , "\\ \\ \\ \\ " ) +
150
- "{" + keys + "}\" " ).concat (suffix );
113
+ // Windows gnuclient escaping:
114
+ // java string: "(insert \\\"\\\\cite{Blah2001}\\\")";
115
+ // so cmd receives: (insert \"\\cite{Blah2001}\")
116
+ // so emacs receives: (insert "\cite{Blah2001}")
117
+ prefix .concat ("\\ \" \\ " + citeCommand .replaceAll ("\\ \\ " , "\\ \\ \\ \\ " ) + "{" + keys + "}\\ \" " ).concat (suffix ) :
118
+ // Linux gnuclient escaping:
119
+ // java string: "(insert \"\\\\cite{Blah2001}\")"
120
+ // so sh receives: (insert "\\cite{Blah2001}")
121
+ // so emacs receives: (insert "\cite{Blah2001}")
122
+ prefix .concat ("\" " + citeCommand .replaceAll ("\\ \\ " , "\\ \\ \\ \\ " ) + "{" + keys + "}\" " ).concat (suffix );
151
123
152
124
final Process p = Runtime .getRuntime ().exec (com );
153
125
@@ -168,46 +140,49 @@ public void run() {
168
140
sb .append ((char ) c );
169
141
}
170
142
} catch (IOException e ) {
171
- e . printStackTrace ( );
143
+ LOGGER . warn ( "Could not read from stderr." );
172
144
}
173
145
// Error stream has been closed. See if there were any errors:
174
146
if (!sb .toString ().trim ().isEmpty ()) {
175
- System . out . println ( sb );
147
+ LOGGER . warn ( "Push to Emacs error: " + sb );
176
148
couldNotConnect = true ;
177
149
}
178
150
}
179
151
};
180
152
JabRefExecutorService .INSTANCE .executeAndWait (errorListener );
181
153
} catch (IOException excep ) {
182
- couldNotRunClient = true ;
154
+ couldNotCall = true ;
183
155
}
184
156
185
157
}
186
158
187
159
@ Override
188
160
public void operationCompleted (BasePanel panel ) {
189
161
if (couldNotConnect ) {
190
- JOptionPane .showMessageDialog (
191
- panel .frame (),
192
- "<HTML>" +
193
- Localization .lang ("Could not connect to a running gnuserv process. Make sure that "
194
- + "Emacs or XEmacs is running,<BR>and that the server has been started "
195
- + "(by running the command 'server-start'/'gnuserv-start')." )
196
- + "</HTML>" ,
162
+ // @formatter:off
163
+ JOptionPane .showMessageDialog (panel .frame (), "<HTML>" +
164
+ Localization .lang ("Could not connect to a running gnuserv process. Make sure that "
165
+ + "Emacs or XEmacs is running,<BR>and that the server has been started "
166
+ + "(by running the command 'server-start'/'gnuserv-start')." ) + "</HTML>" ,
197
167
Localization .lang ("Error" ), JOptionPane .ERROR_MESSAGE );
198
- } else if (couldNotRunClient ) {
199
- JOptionPane .showMessageDialog (
200
- panel .frame (),
168
+ } else if (couldNotCall ) {
169
+ JOptionPane .showMessageDialog (panel .frame (),
201
170
Localization .lang ("Could not run the gnuclient/emacsclient program. Make sure you have "
202
- + "the emacsclient/gnuclient program installed and available in the PATH." ),
171
+ + "the emacsclient/gnuclient program installed and available in the PATH." ),
203
172
Localization .lang ("Error" ), JOptionPane .ERROR_MESSAGE );
204
- } else {
205
- panel .output (Localization .lang ("Pushed citations to %0" , getApplicationName ()));
173
+ // @formatter:on
206
174
}
175
+ super .operationCompleted (panel );
207
176
}
208
177
209
178
@ Override
210
- public boolean requiresBibtexKeys () {
211
- return true ;
179
+ protected void initParameters () {
180
+ commandPathPreferenceKey = JabRefPreferences . EMACS_PATH ;
212
181
}
182
+
183
+ @ Override
184
+ protected String getCommandName () {
185
+ return "gnuclient " + Localization .lang ("or" ) + " emacsclient" ;
186
+ }
187
+
213
188
}
0 commit comments