svn-gvsig-desktop / trunk / libraries / libIverUtiles / src / com / iver / utiles / console / JConsole.java @ 1836
History | View | Annotate | Download (5.51 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.event.KeyEvent; |
49 |
|
50 |
import java.util.ArrayList; |
51 |
|
52 |
import javax.swing.JPanel; |
53 |
import javax.swing.JScrollPane; |
54 |
import javax.swing.JTextArea; |
55 |
import javax.swing.event.CaretEvent; |
56 |
import javax.swing.event.CaretListener; |
57 |
|
58 |
|
59 |
/**
|
60 |
* DOCUMENT ME!
|
61 |
*
|
62 |
* @author Fernando Gonz?lez Cort?s
|
63 |
*/
|
64 |
public class JConsole extends JPanel { |
65 |
private JTextArea txt = null; |
66 |
private int startingCaretPosition = 0; |
67 |
private ArrayList entries = new ArrayList(); |
68 |
private int currentEntry = -1; |
69 |
private JScrollPane jScrollPane = null; |
70 |
private ResponseListenerSupport listenerSupport = new ResponseListenerSupport(); |
71 |
|
72 |
/**
|
73 |
* This is the default constructor
|
74 |
*/
|
75 |
public JConsole() {
|
76 |
super();
|
77 |
initialize(); |
78 |
} |
79 |
|
80 |
/**
|
81 |
* This method initializes this
|
82 |
*/
|
83 |
private void initialize() { |
84 |
this.setLayout(new BorderLayout()); |
85 |
this.setSize(300, 200); |
86 |
this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
|
87 |
} |
88 |
|
89 |
/**
|
90 |
* Obtiene una referencia al JTextArea de la consola
|
91 |
*
|
92 |
* @return javax.swing.JTextArea
|
93 |
*/
|
94 |
public JTextArea getTxt() { |
95 |
if (txt == null) { |
96 |
txt = new JTextArea(); |
97 |
txt.addCaretListener(new CaretListener() { |
98 |
public void caretUpdate(CaretEvent e) { |
99 |
if (txt.getCaretPosition() < startingCaretPosition) {
|
100 |
if (startingCaretPosition <= txt.getText().length()) {
|
101 |
txt.setCaretPosition(startingCaretPosition); |
102 |
} |
103 |
} |
104 |
} |
105 |
}); |
106 |
txt.addKeyListener(new java.awt.event.KeyAdapter() {
|
107 |
public void keyPressed(KeyEvent e) { |
108 |
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { |
109 |
if (startingCaretPosition >= txt.getCaretPosition()) {
|
110 |
int caretPos = txt.getCaretPosition();
|
111 |
String text = txt.getText();
|
112 |
text = text.substring(0, caretPos) + " " + |
113 |
text.substring(caretPos); |
114 |
txt.setText(text); |
115 |
txt.setCaretPosition(caretPos); |
116 |
} |
117 |
} |
118 |
} |
119 |
|
120 |
public void keyReleased(KeyEvent e) { |
121 |
if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
122 |
String texto = txt.getText();
|
123 |
String response = texto.substring(startingCaretPosition);
|
124 |
|
125 |
listenerSupport.callAcceptResponse(response); |
126 |
|
127 |
if (response.trim().length() > 0) { |
128 |
entries.add(response.trim()); |
129 |
} |
130 |
|
131 |
currentEntry = -1;
|
132 |
} else if (e.getKeyCode() == KeyEvent.VK_UP) { |
133 |
if (entries.size() == 0) { |
134 |
return;
|
135 |
} |
136 |
|
137 |
if (currentEntry == -1) { |
138 |
currentEntry = entries.size() - 1;
|
139 |
} else {
|
140 |
currentEntry--; |
141 |
|
142 |
if (currentEntry < 0) { |
143 |
currentEntry = 0;
|
144 |
} |
145 |
} |
146 |
|
147 |
putEntry(); |
148 |
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) { |
149 |
if (entries.size() == 0) { |
150 |
return;
|
151 |
} |
152 |
|
153 |
if (currentEntry != -1) { |
154 |
currentEntry++; |
155 |
|
156 |
if (currentEntry >= entries.size()) {
|
157 |
currentEntry = entries.size() - 1;
|
158 |
} |
159 |
} |
160 |
|
161 |
putEntry(); |
162 |
} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { |
163 |
txt.setText(txt.getText()); |
164 |
listenerSupport.callAcceptResponse(null);
|
165 |
} |
166 |
} |
167 |
|
168 |
private void putEntry() { |
169 |
String anterior = txt.getText();
|
170 |
anterior = anterior.substring(0, startingCaretPosition);
|
171 |
txt.setText(anterior + entries.get(currentEntry)); |
172 |
} |
173 |
}); |
174 |
} |
175 |
|
176 |
return txt;
|
177 |
} |
178 |
|
179 |
/**
|
180 |
* A?ade un texto a la consola
|
181 |
*
|
182 |
* @param text Texto que se a?ade a la consola
|
183 |
*/
|
184 |
public void addText(String text) { |
185 |
txt.setText(txt.getText() + text); |
186 |
txt.setCaretPosition(txt.getText().length()); |
187 |
startingCaretPosition = txt.getText().length(); |
188 |
} |
189 |
|
190 |
/**
|
191 |
* A?ade un texto a la consola que es tomado como si lo
|
192 |
* hubiese escrito el usuario. Formar? parte de la respuesta
|
193 |
*
|
194 |
* @param text Texto que se a?ade a la consola
|
195 |
*/
|
196 |
public void addResponseText(String text) { |
197 |
txt.setText(txt.getText() + text); |
198 |
txt.setCaretPosition(txt.getText().length()); |
199 |
} |
200 |
|
201 |
/**
|
202 |
* This method initializes jScrollPane
|
203 |
*
|
204 |
* @return javax.swing.JScrollPane
|
205 |
*/
|
206 |
private JScrollPane getJScrollPane() { |
207 |
if (jScrollPane == null) { |
208 |
jScrollPane = new JScrollPane(); |
209 |
jScrollPane.setViewportView(getTxt()); |
210 |
} |
211 |
|
212 |
return jScrollPane;
|
213 |
} |
214 |
public void addResponseListener(ResponseListener listener) { |
215 |
listenerSupport.addResponseListener(listener); |
216 |
} |
217 |
public void removeResponseListener(ResponseListener listener) { |
218 |
listenerSupport.removeResponseListener(listener); |
219 |
} |
220 |
} |