Statistics
| Revision:

gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.swing / org.gvsig.vectorediting.swing.impl / src / main / java / org / gvsig / vectorediting / swing / impl / console / DefaultEditingConsole.java @ 472

History | View | Annotate | Download (6.15 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2015 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 2
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.vectorediting.swing.impl.console;
26

    
27
import java.awt.event.ActionEvent;
28
import java.util.List;
29

    
30
import javax.swing.AbstractAction;
31
import javax.swing.Action;
32
import javax.swing.KeyStroke;
33
import javax.swing.event.AncestorEvent;
34
import javax.swing.event.AncestorListener;
35

    
36
import org.gvsig.utils.console.ResponseListener;
37
import org.gvsig.utils.console.ResponseListenerSupport;
38
import org.gvsig.vectorediting.swing.api.console.EditingConsole;
39

    
40
/**
41
 * Default implementation of EditingConsole
42
 *
43
 * @author llmarques
44
 */
45
public class DefaultEditingConsole extends DefaultEditingConsoleView implements
46
    EditingConsole {
47

    
48
    private static final long serialVersionUID = -1068129558465010049L;
49

    
50
    private ResponseListenerSupport responseListenerSupport;
51
    private int startingCaretPosition = 0;
52

    
53
    public DefaultEditingConsole(ResponseListener listener) {
54
        super();
55

    
56
        // Add ancestor listener to request focus when an ancestor becomes
57
        // visible
58
        addAncestorListenerToSetCaretPosition();
59

    
60
        this.responseListenerSupport = new ResponseListenerSupport();
61
        this.responseListenerSupport.addResponseListener(listener);
62

    
63
        getTextArea().setNavigationFilter(
64
            new DefaultEditingNavigationFilter(startingCaretPosition));
65

    
66
        addKeyBindings();
67
    }
68

    
69
    public DefaultEditingConsole(List<ResponseListener> listeners) {
70
        super();
71

    
72
        // Add ancestor listener to request focus when an ancestor becomes
73
        // visible
74
        addAncestorListenerToSetCaretPosition();
75

    
76
        this.responseListenerSupport = new ResponseListenerSupport();
77
        for (ResponseListener responseListener : listeners) {
78
            this.responseListenerSupport.addResponseListener(responseListener);
79
        }
80

    
81
        // Add navigation filter to control caret position
82
        getTextArea().setNavigationFilter(
83
            new DefaultEditingNavigationFilter(startingCaretPosition));
84

    
85
        addKeyBindings();
86
    }
87

    
88
    private void addAncestorListenerToSetCaretPosition() {
89
        this.addAncestorListener(new AncestorListener() {
90

    
91
            public void ancestorRemoved(AncestorEvent event) {
92
            }
93

    
94
            public void ancestorMoved(AncestorEvent event) {
95
            }
96

    
97
            public void ancestorAdded(AncestorEvent event) {
98
                getTextArea().requestFocusInWindow();
99
                getTextArea().setCaretPosition(startingCaretPosition);
100
            }
101
        });
102
    }
103

    
104
    private void addKeyBindings() {
105

    
106
        // Enter binding
107
        getTextArea().getInputMap().put(KeyStroke.getKeyStroke("ENTER"),
108
            "enterAction");
109
        getTextArea().getActionMap().put("enterAction", enterAction);
110

    
111
        // Esc binding
112
        getTextArea().getInputMap().put(KeyStroke.getKeyStroke("ESCAPE"),
113
            "escAction");
114
        getTextArea().getActionMap().put("escAction", escAction);
115

    
116
        // BackSpace binding
117
        getTextArea().getInputMap().put(KeyStroke.getKeyStroke("BACK_SPACE"),
118
            "backSpaceAction");
119
        getTextArea().getActionMap().put("backSpaceAction", backSpaceAction);
120
        // Del binding
121
        getTextArea().getInputMap().put(KeyStroke.getKeyStroke("DELETE"), "none");
122
    }
123

    
124
    public String getResponseText() {
125
        return getTextArea().getText().substring(startingCaretPosition,
126
            getTextArea().getText().length());
127
    }
128

    
129
    public void addText(String text) {
130
        getTextArea().setText(getTextArea().getText() + text);
131
        getTextArea().setCaretPosition(getTextArea().getText().length());
132
        startingCaretPosition = getTextArea().getText().length();
133
        getTextArea().setNavigationFilter(
134
            new DefaultEditingNavigationFilter(startingCaretPosition));
135
    }
136

    
137
    public void addResponseText(String responseText) {
138
        getTextArea().setText(getTextArea().getText() + responseText);
139
        getTextArea().setCaretPosition(getTextArea().getText().length());
140
    }
141

    
142
    public void addResponseListener(ResponseListener listener) {
143
        this.responseListenerSupport.addResponseListener(listener);
144
    }
145

    
146
    public void removeResponseListener(ResponseListener listener) {
147
        this.responseListenerSupport.removeResponseListener(listener);
148
    }
149

    
150
    private void callResponseListener(String response) {
151
        this.responseListenerSupport.callAcceptResponse(response);
152
    }
153

    
154
    public void clear() {
155
        getTextArea().setText(null);
156
    }
157

    
158
    @SuppressWarnings("serial")
159
    private Action enterAction = new AbstractAction() {
160

    
161
        public void actionPerformed(ActionEvent e) {
162
            callResponseListener(getResponseText() + "\n");
163
        }
164
    };
165

    
166
    @SuppressWarnings("serial")
167
    private Action escAction = new AbstractAction() {
168

    
169
        public void actionPerformed(ActionEvent e) {
170
            callResponseListener(null);
171
        }
172
    };
173

    
174
    @SuppressWarnings("serial")
175
    private Action backSpaceAction = new AbstractAction() {
176

    
177
        public void actionPerformed(ActionEvent e) {
178
            if (getTextArea().getCaretPosition() > startingCaretPosition) {
179
                StringBuilder stb = new StringBuilder(getTextArea().getText());
180
                stb.deleteCharAt(getTextArea().getText().length() - 1);
181
                getTextArea().setText(stb.toString());
182
            }
183
        }
184
    };
185

    
186
}