svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libIverUtiles / src / org / gvsig / utils / console / JConsole.java @ 40331
History | View | Annotate | Download (10.5 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 org.gvsig.utils.console; |
46 |
|
47 |
import java.awt.BorderLayout; |
48 |
import java.awt.Component; |
49 |
import java.awt.Container; |
50 |
import java.awt.Dimension; |
51 |
import java.awt.event.KeyAdapter; |
52 |
import java.awt.event.KeyEvent; |
53 |
import java.util.ArrayList; |
54 |
|
55 |
import javax.swing.JPanel; |
56 |
import javax.swing.event.CaretEvent; |
57 |
import javax.swing.event.CaretListener; |
58 |
|
59 |
import org.gvsig.utils.console.jedit.ConsoleInputHandler; |
60 |
import org.gvsig.utils.console.jedit.JEditTextArea; |
61 |
import org.gvsig.utils.console.jedit.TextAreaDefaults; |
62 |
import org.gvsig.utils.console.jedit.TokenMarker; |
63 |
|
64 |
|
65 |
|
66 |
/**
|
67 |
* DOCUMENT ME!
|
68 |
*
|
69 |
* @author Fernando Gonz?lez Cort?s
|
70 |
*/
|
71 |
public class JConsole extends JPanel { |
72 |
private JEditTextArea txt;
|
73 |
private int startingCaretPosition = 0; |
74 |
private ArrayList entries = new ArrayList(); |
75 |
private int currentEntry = -1; |
76 |
//private JScrollPane jScrollPane = null;
|
77 |
private ResponseListenerSupport listenerSupport = new ResponseListenerSupport(); |
78 |
public static int MESSAGE=0; |
79 |
public static int COMMAND=1; |
80 |
public static int INSERT=2; |
81 |
public static int ERROR=3; |
82 |
private static TextAreaDefaults defaults; |
83 |
private JConsole theContainer = null; |
84 |
|
85 |
private boolean redispatch_key_events = false; |
86 |
/**
|
87 |
* This is the default constructor
|
88 |
*/
|
89 |
public JConsole() {
|
90 |
super();
|
91 |
initialize(); |
92 |
defaults=TextAreaDefaults.getDefaults(); |
93 |
((ConsoleInputHandler)defaults.inputHandler).addConsoleListener(this);
|
94 |
theContainer = this;
|
95 |
} |
96 |
|
97 |
|
98 |
/**
|
99 |
* With this constructor, we decide whether the key events
|
100 |
* are re-dispatched to the parent of this component.
|
101 |
* This is necessary to prevent the console from consuming
|
102 |
* key events (short-cuts) while editing. the component
|
103 |
* which originally receives the key event is the JEditTextArea
|
104 |
* but this console is receiving also key-pressed notifications.
|
105 |
*
|
106 |
* @param redisp_key_events
|
107 |
*/
|
108 |
public JConsole(boolean redisp_key_events) { |
109 |
this();
|
110 |
redispatch_key_events = redisp_key_events; |
111 |
} |
112 |
|
113 |
|
114 |
/**
|
115 |
* This method initializes this
|
116 |
*/
|
117 |
private void initialize() { |
118 |
this.setLayout(new BorderLayout()); |
119 |
this.setSize(300, 200); |
120 |
this.setPreferredSize(new Dimension(300,200)); |
121 |
this.add(getTxt(), java.awt.BorderLayout.CENTER);
|
122 |
} |
123 |
|
124 |
public void setTokenMarker(TokenMarker tm){ |
125 |
txt.setTokenMarker(tm); |
126 |
} |
127 |
/**
|
128 |
* Obtiene una referencia al JTextArea de la consola
|
129 |
*
|
130 |
* @return javax.swing.JTextArea
|
131 |
*/
|
132 |
public JEditTextArea getTxt() {
|
133 |
if (txt == null) { |
134 |
txt = new JEditTextArea();
|
135 |
//txt.setTokenMarker(new JavaTokenMarker());
|
136 |
txt.addCaretListener(new CaretListener() { |
137 |
public void caretUpdate(CaretEvent e) { |
138 |
if (txt.getCaretPosition() < startingCaretPosition) {
|
139 |
if (startingCaretPosition <= txt.getText().length()) {
|
140 |
txt.setCaretPosition(startingCaretPosition); |
141 |
} |
142 |
} |
143 |
} |
144 |
}); |
145 |
txt.addKeyListener(new KeyAdapter() { |
146 |
public void keyPressed(KeyEvent e) { |
147 |
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { |
148 |
if (startingCaretPosition >= txt.getCaretPosition()) {
|
149 |
int caretPos = txt.getCaretPosition();
|
150 |
String text = txt.getText();
|
151 |
text = text.substring(0, caretPos) + " " + |
152 |
text.substring(caretPos); |
153 |
txt.setText(text); |
154 |
|
155 |
txt.setCaretPosition(caretPos); |
156 |
|
157 |
}else{
|
158 |
} |
159 |
} |
160 |
|
161 |
|
162 |
} |
163 |
|
164 |
public void keyReleased(KeyEvent e) { |
165 |
if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
166 |
String texto = txt.getText();
|
167 |
String response = texto.substring(startingCaretPosition);
|
168 |
|
169 |
listenerSupport.callAcceptResponse(response); |
170 |
|
171 |
if (response.trim().length() > 0) { |
172 |
entries.add(response.trim()); |
173 |
} |
174 |
|
175 |
currentEntry = -1;
|
176 |
|
177 |
} else if (e.getKeyCode() == KeyEvent.VK_UP) { |
178 |
if (entries.size() == 0) { |
179 |
return;
|
180 |
} |
181 |
|
182 |
if (currentEntry == -1) { |
183 |
currentEntry = entries.size() - 1;
|
184 |
} else {
|
185 |
currentEntry--; |
186 |
|
187 |
if (currentEntry < 0) { |
188 |
currentEntry = 0;
|
189 |
} |
190 |
} |
191 |
|
192 |
putEntry(); |
193 |
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) { |
194 |
if (entries.size() == 0) { |
195 |
return;
|
196 |
} |
197 |
|
198 |
if (currentEntry != -1) { |
199 |
currentEntry++; |
200 |
|
201 |
if (currentEntry >= entries.size()) {
|
202 |
currentEntry = entries.size() - 1;
|
203 |
} |
204 |
} |
205 |
|
206 |
putEntry(); |
207 |
} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { |
208 |
txt.setText(txt.getText()); |
209 |
listenerSupport.callAcceptResponse(null);
|
210 |
} |
211 |
} |
212 |
|
213 |
private void putEntry() { |
214 |
String anterior = txt.getText();
|
215 |
anterior = anterior.substring(0, startingCaretPosition);
|
216 |
txt.setText(anterior + entries.get(currentEntry)); |
217 |
} |
218 |
}); |
219 |
addKeyListener(new java.awt.event.KeyAdapter() {
|
220 |
public void keyPressed(KeyEvent e) { |
221 |
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { |
222 |
if (startingCaretPosition >= txt.getCaretPosition()) {
|
223 |
int caretPos = txt.getCaretPosition();
|
224 |
String text = txt.getText();
|
225 |
text = text.substring(0, caretPos) + " " + |
226 |
text.substring(caretPos); |
227 |
txt.setText(text); |
228 |
|
229 |
txt.setCaretPosition(caretPos); |
230 |
|
231 |
}else{
|
232 |
} |
233 |
} |
234 |
|
235 |
|
236 |
} |
237 |
|
238 |
public void keyReleased(KeyEvent e) { |
239 |
if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
240 |
String texto = txt.getText();
|
241 |
String response = texto.substring(startingCaretPosition);
|
242 |
|
243 |
listenerSupport.callAcceptResponse(response); |
244 |
|
245 |
if (response.trim().length() > 0) { |
246 |
entries.add(response.trim()); |
247 |
} |
248 |
|
249 |
currentEntry = -1;
|
250 |
|
251 |
} else if (e.getKeyCode() == KeyEvent.VK_UP) { |
252 |
if (entries.size() == 0) { |
253 |
return;
|
254 |
} |
255 |
|
256 |
if (currentEntry == -1) { |
257 |
currentEntry = entries.size() - 1;
|
258 |
} else {
|
259 |
currentEntry--; |
260 |
|
261 |
if (currentEntry < 0) { |
262 |
currentEntry = 0;
|
263 |
} |
264 |
} |
265 |
|
266 |
putEntry(); |
267 |
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) { |
268 |
if (entries.size() == 0) { |
269 |
return;
|
270 |
} |
271 |
|
272 |
if (currentEntry != -1) { |
273 |
currentEntry++; |
274 |
|
275 |
if (currentEntry >= entries.size()) {
|
276 |
currentEntry = entries.size() - 1;
|
277 |
} |
278 |
} |
279 |
|
280 |
putEntry(); |
281 |
} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { |
282 |
txt.setText(txt.getText()); |
283 |
listenerSupport.callAcceptResponse(null);
|
284 |
} |
285 |
} |
286 |
|
287 |
private void putEntry() { |
288 |
String anterior = txt.getText();
|
289 |
anterior = anterior.substring(0, startingCaretPosition);
|
290 |
txt.setText(anterior + entries.get(currentEntry)); |
291 |
} |
292 |
}); |
293 |
} |
294 |
|
295 |
return txt;
|
296 |
} |
297 |
|
298 |
/**
|
299 |
* A?ade un texto a la consola
|
300 |
*
|
301 |
* @param text Texto que se a?ade a la consola
|
302 |
*/
|
303 |
public void addText(String text,int type) { |
304 |
txt.setText(txt.getText() + text); |
305 |
txt.setCaretPosition(txt.getText().length()); |
306 |
startingCaretPosition = txt.getText().length(); |
307 |
|
308 |
} |
309 |
/**
|
310 |
* A?ade un texto a la consola que es tomado como si lo
|
311 |
* hubiese escrito el usuario. Formar? parte de la respuesta
|
312 |
*
|
313 |
* @param text Texto que se a?ade a la consola
|
314 |
*/
|
315 |
public void addResponseText(String text) { |
316 |
txt.setText(txt.getText() + text); |
317 |
txt.setCaretPosition(txt.getText().length()); |
318 |
} |
319 |
|
320 |
/**
|
321 |
* This method initializes jScrollPane
|
322 |
*
|
323 |
* @return javax.swing.JScrollPane
|
324 |
*/
|
325 |
/*private JScrollPane getJScrollPane() {
|
326 |
if (jScrollPane == null) {
|
327 |
jScrollPane = new JScrollPane();
|
328 |
jScrollPane.setViewportView(getTxt());
|
329 |
}
|
330 |
|
331 |
return jScrollPane;
|
332 |
}
|
333 |
*/
|
334 |
public void addResponseListener(ResponseListener listener) { |
335 |
listenerSupport.addResponseListener(listener); |
336 |
} |
337 |
public void removeResponseListener(ResponseListener listener) { |
338 |
listenerSupport.removeResponseListener(listener); |
339 |
} |
340 |
|
341 |
/**
|
342 |
* Useful to know from where it comes a key event. See CADExtension
|
343 |
* @param name
|
344 |
*/
|
345 |
public void setJTextName(String name) |
346 |
{ |
347 |
txt.setName(name); |
348 |
} |
349 |
|
350 |
public void keyPressed(KeyEvent e) { |
351 |
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { |
352 |
if (startingCaretPosition >= txt.getCaretPosition()) {
|
353 |
int caretPos = txt.getCaretPosition();
|
354 |
String text = txt.getText();
|
355 |
text = text.substring(0, caretPos) + " " + |
356 |
text.substring(caretPos); |
357 |
txt.setText(text); |
358 |
|
359 |
txt.setCaretPosition(caretPos); |
360 |
|
361 |
}else{
|
362 |
} |
363 |
} |
364 |
|
365 |
if (redispatch_key_events) {
|
366 |
Container cont = this.getParent(); |
367 |
if (cont != null && cont instanceof Component) { |
368 |
Component comp = (Component) cont; |
369 |
comp.dispatchEvent(e); |
370 |
} |
371 |
} |
372 |
} |
373 |
|
374 |
public void keyReleased(KeyEvent e) { |
375 |
if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
376 |
String texto = txt.getText();
|
377 |
String response = texto.substring(startingCaretPosition);
|
378 |
|
379 |
listenerSupport.callAcceptResponse(response); |
380 |
|
381 |
if (response.trim().length() > 0) { |
382 |
entries.add(response.trim()); |
383 |
} |
384 |
|
385 |
currentEntry = -1;
|
386 |
|
387 |
} else if (e.getKeyCode() == KeyEvent.VK_UP) { |
388 |
if (entries.size() == 0) { |
389 |
return;
|
390 |
} |
391 |
|
392 |
if (currentEntry == -1) { |
393 |
currentEntry = entries.size() - 1;
|
394 |
} else {
|
395 |
currentEntry--; |
396 |
|
397 |
if (currentEntry < 0) { |
398 |
currentEntry = 0;
|
399 |
} |
400 |
} |
401 |
|
402 |
putEntry(); |
403 |
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) { |
404 |
if (entries.size() == 0) { |
405 |
return;
|
406 |
} |
407 |
|
408 |
if (currentEntry != -1) { |
409 |
currentEntry++; |
410 |
|
411 |
if (currentEntry >= entries.size()) {
|
412 |
currentEntry = entries.size() - 1;
|
413 |
} |
414 |
} |
415 |
|
416 |
putEntry(); |
417 |
} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { |
418 |
txt.setText(txt.getText()); |
419 |
listenerSupport.callAcceptResponse(null);
|
420 |
} |
421 |
} |
422 |
private void putEntry() { |
423 |
String anterior = txt.getText();
|
424 |
anterior = anterior.substring(0, startingCaretPosition);
|
425 |
txt.setText(anterior + entries.get(currentEntry)); |
426 |
} |
427 |
|
428 |
|
429 |
} |