]> Dogcows Code - chaz/vimcoder/blob - src/com/dogcows/VimCoder.java
don't wait on vim processes
[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 *
25 */
26 public final static String version = "VimCoder 0.1";
27 public final static String website = "http://www.dogcows.com/vimcoder";
28
29 private JPanel panel;
30 private JTextArea logArea;
31
32 private Editor editor;
33
34
35 public VimCoder()
36 {
37 logArea = new JTextArea();
38 logArea.setForeground(Color.GREEN);
39 logArea.setBackground(Color.BLACK);
40 logArea.setEditable(false);
41 Font font = new Font("Courier", Font.PLAIN, 12);
42 if (font != null) logArea.setFont(font);
43
44 panel = new JPanel(new BorderLayout());
45 panel.add(new JScrollPane(logArea), BorderLayout.CENTER);
46 }
47
48
49 public void startUsing()
50 {
51 Runnable task = new Runnable()
52 {
53 public void run()
54 {
55 logArea.setText("");
56 }
57 };
58 if (SwingUtilities.isEventDispatchThread())
59 {
60 task.run();
61 }
62 else
63 {
64 SwingUtilities.invokeLater(task);
65 }
66 }
67
68 public void stopUsing()
69 {
70 editor = null;
71 }
72
73 public JPanel getEditorPanel()
74 {
75 return panel;
76 }
77
78 public String getSource() throws IOException
79 {
80 try
81 {
82 String source = editor.getSource();
83 logInfo("Source code uploaded to server.");
84 return source;
85 }
86 catch (IOException exception)
87 {
88 logError("Failed to open source file for reading.");
89 throw exception;
90 }
91 }
92
93 public void setSource(String source)
94 {
95 try
96 {
97 editor.setSource(source);
98 logInfo("Source code downloaded from server.");
99 }
100 catch (IOException exception)
101 {
102 logError("Failed to save the source given by the server.");
103 return;
104 }
105 }
106
107 public void setProblemComponent(ProblemComponentModel component,
108 Language language,
109 Renderer renderer)
110 {
111 try
112 {
113 editor = new Editor(component, language, renderer);
114 }
115 catch (IOException exception)
116 {
117 logError("An error occured while loading the problem.");
118 }
119 }
120
121
122 private void log(final String what)
123 {
124 Runnable task = new Runnable()
125 {
126 public void run()
127 {
128 SimpleDateFormat format = new SimpleDateFormat("kk:mm:ss");
129 logArea.append(format.format(new Date()) + ", " + what);
130 }
131 };
132 if (SwingUtilities.isEventDispatchThread())
133 {
134 task.run();
135 }
136 else
137 {
138 SwingUtilities.invokeLater(task);
139 }
140 }
141
142 private void logInfo(String what)
143 {
144 log(" INFO: " + what + "\n");
145 }
146
147 private void logWarning(String what)
148 {
149 log(" WARN: " + what + "\n");
150 }
151
152 private void logError(String what)
153 {
154 log("ERROR: " + what + "\n");
155 }
156
157
158 public static void main(String args[])
159 {
160 VimCoder plugin = new VimCoder();
161
162 JFrame frame = new JFrame("VimCoder");
163 frame.add(plugin.getEditorPanel());
164 frame.setSize(640, 480);
165 frame.setVisible(true);
166 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
167
168 plugin.startUsing();
169 }
170 }
171
This page took 0.037568 seconds and 4 git commands to generate.