Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / console / JConsole.java @ 40561

History | View | Annotate | Download (10.2 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
package org.gvsig.utils.console;
26

    
27
import java.awt.BorderLayout;
28
import java.awt.Component;
29
import java.awt.Container;
30
import java.awt.Dimension;
31
import java.awt.event.KeyAdapter;
32
import java.awt.event.KeyEvent;
33
import java.util.ArrayList;
34

    
35
import javax.swing.JPanel;
36
import javax.swing.event.CaretEvent;
37
import javax.swing.event.CaretListener;
38

    
39
import org.gvsig.utils.console.jedit.ConsoleInputHandler;
40
import org.gvsig.utils.console.jedit.JEditTextArea;
41
import org.gvsig.utils.console.jedit.TextAreaDefaults;
42
import org.gvsig.utils.console.jedit.TokenMarker;
43

    
44

    
45

    
46
/**
47
 * DOCUMENT ME!
48
 *
49
 * @author Fernando Gonz?lez Cort?s
50
 */
51
public class JConsole extends JPanel {
52
        private JEditTextArea txt;
53
        private int startingCaretPosition = 0;
54
        private ArrayList entries = new ArrayList();
55
        private int currentEntry = -1;
56
        //private JScrollPane jScrollPane = null;
57
        private ResponseListenerSupport listenerSupport = new ResponseListenerSupport();
58
        public static int MESSAGE=0;
59
        public static int COMMAND=1;
60
        public static int INSERT=2;
61
        public static int ERROR=3;
62
        private static TextAreaDefaults defaults;
63
        private JConsole theContainer = null;
64
        
65
        private boolean redispatch_key_events = false;
66
        /**
67
         * This is the default constructor
68
         */
69
        public JConsole() {
70
                super();
71
                initialize();
72
                defaults=TextAreaDefaults.getDefaults();
73
                ((ConsoleInputHandler)defaults.inputHandler).addConsoleListener(this);
74
                theContainer = this;
75
        }
76
        
77

    
78
        /**
79
         * With this constructor, we decide whether the key events
80
         * are re-dispatched to the parent of this component.
81
         * This is necessary to prevent the console from consuming
82
         * key events (short-cuts) while editing. the component
83
         * which originally receives the key event is the JEditTextArea
84
         * but this console is receiving also key-pressed notifications.
85
         * 
86
         * @param redisp_key_events
87
         */
88
        public JConsole(boolean redisp_key_events) {
89
            this();
90
            redispatch_key_events = redisp_key_events;
91
        }
92

    
93

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

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

    
135
                                                        txt.setCaretPosition(caretPos);
136

    
137
                                                }else{
138
                                                }
139
                                        }
140

    
141

    
142
                                }
143

    
144
                                public void keyReleased(KeyEvent e) {
145
                                        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
146
                                                String texto = txt.getText();
147
                                                String response = texto.substring(startingCaretPosition);
148

    
149
                                                listenerSupport.callAcceptResponse(response);
150

    
151
                                                if (response.trim().length() > 0) {
152
                                                        entries.add(response.trim());
153
                                                }
154

    
155
                                                currentEntry = -1;
156

    
157
                                        } else if (e.getKeyCode() == KeyEvent.VK_UP) {
158
                                                if (entries.size() == 0) {
159
                                                        return;
160
                                                }
161

    
162
                                                if (currentEntry == -1) {
163
                                                        currentEntry = entries.size() - 1;
164
                                                } else {
165
                                                        currentEntry--;
166

    
167
                                                        if (currentEntry < 0) {
168
                                                                currentEntry = 0;
169
                                                        }
170
                                                }
171

    
172
                                                putEntry();
173
                                        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
174
                                                if (entries.size() == 0) {
175
                                                        return;
176
                                                }
177

    
178
                                                if (currentEntry != -1) {
179
                                                        currentEntry++;
180

    
181
                                                        if (currentEntry >= entries.size()) {
182
                                                                currentEntry = entries.size() - 1;
183
                                                        }
184
                                                }
185

    
186
                                                putEntry();
187
                                        } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
188
                                                txt.setText(txt.getText());
189
                                                listenerSupport.callAcceptResponse(null);
190
                                        }
191
                                }
192

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

    
209
                                                                txt.setCaretPosition(caretPos);
