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