Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / awt / event / specificCaretPosition / SpecificCaretPositionListeners.java @ 40561

History | View | Annotate | Download (6.7 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
package org.gvsig.gui.awt.event.specificCaretPosition;
25

    
26
import java.awt.event.FocusAdapter;
27
import java.awt.event.FocusEvent;
28

    
29
import javax.swing.event.CaretEvent;
30
import javax.swing.event.CaretListener;
31
import javax.swing.event.DocumentEvent;
32
import javax.swing.event.DocumentListener;
33
import javax.swing.text.JTextComponent;
34

    
35
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
36
 *
37
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
38
 *
39
 * This program is free software; you can redistribute it and/or
40
 * modify it under the terms of the GNU General Public License
41
 * as published by the Free Software Foundation; either version 2
42
 * of the License, or (at your option) any later version.
43
 *
44
 * This program is distributed in the hope that it will be useful,
45
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
47
 * GNU General Public License for more details.
48
 *
49
 * You should have received a copy of the GNU General Public License
50
 * along with this program; if not, write to the Free Software
51
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
52
 *
53
 * For more information, contact:
54
 *
55
 *  Generalitat Valenciana
56
 *   Conselleria d'Infraestructures i Transport
57
 *   Av. Blasco Ib??ez, 50
58
 *   46010 VALENCIA
59
 *   SPAIN
60
 *
61
 *      +34 963862235
62
 *   gvsig@gva.es
63
 *      www.gvsig.gva.es
64
 *
65
 *    or
66
 *
67
 *   IVER T.I. S.A
68
 *   Salamanca 50
69
 *   46005 Valencia
70
 *   Spain
71
 *
72
 *   +34 963163400
73
 *   dac@iver.es
74
 */
75

    
76
/**
77
 * This class has a method which adds the necessary listeners for convert a JTextComponent to another which can be configurated
78
 *   its 'text visible positions when isn't edited' behaviour: left positions, right positions or like JTextComponent (doesn't change text visible positions)
79
 * 
80
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
81
 */
82
public class SpecificCaretPositionListeners {
83
        /**
84
         * Adds three listeners to a JTextComponent component:
85
         *  - A FocusListener -> if this component loses its focus -> set caret position to 0
86
         *  - A DocumentListener -> if this component doesn't have the focus and its caret position has changed -> set caret position to 0
87
         *  - A CaretListener -> sometimes DocumentListener doesn't take effect, but this listener does
88
         * @param component
89
         */
90
        public static void setListeners(final JTextComponent component) {
91
                // The Focus Listener
92
                component.addFocusListener(new FocusAdapter() {
93
                        /*
94
                         *  (non-Javadoc)
95
                         * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
96
                         */
97
                        public void focusLost(FocusEvent e) {
98
                                switch (((ISpecificCaretPosition)component).getCaretPositionMode()) {
99
                                        case ISpecificCaretPosition.LEFT_POSITIONS:
100
                                                (component).setCaretPosition(0);
101
                                                break;
102
                                        case ISpecificCaretPosition.RIGHT_POSITIONS:
103
                                                (component).setCaretPosition(component.getDocument().getLength());
104
                                                break;
105
                                        case ISpecificCaretPosition.LIKE_JTEXTCOMPONENT: // Do nothing
106
                                                break;
107
                                }
108
                        }
109
                });
110
                
111
                // The Document Listener
112
                component.getDocument().addDocumentListener(new DocumentListener() {
113
                        /*
114
                         *  (non-Javadoc)
115
                         * @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
116
                         */
117
                        public void changedUpdate(DocumentEvent e) {                                
118
                                if (!component.isFocusOwner()) {
119
                                        switch (((ISpecificCaretPosition)component).getCaretPositionMode()) {
120
                                                case ISpecificCaretPosition.LEFT_POSITIONS:
121
                                                        (component).setCaretPosition(0);
122
                                                        break;
123
                                                case ISpecificCaretPosition.RIGHT_POSITIONS:
124
                                                        (component).setCaretPosition(component.getDocument().getLength());
125
                                                        break;
126
                                                case ISpecificCaretPosition.LIKE_JTEXTCOMPONENT: // Do nothing
127
                                                        break;
128
                                        }
129
                                }
130
                        }
131

    
132
                        /*
133
                         *  (non-Javadoc)
134
                         * @see javax.swing.event.DocumentListener#insertUpdate(javax.swing.event.DocumentEvent)
135
                         */
136
                        public void insertUpdate(DocumentEvent e) {
137
                                if (!component.isFocusOwner()) {
138
                                        switch (((ISpecificCaretPosition)component).getCaretPositionMode()) {
139
                                                case ISpecificCaretPosition.LEFT_POSITIONS:
140
                                                        (component).setCaretPosition(0);
141
                                                        break;
142
                                                case ISpecificCaretPosition.RIGHT_POSITIONS:
143
                                                        (component).setCaretPosition(component.getDocument().getLength());
144
                                                        break;
145
                                                case ISpecificCaretPosition.LIKE_JTEXTCOMPONENT: // Do nothing
146
                                                        break;
147
                                        }
148
                                }
149
                        }
150

    
151
                        /*
152
                         *  (non-Javadoc)
153
                         * @see javax.swing.event.DocumentListener#removeUpdate(javax.swing.event.DocumentEvent)
154
                         */
155
                        public void removeUpdate(DocumentEvent e) {
156
                                if (!component.isFocusOwner()) {
157
                                        switch (((ISpecificCaretPosition)component).getCaretPositionMode()) {
158
                                                case ISpecificCaretPosition.LEFT_POSITIONS:
159
                                                        (component).setCaretPosition(0);
160
                                                        break;
161
                                                case ISpecificCaretPosition.RIGHT_POSITIONS:
162
                                                        (component).setCaretPosition(component.getDocument().getLength());
163
                                                        break;
164
                                                case ISpecificCaretPosition.LIKE_JTEXTCOMPONENT: // Do nothing
165
                                                        break;
166
                                        }
167
                                }
168
                        }                        
169
                });
170

    
171
                // The Caret Listener
172
                component.addCaretListener(new CaretListener() {
173
                        /*
174
                         *  (non-Javadoc)
175
                         * @see javax.swing.event.CaretListener#caretUpdate(javax.swing.event.CaretEvent)
176
                         */
177
                        public void caretUpdate(CaretEvent e) {                                
178
                                if (!component.isFocusOwner()) {
179
                                        switch (((ISpecificCaretPosition)component).getCaretPositionMode()) {
180
                                                case ISpecificCaretPosition.LEFT_POSITIONS:
181
                                                        if ((component).getCaretPosition() != 0)
182
                                                                (component).setCaretPosition(0);
183
                                                        break;
184
                                                case ISpecificCaretPosition.RIGHT_POSITIONS:
185
                                                        if ((component).getCaretPosition() != component.getDocument().getLength())
186
                                                        (component).setCaretPosition(component.getDocument().getLength());
187
                                                        break;
188
                                                case ISpecificCaretPosition.LIKE_JTEXTCOMPONENT: // Do nothing
189
                                                        break;
190
                                        }
191
                                }
192
                        }
193
                });
194
        }
195
}