1 /*
2 * Copyright 2006-2016 The JGUIraffe Team.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.jguiraffe.gui.platform.swing.builder.components;
17
18 import javax.swing.event.DocumentEvent;
19 import javax.swing.event.DocumentListener;
20 import javax.swing.text.JTextComponent;
21
22 import net.sf.jguiraffe.gui.builder.components.model.TextHandler;
23
24 /**
25 * <p>
26 * A specific Swing component handler implementation that deals with text
27 * components.
28 * </p>
29 * <p>
30 * This component handler deals with single and multi line text fields. Data
31 * type is String in both cases. Change events are mapped to edit events like
32 * insert and remove. Action events are not supported.
33 * </p>
34 *
35 * @author Oliver Heger
36 * @version $Id: SwingTextHandler.java 205 2012-01-29 18:29:57Z oheger $
37 */
38 class SwingTextHandler extends SwingComponentHandler<String> implements
39 DocumentListener, TextHandler
40 {
41 /**
42 * Creates a new instance of <code>SwingTextHandler</code>.
43 *
44 * @param text the managed text component
45 */
46 public SwingTextHandler(JTextComponent text)
47 {
48 super(text);
49 }
50
51 /**
52 * Returns the managed text component.
53 *
54 * @return the text component
55 */
56 public JTextComponent getTextComponent()
57 {
58 return (JTextComponent) getComponent();
59 }
60
61 /**
62 * Returns the data of the managed text field. This is of type string.
63 *
64 * @return the text field's data
65 */
66 public String getData()
67 {
68 return getTextComponent().getText();
69 }
70
71 /**
72 * Sets the text field's data.
73 *
74 * @param data the data, may be <b>null</b>
75 */
76 public void setData(String data)
77 {
78 getTextComponent().setText(data);
79 }
80
81 /**
82 * Returns this handler's data type. This is String.
83 *
84 * @return the handler's data type
85 */
86 public Class<?> getType()
87 {
88 return String.class;
89 }
90
91 /**
92 * Clears the selection. This implementation sets the selection end of the
93 * wrapped text component to the same index as the selection start. This
94 * means that there is no selection, but the caret position should have a
95 * defined value.
96 */
97 public void clearSelection()
98 {
99 getTextComponent().setSelectionEnd(getSelectionStart());
100 }
101
102 /**
103 * Performs a copy operation. This implementation just delegates to the
104 * wrapped text component.
105 */
106 public void copy()
107 {
108 getTextComponent().copy();
109 }
110
111 /**
112 * Performs a cut operation. This implementation just delegates to the
113 * wrapped text component.
114 */
115 public void cut()
116 {
117 getTextComponent().cut();
118 }
119
120 /**
121 * Returns the text which is currently selected. This implementation just
122 * delegates to the wrapped text component.
123 *
124 * @return the selected text
125 */
126 public String getSelectedText()
127 {
128 return getTextComponent().getSelectedText();
129 }
130
131 /**
132 * Returns the start index of the selection. This implementation just
133 * delegates to the wrapped text component.
134 *
135 * @return the start index of the selection
136 */
137 public int getSelectionEnd()
138 {
139 return getTextComponent().getSelectionEnd();
140 }
141
142 /**
143 * Returns the end index of the selection. This implementation just
144 * delegates to the wrapped text component.
145 *
146 * @return the end index of the selection
147 */
148 public int getSelectionStart()
149 {
150 return getTextComponent().getSelectionStart();
151 }
152
153 /**
154 * Tests whether a selection exists. This implementation checks the
155 * selection start and the selection end from the wrapped text component.
156 *
157 * @return a flag whether currently text is selected
158 */
159 public boolean hasSelection()
160 {
161 return getTextComponent().getSelectionStart() < getTextComponent()
162 .getSelectionEnd();
163 }
164
165 /**
166 * Performs a paste operation. This implementation just delegates to the
167 * wrapped text component.
168 */
169 public void paste()
170 {
171 getTextComponent().paste();
172 }
173
174 /**
175 * Replaces the selected text. This implementation just delegates to the
176 * wrapped text component.
177 *
178 * @param text the replacement text
179 */
180 public void replaceSelectedText(String text)
181 {
182 getTextComponent().replaceSelection(text);
183 }
184
185 /**
186 * Selects a range of text. This implementation just delegates to the
187 * wrapped text component. It is possible to set the new indices of the
188 * selection to invalid values; the text component will trim them correctly.
189 *
190 * @param start the start index of the new selection
191 * @param end the end index of the new selection
192 */
193 public void select(int start, int end)
194 {
195 getTextComponent().select(start, end);
196 }
197
198 /**
199 * Selects the whole text. This implementation just delegates to the wrapped
200 * text component.
201 */
202 public void selectAll()
203 {
204 getTextComponent().selectAll();
205 }
206
207 /**
208 * Event listener callback for text change events.
209 *
210 * @param event the event
211 */
212 public void changedUpdate(DocumentEvent event)
213 {
214 fireChangeEvent(event);
215 }
216
217 /**
218 * Event listener callback for text insert events.
219 *
220 * @param event the event
221 */
222 public void insertUpdate(DocumentEvent event)
223 {
224 fireChangeEvent(event);
225 }
226
227 /**
228 * Event listener callback for text remove events.
229 *
230 * @param event the event
231 */
232 public void removeUpdate(DocumentEvent event)
233 {
234 fireChangeEvent(event);
235 }
236
237 /**
238 * Registers this handler as change listener at the managed text component.
239 * Incoming text update notifications will then be broadcasted as change
240 * events.
241 */
242 @Override
243 protected void registerChangeListener()
244 {
245 getTextComponent().getDocument().addDocumentListener(this);
246 }
247
248 /**
249 * Unregisters this handler as change listener at the managed text
250 * component.
251 */
252 @Override
253 protected void unregisterChangeListener()
254 {
255 getTextComponent().getDocument().removeDocumentListener(this);
256 }
257 }