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.6";
34 * The first part of the command used to invoke the Vim server.
36 private static String vimCommand
= "gvim";
39 * The path to the main VimCoder directory.
41 private static File rootDir
;
44 if (System
.getProperty("os.name").toLowerCase().equals("win"))
46 vimCommand
= "C:\\WINDOWS\\gvim.bat";
48 rootDir
= new File(System
.getProperty("user.home") + System
.getProperty("file.separator") + ".vimcoder");
52 * Whether or not to use the contest name and point value as problem
55 private static boolean contestDirNames
= false;
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 key for the problem directory name preference.
92 private final static String CONTESTDIRNAMES
= "com.dogcows.VimCoder.config.contestdirnames";
95 * The preferences object for storing plugin settings.
97 private static LocalPreferences prefs
= LocalPreferences
.getInstance();
101 * Get the command for invoking vim.
102 * @return The command.
104 public static String
getVimCommand()
110 * Get the storage directory.
111 * @return The directory.
113 public static File
getStorageDirectory()
119 * Get whether or not to save problems in a human-readable directory
121 * @return The directory name setting.
123 public static boolean isContestDirNames()
125 return contestDirNames
;
130 * Instantiate the entry point of the editor plugin.
131 * Sets up the log widget and panel.
135 logArea
= new JTextArea();
136 logArea
.setForeground(Color
.GREEN
);
137 logArea
.setBackground(Color
.BLACK
);
138 logArea
.setEditable(false);
139 Font font
= new Font("Courier", Font
.PLAIN
, 12);
140 if (font
!= null) logArea
.setFont(font
);
142 panel
= new JPanel(new BorderLayout());
143 panel
.add(new JScrollPane(logArea
), BorderLayout
.CENTER
);
148 * Called by the Arena when the plugin is about to be used.
150 public void startUsing()
152 Runnable task
= new Runnable()
159 if (SwingUtilities
.isEventDispatchThread())
165 SwingUtilities
.invokeLater(task
);
171 * Called by the Arena when the plugin is no longer needed.
173 public void stopUsing()
179 * Called by the Arena to obtain the editor panel which we will use to
181 * @return The editor panel.
183 public JPanel
getEditorPanel()
189 * Called by the Arena to obtain the current source.
190 * This happens when the user is saving, compiling, and/or submitting.
191 * @return The current source code.
192 * @throws Exception If the source file edited by Vim couldn't be read.
194 public String
getSource() throws Exception
198 String source
= editor
.getSource();
199 logInfo("Source code uploaded to server.");
202 catch (Exception exception
)
204 logError("Failed to get source code: " + exception
.getLocalizedMessage());
210 * Called by the Arena to pass the source it has.
211 * @param source The source code.
213 public void setSource(String source
)
217 editor
.setSource(source
);
218 logInfo("Source code downloaded from server.");
220 catch (Exception exception
)
222 logError("Failed to save the source given by the server: " + exception
.getLocalizedMessage());
228 * Called by the Arena to pass along information about the current
230 * @param component A container for the particulars of the problem.
231 * @param language The currently selected language.
232 * @param renderer A helper object to help format the problem
235 public void setProblemComponent(ProblemComponentModel component
,
236 Language language
, Renderer renderer
)
240 editor
= new Editor(component
, language
, renderer
);
242 catch (Exception exception
)
244 logError("An error occurred while loading the problem: " + exception
.getLocalizedMessage());
249 * Called by the Arena when it's time to show our configuration panel.
251 public void configure()
253 final int border
= 10;
258 configDialog
= new JDialog();
259 Container container
= configDialog
.getContentPane();
260 container
.setForeground(Common
.FG_COLOR
);
261 container
.setBackground(Common
.WPB_COLOR
);
263 JPanel pane
= new JPanel();
266 BoxLayout boxLayout
= new BoxLayout(pane
, BoxLayout
.Y_AXIS
);
267 pane
.setLayout(boxLayout
);
268 pane
.setBorder(BorderFactory
.createEmptyBorder(border
, border
, border
, border
));
270 JPanel fieldPanel
= new JPanel(new GridBagLayout());
271 pane
.add(fieldPanel
);
272 pane
.add(Box
.createRigidArea(new Dimension(0, border
)));
274 GridBagConstraints c
= new GridBagConstraints();
275 c
.fill
= GridBagConstraints
.HORIZONTAL
;
276 c
.insets
= new Insets(inset
, inset
, inset
, inset
);
278 JLabel rootDirLabel
= new JLabel("Storage Directory:");
279 rootDirLabel
.setForeground(Common
.FG_COLOR
);
283 fieldPanel
.add(rootDirLabel
, c
);
285 final JTextField rootDirField
= new JTextField(rootDir
.getPath());
286 rootDirField
.setPreferredSize(new Dimension(0, 24));
290 fieldPanel
.add(rootDirField
, c
);
292 JButton browseButton
= new JButton("Browse");
296 c
.anchor
= GridBagConstraints
.BASELINE_LEADING
;
297 fieldPanel
.add(browseButton
, c
);
299 final JCheckBox contestDirNamesButton
= new JCheckBox(
300 "Store problems according to contest name and point value.",
303 contestDirNamesButton
.setForeground(Common
.FG_COLOR
);
304 contestDirNamesButton
.setBackground(Common
.WPB_COLOR
);
305 contestDirNamesButton
.setFont(rootDirLabel
.getFont());
309 fieldPanel
.add(contestDirNamesButton
, c
);
311 JLabel vimCommandLabel
= new JLabel("Vim Command:");
312 vimCommandLabel
.setForeground(Common
.FG_COLOR
);
316 fieldPanel
.add(vimCommandLabel
, c
);
318 final JTextField vimCommandField
= new JTextField(vimCommand
);
319 vimCommandField
.setPreferredSize(new Dimension(0, 24));
324 fieldPanel
.add(vimCommandField
, c
);
326 JPanel buttonPanel
= new JPanel(new FlowLayout(FlowLayout
.TRAILING
, inset
, inset
));
327 buttonPanel
.setPreferredSize(new Dimension(400, 24 + 2 * inset
));
328 pane
.add(buttonPanel
);
330 JButton saveButton
= new JButton("Save");
331 buttonPanel
.add(saveButton
);
332 buttonPanel
.add(Box
.createRigidArea(new Dimension(1, 0)));
334 JButton closeButton
= new JButton("Close");
335 buttonPanel
.add(closeButton
);
337 browseButton
.addActionListener(new ActionListener()
339 public void actionPerformed(ActionEvent actionEvent
)
341 JFileChooser chooser
= new JFileChooser();
342 chooser
.setCurrentDirectory(new File("."));
343 chooser
.setDialogTitle("Choose Storage Directory");
344 chooser
.setFileSelectionMode(JFileChooser
.DIRECTORIES_ONLY
);
345 chooser
.setAcceptAllFileFilterUsed(false);
347 if (chooser
.showOpenDialog(configDialog
) == JFileChooser
.APPROVE_OPTION
)
349 rootDirField
.setText(chooser
.getSelectedFile().getPath());
354 closeButton
.addActionListener(new ActionListener()
356 public void actionPerformed(ActionEvent actionEvent
)
358 configDialog
.dispose();
362 saveButton
.addActionListener(new ActionListener()
364 public void actionPerformed(ActionEvent actionEvent
)
366 prefs
.setProperty(VIMCOMMAND
, vimCommandField
.getText());
367 prefs
.setProperty(ROOTDIR
, rootDirField
.getText());
368 prefs
.setProperty(CONTESTDIRNAMES
, String
.valueOf(contestDirNamesButton
.isSelected()));
369 JOptionPane
.showMessageDialog(null, "Preferences were saved successfully.");
373 configDialog
.setTitle("VimCoder Preferences");
375 configDialog
.setLocationRelativeTo(null); // Center dialog in screen.
376 configDialog
.setModalityType(Dialog
.DEFAULT_MODALITY_TYPE
);
377 configDialog
.setDefaultCloseOperation(WindowConstants
.DISPOSE_ON_CLOSE
);
378 configDialog
.setVisible(true);
383 * Load the local preferences related to this plugin.
385 private void loadConfiguration()
387 String vc
= prefs
.getProperty(VIMCOMMAND
);
388 if (vc
!= null) vimCommand
= vc
;
390 String dir
= prefs
.getProperty(ROOTDIR
);
391 if (dir
!= null) rootDir
= new File(dir
);
393 String cn
= prefs
.getProperty(CONTESTDIRNAMES
);
394 if (cn
!= null) contestDirNames
= Boolean
.parseBoolean(cn
);
399 * A generic logging function, appends text to the text area. A timestamp
400 * is also prepended to the next text.
401 * @param what The text to append.
403 private void log(final String what
)
405 Runnable task
= new Runnable()
409 SimpleDateFormat format
= new SimpleDateFormat("kk:mm:ss");
410 logArea
.append(format
.format(new Date()) + ", " + what
);
413 if (SwingUtilities
.isEventDispatchThread())
419 SwingUtilities
.invokeLater(task
);
424 * Output non-critical messages to the log.
425 * @param what The text of the message.
427 private void logInfo(String what
)
429 log(" INFO: " + what
+ System
.getProperty("line.separator"));
433 * Output critical messages and errors to the log.
434 * @param what The text of the message.
436 private void logError(String what
)
438 log("ERROR: " + what
+ System
.getProperty("line.separator"));
442 // vim:et:ts=8:sts=4:sw=4