1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| package edu.rice.cs.util.text; |
38 |
| |
39 |
| import java.io.*; |
40 |
| import java.awt.EventQueue; |
41 |
| import java.awt.print.*; |
42 |
| import java.awt.EventQueue; |
43 |
| |
44 |
| import edu.rice.cs.drjava.model.print.DrJavaBook; |
45 |
| |
46 |
| import edu.rice.cs.drjava.model.FileSaveSelector; |
47 |
| import edu.rice.cs.util.UnexpectedException; |
48 |
| import edu.rice.cs.util.OperationCanceledException; |
49 |
| import edu.rice.cs.util.swing.Utilities; |
50 |
| import edu.rice.cs.util.text.ConsoleDocumentInterface; |
51 |
| import edu.rice.cs.util.text.DocumentEditCondition; |
52 |
| import edu.rice.cs.util.text.EditDocumentException; |
53 |
| import edu.rice.cs.util.FileOps; |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| public class ConsoleDocument implements ConsoleDocumentInterface { |
60 |
| |
61 |
| |
62 |
| public static final String DEFAULT_CONSOLE_PROMPT = ""; |
63 |
| |
64 |
| |
65 |
| public static final String DEFAULT_STYLE = "default"; |
66 |
| |
67 |
| |
68 |
| public static final String SYSTEM_OUT_STYLE = "System.out"; |
69 |
| |
70 |
| |
71 |
| public static final String SYSTEM_ERR_STYLE = "System.err"; |
72 |
| |
73 |
| |
74 |
| public static final String SYSTEM_IN_STYLE = "System.in"; |
75 |
| |
76 |
| |
77 |
| protected final ConsoleDocumentInterface _document; |
78 |
| |
79 |
| |
80 |
| protected volatile Runnable _beep; |
81 |
| |
82 |
| |
83 |
| private volatile int _promptPos; |
84 |
| |
85 |
| |
86 |
| protected volatile String _prompt; |
87 |
| |
88 |
| |
89 |
| protected volatile DrJavaBook _book; |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
355
| public ConsoleDocument(ConsoleDocumentInterface doc) {
|
95 |
355
| _document = doc;
|
96 |
| |
97 |
0
| _beep = new Runnable() { public void run() { } };
|
98 |
355
| _prompt = DEFAULT_CONSOLE_PROMPT;
|
99 |
355
| _promptPos = DEFAULT_CONSOLE_PROMPT.length();
|
100 |
355
| _document.setHasPrompt(false);
|
101 |
355
| _document.setEditCondition(new ConsoleEditCondition());
|
102 |
| } |
103 |
| |
104 |
| |
105 |
4
| public boolean hasPrompt() { return _document.hasPrompt(); }
|
106 |
| |
107 |
32
| public void setHasPrompt(boolean val) { _document.setHasPrompt(val); }
|
108 |
| |
109 |
| |
110 |
28
| public String getPrompt() { return _prompt; }
|
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
1
| public void setPrompt(String prompt) { _prompt = prompt; }
|
116 |
| |
117 |
| |
118 |
0
| public int getPromptLength() { return _prompt.length(); }
|
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
0
| public DocumentEditCondition getEditCondition() { return _document.getEditCondition(); }
|
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
2
| public void setEditCondition(DocumentEditCondition condition) { _document.setEditCondition(condition); }
|
129 |
| |
130 |
| |
131 |
23
| public int getPromptPos() { return _promptPos; }
|
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
226
| public void setPromptPos(int newPos) {
|
137 |
226
| _promptPos = newPos;
|
138 |
| } |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
205
| public void setBeep(Runnable beep) { _beep = beep; }
|
144 |
| |
145 |
| |
146 |
6
| public void reset(String banner) {
|
147 |
6
| assert EventQueue.isDispatchThread();
|
148 |
6
| try {
|
149 |
6
| forceRemoveText(0, _document.getLength());
|
150 |
6
| forceInsertText(0, banner, DEFAULT_STYLE);
|
151 |
6
| _promptPos = banner.length();
|
152 |
| } |
153 |
0
| catch (EditDocumentException e) { throw new UnexpectedException(e); }
|
154 |
| } |
155 |
| |
156 |
| |
157 |
270
| public void insertPrompt() {
|
158 |
270
| try {
|
159 |
270
| int len = _document.getLength();
|
160 |
| |
161 |
270
| _promptPos = len + _prompt.length();
|
162 |
270
| forceInsertText(len, _prompt, DEFAULT_STYLE);
|
163 |
270
| _document.setHasPrompt(true);
|
164 |
| } |
165 |
0
| catch (EditDocumentException e) { throw new UnexpectedException(e); }
|
166 |
| } |
167 |
| |
168 |
| |
169 |
0
| public void disablePrompt() {
|
170 |
0
| _document.setHasPrompt(false);
|
171 |
0
| _promptPos = _document.getLength();
|
172 |
| } |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
3
| public void insertNewline(int pos) {
|
178 |
| |
179 |
3
| try {
|
180 |
3
| int len = _document.getLength();
|
181 |
0
| if (pos > len) pos = len;
|
182 |
0
| else if (pos < 0) pos = 0;
|
183 |
| |
184 |
3
| String newLine = "\n";
|
185 |
3
| insertText(pos, newLine, DEFAULT_STYLE);
|
186 |
| } |
187 |
0
| catch (EditDocumentException e) { throw new UnexpectedException(e); }
|
188 |
| } |
189 |
| |
190 |
| |
191 |
| |
192 |
51
| private int _getPositionBeforePrompt() {
|
193 |
51
| int len = _document.getLength();
|
194 |
51
| if (_document.hasPrompt()) {
|
195 |
29
| int promptStart = _promptPos - _prompt.length();
|
196 |
29
| return (promptStart < len && promptStart >= 0) ? promptStart : len;
|
197 |
| } |
198 |
22
| return len;
|
199 |
| } |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
51
| public void insertBeforeLastPrompt(String text, String style) {
|
206 |
51
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
207 |
51
| try {
|
208 |
51
| int pos = _getPositionBeforePrompt();
|
209 |
| |
210 |
51
| _promptPos = _promptPos + text.length();
|
211 |
51
| forceInsertText(pos, text, style);
|
212 |
| } |
213 |
0
| catch (EditDocumentException ble) { throw new UnexpectedException(ble); }
|
214 |
| } |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
250
| public void insertText(int offs, String str, String style) throws EditDocumentException {
|
223 |
2
| if (offs < _promptPos) _beep.run();
|
224 |
| else { |
225 |
248
| _addToStyleLists(offs, str, style);
|
226 |
248
| _document.insertText(offs, str, style);
|
227 |
| } |
228 |
| } |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
127
| public void append(String str, String style) throws EditDocumentException {
|
236 |
127
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
237 |
127
| int offs = _document.getLength();
|
238 |
127
| _addToStyleLists(offs, str, style);
|
239 |
127
| _document.insertText(offs, str, style);
|
240 |
| } |
241 |
| |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
328
| public void forceInsertText(int offs, String str, String style) throws EditDocumentException {
|
249 |
328
| _addToStyleLists(offs, str, style);
|
250 |
| |
251 |
328
| _document.forceInsertText(offs, str, style);
|
252 |
| } |
253 |
| |
254 |
| |
255 |
703
| private void _addToStyleLists(int offs, String str, String style) {
|
256 |
703
| if (_document instanceof SwingDocument)
|
257 |
703
| ((SwingDocument)_document).addColoring(offs, offs + str.length(), style);
|
258 |
| } |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
51
| public void removeText(int offs, int len) throws EditDocumentException {
|
266 |
0
| if (offs < _promptPos) _beep.run();
|
267 |
51
| else _document.removeText(offs, len);
|
268 |
| } |
269 |
| |
270 |
| |
271 |
| |
272 |
| |
273 |
| |
274 |
| |
275 |
7
| public void forceRemoveText(int offs, int len) throws EditDocumentException {
|
276 |
7
| _document.forceRemoveText(offs, len);
|
277 |
| } |
278 |
| |
279 |
| |
280 |
327
| public int getLength() { return _document.getLength(); }
|
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
| |
286 |
| |
287 |
124
| public String getDocText(int offs, int len) throws EditDocumentException {
|
288 |
124
| return _document.getDocText(offs, len);
|
289 |
| } |
290 |
| |
291 |
| |
292 |
| |
293 |
| |
294 |
25
| public String getText() { return _document.getDocText(0, getLength()); }
|
295 |
| |
296 |
| |
297 |
60
| public String getCurrentInput() {
|
298 |
60
| try { return getDocText(_promptPos, _document.getLength() - _promptPos); }
|
299 |
0
| catch (EditDocumentException e) { throw new UnexpectedException(e); }
|
300 |
| } |
301 |
| |
302 |
| |
303 |
13
| public void clearCurrentInput() { _clearCurrentInputText(); }
|
304 |
| |
305 |
| |
306 |
15
| protected void _clearCurrentInputText() {
|
307 |
15
| try {
|
308 |
| |
309 |
15
| removeText(_promptPos, _document.getLength() - _promptPos);
|
310 |
| } |
311 |
0
| catch (EditDocumentException ble) { throw new UnexpectedException(ble); }
|
312 |
| } |
313 |
| |
314 |
| |
315 |
0
| public String getDefaultStyle() { return ConsoleDocument.DEFAULT_STYLE; }
|
316 |
| |
317 |
| |
318 |
| |
319 |
| |
320 |
0
| public Pageable getPageable() throws IllegalStateException { return _book; }
|
321 |
| |
322 |
| |
323 |
0
| public void preparePrintJob() {
|
324 |
0
| _book = new DrJavaBook(getDocText(0, getLength()), "Console", new PageFormat());
|
325 |
| } |
326 |
| |
327 |
| |
328 |
0
| public void print() throws PrinterException {
|
329 |
0
| preparePrintJob();
|
330 |
0
| PrinterJob printJob = PrinterJob.getPrinterJob();
|
331 |
0
| printJob.setPageable(_book);
|
332 |
0
| if (printJob.printDialog()) printJob.print();
|
333 |
0
| cleanUpPrintJob();
|
334 |
| } |
335 |
| |
336 |
| |
337 |
0
| public void cleanUpPrintJob() { _book = null; }
|
338 |
| |
339 |
| |
340 |
| class ConsoleEditCondition extends DocumentEditCondition { |
341 |
377
| public boolean canInsertText(int offs) { return canRemoveText(offs); }
|
342 |
| |
343 |
425
| public boolean canRemoveText(int offs) {
|
344 |
425
| if (offs < _promptPos) {
|
345 |
1
| _beep.run();
|
346 |
1
| return false;
|
347 |
| } |
348 |
424
| return true;
|
349 |
| } |
350 |
| } |
351 |
| |
352 |
| |
353 |
| |
354 |
| |
355 |
0
| public void saveCopy(FileSaveSelector selector) throws IOException {
|
356 |
0
| assert EventQueue.isDispatchThread();
|
357 |
0
| try {
|
358 |
0
| final File file = selector.getFile().getCanonicalFile();
|
359 |
0
| if (! file.exists() || selector.verifyOverwrite(file)) {
|
360 |
0
| FileOps.saveFile(new FileOps.DefaultFileSaver(file) {
|
361 |
| |
362 |
0
| public void saveTo(OutputStream os) throws IOException {
|
363 |
0
| final String text = getDocText(0, getLength());
|
364 |
0
| OutputStreamWriter osw = new OutputStreamWriter(os);
|
365 |
0
| osw.write(text,0,text.length());
|
366 |
0
| osw.flush();
|
367 |
| } |
368 |
| }); |
369 |
| } |
370 |
| } |
371 |
| catch (OperationCanceledException oce) { |
372 |
| |
373 |
| |
374 |
0
| return;
|
375 |
| } |
376 |
| } |
377 |
| } |