Statistics
| Revision:

root / trunk / libraries / libIverUtiles / src / com / iver / utiles / console / JConsole.java @ 4900

History | View | Annotate | Download (9.61 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.Dimension;
49
import java.awt.event.KeyEvent;
50
import java.util.ArrayList;
51

    
52
import javax.swing.FocusManager;
53
import javax.swing.JPanel;
54
import javax.swing.event.CaretEvent;
55
import javax.swing.event.CaretListener;
56

    
57
import com.iver.utiles.console.jedit.ConsoleInputHandler;
58
import com.iver.utiles.console.jedit.JEditTextArea;
59
import com.iver.utiles.console.jedit.TextAreaDefaults;
60
import com.iver.utiles.console.jedit.TokenMarker;
61

    
62

    
63
/**
64
 * DOCUMENT ME!
65
 *
66
 * @author Fernando Gonz?lez Cort?s
67
 */
68
public class JConsole extends JPanel {
69
        private JEditTextArea txt;
70
        private int startingCaretPosition = 0;
71
        private ArrayList entries = new ArrayList();
72
        private int currentEntry = -1;
73
        //private JScrollPane jScrollPane = null;
74
        private ResponseListenerSupport listenerSupport = new ResponseListenerSupport();
75
        public static int MESSAGE=0;
76
        public static int COMMAND=1;
77
        public static int INSERT=2;
78
        public static int ERROR=3;
79
        private static TextAreaDefaults defaults;
80
        /**
81
         * This is the default constructor
82
         */
83
        public JConsole() {
84
                super();
85
                initialize();
86
                defaults=TextAreaDefaults.getDefaults();
87
                ((ConsoleInputHandler)defaults.inputHandler).addConsoleListener(this);
88
        }
89

    
90
        /**
91
         * This method initializes this
92
         */
93
        private void initialize() {
94
                this.setLayout(new BorderLayout());
95
                this.setSize(300, 200);
96
                this.setPreferredSize(new Dimension(300,200));
97
                this.add(getTxt(), java.awt.BorderLayout.CENTER);
98
        }
99

    
100
        public void setTokenMarker(TokenMarker tm){
101
                txt.setTokenMarker(tm);
102
        }
103
        /**
104
         * Obtiene una referencia al JTextArea de la consola
105
         *
106
         * @return javax.swing.JTextArea
107
         */
108
        public JEditTextArea getTxt() {
109
                if (txt == null) {
110
                        txt = new JEditTextArea();
111
                        //txt.setTokenMarker(new JavaTokenMarker());
112
                        /*        txt.addCaretListener(new CaretListener() {
113
                                        public void caretUpdate(CaretEvent e) {
114
                                                if (txt.getCaretPosition() < startingCaretPosition) {
115
                                                        if (startingCaretPosition <= txt.getText().length()) {
116
                                                                txt.setCaretPosition(startingCaretPosition);
117
                                                        }
118
                                                }
119
                                        }
120
                                });
121
                        txt.addKeyListener(new KeyAdapter() {
122
                                public void keyPressed(KeyEvent e) {
123
                                        if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
124
                                                if (startingCaretPosition >= txt.getCaretPosition()) {
125
                                                        int caretPos = txt.getCaretPosition();
126
                                                        String text = txt.getText();
127
                                                        text = text.substring(0, caretPos) + " " +
128
                                                                text.substring(caretPos);
129
                                                        txt.setText(text);
130

131
                                                        txt.setCaretPosition(caretPos);
132

133
                                                }else{
134
                                                }
135
                                        }
136

137

138
                                }
139

140
                                public void keyReleased(KeyEvent e) {
141
                                        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
142
                                                String texto = txt.getText();
143
                                                String response = texto.substring(startingCaretPosition);
144

145
                                                listenerSupport.callAcceptResponse(response);
146

147
                                                if (response.trim().length() > 0) {
148
                                                        entries.add(response.trim());
149
                                                }
150

151
                                                currentEntry = -1;
152

153
                                        } else if (e.getKeyCode() == KeyEvent.VK_UP) {
154
                                                if (entries.size() == 0) {
155
                                                        return;
156
                                                }
157

158
                                                if (currentEntry == -1) {
159
                                                        currentEntry = entries.size() - 1;
160
                                                } else {
161
                                                        currentEntry--;
162

163
                                                        if (currentEntry < 0) {
164
                                                                currentEntry = 0;
165
                                                        }
166
                                                }
167

168
                                                putEntry();
169
                                        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
170
                                                if (entries.size() == 0) {
171
                                                        return;
172
                                                }
173

174
                                                if (currentEntry != -1) {
175
                                                        currentEntry++;
176

177
                                                        if (currentEntry >= entries.size()) {
178
                                                                currentEntry = entries.size() - 1;
179
                                                        }
180
                                                }
181

182
                                                putEntry();
183
                                        } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
184
                                                txt.setText(txt.getText());
185
                                                listenerSupport.callAcceptResponse(null);
186
                                        }
187
                                }
188

189
                                private void putEntry() {
190
                                        String anterior = txt.getText();
191
                                        anterior = anterior.substring(0, startingCaretPosition);
192
                                        txt.setText(anterior + entries.get(currentEntry));
193
                                }
194
                        });
195
                        addKeyListener(new java.awt.event.KeyAdapter() {
196
                                        public void keyPressed(KeyEvent e) {
197
                                                if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
198
                                                        if (startingCaretPosition >= txt.getCaretPosition()) {
199
                                                                int caretPos = txt.getCaretPosition();
200
                                                                String text = txt.getText();
201
                                                                text = text.substring(0, caretPos) + " " +
202
                                                                        text.substring(caretPos);
203
                                                                txt.setText(text);
204

205
                                                                txt.setCaretPosition(caretPos);
206

207
                                                        }else{
208
                                                        }
209
                                                }
210

211

212
                                        }
213

214
                                        public void keyReleased(KeyEvent e) {
215
                                                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
216
                                                        String texto = txt.getText();
217
                                                        String response = texto.substring(startingCaretPosition);
218

219
                                                        listenerSupport.callAcceptResponse(response);
220

221
                                                        if (response.trim().length() > 0) {
222
                                                                entries.add(response.trim());
223
                                                        }
224

225
                                                        currentEntry = -1;
226

227
                                                } else if (e.getKeyCode() == KeyEvent.VK_UP) {
228
                                                        if (entries.size() == 0) {
229
                                                                return;
230
                                                        }
231

232
                                                        if (currentEntry == -1) {
233
                                                                currentEntry = entries.size() - 1;
234
                                                        } else {
235
                                                                currentEntry--;
236

237
                                                                if (currentEntry < 0) {
238
                                                                        currentEntry = 0;
239
                                                                }
240
                                                        }
241

242
                                                        putEntry();
243
                                                } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
244
                                                        if (entries.size() == 0) {
245
                                                                return;
246
                                                        }
247

248
                                                        if (currentEntry != -1) {
249
                                                                currentEntry++;
250

251
                                                                if (currentEntry >= entries.size()) {
252
                                                                        currentEntry = entries.size() - 1;
253
                                                                }
254
                                                        }
255

256
                                                        putEntry();
257
                                                } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
258
                                                        txt.setText(txt.getText());
259
                                                        listenerSupport.callAcceptResponse(null);
260
                                                }
261
                                        }
262

263
                                        private void putEntry() {
264
                                                String anterior = txt.getText();
265
                                                anterior = anterior.substring(0, startingCaretPosition);
266
                                                txt.setText(anterior + entries.get(currentEntry));
267
                                        }
268
                                });*/
269
                }
270

    
271
                return txt;
272
        }
273

    
274
        /**
275
         * A?ade un texto a la consola
276
         *
277
         * @param text Texto que se a?ade a la consola
278
         */
279
        public void addText(String text,int type) {
280
                txt.setText(txt.getText() + text);
281
                txt.setCaretPosition(txt.getText().length());
282
                startingCaretPosition = txt.getText().length();
283

    
284
        }
285
        /**
286
         * A?ade un texto a la consola que es tomado como si lo
287
         * hubiese escrito el usuario. Formar? parte de la respuesta
288
         *
289
         * @param text Texto que se a?ade a la consola
290
         */
291
        public void addResponseText(String text) {
292
                txt.setText(txt.getText() + text);
293
                txt.setCaretPosition(txt.getText().length());
294
        }
295

    
296
        /**
297
         * This method initializes jScrollPane
298
         *
299
         * @return javax.swing.JScrollPane
300
         */
301
        /*private JScrollPane getJScrollPane() {
302
                if (jScrollPane == null) {
303
                        jScrollPane = new JScrollPane();
304
                        jScrollPane.setViewportView(getTxt());
305
                }
306

307
                return jScrollPane;
308
        }
309
        */
310
        public void addResponseListener(ResponseListener listener) {
311
                listenerSupport.addResponseListener(listener);
312
        }
313
        public void removeResponseListener(ResponseListener listener) {
314
                listenerSupport.removeResponseListener(listener);
315
        }
316

    
317
        /**
318
         * Useful to know from where it comes a key event. See CADExtension
319
         * @param name
320
         */
321
        public void setJTextName(String name)
322
        {
323
                txt.setName(name);
324
        }
325

    
326
        public void keyPressed(KeyEvent e) {
327
                if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
328
                        if (startingCaretPosition >= txt.getCaretPosition()) {
329
                                int caretPos = txt.getCaretPosition();
330
                                String text = txt.getText();
331
                                text = text.substring(0, caretPos) + " " +
332
                                        text.substring(caretPos);
333
                                txt.setText(text);
334

    
335
                                txt.setCaretPosition(caretPos);
336

    
337
                        }else{
338
                        }
339
                }
340
        }
341

    
342
        public void keyReleased(KeyEvent e) {
343
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
344
                        String texto = txt.getText();
345
                        String response = texto.substring(startingCaretPosition);
346

    
347
                        listenerSupport.callAcceptResponse(response);
348

    
349
                        if (response.trim().length() > 0) {
350
                                entries.add(response.trim());
351
                        }
352

    
353
                        currentEntry = -1;
354

    
355
                } else if (e.getKeyCode() == KeyEvent.VK_UP) {
356
                        if (entries.size() == 0) {
357
                                return;
358
                        }
359

    
360
                        if (currentEntry == -1) {
361
                                currentEntry = entries.size() - 1;
362
                        } else {
363
                                currentEntry--;
364

    
365
                                if (currentEntry < 0) {
366
                                        currentEntry = 0;
367
                                }
368
                        }
369

    
370
                        putEntry();
371
                } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
372
                        if (entries.size() == 0) {
373
                                return;
374
                        }
375

    
376
                        if (currentEntry != -1) {
377
                                currentEntry++;
378

    
379
                                if (currentEntry >= entries.size()) {
380
                                        currentEntry = entries.size() - 1;
381
                                }
382
                        }
383

    
384
                        putEntry();
385
                } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
386
                        txt.setText(txt.getText());
387
                        listenerSupport.callAcceptResponse(null);
388
                }
389
        }
390
        private void putEntry() {
391
                String anterior = txt.getText();
392
                anterior = anterior.substring(0, startingCaretPosition);
393
                txt.setText(anterior + entries.get(currentEntry));
394
        }
395

    
396
}