Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / awt / event / specificCaretPosition / SpecificCaretPositionListeners.java @ 13136

History | View | Annotate | Download (5.76 KB)

1
package org.gvsig.gui.awt.event.specificCaretPosition;
2

    
3
import java.awt.event.FocusAdapter;
4
import java.awt.event.FocusEvent;
5

    
6
import javax.swing.event.CaretEvent;
7
import javax.swing.event.CaretListener;
8
import javax.swing.event.DocumentEvent;
9
import javax.swing.event.DocumentListener;
10
import javax.swing.text.JTextComponent;
11

    
12
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
13
 *
14
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
29
 *
30
 * For more information, contact:
31
 *
32
 *  Generalitat Valenciana
33
 *   Conselleria d'Infraestructures i Transport
34
 *   Av. Blasco Ib??ez, 50
35
 *   46010 VALENCIA
36
 *   SPAIN
37
 *
38
 *      +34 963862235
39
 *   gvsig@gva.es
40
 *      www.gvsig.gva.es
41
 *
42
 *    or
43
 *
44
 *   IVER T.I. S.A
45
 *   Salamanca 50
46
 *   46005 Valencia
47
 *   Spain
48
 *
49
 *   +34 963163400
50
 *   dac@iver.es
51
 */
52

    
53
/**
54
 * This class has a method which adds the necessary listeners for convert a JTextComponent to another which can be configurated
55
 *   its 'text visible positions when isn't edited' behaviour: left positions, right positions or like JTextComponent (doesn't change text visible positions)
56
 * 
57
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
58
 */
59
public class SpecificCaretPositionListeners {
60
        /**
61
         * Adds three listeners to a JTextComponent component:
62
         *  - A FocusListener -> if this component loses its focus -> set caret position to 0
63
         *  - A DocumentListener -> if this component doesn't have the focus and its caret position has changed -> set caret position to 0
64
         *  - A CaretListener -> sometimes DocumentListener doesn't take effect, but this listener does
65
         * @param component
66
         */
67
        public static void setListeners(final JTextComponent component) {
68
                // The Focus Listener
69
                component.addFocusListener(new FocusAdapter() {
70
                        /*
71
                         *  (non-Javadoc)
72
                         * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
73
                         */
74
                        public void focusLost(FocusEvent e) {
75
                                switch (((ISpecificCaretPosition)component).getCaretPositionMode()) {
76
                                        case ISpecificCaretPosition.LEFT_POSITIONS:
77
                                                (component).setCaretPosition(0);
78
                                                break;
79
                                        case ISpecificCaretPosition.RIGHT_POSITIONS:
80
                                                (component).setCaretPosition(component.getDocument().getLength());
81
                                                break;
82
                                        case ISpecificCaretPosition.LIKE_JTEXTCOMPONENT: // Do nothing
83
                                                break;
84
                                }
85
                        }
86
                });
87
                
88
                // The Document Listener
89
                component.getDocument().addDocumentListener(new DocumentListener() {
90
                        /*
91
                         *  (non-Javadoc)
92
                         * @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
93
                         */
94
                        public void changedUpdate(DocumentEvent e) {                                
95
                                if (!component.isFocusOwner()) {
96
                                        switch (((ISpecificCaretPosition)component).getCaretPositionMode()) {
97
                                                case ISpecificCaretPosition.LEFT_POSITIONS:
98
                                                        (component).setCaretPosition(0);
99
                                                        break;
100
                                                case ISpecificCaretPosition.RIGHT_POSITIONS:
101
                                                        (component).setCaretPosition(component.getDocument().getLength());
102
                                                        break;
103
                                                case ISpecificCaretPosition.LIKE_JTEXTCOMPONENT: // Do nothing
104
                                                        break;
105
                                        }
106
                                }
107
                        }
108

    
109
                        /*
110
                         *  (non-Javadoc)
111
                         * @see javax.swing.event.DocumentListener#insertUpdate(javax.swing.event.DocumentEvent)
112
                         */
113
                        public void insertUpdate(DocumentEvent e) {
114
                                if (!component.isFocusOwner()) {
115
                                        switch (((ISpecificCaretPosition)component).getCaretPositionMode()) {
116
                                                case ISpecificCaretPosition.LEFT_POSITIONS:
117
                                                        (component).setCaretPosition(0);
118
                                                        break;
119
                                                case ISpecificCaretPosition.RIGHT_POSITIONS:
120
                                                        (component).setCaretPosition(component.getDocument().getLength());
121
                                                        break;
122
                                                case ISpecificCaretPosition.LIKE_JTEXTCOMPONENT: // Do nothing
123
                                                        break;
124
                                        }
125
                                }
126
                        }
127

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

    
148
                // The Caret Listener
149
                component.addCaretListener(new CaretListener() {
150
                        /*
151
                         *  (non-Javadoc)
152
                         * @see javax.swing.event.CaretListener#caretUpdate(javax.swing.event.CaretEvent)
153
                         */
154
                        public void caretUpdate(CaretEvent e) {                                
155
                                if (!component.isFocusOwner()) {
156
                                        switch (((ISpecificCaretPosition)component).getCaretPositionMode()) {
157
                                                case ISpecificCaretPosition.LEFT_POSITIONS:
158
                                                        if ((component).getCaretPosition() != 0)
159
                                                                (component).setCaretPosition(0);
160
                                                        break;
161
                                                case ISpecificCaretPosition.RIGHT_POSITIONS:
162
                                                        if ((component).getCaretPosition() != component.getDocument().getLength())
163
                                                        (component).setCaretPosition(component.getDocument().getLength());
164
                                                        break;
165
                                                case ISpecificCaretPosition.LIKE_JTEXTCOMPONENT: // Do nothing
166
                                                        break;
167
                                        }
168
                                }
169
                        }
170
                });
171
        }
172
}