5 import java
.awt
.event
.ActionEvent
;
6 import java
.awt
.event
.ActionListener
;
7 import java
.beans
.PropertyChangeListener
;
9 import java
.text
.SimpleDateFormat
;
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
;
20 * @author Charles McGarvey
21 * The TopCoder Arena editor plug-in providing support for Vim.
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.
29 * The name and version of this plugin.
31 public final static String version
= "VimCoder 0.3";
34 * The website of the plugin project.
36 public final static String website
= "http://www.dogcows.com/vimcoder";
40 * The first part of the command used to invoke the Vim server.
42 private static String vimCommand
= "gvim";
45 * The path to the main VimCoder directory.
47 private static File rootDir
;
50 if (System
.getProperty("os.name").toLowerCase().equals("win"))
52 vimCommand
= "C:\\WINDOWS\\gvim.bat";
54 rootDir
= new File(System
.getProperty("user.home") +
55 System
.getProperty("file.separator") + ".vimcoder");
60 * The panel given to the Arena applet when it is requested.
65 * The text widget where log messages are appended.
67 private JTextArea logArea
;
70 * The current editor object (or null if there is none).
72 private Editor editor
;
75 * The configuration panel.
77 private JDialog configDialog
;
81 * The key for the vim command preference.
83 private final static String VIMCOMMAND
= "com.dogcows.VimCoder.config.vimcommand";
86 * The key for the root directory preference.
88 private final static String ROOTDIR
= "com.dogcows.VimCoder.config.rootdir";
91 * The preferences object for storing plugin settings.
93 private static LocalPreferences prefs
= LocalPreferences
.getInstance();
97 * Get the command for invoking vim.
98 * @return The command.
100 public static String
getVimCommand()
106 * Get the storage directory.
107 * @return The directory.
109 public static File
getStorageDirectory()
116 * Instantiate the entry point of the editor plugin.
117 * Sets up the log widget and panel.
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
);
128 panel
= new JPanel(new BorderLayout());
129 panel
.add(new JScrollPane(logArea
), BorderLayout
.CENTER
);
134 * Called by the Arena when the plugin is about to be used.
136 public void startUsing()
138 Runnable task
= new Runnable()
145 if (SwingUtilities
.isEventDispatchThread())
151 SwingUtilities
.invokeLater(task
);
157 * Called by the Arena when the plugin is no longer needed.
159 public void stopUsing()
165 * Called by the Arena to obtain the editor panel which we will use to
167 * @return The editor panel.
169 public JPanel
getEditorPanel()
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.
180 public String
getSource() throws Exception
184 String source
= editor
.getSource();
185 logInfo("Source code uploaded to server.");
188 catch (Exception exception
)
190 logError("Failed to get source code: " +
191 exception
.getLocalizedMessage());
197 * Called by the Arena to pass the source it has.
198 * @param source The source code.
200 public void setSource(String source
)
204 editor
.setSource(source
);
205 logInfo("Source code downloaded from server.");
207 catch (Exception exception
)
209 logError("Failed to save the source given by the server: " +
210 exception
.getLocalizedMessage());
216 * Called by the Arena to pass along information about the current
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
223 public void setProblemComponent(ProblemComponentModel component
,
229 editor
= new Editor(component
, language
, renderer
);
231 catch (Exception exception
)
233 logError("An error occured while loading the problem: " +
234 exception
.getLocalizedMessage());
239 * Called by the Arena when it's time to show our configuration panel.
241 public void configure()
245 configDialog
= new JDialog();
246 Container pane
= configDialog
.getContentPane();
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();
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);
261 pane
.add(rootDirLabel
, c
);
263 final JTextField rootDirField
= new JTextField(rootDir
.getPath(), 25);
266 pane
.add(rootDirField
, c
);
268 JButton browseButton
= new JButton("Browse");
271 c
.anchor
= GridBagConstraints
.BASELINE_LEADING
;
272 pane
.add(browseButton
, c
);
274 JLabel vimCommandLabel
= new JLabel("Vim Command:", SwingConstants
.RIGHT
);
275 vimCommandLabel
.setForeground(Common
.FG_COLOR
);
276 c
.fill
= GridBagConstraints
.HORIZONTAL
;
279 pane
.add(vimCommandLabel
, c
);
281 final JTextField vimCommandField
= new JTextField(vimCommand
, 25);
285 pane
.add(vimCommandField
, c
);
287 JButton closeButton
= new JButton("Cancel");
288 c
.fill
= GridBagConstraints
.NONE
;
292 c
.anchor
= GridBagConstraints
.EAST
;
293 pane
.add(closeButton
, c
);
295 JButton saveButton
= new JButton("Save");
296 c
.fill
= GridBagConstraints
.HORIZONTAL
;
299 c
.anchor
= GridBagConstraints
.EAST
;
300 pane
.add(saveButton
, c
);
301 configDialog
.getRootPane().setDefaultButton(saveButton
);
303 browseButton
.addActionListener(new ActionListener()
305 public void actionPerformed(ActionEvent actionEvent
)
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);
313 if (chooser
.showOpenDialog(configDialog
) == JFileChooser
.APPROVE_OPTION
)
315 rootDirField
.setText(chooser
.getSelectedFile().getPath());
320 closeButton
.addActionListener(new ActionListener()
322 public void actionPerformed(ActionEvent actionEvent
)
324 configDialog
.dispose();
328 saveButton
.addActionListener(new ActionListener()
330 public void actionPerformed(ActionEvent actionEvent
)
332 prefs
.setProperty(VIMCOMMAND
, vimCommandField
.getText());
333 prefs
.setProperty(ROOTDIR
, rootDirField
.getText());
334 configDialog
.dispose();
338 configDialog
.setTitle("VimCoder Preferences");
340 configDialog
.setLocationByPlatform(true);
341 configDialog
.setModalityType(Dialog
.DEFAULT_MODALITY_TYPE
);
342 configDialog
.setDefaultCloseOperation(WindowConstants
.DISPOSE_ON_CLOSE
);
343 configDialog
.setVisible(true);
348 * Load the local preferences related to this plugin.
350 private void loadConfiguration()
352 String vc
= prefs
.getProperty(VIMCOMMAND
);
353 if (vc
!= null) vimCommand
= vc
;
355 String dir
= prefs
.getProperty(ROOTDIR
);
356 if (dir
!= null) rootDir
= new File(dir
);
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.
365 private void log(final String what
)
367 Runnable task
= new Runnable()
371 SimpleDateFormat format
= new SimpleDateFormat("kk:mm:ss");
372 logArea
.append(format
.format(new Date()) + ", " + what
);
375 if (SwingUtilities
.isEventDispatchThread())
381 SwingUtilities
.invokeLater(task
);
386 * Output non-critical messages to the log.
387 * @param what The text of the message.
389 private void logInfo(String what
)
391 log(" INFO: " + what
+ System
.getProperty("line.separator"));
395 * Output critical messages and errors to the log.
396 * @param what The text of the message.
398 private void logError(String what
)
400 log("ERROR: " + what
+ System
.getProperty("line.separator"));