]> Dogcows Code - chaz/vimcoder/blob - src/com/dogcows/VimCoder.java
just some whitespace cleanup
[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.1";
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: " + exception.getLocalizedMessage());
190 throw exception;
191 }
192 }
193
194 /**
195 * Called by the Arena to pass the source it has.
196 * @param source The source code.
197 */
198 public void setSource(String source)
199 {
200 try
201 {
202 editor.setSource(source);
203 logInfo("Source code downloaded from server.");
204 }
205 catch (Exception exception)
206 {
207 logError("Failed to save the source given by the server: " +
208 exception.getLocalizedMessage());
209 return;
210 }
211 }
212
213 /**
214 * Called by the Arena to pass along information about the current
215 * problem.
216 * @param component A container for the particulars of the problem.
217 * @param language The currently selected language.
218 * @param renderer A helper object to help format the problem
219 * statement.
220 */
221 public void setProblemComponent(ProblemComponentModel component,
222 Language language, Renderer renderer)
223 {
224 try
225 {
226 editor = new Editor(component, language, renderer);
227 }
228 catch (Exception exception)
229 {
230 logError("An error occured while loading the problem: " +
231 exception.getLocalizedMessage());
232 }
233 }
234
235 /**
236 * Called by the Arena when it's time to show our configuration panel.
237 */
238 public void configure()
239 {
240 loadConfiguration();
241
242 configDialog = new JDialog();
243 Container pane = configDialog.getContentPane();
244
245 pane.setPreferredSize(new Dimension(550, 135));
246 pane.setLayout(new GridBagLayout());
247 pane.setForeground(Common.FG_COLOR);
248 pane.setBackground(Common.WPB_COLOR);
249 GridBagConstraints c = new GridBagConstraints();
250
251 JLabel rootDirLabel = new JLabel("Storage Directory:", SwingConstants.RIGHT);
252 rootDirLabel.setForeground(Common.FG_COLOR);
253 c.fill = GridBagConstraints.HORIZONTAL;
254 c.insets = new Insets(5, 5, 5, 5);
255 c.gridx = 0;
256 c.gridy = 0;
257 c.gridwidth = 1;
258 pane.add(rootDirLabel, c);
259
260 final JTextField rootDirField = new JTextField(rootDir.getPath(), 25);
261 c.gridx = 1;
262 c.gridy = 0;
263 pane.add(rootDirField, c);
264
265 JButton browseButton = new JButton("Browse");
266 c.gridx = 2;
267 c.gridy = 0;
268 c.anchor = GridBagConstraints.BASELINE_LEADING;
269 pane.add(browseButton, c);
270
271 JLabel vimCommandLabel = new JLabel("Vim Command:", SwingConstants.RIGHT);
272 vimCommandLabel.setForeground(Common.FG_COLOR);
273 c.fill = GridBagConstraints.HORIZONTAL;
274 c.gridx = 0;
275 c.gridy = 1;
276 pane.add(vimCommandLabel, c);
277
278 final JTextField vimCommandField = new JTextField(vimCommand, 25);
279 c.gridx = 1;
280 c.gridy = 1;
281 c.gridwidth = 2;
282 pane.add(vimCommandField, c);
283
284 JButton closeButton = new JButton("Cancel");
285 c.fill = GridBagConstraints.NONE;
286 c.gridx = 1;
287 c.gridy = 2;
288 c.gridwidth = 1;
289 c.anchor = GridBagConstraints.EAST;
290 pane.add(closeButton, c);
291
292 JButton saveButton = new JButton("Save");
293 c.fill = GridBagConstraints.HORIZONTAL;
294 c.gridx = 2;
295 c.gridy = 2;
296 c.anchor = GridBagConstraints.EAST;
297 pane.add(saveButton, c);
298 configDialog.getRootPane().setDefaultButton(saveButton);
299
300 browseButton.addActionListener(new ActionListener()
301 {
302 public void actionPerformed(ActionEvent actionEvent)
303 {
304 JFileChooser chooser = new JFileChooser();
305 chooser.setCurrentDirectory(new File("."));
306 chooser.setDialogTitle("Choose Storage Directory");
307 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
308 chooser.setAcceptAllFileFilterUsed(false);
309
310 if (chooser.showOpenDialog(configDialog) == JFileChooser.APPROVE_OPTION)
311 {
312 rootDirField.setText(chooser.getSelectedFile().getPath());
313 }
314 }
315 });
316
317 closeButton.addActionListener(new ActionListener()
318 {
319 public void actionPerformed(ActionEvent actionEvent)
320 {
321 configDialog.dispose();
322 }
323 });
324
325 saveButton.addActionListener(new ActionListener()
326 {
327 public void actionPerformed(ActionEvent actionEvent)
328 {
329 prefs.setProperty(VIMCOMMAND, vimCommandField.getText());
330 prefs.setProperty(ROOTDIR, rootDirField.getText());
331 configDialog.dispose();
332 }
333 });
334
335 configDialog.setTitle("VimCoder Preferences");
336 configDialog.pack();
337 configDialog.setLocationByPlatform(true);
338 configDialog.setModalityType(Dialog.DEFAULT_MODALITY_TYPE);
339 configDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
340 configDialog.setVisible(true);
341 }
342
343
344 /**
345 * Load the local preferences related to this plugin.
346 */
347 private void loadConfiguration()
348 {
349 String vc = prefs.getProperty(VIMCOMMAND);
350 if (vc != null) vimCommand = vc;
351
352 String dir = prefs.getProperty(ROOTDIR);
353 if (dir != null) rootDir = new File(dir);
354 }
355
356
357 /**
358 * A generic logging function, appends text to the text area. A timestamp
359 * is also prepended to the next text.
360 * @param what The text to append.
361 */
362 private void log(final String what)
363 {
364 Runnable task = new Runnable()
365 {
366 public void run()
367 {
368 SimpleDateFormat format = new SimpleDateFormat("kk:mm:ss");
369 logArea.append(format.format(new Date()) + ", " + what);
370 }
371 };
372 if (SwingUtilities.isEventDispatchThread())
373 {
374 task.run();
375 }
376 else
377 {
378 SwingUtilities.invokeLater(task);
379 }
380 }
381
382 /**
383 * Output non-critical messages to the log.
384 * @param what The text of the message.
385 */
386 private void logInfo(String what)
387 {
388 log(" INFO: " + what + System.getProperty("line.separator"));
389 }
390
391 /**
392 * Output critical messages and errors to the log.
393 * @param what The text of the message.
394 */
395 private void logError(String what)
396 {
397 log("ERROR: " + what + System.getProperty("line.separator"));
398 }
399 }
400
This page took 0.050344 seconds and 4 git commands to generate.