210

    
211
                                                        }else{
212
                                                        }
213
                                                }
214

    
215

    
216
                                        }
217

    
218
                                        public void keyReleased(KeyEvent e) {
219
                                                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
220
                                                        String texto = txt.getText();
221
                                                        String response = texto.substring(startingCaretPosition);
222

    
223
                                                        listenerSupport.callAcceptResponse(response);
224

    
225
                                                        if (response.trim().length() > 0) {
226
                                                                entries.add(response.trim());
227
                                                        }
228

    
229
                                                        currentEntry = -1;
230

    
231
                                                } else if (e.getKeyCode() == KeyEvent.VK_UP) {
232
                                                        if (entries.size() == 0) {
233
                                                                return;
234
                                                        }
235

    
236
                                                        if (currentEntry == -1) {
237
                                                                currentEntry = entries.size() - 1;
238
                                                        } else {
239
                                                                currentEntry--;
240

    
241
                                                                if (currentEntry < 0) {
242
                                                                        currentEntry = 0;
243
                                                                }
244
                                                        }
245

    
246
                                                        putEntry();
247
                                                } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
248
                                                        if (entries.size() == 0) {
249
                                                                return;
250
                                                        }
251

    
252
                                                        if (currentEntry != -1) {
253
                                                                currentEntry++;
254

    
255
                                                                if (currentEntry >= entries.size()) {
256
                                                                        currentEntry = entries.size() - 1;
257
                                                                }
258
                                                        }
259

    
260
                                                        putEntry();
261
                                                } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
262
                                                        txt.setText(txt.getText());
263
                                                        listenerSupport.callAcceptResponse(null);
264
                                                }
265
                                        }
266

    
267
                                        private void putEntry() {
268
                                                String anterior = txt.getText();
269
                                                anterior = anterior.substring(0, startingCaretPosition);
270
                                                txt.setText(anterior + entries.get(currentEntry));
271
                                        }
272
                                });
273
                }
274

    
275
                return txt;
276
        }
277

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

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

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

311
                return jScrollPane;
312
        }
313
        */
314
        public void addResponseListener(ResponseListener listener) {
315
                listenerSupport.addResponseListener(listener);
316
        }
317
        public void removeResponseListener(ResponseListener listener) {
318
                listenerSupport.removeResponseListener(listener);
319
        }
320

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

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

    
339
                                txt.setCaretPosition(caretPos);
340

    
341
                        }else{
342
                        }
343
                }
344
                
345
                if (redispatch_key_events) {
346
                Container cont = this.getParent();
347
                if (cont != null && cont instanceof Component) {
348
                    Component comp = (Component) cont;
349
                    comp.dispatchEvent(e);
350
                }
351
                }
352
        }
353

    
354
        public void keyReleased(KeyEvent e) {
355
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
356
                        String texto = txt.getText();
357
                        String response = texto.substring(startingCaretPosition);
358

    
359
                        listenerSupport.callAcceptResponse(response);
360

    
361
                        if (response.trim().length() > 0) {
362
                                entries.add(response.trim());
363
                        }
364

    
365
                        currentEntry = -1;
366

    
367
                } else if (e.getKeyCode() == KeyEvent.VK_UP) {
368
                        if (entries.size() == 0) {
369
                                return;
370
                        }
371

    
372
                        if (currentEntry == -1) {
373
                                currentEntry = entries.size() - 1;
374
                        } else {
375
                                currentEntry--;
376

    
377
                                if (currentEntry < 0) {
378
                                        currentEntry = 0;
379
                                }
380
                        }
381

    
382
                        putEntry();
383
                } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
384
                        if (entries.size() == 0) {
385
                                return;
386
                        }
387

    
388
                        if (currentEntry != -1) {
389
                                currentEntry++;
390

    
391
                                if (currentEntry >= entries.size()) {
392
                                        currentEntry = entries.size() - 1;
393
                                }
394
                        }
395

    
396
                        putEntry();
397
                } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
398
                        txt.setText(txt.getText());
399
                        listenerSupport.callAcceptResponse(null);
400
                }
401
        }
402
        private void putEntry() {
403
                String anterior = txt.getText();
404
                anterior = anterior.substring(0, startingCaretPosition);
405
                txt.setText(anterior + entries.get(currentEntry));
406
        }
407

    
408

    
409
}