5 import java
.awt
.event
.ActionEvent
;
6 import java
.awt
.event
.ActionListener
;
8 import java
.text
.SimpleDateFormat
;
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
;
19 * @author Charles McGarvey
20 * The TopCoder Arena editor plug-in providing support for Vim.
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.
28 * The name and version of this plugin.
30 public final static String version
= "VimCoder 0.3.1";
33 * The website of the plugin project.
35 public final static String website
= "http://www.dogcows.com/vimcoder";
39 * The first part of the command used to invoke the Vim server.
41 private static String vimCommand
= "gvim";
44 * The path to the main VimCoder directory.
46 private static File rootDir
;
49 if (System
.getProperty("os.name").toLowerCase().equals("win"))
51 vimCommand
= "C:\\WINDOWS\\gvim.bat";
53 rootDir
= new File(System
.getProperty("user.home") +
54 System
.getProperty("file.separator") + ".vimcoder");
59 * The panel given to the Arena applet when it is requested.
64 * The text widget where log messages are appended.
66 private JTextArea logArea
;
69 * The current editor object (or null if there is none).
71 private Editor editor
;
74 * The configuration panel.
76 private JDialog configDialog
;
80 * The key for the vim command preference.
82 private final static String VIMCOMMAND
= "com.dogcows.VimCoder.config.vimcommand";
85 * The key for the root directory preference.
87 private final static String ROOTDIR
= "com.dogcows.VimCoder.config.rootdir";
90 * The preferences object for storing plugin settings.
92 private static LocalPreferences prefs
= LocalPreferences
.getInstance();
96 * Get the command for invoking vim.
97 * @return The command.
99 public static String
getVimCommand()
105 * Get the storage directory.
106 * @return The directory.
108 public static File
getStorageDirectory()
115 * Instantiate the entry point of the editor plugin.
116 * Sets up the log widget and panel.
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
);
127 panel
= new JPanel(new BorderLayout());
128 panel
.add(new JScrollPane(logArea
), BorderLayout
.CENTER
);
133 * Called by the Arena when the plugin is about to be used.
135 public void startUsing()
137 Runnable task
= new Runnable()
144 if (SwingUtilities
.isEventDispatchThread())
150 SwingUtilities
.invokeLater(task
);
156 * Called by the Arena when the plugin is no longer needed.
158 public void stopUsing()
164 * Called by the Arena to obtain the editor panel which we will use to
166 * @return The editor panel.
168 public JPanel
getEditorPanel()
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.
179 public String
getSource() throws Exception
183 String source
= editor
.getSource();
184 logInfo("Source code uploaded to server.");
187 catch (Exception exception
)
189 logError("Failed to get source code: " +
190 exception
.getLocalizedMessage());
196 * Called by the Arena to pass the source it has.
197 * @param source The source code.
199 public void setSource(String source
)
203 editor
.setSource(source
);
204 logInfo("Source code downloaded from server.");
206 catch (Exception exception
)
208 logError("Failed to save the source given by the server: " +
209 exception
.getLocalizedMessage());
215 * Called by the Arena to pass along information about the current
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
222 public void setProblemComponent(ProblemComponentModel component
,
228 editor
= new Editor(component
, language
, renderer
);
230 catch (Exception exception
)
232 logError("An error occured while loading the problem: " +
233 exception
.getLocalizedMessage());
238 * Called by the Arena when it's time to show our configuration panel.
240 public void configure()
244 configDialog
= new JDialog();
245 Container pane
= configDialog
.getContentPane();
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();
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);
260 pane
.add(rootDirLabel
, c
);
262 final JTextField rootDirField
= new JTextField(rootDir
.getPath(), 25);
265 pane
.add(rootDirField
, c
);
267 JButton browseButton
= new JButton("Browse");
270 c
.anchor
= GridBagConstraints
.BASELINE_LEADING
;
271 pane
.add(browseButton
, c
);
273 JLabel vimCommandLabel
= new JLabel("Vim Command:", SwingConstants
.RIGHT
);
274 vimCommandLabel
.setForeground(Common
.FG_COLOR
);
275 c
.fill
= GridBagConstraints
.HORIZONTAL
;
278 pane
.add(vimCommandLabel
, c
);
280 final JTextField vimCommandField
= new JTextField(vimCommand
, 25);
284 pane
.add(vimCommandField
, c
);
286 JButton closeButton
= new JButton("Cancel");
287 c
.fill
= GridBagConstraints
.NONE
;
291 c
.anchor
= GridBagConstraints
.EAST
;
292 pane
.add(closeButton
, c
);
294 JButton saveButton
= new JButton("Save");
295 c
.fill
= GridBagConstraints
.HORIZONTAL
;
298 c
.anchor
= GridBagConstraints
.EAST
;
299 pane
.add(saveButton
, c
);
300 configDialog
.getRootPane().setDefaultButton(saveButton
);
302 browseButton
.addActionListener(new ActionListener()
304 public void actionPerformed(ActionEvent actionEvent
)
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);
312 if (chooser
.showOpenDialog(configDialog
) == JFileChooser
.APPROVE_OPTION
)
314 rootDirField
.setText(chooser
.getSelectedFile().getPath());
319 closeButton
.addActionListener(new ActionListener()
321 public void actionPerformed(ActionEvent actionEvent
)
323 configDialog
.dispose();
327 saveButton
.addActionListener(new ActionListener()
329 public void actionPerformed(ActionEvent actionEvent
)
331 prefs
.setProperty(VIMCOMMAND
, vimCommandField
.getText());
332 prefs
.setProperty(ROOTDIR
, rootDirField
.getText());
333 configDialog
.dispose();
337 configDialog
.setTitle("VimCoder Preferences");
339 configDialog
.setLocationByPlatform(true);
340 configDialog
.setModalityType(Dialog
.DEFAULT_MODALITY_TYPE
);
341 configDialog
.setDefaultCloseOperation(WindowConstants
.DISPOSE_ON_CLOSE
);
342 configDialog
.setVisible(true);
347 * Load the local preferences related to this plugin.
349 private void loadConfiguration()
351 String vc
= prefs
.getProperty(VIMCOMMAND
);
352 if (vc
!= null) vimCommand
= vc
;
354 String dir
= prefs
.getProperty(ROOTDIR
);
355 if (dir
!= null) rootDir
= new File(dir
);
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.
364 private void log(final String what
)
366 Runnable task
= new Runnable()
370 SimpleDateFormat format
= new SimpleDateFormat("kk:mm:ss");
371 logArea
.append(format
.format(new Date()) + ", " + what
);
374 if (SwingUtilities
.isEventDispatchThread())
380 SwingUtilities
.invokeLater(task
);
385 * Output non-critical messages to the log.
386 * @param what The text of the message.
388 private void logInfo(String what
)
390 log(" INFO: " + what
+ System
.getProperty("line.separator"));
394 * Output critical messages and errors to the log.
395 * @param what The text of the message.
397 private void logError(String what
)
399 log("ERROR: " + what
+ System
.getProperty("line.separator"));