Revision 4893 trunk/libraries/libIverUtiles/src/com/iver/utiles/console/JConsole.java

View differences:

JConsole.java
45 45
package com.iver.utiles.console;
46 46

  
47 47
import java.awt.BorderLayout;
48
import java.awt.Dimension;
48 49
import java.awt.event.KeyEvent;
49

  
50 50
import java.util.ArrayList;
51 51

  
52 52
import javax.swing.JPanel;
53 53
import javax.swing.JScrollPane;
54
import javax.swing.JTextArea;
55 54
import javax.swing.event.CaretEvent;
56 55
import javax.swing.event.CaretListener;
57 56

  
57
import com.iver.utiles.console.jedit.JEditTextArea;
58
import com.iver.utiles.console.jedit.JavaTokenMarker;
59
import com.iver.utiles.console.jedit.TokenMarker;
58 60

  
61

  
59 62
/**
60 63
 * DOCUMENT ME!
61 64
 *
62 65
 * @author Fernando Gonz?lez Cort?s
63 66
 */
64 67
public class JConsole extends JPanel {
65
	private JTextArea txt = null;
68
	private JEditTextArea txt;
66 69
	private int startingCaretPosition = 0;
67 70
	private ArrayList entries = new ArrayList();
68 71
	private int currentEntry = -1;
69
	private JScrollPane jScrollPane = null;
72
	//private JScrollPane jScrollPane = null;
70 73
	private ResponseListenerSupport listenerSupport = new ResponseListenerSupport();
71

  
74
	public static int MESSAGE=0;
75
	public static int COMMAND=1;
76
	public static int INSERT=2;
77
	public static int ERROR=3;
72 78
	/**
73 79
	 * This is the default constructor
74 80
	 */
......
83 89
	private void initialize() {
84 90
		this.setLayout(new BorderLayout());
85 91
		this.setSize(300, 200);
86
		this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
92
		this.setPreferredSize(new Dimension(300,200));
93
		this.add(getTxt(), java.awt.BorderLayout.CENTER);
87 94
	}
88 95

  
96
	public void setTokenMarker(TokenMarker tm){
97
		txt.setTokenMarker(tm);
98
	}
89 99
	/**
90 100
	 * Obtiene una referencia al JTextArea de la consola
91 101
	 *
92 102
	 * @return javax.swing.JTextArea
93 103
	 */
94
	public JTextArea getTxt() {
104
	public JEditTextArea getTxt() {
95 105
		if (txt == null) {
96
			txt = new JTextArea();
106
			txt = new JEditTextArea();
107
			//txt.setTokenMarker(new JavaTokenMarker());
97 108
			txt.addCaretListener(new CaretListener() {
98 109
					public void caretUpdate(CaretEvent e) {
99 110
						if (txt.getCaretPosition() < startingCaretPosition) {
......
104 115
					}
105 116
				});
106 117
			txt.addKeyListener(new java.awt.event.KeyAdapter() {
118
				public void keyPressed(KeyEvent e) {
119
					if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
120
						if (startingCaretPosition >= txt.getCaretPosition()) {
121
							int caretPos = txt.getCaretPosition();
122
							String text = txt.getText();
123
							text = text.substring(0, caretPos) + " " +
124
								text.substring(caretPos);
125
							txt.setText(text);
126

  
127
							txt.setCaretPosition(caretPos);
128

  
129
						}else{
130
						}
131
					}
132

  
133

  
134
				}
135

  
136
				public void keyReleased(KeyEvent e) {
137
					if (e.getKeyCode() == KeyEvent.VK_ENTER) {
138
						String texto = txt.getText();
139
						String response = texto.substring(startingCaretPosition);
140

  
141
						listenerSupport.callAcceptResponse(response);
142

  
143
						if (response.trim().length() > 0) {
144
							entries.add(response.trim());
145
						}
146

  
147
						currentEntry = -1;
148

  
149
					} else if (e.getKeyCode() == KeyEvent.VK_UP) {
150
						if (entries.size() == 0) {
151
							return;
152
						}
153

  
154
						if (currentEntry == -1) {
155
							currentEntry = entries.size() - 1;
156
						} else {
157
							currentEntry--;
158

  
159
							if (currentEntry < 0) {
160
								currentEntry = 0;
161
							}
162
						}
163

  
164
						putEntry();
165
					} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
166
						if (entries.size() == 0) {
167
							return;
168
						}
169

  
170
						if (currentEntry != -1) {
171
							currentEntry++;
172

  
173
							if (currentEntry >= entries.size()) {
174
								currentEntry = entries.size() - 1;
175
							}
176
						}
177

  
178
						putEntry();
179
					} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
180
						txt.setText(txt.getText());
181
						listenerSupport.callAcceptResponse(null);
182
					}
183
				}
184

  
185
				private void putEntry() {
186
					String anterior = txt.getText();
187
					anterior = anterior.substring(0, startingCaretPosition);
188
					txt.setText(anterior + entries.get(currentEntry));
189
				}
190
			});
191
			addKeyListener(new java.awt.event.KeyAdapter() {
107 192
					public void keyPressed(KeyEvent e) {
108 193
						if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
109 194
							if (startingCaretPosition >= txt.getCaretPosition()) {
......
112 197
								text = text.substring(0, caretPos) + " " +
113 198
									text.substring(caretPos);
114 199
								txt.setText(text);
200

  
115 201
								txt.setCaretPosition(caretPos);
202

  
203
							}else{
116 204
							}
117 205
						}
206

  
207

  
118 208
					}
119 209

  
120 210
					public void keyReleased(KeyEvent e) {
......
129 219
							}
130 220

  
131 221
							currentEntry = -1;
222

  
132 223
						} else if (e.getKeyCode() == KeyEvent.VK_UP) {
133 224
							if (entries.size() == 0) {
134 225
								return;
......
181 272
	 *
182 273
	 * @param text Texto que se a?ade a la consola
183 274
	 */
184
	public void addText(String text) {
275
	public void addText(String text,int type) {
185 276
		txt.setText(txt.getText() + text);
186 277
		txt.setCaretPosition(txt.getText().length());
187 278
		startingCaretPosition = txt.getText().length();
279

  
188 280
	}
189

  
190 281
	/**
191 282
	 * A?ade un texto a la consola que es tomado como si lo
192 283
	 * hubiese escrito el usuario. Formar? parte de la respuesta
......
203 294
	 *
204 295
	 * @return javax.swing.JScrollPane
205 296
	 */
206
	private JScrollPane getJScrollPane() {
297
	/*private JScrollPane getJScrollPane() {
207 298
		if (jScrollPane == null) {
208 299
			jScrollPane = new JScrollPane();
209 300
			jScrollPane.setViewportView(getTxt());
......
211 302

  
212 303
		return jScrollPane;
213 304
	}
305
	*/
214 306
	public void addResponseListener(ResponseListener listener) {
215 307
		listenerSupport.addResponseListener(listener);
216 308
	}
217 309
	public void removeResponseListener(ResponseListener listener) {
218 310
		listenerSupport.removeResponseListener(listener);
219 311
	}
220
	
312

  
221 313
	/**
222 314
	 * Useful to know from where it comes a key event. See CADExtension
223 315
	 * @param name
224 316
	 */
225 317
	public void setJTextName(String name)
226 318
	{
227
		getTxt().setName(name);
319
		txt.setName(name);
228 320
	}
321

  
229 322
}

Also available in: Unified diff