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") +
49 System
.getProperty("file.separator") + ".vimcoder");
53 * Whether or not to use the contest name and point value as problem
56 private static boolean contestDirNames
= false;
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 key for the problem directory name preference.
93 private final static String CONTESTDIRNAMES
= "com.dogcows.VimCoder.config.contestdirnames";
96 * The preferences object for storing plugin settings.
98 private static LocalPreferences prefs
= LocalPreferences
.getInstance();
102 * Get the command for invoking vim.
103 * @return The command.
105 public static String
getVimCommand()
111 * Get the storage directory.
112 * @return The directory.
114 public static File
getStorageDirectory()
120 * Get whether or not to save problems in a human-readable directory
122 * @return The directory name setting.
124 public static boolean isContestDirNames()
126 return contestDirNames
;
131 * Instantiate the entry point of the editor plugin.
132 * Sets up the log widget and panel.
136 logArea
= new JTextArea();
137 logArea
.setForeground(Color
.GREEN
);
138 logArea
.setBackground(Color
.BLACK
);
139 logArea
.setEditable(false);
140 Font font
= new Font("Courier", Font
.PLAIN
, 12);
141 if (font
!= null) logArea
.setFont(font
);
143 panel
= new JPanel(new BorderLayout());
144 panel
.add(new JScrollPane(logArea
), BorderLayout
.CENTER
);
149 * Called by the Arena when the plugin is about to be used.
151 public void startUsing()
153 Runnable task
= new Runnable()
160 if (SwingUtilities
.isEventDispatchThread())
166 SwingUtilities
.invokeLater(task
);
172 * Called by the Arena when the plugin is no longer needed.
174 public void stopUsing()
180 * Called by the Arena to obtain the editor panel which we will use to
182 * @return The editor panel.
184 public JPanel
getEditorPanel()
190 * Called by the Arena to obtain the current source.
191 * This happens when the user is saving, compiling, and/or submitting.
192 * @return The current source code.
193 * @throws Exception If the source file edited by Vim couldn't be read.
195 public String
getSource() throws Exception
199 String source
= editor
.getSource();
200 logInfo("Source code uploaded to server.");
203 catch (Exception exception
)
205 logError("Failed to get source code: " + exception
.getLocalizedMessage());
211 * Called by the Arena to pass the source it has.
212 * @param source The source code.
214 public void setSource(String source
)
218 editor
.setSource(source
);
219 logInfo("Source code downloaded from server.");
221 catch (Exception exception
)
223 logError("Failed to save the source given by the server: " +
224 exception
.getLocalizedMessage());
230 * Called by the Arena to pass along information about the current
232 * @param component A container for the particulars of the problem.
233 * @param language The currently selected language.
234 * @param renderer A helper object to help format the problem
237 public void setProblemComponent(ProblemComponentModel component
,
238 Language language
, Renderer renderer
)
242 editor
= new Editor(component
, language
, renderer
);
244 catch (Exception exception
)
246 logError("An error occurred while loading the problem: " +
247 exception
.getLocalizedMessage());
252 * Called by the Arena when it's time to show our configuration panel.
254 public void configure()
256 final int border
= 10;
261 configDialog
= new JDialog();
262 Container container
= configDialog
.getContentPane();
263 container
.setForeground(Common
.FG_COLOR
);
264 container
.setBackground(Common
.WPB_COLOR
);
266 JPanel pane
= new JPanel();
269 BoxLayout boxLayout
= new BoxLayout(pane
, BoxLayout
.Y_AXIS
);
270 pane
.setLayout(boxLayout
);
271 pane
.setBorder(BorderFactory
.createEmptyBorder(border
, border
, border
, border
));
273 JPanel fieldPanel
= new JPanel(new GridBagLayout());
274 pane
.add(fieldPanel
);
275 pane
.add(Box
.createRigidArea(new Dimension(0, border
)));
277 GridBagConstraints c
= new GridBagConstraints();
278 c
.fill
= GridBagConstraints
.HORIZONTAL
;
279 c
.insets
= new Insets(inset
, inset
, inset
, inset
);
281 JLabel rootDirLabel
= new JLabel("Storage Directory:");
282 rootDirLabel
.setForeground(Common
.FG_COLOR
);
286 fieldPanel
.add(rootDirLabel
, c
);
288 final JTextField rootDirField
= new JTextField(rootDir
.getPath());
289 rootDirField
.setPreferredSize(new Dimension(0, 24));
293 fieldPanel
.add(rootDirField
, c
);
295 JButton browseButton
= new JButton("Browse");
299 c
.anchor
= GridBagConstraints
.BASELINE_LEADING
;
300 fieldPanel
.add(browseButton
, c
);
302 final JCheckBox contestDirNamesButton
= new JCheckBox(
303 "Store problems according to contest name and point value.",
306 contestDirNamesButton
.setForeground(Common
.FG_COLOR
);
307 contestDirNamesButton
.setBackground(Common
.WPB_COLOR
);
308 contestDirNamesButton
.setFont(rootDirLabel
.getFont());
312 fieldPanel
.add(contestDirNamesButton
, c
);
314 JLabel vimCommandLabel
= new JLabel("Vim Command:");
315 vimCommandLabel
.setForeground(Common
.FG_COLOR
);
319 fieldPanel
.add(vimCommandLabel
, c
);
321 final JTextField vimCommandField
= new JTextField(vimCommand
);
322 vimCommandField
.setPreferredSize(new Dimension(0, 24));
327 fieldPanel
.add(vimCommandField
, c
);
329 JPanel buttonPanel
= new JPanel(new FlowLayout(FlowLayout
.TRAILING
, inset
, inset
));
330 buttonPanel
.setPreferredSize(new Dimension(400, 24 + 2 * inset
));
331 pane
.add(buttonPanel
);
333 JButton saveButton
= new JButton("Save");
334 buttonPanel
.add(saveButton
);
335 buttonPanel
.add(Box
.createRigidArea(new Dimension(1, 0)));
337 JButton closeButton
= new JButton("Close");
338 buttonPanel
.add(closeButton
);
340 browseButton
.addActionListener(new ActionListener()
342 public void actionPerformed(ActionEvent actionEvent
)
344 JFileChooser chooser
= new JFileChooser();
345 chooser
.setCurrentDirectory(new File("."));
346 chooser
.setDialogTitle("Choose Storage Directory");
347 chooser
.setFileSelectionMode(JFileChooser
.DIRECTORIES_ONLY
);
348 chooser
.setAcceptAllFileFilterUsed(false);
350 if (chooser
.showOpenDialog(configDialog
) == JFileChooser
.APPROVE_OPTION
)
352 rootDirField
.setText(chooser
.getSelectedFile().getPath());
357 closeButton
.addActionListener(new ActionListener()
359 public void actionPerformed(ActionEvent actionEvent
)
361 configDialog
.dispose();
365 saveButton
.addActionListener(new ActionListener()
367 public void actionPerformed(ActionEvent actionEvent
)
369 prefs
.setProperty(VIMCOMMAND
, vimCommandField
.getText());
370 prefs
.setProperty(ROOTDIR
, rootDirField
.getText());
371 prefs
.setProperty(CONTESTDIRNAMES
, String
.valueOf(contestDirNamesButton
.isSelected()));
372 JOptionPane
.showMessageDialog(null, "Preferences were saved successfully.");
376 configDialog
.setTitle("VimCoder Preferences");
378 configDialog
.setLocationRelativeTo(null); // Center dialog in screen.
379 configDialog
.setModalityType(Dialog
.DEFAULT_MODALITY_TYPE
);
380 configDialog
.setDefaultCloseOperation(WindowConstants
.DISPOSE_ON_CLOSE
);
381 configDialog
.setVisible(true);
386 * Load the local preferences related to this plugin.
388 private void loadConfiguration()
390 String vc
= prefs
.getProperty(VIMCOMMAND
);
391 if (vc
!= null) vimCommand
= vc
;
393 String dir
= prefs
.getProperty(ROOTDIR
);
394 if (dir
!= null) rootDir
= new File(dir
);
396 String cn
= prefs
.getProperty(CONTESTDIRNAMES
);
397 if (cn
!= null) contestDirNames
= Boolean
.parseBoolean(cn
);
402 * A generic logging function, appends text to the text area. A timestamp
403 * is also prepended to the next text.
404 * @param what The text to append.
406 private void log(final String what
)
408 Runnable task
= new Runnable()
412 SimpleDateFormat format
= new SimpleDateFormat("kk:mm:ss");
413 logArea
.append(format
.format(new Date()) + ", " + what
);
416 if (SwingUtilities
.isEventDispatchThread())
422 SwingUtilities
.invokeLater(task
);
427 * Output non-critical messages to the log.
428 * @param what The text of the message.
430 private void logInfo(String what
)
432 log(" INFO: " + what
+ System
.getProperty("line.separator"));
436 * Output critical messages and errors to the log.
437 * @param what The text of the message.
439 private void logError(String what
)
441 log("ERROR: " + what
+ System
.getProperty("line.separator"));