5 import java
.awt
.event
.*;
7 import java
.text
.SimpleDateFormat
;
10 import javax
.swing
.border
.*;
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.4";
33 * The website of the plugin project.
35 public final static String website
= "http://github.com/chazmcgarvey/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: " + exception
.getLocalizedMessage());
195 * Called by the Arena to pass the source it has.
196 * @param source The source code.
198 public void setSource(String source
)
202 editor
.setSource(source
);
203 logInfo("Source code downloaded from server.");
205 catch (Exception exception
)
207 logError("Failed to save the source given by the server: " +
208 exception
.getLocalizedMessage());
214 * Called by the Arena to pass along information about the current
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
221 public void setProblemComponent(ProblemComponentModel component
,
222 Language language
, Renderer renderer
)
226 editor
= new Editor(component
, language
, renderer
);
228 catch (Exception exception
)
230 logError("An error occured while loading the problem: " +
231 exception
.getLocalizedMessage());
236 * Called by the Arena when it's time to show our configuration panel.
238 public void configure()
240 final int border
= 10;
245 configDialog
= new JDialog();
246 Container container
= configDialog
.getContentPane();
247 container
.setForeground(Common
.FG_COLOR
);
248 container
.setBackground(Common
.WPB_COLOR
);
250 JPanel pane
= new JPanel();
253 BoxLayout boxLayout
= new BoxLayout(pane
, BoxLayout
.Y_AXIS
);
254 pane
.setLayout(boxLayout
);
255 pane
.setBorder(BorderFactory
.createEmptyBorder(border
, border
, border
, border
));
257 JPanel fieldPanel
= new JPanel(new GridBagLayout());
258 pane
.add(fieldPanel
);
259 pane
.add(Box
.createRigidArea(new Dimension(0, border
)));
261 GridBagConstraints c
= new GridBagConstraints();
262 c
.fill
= GridBagConstraints
.HORIZONTAL
;
263 c
.insets
= new Insets(inset
, inset
, inset
, inset
);
265 JLabel rootDirLabel
= new JLabel("Storage Directory:");
266 rootDirLabel
.setForeground(Common
.FG_COLOR
);
270 fieldPanel
.add(rootDirLabel
, c
);
272 final JTextField rootDirField
= new JTextField(rootDir
.getPath());
273 rootDirField
.setPreferredSize(new Dimension(0, 24));
277 fieldPanel
.add(rootDirField
, c
);
279 JButton browseButton
= new JButton("Browse");
283 c
.anchor
= GridBagConstraints
.BASELINE_LEADING
;
284 fieldPanel
.add(browseButton
, c
);
286 JLabel vimCommandLabel
= new JLabel("Vim Command:");
287 vimCommandLabel
.setForeground(Common
.FG_COLOR
);
290 fieldPanel
.add(vimCommandLabel
, c
);
292 final JTextField vimCommandField
= new JTextField(vimCommand
);
293 vimCommandField
.setPreferredSize(new Dimension(0, 24));
298 fieldPanel
.add(vimCommandField
, c
);
300 JPanel buttonPanel
= new JPanel(new FlowLayout(FlowLayout
.TRAILING
, inset
, inset
));
301 buttonPanel
.setPreferredSize(new Dimension(400, 24 + 2 * inset
));
302 pane
.add(buttonPanel
);
304 JButton saveButton
= new JButton("Save");
305 buttonPanel
.add(saveButton
);
306 buttonPanel
.add(Box
.createRigidArea(new Dimension(1, 0)));
308 JButton closeButton
= new JButton("Close");
309 buttonPanel
.add(closeButton
);
311 browseButton
.addActionListener(new ActionListener()
313 public void actionPerformed(ActionEvent actionEvent
)
315 JFileChooser chooser
= new JFileChooser();
316 chooser
.setCurrentDirectory(new File("."));
317 chooser
.setDialogTitle("Choose Storage Directory");
318 chooser
.setFileSelectionMode(JFileChooser
.DIRECTORIES_ONLY
);
319 chooser
.setAcceptAllFileFilterUsed(false);
321 if (chooser
.showOpenDialog(configDialog
) == JFileChooser
.APPROVE_OPTION
)
323 rootDirField
.setText(chooser
.getSelectedFile().getPath());
328 closeButton
.addActionListener(new ActionListener()
330 public void actionPerformed(ActionEvent actionEvent
)
332 configDialog
.dispose();
336 saveButton
.addActionListener(new ActionListener()
338 public void actionPerformed(ActionEvent actionEvent
)
340 prefs
.setProperty(VIMCOMMAND
, vimCommandField
.getText());
341 prefs
.setProperty(ROOTDIR
, rootDirField
.getText());
342 JOptionPane
.showMessageDialog(null, "Preferences were saved successfully.");
346 configDialog
.setTitle("VimCoder Preferences");
348 configDialog
.setLocationRelativeTo(null); // Center dialog in screen.
349 configDialog
.setModalityType(Dialog
.DEFAULT_MODALITY_TYPE
);
350 configDialog
.setDefaultCloseOperation(WindowConstants
.DISPOSE_ON_CLOSE
);
351 configDialog
.setVisible(true);
356 * Load the local preferences related to this plugin.
358 private void loadConfiguration()
360 String vc
= prefs
.getProperty(VIMCOMMAND
);
361 if (vc
!= null) vimCommand
= vc
;
363 String dir
= prefs
.getProperty(ROOTDIR
);
364 if (dir
!= null) rootDir
= new File(dir
);
369 * A generic logging function, appends text to the text area. A timestamp
370 * is also prepended to the next text.
371 * @param what The text to append.
373 private void log(final String what
)
375 Runnable task
= new Runnable()
379 SimpleDateFormat format
= new SimpleDateFormat("kk:mm:ss");
380 logArea
.append(format
.format(new Date()) + ", " + what
);
383 if (SwingUtilities
.isEventDispatchThread())
389 SwingUtilities
.invokeLater(task
);
394 * Output non-critical messages to the log.
395 * @param what The text of the message.
397 private void logInfo(String what
)
399 log(" INFO: " + what
+ System
.getProperty("line.separator"));
403 * Output critical messages and errors to the log.
404 * @param what The text of the message.
406 private void logError(String what
)
408 log("ERROR: " + what
+ System
.getProperty("line.separator"));