Skip to content

Commit d5c3685

Browse files
committed
storage directory and vim command now configurable
closes chazmcgarvey#5
1 parent c981f98 commit d5c3685

File tree

2 files changed

+185
-11
lines changed

2 files changed

+185
-11
lines changed

src/com/dogcows/Editor.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public Editor(ProblemComponentModel component,
7474
this.name = component.getClassName();
7575

7676
// Make sure the top-level vimcoder directory exists.
77-
File topDir = new File(System.getProperty("user.home"), ".vimcoder");
77+
File topDir = VimCoder.getStorageDirectory();
7878
if (!topDir.isDirectory())
7979
{
8080
if (!topDir.mkdirs()) throw new IOException(topDir.getPath());
@@ -224,12 +224,14 @@ private void sendVimCommand(String command,
224224
* @param argument Arguments for the remote command.
225225
* @throws Exception If the command could not be sent.
226226
*/
227-
private void sendVimCommand(String command,
227+
private void sendVimCommand(String command,
228228
String[] arguments) throws Exception
229229
{
230-
String[] exec = {"gvim", "--servername", "VimCoder" + id, command};
231-
exec = Util.concat(exec, arguments);
232-
Process child = Runtime.getRuntime().exec(exec, null, directory);
230+
String[] vimCommand = VimCoder.getVimCommand().split("\\s");
231+
String[] flags = {"--servername", "VimCoder" + id, command};
232+
vimCommand = Util.concat(vimCommand, flags);
233+
vimCommand = Util.concat(vimCommand, arguments);
234+
Process child = Runtime.getRuntime().exec(vimCommand, null, directory);
233235

234236
/* FIXME: This is a hack with a magic number. The problem is the Vim
235237
* process doesn't fork to the background on some systems, so we can't

src/com/dogcows/VimCoder.java

Lines changed: 178 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33

44
import javax.swing.*;
55
import java.awt.*;
6+
import java.awt.event.ActionEvent;
7+
import java.awt.event.ActionListener;
8+
import java.beans.PropertyChangeListener;
69
import java.io.*;
710
import java.text.SimpleDateFormat;
811
import java.util.*;
912
import com.topcoder.client.contestant.ProblemComponentModel;
1013
import com.topcoder.shared.language.*;
1114
import com.topcoder.shared.problem.*;
1215
import com.topcoder.shared.problem.Renderer;
16+
import com.topcoder.client.contestApplet.common.Common;
17+
import com.topcoder.client.contestApplet.common.LocalPreferences;
1318

1419
/**
1520
* @author Charles McGarvey
@@ -31,6 +36,17 @@ public class VimCoder
3136
public final static String website = "http://www.dogcows.com/vimcoder";
3237

3338

39+
/**
40+
* The first part of the command used to invoke the Vim server.
41+
*/
42+
private static String vimCommand = "gvim";
43+
44+
/**
45+
* The path to the main VimCoder directory.
46+
*/
47+
private static File rootDir = new File("~/.vimcoder");
48+
49+
3450
/**
3551
* The panel given to the Arena applet when it is requested.
3652
*/
@@ -45,6 +61,46 @@ public class VimCoder
4561
* The current editor object (or null if there is none).
4662
*/
4763
private Editor editor;
64+
65+
/**
66+
* The configuration panel.
67+
*/
68+
private JDialog configDialog;
69+
70+
71+
/**
72+
* The key for the vim command preference.
73+
*/
74+
private final static String VIMCOMMAND = "com.dogcows.VimCoder.config.vimcommand";
75+
76+
/**
77+
* The key for the root directory preference.
78+
*/
79+
private final static String ROOTDIR = "com.dogcows.VimCoder.config.rootdir";
80+
81+
/**
82+
* The preferences object for storing plugin settings.
83+
*/
84+
private static LocalPreferences prefs = LocalPreferences.getInstance();
85+
86+
87+
/**
88+
* Get the command for invoking vim.
89+
* @return The command.
90+
*/
91+
public static String getVimCommand()
92+
{
93+
return vimCommand;
94+
}
95+
96+
/**
97+
* Get the storage directory.
98+
* @return The directory.
99+
*/
100+
public static File getStorageDirectory()
101+
{
102+
return rootDir;
103+
}
48104

49105

50106
/**
@@ -70,7 +126,6 @@ public VimCoder()
70126
*/
71127
public void startUsing()
72128
{
73-
System.out.println("startUsing");
74129
Runnable task = new Runnable()
75130
{
76131
public void run()
@@ -86,14 +141,14 @@ public void run()
86141
{
87142
SwingUtilities.invokeLater(task);
88143
}
144+
loadConfiguration();
89145
}
90146

91147
/**
92148
* Called by the Arena when the plugin is no longer needed.
93149
*/
94150
public void stopUsing()
95151
{
96-
System.out.println("stopUsing");
97152
editor = null;
98153
}
99154

@@ -104,7 +159,6 @@ public void stopUsing()
104159
*/
105160
public JPanel getEditorPanel()
106161
{
107-
System.out.println("getEditorPanel");
108162
return panel;
109163
}
110164

@@ -116,7 +170,6 @@ public JPanel getEditorPanel()
116170
*/
117171
public String getSource() throws Exception
118172
{
119-
System.out.println("getSource");
120173
try
121174
{
122175
String source = editor.getSource();
@@ -137,7 +190,6 @@ public String getSource() throws Exception
137190
*/
138191
public void setSource(String source)
139192
{
140-
System.out.println("setSource: " + source);
141193
try
142194
{
143195
editor.setSource(source);
@@ -161,7 +213,6 @@ public void setProblemComponent(ProblemComponentModel component,
161213
Language language,
162214
Renderer renderer)
163215
{
164-
System.out.println("setProblemComponent");
165216
try
166217
{
167218
editor = new Editor(component, language, renderer);
@@ -172,6 +223,127 @@ public void setProblemComponent(ProblemComponentModel component,
172223
exception.getLocalizedMessage());
173224
}
174225
}
226+
227+
/**
228+
* Called by the Arena when it's time to show our configuration panel.
229+
*/
230+
public void configure()
231+
{
232+
loadConfiguration();
233+
234+
configDialog = new JDialog();
235+
Container pane = configDialog.getContentPane();
236+
237+
pane.setPreferredSize(new Dimension(550, 135));
238+
pane.setLayout(new GridBagLayout());
239+
pane.setForeground(Common.FG_COLOR);
240+
pane.setBackground(Common.WPB_COLOR);
241+
GridBagConstraints c = new GridBagConstraints();
242+
243+
JLabel vimCommandLabel = new JLabel("Vim Command:");
244+
vimCommandLabel.setForeground(Common.FG_COLOR);
245+
vimCommandLabel.setAlignmentX(1.0f);
246+
c.fill = GridBagConstraints.HORIZONTAL;
247+
c.gridx = 0;
248+
c.gridy = 0;
249+
c.insets = new Insets(5, 5, 5, 5);
250+
pane.add(vimCommandLabel, c);
251+
252+
final JTextField vimCommandField = new JTextField(vimCommand, 25);
253+
c.gridx = 1;
254+
c.gridy = 0;
255+
c.gridwidth = 3;
256+
pane.add(vimCommandField, c);
257+
258+
JLabel rootDirLabel = new JLabel("Storage Directory:");
259+
rootDirLabel.setForeground(Common.FG_COLOR);
260+
c.gridx = 0;
261+
c.gridy = 1;
262+
c.gridwidth = 1;
263+
pane.add(rootDirLabel, c);
264+
265+
final JTextField rootDirField = new JTextField(rootDir.getPath(), 25);
266+
c.gridx = 1;
267+
c.gridy = 1;
268+
c.gridwidth = 2;
269+
pane.add(rootDirField, c);
270+
271+
JButton browseButton = new JButton("Browse");
272+
c.fill = GridBagConstraints.NONE;
273+
c.gridx = 3;
274+
c.gridy = 1;
275+
c.gridwidth = 1;
276+
c.anchor = GridBagConstraints.BASELINE_LEADING;
277+
pane.add(browseButton, c);
278+
279+
JButton closeButton = new JButton("Close");
280+
c.fill = GridBagConstraints.HORIZONTAL;
281+
c.gridx = 1;
282+
c.gridy = 2;
283+
c.anchor = GridBagConstraints.PAGE_END;
284+
pane.add(closeButton, c);
285+
286+
JButton saveButton = new JButton("Save");
287+
c.gridx = 2;
288+
c.gridy = 2;
289+
c.gridwidth = 2;
290+
pane.add(saveButton, c);
291+
292+
browseButton.addActionListener(new ActionListener()
293+
{
294+
public void actionPerformed(ActionEvent actionEvent)
295+
{
296+
JFileChooser chooser = new JFileChooser();
297+
chooser.setCurrentDirectory(new File("."));
298+
chooser.setDialogTitle("Choose Storage Directory");
299+
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
300+
chooser.setAcceptAllFileFilterUsed(false);
301+
302+
if (chooser.showOpenDialog(configDialog) == JFileChooser.APPROVE_OPTION)
303+
{
304+
rootDirField.setText(chooser.getSelectedFile().getPath());
305+
}
306+
}
307+
});
308+
309+
closeButton.addActionListener(new ActionListener()
310+
{
311+
public void actionPerformed(ActionEvent actionEvent)
312+
{
313+
configDialog.dispose();
314+
}
315+
});
316+
317+
saveButton.addActionListener(new ActionListener()
318+
{
319+
public void actionPerformed(ActionEvent actionEvent)
320+
{
321+
prefs.setProperty(VIMCOMMAND, vimCommandField.getText());
322+
prefs.setProperty(ROOTDIR, rootDirField.getText());
323+
configDialog.dispose();
324+
}
325+
});
326+
327+
configDialog.setTitle("VimCoder Preferences");
328+
configDialog.pack();
329+
configDialog.setLocationByPlatform(true);
330+
configDialog.setModalityType(Dialog.DEFAULT_MODALITY_TYPE);
331+
configDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
332+
configDialog.setVisible(true);
333+
}
334+
335+
336+
/**
337+
* Load the local preferences related to this plugin.
338+
*/
339+
private void loadConfiguration()
340+
{
341+
String vc = prefs.getProperty(VIMCOMMAND);
342+
if (vc != null) vimCommand = vc;
343+
344+
String dir = prefs.getProperty(ROOTDIR);
345+
if (dir != null) rootDir = new File(dir);
346+
}
175347

176348

177349
/**

0 commit comments

Comments
 (0)