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