7 import java
.text
.SimpleDateFormat
;
9 import com
.topcoder
.client
.contestant
.ProblemComponentModel
;
10 import com
.topcoder
.shared
.language
.*;
11 import com
.topcoder
.shared
.problem
.*;
12 import com
.topcoder
.shared
.problem
.Renderer
;
15 * @author Charles McGarvey
16 * The TopCoder Arena editor plug-in providing support for Vim.
18 * Distributable under the terms and conditions of the 2-clause BSD license;
19 * see the file COPYING for a complete text of the license.
24 * The name and version of this plugin.
26 public final static String version
= "VimCoder 0.1";
29 * The website of the plugin project.
31 public final static String website
= "http://www.dogcows.com/vimcoder";
35 * The panel given to the Arena applet when it is requested.
40 * The text widget where log messages are appended.
42 private JTextArea logArea
;
45 * The current editor object (or null if there is none).
47 private Editor editor
;
51 * Instantiate the entry point of the editor plugin. Sets up the log widget
56 logArea
= new JTextArea();
57 logArea
.setForeground(Color
.GREEN
);
58 logArea
.setBackground(Color
.BLACK
);
59 logArea
.setEditable(false);
60 Font font
= new Font("Courier", Font
.PLAIN
, 12);
61 if (font
!= null) logArea
.setFont(font
);
63 panel
= new JPanel(new BorderLayout());
64 panel
.add(new JScrollPane(logArea
), BorderLayout
.CENTER
);
69 * Called by the Arena when the plugin is about to be used.
71 public void startUsing()
73 System
.out
.println("startUsing");
74 Runnable task
= new Runnable()
81 if (SwingUtilities
.isEventDispatchThread())
87 SwingUtilities
.invokeLater(task
);
92 * Called by the Arena when the plugin is no longer needed.
94 public void stopUsing()
96 System
.out
.println("stopUsing");
101 * Called by the Arena to obtain the editor panel which we will use to show
103 * @return The editor panel.
105 public JPanel
getEditorPanel()
107 System
.out
.println("getEditorPanel");
112 * Called by the Arena to obtain the current source. This happens when the
113 * user is saving, compiling, and/or submitting.
114 * @return The current source code.
115 * @throws Exception If the source file edited by Vim couldn't be read.
117 public String
getSource() throws Exception
119 System
.out
.println("getSource");
122 String source
= editor
.getSource();
123 logInfo("Source code uploaded to server.");
126 catch (Exception exception
)
128 logError("Failed to get source code: " +
129 exception
.getLocalizedMessage());
135 * Called by the Arena to pass the source it has.
136 * @param source The source code.
138 public void setSource(String source
)
140 System
.out
.println("setSource: " + source
);
143 editor
.setSource(source
);
144 logInfo("Source code downloaded from server.");
146 catch (Exception exception
)
148 logError("Failed to save the source given by the server: " +
149 exception
.getLocalizedMessage());
155 * Called by the Arena to pass along information about the current problem.
156 * @param component A container for the particulars of the problem.
157 * @param language The currently selected language.
158 * @param renderer A helper object to help format the problem statement.
160 public void setProblemComponent(ProblemComponentModel component
,
164 System
.out
.println("setProblemComponent");
167 editor
= new Editor(component
, language
, renderer
);
169 catch (Exception exception
)
171 logError("An error occured while loading the problem: " +
172 exception
.getLocalizedMessage());
178 * A generic logging function, appends text to the text area. A timestamp
179 * is also prepended to the next text.
180 * @param what The text to append.
182 private void log(final String what
)
184 Runnable task
= new Runnable()
188 SimpleDateFormat format
= new SimpleDateFormat("kk:mm:ss");
189 logArea
.append(format
.format(new Date()) + ", " + what
);
192 if (SwingUtilities
.isEventDispatchThread())
198 SwingUtilities
.invokeLater(task
);
203 * Output non-critical messages to the log.
204 * @param what The text of the message.
206 private void logInfo(String what
)
208 log(" INFO: " + what
+ System
.getProperty("line.separator"));
212 * Output potentially important messages to the log.
213 * @param what The text of the message.
215 private void logWarning(String what
)
217 log(" WARN: " + what
+ System
.getProperty("line.separator"));
221 * Output critical messages and errors to the log.
222 * @param what The text of the message.
224 private void logError(String what
)
226 log("ERROR: " + what
+ System
.getProperty("line.separator"));