root / trunk / libraries / libIverUtiles / src / com / iver / utiles / console / JConsole.java @ 8308
History | View | Annotate | Download (9.92 KB)
1 |
/*
|
---|---|
2 |
* Created on 31-ene-2005
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
21 |
USA.
|
22 |
*
|
23 |
* For more information, contact:
|
24 |
*
|
25 |
* Generalitat Valenciana
|
26 |
* Conselleria d'Infraestructures i Transport
|
27 |
* Av. Blasco Ib??ez, 50
|
28 |
* 46010 VALENCIA
|
29 |
* SPAIN
|
30 |
*
|
31 |
* +34 963862235
|
32 |
* gvsig@gva.es
|
33 |
* www.gvsig.gva.es
|
34 |
*
|
35 |
* or
|
36 |
*
|
37 |
* IVER T.I. S.A
|
38 |
* Salamanca 50
|
39 |
* 46005 Valencia
|
40 |
* Spain
|
41 |
*
|
42 |
* +34 963163400
|
43 |
* dac@iver.es
|
44 |
*/
|
45 |
package com.iver.utiles.console; |
46 |
|
47 |
import java.awt.BorderLayout; |
48 |
import java.awt.Cursor; |
49 |
import java.awt.Dimension; |
50 |
import java.awt.event.KeyAdapter; |
51 |
import java.awt.event.KeyEvent; |
52 |
import java.awt.event.MouseEvent; |
53 |
import java.awt.event.MouseMotionAdapter; |
54 |
import java.util.ArrayList; |
55 |
|
56 |
import javax.swing.ButtonGroup; |
57 |
import javax.swing.ImageIcon; |
58 |
import javax.swing.JComponent; |
59 |
import javax.swing.JPanel; |
60 |
import javax.swing.JToggleButton; |
61 |
import javax.swing.SwingUtilities; |
62 |
import javax.swing.event.CaretEvent; |
63 |
import javax.swing.event.CaretListener; |
64 |
|
65 |
import com.iver.utiles.console.jedit.ConsoleInputHandler; |
66 |
import com.iver.utiles.console.jedit.JEditTextArea; |
67 |
import com.iver.utiles.console.jedit.TextAreaDefaults; |
68 |
import com.iver.utiles.console.jedit.TokenMarker; |
69 |
|
70 |
|
71 |
/**
|
72 |
* DOCUMENT ME!
|
73 |
*
|
74 |
* @author Fernando Gonz?lez Cort?s
|
75 |
*/
|
76 |
public class JConsole extends JPanel { |
77 |
private JEditTextArea txt;
|
78 |
private int startingCaretPosition = 0; |
79 |
private ArrayList entries = new ArrayList(); |
80 |
private int currentEntry = -1; |
81 |
//private JScrollPane jScrollPane = null;
|
82 |
private ResponseListenerSupport listenerSupport = new ResponseListenerSupport(); |
83 |
public static int MESSAGE=0; |
84 |
public static int COMMAND=1; |
85 |
public static int INSERT=2; |
86 |
public static int ERROR=3; |
87 |
private static TextAreaDefaults defaults; |
88 |
private JConsole theContainer = null; |
89 |
/**
|
90 |
* This is the default constructor
|
91 |
*/
|
92 |
public JConsole() {
|
93 |
super();
|
94 |
initialize(); |
95 |
defaults=TextAreaDefaults.getDefaults(); |
96 |
((ConsoleInputHandler)defaults.inputHandler).addConsoleListener(this);
|
97 |
theContainer = this;
|
98 |
} |
99 |
|
100 |
/**
|
101 |
* This method initializes this
|
102 |
*/
|
103 |
private void initialize() { |
104 |
this.setLayout(new BorderLayout()); |
105 |
this.setSize(300, 200); |
106 |
this.setPreferredSize(new Dimension(300,200)); |
107 |
this.add(getTxt(), java.awt.BorderLayout.CENTER);
|
108 |
} |
109 |
|
110 |
public void setTokenMarker(TokenMarker tm){ |
111 |
txt.setTokenMarker(tm); |
112 |
} |
113 |
/**
|
114 |
* Obtiene una referencia al JTextArea de la consola
|
115 |
*
|
116 |
* @return javax.swing.JTextArea
|
117 |
*/
|
118 |
public JEditTextArea getTxt() {
|
119 |
if (txt == null) { |
120 |
txt = new JEditTextArea();
|
121 |
//txt.setTokenMarker(new JavaTokenMarker());
|
122 |
txt.addCaretListener(new CaretListener() { |
123 |
public void caretUpdate(CaretEvent e) { |
124 |
if (txt.getCaretPosition() < startingCaretPosition) {
|
125 |
if (startingCaretPosition <= txt.getText().length()) {
|
126 |
txt.setCaretPosition(startingCaretPosition); |
127 |
} |
128 |
} |
129 |
} |
130 |
}); |
131 |
txt.addKeyListener(new KeyAdapter() { |
132 |
public void keyPressed(KeyEvent e) { |
133 |
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { |
134 |
if (startingCaretPosition >= txt.getCaretPosition()) {
|
135 |
int caretPos = txt.getCaretPosition();
|
136 |
String text = txt.getText();
|
137 |
text = text.substring(0, caretPos) + " " + |
138 |
text.substring(caretPos); |
139 |
txt.setText(text); |
140 |
|
141 |
txt.setCaretPosition(caretPos); |
142 |
|
143 |
}else{
|
144 |
} |
145 |
} |
146 |
|
147 |
|
148 |
} |
149 |
|
150 |
public void keyReleased(KeyEvent e) { |
151 |
if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
152 |
String texto = txt.getText();
|
153 |
String response = texto.substring(startingCaretPosition);
|
154 |
|
155 |
listenerSupport.callAcceptResponse(response); |
156 |
|
157 |
if (response.trim().length() > 0) { |
158 |
entries.add(response.trim()); |
159 |
} |
160 |
|
161 |
currentEntry = -1;
|
162 |
|
163 |
} else if (e.getKeyCode() == KeyEvent.VK_UP) { |
164 |
if (entries.size() == 0) { |
165 |
return;
|
166 |
} |
167 |
|
168 |
if (currentEntry == -1) { |
169 |
currentEntry = entries.size() - 1;
|
170 |
} else {
|
171 |
currentEntry--; |
172 |
|
173 |
if (currentEntry < 0) { |
174 |
currentEntry = 0;
|
175 |
} |
176 |
} |
177 |
|
178 |
putEntry(); |
179 |
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) { |
180 |
if (entries.size() == 0) { |
181 |
return;
|
182 |
} |
183 |
|
184 |
if (currentEntry != -1) { |
185 |
currentEntry++; |
186 |
|
187 |
if (currentEntry >= entries.size()) {
|
188 |
currentEntry = entries.size() - 1;
|
189 |
} |
190 |
} |
191 |
|
192 |
putEntry(); |
193 |
} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { |
194 |
txt.setText(txt.getText()); |
195 |
listenerSupport.callAcceptResponse(null);
|
196 |
} |
197 |
} |
198 |
|
199 |
private void putEntry() { |
200 |
String anterior = txt.getText();
|
201 |
anterior = anterior.substring(0, startingCaretPosition);
|
202 |
txt.setText(anterior + entries.get(currentEntry)); |
203 |
} |
204 |
}); |
205 |
addKeyListener(new java.awt.event.KeyAdapter() {
|
206 |
public void keyPressed(KeyEvent e) { |
207 |
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { |
208 |
if (startingCaretPosition >= txt.getCaretPosition()) {
|
209 |
int caretPos = txt.getCaretPosition();
|
210 |
String text = txt.getText();
|
211 |
text = text.substring(0, caretPos) + " " + |
212 |
text.substring(caretPos); |
213 |
txt.setText(text); |
214 |
|
215 |
txt.setCaretPosition(caretPos); |
216 |
|
217 |
}else{
|
218 |
} |
219 |
} |
220 |
|
221 |
|
222 |
} |
223 |
|
224 |
public void keyReleased(KeyEvent e) { |
225 |
if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
226 |
String texto = txt.getText();
|
227 |
String response = texto.substring(startingCaretPosition);
|
228 |
|
229 |
listenerSupport.callAcceptResponse(response); |
230 |
|
231 |
if (response.trim().length() > 0) { |
232 |
entries.add(response.trim()); |
233 |
} |
234 |
|
235 |
currentEntry = -1;
|
236 |
|
237 |
} else if (e.getKeyCode() == KeyEvent.VK_UP) { |
238 |
if (entries.size() == 0) { |
239 |
return;
|
240 |
} |
241 |
|
242 |
if (currentEntry == -1) { |
243 |
currentEntry = entries.size() - 1;
|
244 |
} else {
|
245 |
currentEntry--; |
246 |
|
247 |
if (currentEntry < 0) { |
248 |
currentEntry = 0;
|
249 |
} |
250 |
} |
251 |
|
252 |
putEntry(); |
253 |
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) { |
254 |
if (entries.size() == 0) { |
255 |
return;
|
256 |
} |
257 |
|
258 |
if (currentEntry != -1) { |
259 |
currentEntry++; |
260 |
|
261 |
if (currentEntry >= entries.size()) {
|
262 |
currentEntry = entries.size() - 1;
|
263 |
} |
264 |
} |
265 |
|
266 |
putEntry(); |
267 |
} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { |
268 |
txt.setText(txt.getText()); |
269 |
listenerSupport.callAcceptResponse(null);
|
270 |
} |
271 |
} |
272 |
|
273 |
private void putEntry() { |
274 |
String anterior = txt.getText();
|
275 |
anterior = anterior.substring(0, startingCaretPosition);
|
276 |
txt.setText(anterior + entries.get(currentEntry)); |
277 |
} |
278 |
}); |
279 |
} |
280 |
|
281 |
return txt;
|
282 |
} |
283 |
|
284 |
/**
|
285 |
* A?ade un texto a la consola
|
286 |
*
|
287 |
* @param text Texto que se a?ade a la consola
|
288 |
*/
|
289 |
public void addText(String text,int type) { |
290 |
txt.setText(txt.getText() + text); |
291 |
txt.setCaretPosition(txt.getText().length()); |
292 |
startingCaretPosition = txt.getText().length(); |
293 |
|
294 |
} |
295 |
/**
|
296 |
* A?ade un texto a la consola que es tomado como si lo
|
297 |
* hubiese escrito el usuario. Formar? parte de la respuesta
|
298 |
*
|
299 |
* @param text Texto que se a?ade a la consola
|
300 |
*/
|
301 |
public void addResponseText(String text) { |
302 |
txt.setText(txt.getText() + text); |
303 |
txt.setCaretPosition(txt.getText().length()); |
304 |
} |
305 |
|
306 |
/**
|
307 |
* This method initializes jScrollPane
|
308 |
*
|
309 |
* @return javax.swing.JScrollPane
|
310 |
*/
|
311 |
/*private JScrollPane getJScrollPane() {
|
312 |
if (jScrollPane == null) {
|
313 |
jScrollPane = new JScrollPane();
|
314 |
jScrollPane.setViewportView(getTxt());
|
315 |
}
|
316 |
|
317 |
return jScrollPane;
|
318 |
}
|
319 |
*/
|
320 |
public void addResponseListener(ResponseListener listener) { |
321 |
listenerSupport.addResponseListener(listener); |
322 |
} |
323 |
public void removeResponseListener(ResponseListener listener) { |
324 |
listenerSupport.removeResponseListener(listener); |
325 |
} |
326 |
|
327 |
/**
|
328 |
* Useful to know from where it comes a key event. See CADExtension
|
329 |
* @param name
|
330 |
*/
|
331 |
public void setJTextName(String name) |
332 |
{ |
333 |
txt.setName(name); |
334 |
} |
335 |
|
336 |
public void keyPressed(KeyEvent e) { |
337 |
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { |
338 |
if (startingCaretPosition >= txt.getCaretPosition()) {
|
339 |
int caretPos = txt.getCaretPosition();
|
340 |
String text = txt.getText();
|
341 |
text = text.substring(0, caretPos) + " " + |
342 |
text.substring(caretPos); |
343 |
txt.setText(text); |
344 |
|
345 |
txt.setCaretPosition(caretPos); |
346 |
|
347 |
}else{
|
348 |
} |
349 |
} |
350 |
} |
351 |
|
352 |
public void keyReleased(KeyEvent e) { |
353 |
if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
354 |
String texto = txt.getText();
|
355 |
String response = texto.substring(startingCaretPosition);
|
356 |
|
357 |
listenerSupport.callAcceptResponse(response); |
358 |
|
359 |
if (response.trim().length() > 0) { |
360 |
entries.add(response.trim()); |
361 |
} |
362 |
|
363 |
currentEntry = -1;
|
364 |
|
365 |
} else if (e.getKeyCode() == KeyEvent.VK_UP) { |
366 |
if (entries.size() == 0) { |
367 |
return;
|
368 |
} |
369 |
|
370 |
if (currentEntry == -1) { |
371 |
currentEntry = entries.size() - 1;
|
372 |
} else {
|
373 |
currentEntry--; |
374 |
|
375 |
if (currentEntry < 0) { |
376 |
currentEntry = 0;
|
377 |
} |
378 |
} |
379 |
|
380 |
putEntry(); |
381 |
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) { |
382 |
if (entries.size() == 0) { |
383 |
return;
|
384 |
} |
385 |
|
386 |
if (currentEntry != -1) { |
387 |
currentEntry++; |
388 |
|
389 |
if (currentEntry >= entries.size()) {
|
390 |
currentEntry = entries.size() - 1;
|
391 |
} |
392 |
} |
393 |
|
394 |
putEntry(); |
395 |
} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { |
396 |
txt.setText(txt.getText()); |
397 |
listenerSupport.callAcceptResponse(null);
|
398 |
} |
399 |
} |
400 |
private void putEntry() { |
401 |
String anterior = txt.getText();
|
402 |
anterior = anterior.substring(0, startingCaretPosition);
|
403 |
txt.setText(anterior + entries.get(currentEntry)); |
404 |
} |
405 |
|
406 |
|
407 |
} |