]> Dogcows Code - chaz/vimcoder/blob - src/com/dogcows/VimCoder.java
line-endings now correct for Windows
[chaz/vimcoder] / src / com / dogcows / VimCoder.java
1
2 package com.dogcows;
3
4 import java.awt.*;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.io.*;
8 import java.text.SimpleDateFormat;
9 import java.util.*;
10 import javax.swing.*;
11
12 import com.topcoder.client.contestApplet.common.Common;
13 import com.topcoder.client.contestApplet.common.LocalPreferences;
14 import com.topcoder.client.contestant.ProblemComponentModel;
15 import com.topcoder.shared.language.Language;
16 import com.topcoder.shared.problem.Renderer;
17
18 /**
19 * @author Charles McGarvey
20 * The TopCoder Arena editor plug-in providing support for Vim.
21 *
22 * Distributable under the terms and conditions of the 2-clause BSD license;
23 * see the file COPYING for a complete text of the license.
24 */
25 public class VimCoder
26 {
27 /**
28 * The name and version of this plugin.
29 */
30 public final static String version = "VimCoder 0.3";
31
32 /**
33 * The website of the plugin project.
34 */
35 public final static String website = "http://www.dogcows.com/vimcoder";
36
37
38 /**
39 * The first part of the command used to invoke the Vim server.
40 */
41 private static String vimCommand = "gvim";
42
43 /**
44 * The path to the main VimCoder directory.
45 */
46 private static File rootDir;
47 static
48 {
49 if (System.getProperty("os.name").toLowerCase().equals("win"))
50 {
51 vimCommand = "C:\\WINDOWS\\gvim.bat";
52 }
53 rootDir = new File(System.getProperty("user.home") +
54 System.getProperty("file.separator") + ".vimcoder");
55 }
56
57
58 /**
59 * The panel given to the Arena applet when it is requested.
60 */
61 private JPanel panel;
62
63 /**
64 * The text widget where log messages are appended.
65 */
66 private JTextArea logArea;
67
68 /**
69 * The current editor object (or null if there is none).
70 */
71 private Editor editor;
72
73 /**
74 * The configuration panel.
75 */
76 private JDialog configDialog;
77
78
79 /**
80 * The key for the vim command preference.
81 */
82 private final static String VIMCOMMAND = "com.dogcows.VimCoder.config.vimcommand";
83
84 /**
85 * The key for the root directory preference.
86 */
87 private final static String ROOTDIR = "com.dogcows.VimCoder.config.rootdir";
88
89 /**
90 * The preferences object for storing plugin settings.
91 */
92 private static LocalPreferences prefs = LocalPreferences.getInstance();
93
94
95 /**
96 * Get the command for invoking vim.
97 * @return The command.
98 */
99 public static String getVimCommand()
100 {
101 return vimCommand;
102 }
103
104 /**
105 * Get the storage directory.
106 * @return The directory.
107 */
108 public static File getStorageDirectory()
109 {
110 return rootDir;
111 }
112
113
114 /**
115 * Instantiate the entry point of the editor plugin.
116 * Sets up the log widget and panel.
117 */
118 public VimCoder()
119 {
120 logArea = new JTextArea();
121 logArea.setForeground(Color.GREEN);
122 logArea.setBackground(Color.BLACK);
123 logArea.setEditable(false);
124 Font font = new Font("Courier", Font.PLAIN, 12);
125 if (font != null) logArea.setFont(font);
126
127 panel = new JPanel(new BorderLayout());
128 panel.add(new JScrollPane(logArea), BorderLayout.CENTER);
129 }
130
131
132 /**
133 * Called by the Arena when the plugin is about to be used.
134 */
135 public void startUsing()
136 {
137 Runnable task = new Runnable()
138 {
139 public void run()
140 {
141 logArea.setText("");
142 }
143 };
144 if (SwingUtilities.isEventDispatchThread())
145 {
146 task.run();
147 }
148 else
149 {
150 SwingUtilities.invokeLater(task);
151 }
152 loadConfiguration();
153 }
154
155 /**
156 * Called by the Arena when the plugin is no longer needed.
157 */
158 public void stopUsing()
159 {
160 editor = null;
161 }
162
163 /**
164 * Called by the Arena to obtain the editor panel which we will use to
165 * show log messages.
166 * @return The editor panel.
167 */
168 public JPanel getEditorPanel()
169 {
170 return panel;
171 }
172
173 /**
174 * Called by the Arena to obtain the current source.
175 * This happens when the user is saving, compiling, and/or submitting.
176 * @return The current source code.
177 * @throws Exception If the source file edited by Vim couldn't be read.
178 */
179 public String getSource() throws Exception
180 {
181 try
182 {
183 String source = editor.getSource();
184 logInfo("Source code uploaded to server.");
185 return source;
186 }
187 catch (Exception exception)
188 {
189 logError("Failed to get source code: " +
190 exception.getLocalizedMessage());
191 throw exception;
192 }
193 }
194
195 /**
196 * Called by the Arena to pass the source it has.
197 * @param source The source code.
198 */
199 public void setSource(String source)
200 {
201 try
202 {
203 editor.setSource(source);
204 logInfo("Source code downloaded from server.");
205 }
206 catch (Exception exception)
207 {
208 logError("Failed to save the source given by the server: " +
209 exception.getLocalizedMessage());
210 return;
211 }
212 }
213
214 /**
215 * Called by the Arena to pass along information about the current
216 * problem.
217 * @param component A container for the particulars of the problem.
218 * @param language The currently selected language.
219 * @param renderer A helper object to help format the problem
220 * statement.
221 */
222 public void setProblemComponent(ProblemComponentModel component,
223 Language language,
224 Renderer renderer)
225 {
226 try
227 {
228 editor = new Editor(component, language, renderer);
229 }
230 catch (Exception exception)
231 {
232 logError("An error occured while loading the problem: " +
233 exception.getLocalizedMessage());
234 }
235 }
236
237 /**
238 * Called by the Arena when it's time to show our configuration panel.
239 */
240 public void configure()
241 {
242 loadConfiguration();
243
244 configDialog = new JDialog();
245 Container pane = configDialog.getContentPane();
246
247 pane.setPreferredSize(new Dimension(550, 135));
248 pane.setLayout(new GridBagLayout());
249 pane.setForeground(Common.FG_COLOR);
250 pane.setBackground(Common.WPB_COLOR);
251 GridBagConstraints c = new GridBagConstraints();
252
253 JLabel rootDirLabel = new JLabel("Storage Directory:", SwingConstants.RIGHT);
254 rootDirLabel.setForeground(Common.FG_COLOR);
255 c.fill = GridBagConstraints.HORIZONTAL;
256 c.insets = new Insets(5, 5, 5, 5);
257 c.gridx = 0;
258 c.gridy = 0;
259 c.gridwidth = 1;
260 pane.add(rootDirLabel, c);
261
262 final JTextField rootDirField = new JTextField(rootDir.getPath(), 25);
263 c.gridx = 1;
264 c.gridy = 0;
265 pane.add(rootDirField, c);
266
267 JButton browseButton = new JButton("Browse");
268 c.gridx = 2;
269 c.gridy = 0;
270 c.anchor = GridBagConstraints.BASELINE_LEADING;
271 pane.add(browseButton, c);
272
273 JLabel vimCommandLabel = new JLabel("Vim Command:", SwingConstants.RIGHT);
274 vimCommandLabel.setForeground(Common.FG_COLOR);
275 c.fill = GridBagConstraints.HORIZONTAL;
276 c.gridx = 0;
277 c.gridy = 1;
278 pane.add(vimCommandLabel, c);
279
280 final JTextField vimCommandField = new JTextField(vimCommand, 25);
281 c.gridx = 1;
282 c.gridy = 1;
283 c.gridwidth = 2;
284 pane.add(vimCommandField, c);
285
286 JButton closeButton = new JButton("Cancel");
287 c.fill = GridBagConstraints.NONE;
288 c.gridx = 1;
289 c.gridy = 2;
290 c.gridwidth = 1;
291 c.anchor = GridBagConstraints.EAST;
292 pane.add(closeButton, c);
293
294 JButton saveButton = new JButton("Save");
295 c.fill = GridBagConstraints.HORIZONTAL;
296 c.gridx = 2;
297 c.gridy = 2;
298 c.anchor = GridBagConstraints.EAST;
299 pane.add(saveButton, c);
300 configDialog.getRootPane().setDefaultButton(saveButton);
301
302 browseButton.addActionListener(new ActionListener()
303 {
304 public void actionPerformed(ActionEvent actionEvent)
305 {
306 JFileChooser chooser = new JFileChooser();
307 chooser.setCurrentDirectory(new File("."));
308 chooser.setDialogTitle("Choose Storage Directory");
309 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
310 chooser.setAcceptAllFileFilterUsed(false);
311
312 if (chooser.showOpenDialog(configDialog) == JFileChooser.APPROVE_OPTION)
313 {
314 rootDirField.setText(chooser.getSelectedFile().getPath());
315 }
316 }
317 });
318
319 closeButton.addActionListener(new ActionListener()
320 {
321 public void actionPerformed(ActionEvent actionEvent)
322 {
323 configDialog.dispose();
324 }
325 });
326
327 saveButton.addActionListener(new ActionListener()
328 {
329 public void actionPerformed(ActionEvent actionEvent)
330 {
331 prefs.setProperty(VIMCOMMAND, vimCommandField.getText());
332 prefs.setProperty(ROOTDIR, rootDirField.getText());
333 configDialog.dispose();
334 }
335 });
336
337 configDialog.setTitle("VimCoder Preferences");
338 configDialog.pack();
339 configDialog.setLocationByPlatform(true);
340 configDialog.setModalityType(Dialog.DEFAULT_MODALITY_TYPE);
341 configDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
342 configDialog.setVisible(true);
343 }
344
345
346 /**
347 * Load the local preferences related to this plugin.
348 */
349 private void loadConfiguration()
350 {
351 String vc = prefs.getProperty(VIMCOMMAND);
352 if (vc != null) vimCommand = vc;
353
354 String dir = prefs.getProperty(ROOTDIR);
355 if (dir != null) rootDir = new File(dir);
356 }
357
358
359 /**
360 * A generic logging function, appends text to the text area. A timestamp
361 * is also prepended to the next text.
362 * @param what The text to append.
363 */
364 private void log(final String what)
365 {
366 Runnable task = new Runnable()
367 {
368 public void run()
369 {
370 SimpleDateFormat format = new SimpleDateFormat("kk:mm:ss");
371 logArea.append(format.format(new Date()) + ", " + what);
372 }
373 };
374 if (SwingUtilities.isEventDispatchThread())
375 {
376 task.run();
377 }
378 else
379 {
380 SwingUtilities.invokeLater(task);
381 }
382 }
383
384 /**
385 * Output non-critical messages to the log.
386 * @param what The text of the message.
387 */
388 private void logInfo(String what)
389 {
390 log(" INFO: " + what + System.getProperty("line.separator"));
391 }
392
393 /**
394 * Output critical messages and errors to the log.
395 * @param what The text of the message.
396 */
397 private void logError(String what)
398 {
399 log("ERROR: " + what + System.getProperty("line.separator"));
400 }
401 }
402
This page took 0.051444 seconds and 4 git commands to generate.