]> Dogcows Code - chaz/vimcoder/blob - src/com/dogcows/VimCoder.java
small cleanups; documented the code
[chaz/vimcoder] / src / com / dogcows / VimCoder.java
1
2 package com.dogcows;
3
4 import javax.swing.*;
5 import java.awt.*;
6 import java.io.*;
7 import java.text.SimpleDateFormat;
8 import java.util.*;
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;
13
14 /**
15 * @author Charles McGarvey
16 * The TopCoder Arena editor plug-in providing support for Vim.
17 *
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.
20 */
21 public class VimCoder
22 {
23 /**
24 * The name and version of this plugin.
25 */
26 public final static String version = "VimCoder 0.1";
27
28 /**
29 * The website of the plugin project.
30 */
31 public final static String website = "http://www.dogcows.com/vimcoder";
32
33
34 /**
35 * The panel given to the Arena applet when it is requested.
36 */
37 private JPanel panel;
38
39 /**
40 * The text widget where log messages are appended.
41 */
42 private JTextArea logArea;
43
44 /**
45 * The current editor object (or null if there is none).
46 */
47 private Editor editor;
48
49
50 /**
51 * Instantiate the entry point of the editor plugin. Sets up the log widget
52 * and panel.
53 */
54 public VimCoder()
55 {
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);
62
63 panel = new JPanel(new BorderLayout());
64 panel.add(new JScrollPane(logArea), BorderLayout.CENTER);
65 }
66
67
68 /**
69 * Called by the Arena when the plugin is about to be used.
70 */
71 public void startUsing()
72 {
73 System.out.println("startUsing");
74 Runnable task = new Runnable()
75 {
76 public void run()
77 {
78 logArea.setText("");
79 }
80 };
81 if (SwingUtilities.isEventDispatchThread())
82 {
83 task.run();
84 }
85 else
86 {
87 SwingUtilities.invokeLater(task);
88 }
89 }
90
91 /**
92 * Called by the Arena when the plugin is no longer needed.
93 */
94 public void stopUsing()
95 {
96 System.out.println("stopUsing");
97 editor = null;
98 }
99
100 /**
101 * Called by the Arena to obtain the editor panel which we will use to show
102 * log messages.
103 * @return The editor panel.
104 */
105 public JPanel getEditorPanel()
106 {
107 System.out.println("getEditorPanel");
108 return panel;
109 }
110
111 /**
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.
116 */
117 public String getSource() throws Exception
118 {
119 System.out.println("getSource");
120 try
121 {
122 String source = editor.getSource();
123 logInfo("Source code uploaded to server.");
124 return source;
125 }
126 catch (Exception exception)
127 {
128 logError("Failed to get source code: " +
129 exception.getLocalizedMessage());
130 throw exception;
131 }
132 }
133
134 /**
135 * Called by the Arena to pass the source it has.
136 * @param source The source code.
137 */
138 public void setSource(String source)
139 {
140 System.out.println("setSource: " + source);
141 try
142 {
143 editor.setSource(source);
144 logInfo("Source code downloaded from server.");
145 }
146 catch (Exception exception)
147 {
148 logError("Failed to save the source given by the server: " +
149 exception.getLocalizedMessage());
150 return;
151 }
152 }
153
154 /**
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.
159 */
160 public void setProblemComponent(ProblemComponentModel component,
161 Language language,
162 Renderer renderer)
163 {
164 System.out.println("setProblemComponent");
165 try
166 {
167 editor = new Editor(component, language, renderer);
168 }
169 catch (Exception exception)
170 {
171 logError("An error occured while loading the problem: " +
172 exception.getLocalizedMessage());
173 }
174 }
175
176
177 /**
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.
181 */
182 private void log(final String what)
183 {
184 Runnable task = new Runnable()
185 {
186 public void run()
187 {
188 SimpleDateFormat format = new SimpleDateFormat("kk:mm:ss");
189 logArea.append(format.format(new Date()) + ", " + what);
190 }
191 };
192 if (SwingUtilities.isEventDispatchThread())
193 {
194 task.run();
195 }
196 else
197 {
198 SwingUtilities.invokeLater(task);
199 }
200 }
201
202 /**
203 * Output non-critical messages to the log.
204 * @param what The text of the message.
205 */
206 private void logInfo(String what)
207 {
208 log(" INFO: " + what + System.getProperty("line.separator"));
209 }
210
211 /**
212 * Output potentially important messages to the log.
213 * @param what The text of the message.
214 */
215 private void logWarning(String what)
216 {
217 log(" WARN: " + what + System.getProperty("line.separator"));
218 }
219
220 /**
221 * Output critical messages and errors to the log.
222 * @param what The text of the message.
223 */
224 private void logError(String what)
225 {
226 log("ERROR: " + what + System.getProperty("line.separator"));
227 }
228 }
229
This page took 0.051996 seconds and 5 git commands to generate.