svn-gvsig-desktop / branches / v10 / libraries / libUI / src / de / ios / framework / swing / JIoSTextField.java @ 8848
History | View | Annotate | Download (12.5 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
|
42 |
/* CVS MESSAGES:
|
43 |
*
|
44 |
* $Id: JIoSTextField.java 8848 2006-11-17 11:38:04Z ppiqueras $
|
45 |
* $Log$
|
46 |
* Revision 1.2.2.2 2006-11-17 11:34:16 ppiqueras
|
47 |
* Corregidos bugs y a?adida nueva funcionalidad. Del HEAD.
|
48 |
*
|
49 |
* Revision 1.2 2006/10/26 17:59:33 jaume
|
50 |
* *** empty log message ***
|
51 |
*
|
52 |
* Revision 1.1 2006/10/25 15:53:25 jaume
|
53 |
* new components (text fields that only accepts numbers and text fields that only accepts decimal numbers)
|
54 |
*
|
55 |
*
|
56 |
*/
|
57 |
package de.ios.framework.swing; |
58 |
|
59 |
import java.awt.event.FocusEvent; |
60 |
import java.awt.event.FocusListener; |
61 |
import java.awt.event.MouseEvent; |
62 |
import java.awt.event.MouseListener; |
63 |
|
64 |
import javax.swing.JTextField; |
65 |
import javax.swing.SwingUtilities; |
66 |
import javax.swing.event.DocumentEvent; |
67 |
import javax.swing.event.DocumentListener; |
68 |
|
69 |
|
70 |
/**
|
71 |
* Textfield, ABSTRACT basic class for any Singleline-Input-Fields.
|
72 |
* Includes support for key-substitution (to solve problems with i.e.
|
73 |
* the '@'), limited Charactersets, auto-formating on lost focus,
|
74 |
* automatic selection on gained focus.
|
75 |
* Implementation of several Listener-Interfaces only for internal use!
|
76 |
* @version $Id: JIoSTextField.java 8848 2006-11-17 11:38:04Z ppiqueras $
|
77 |
*/
|
78 |
public abstract class JIoSTextField extends JTextField |
79 |
implements DocumentListener, FocusListener, MouseListener { |
80 |
|
81 |
// FIXME: NOT SUPPORTED !!!!!!
|
82 |
public void setEchoChar( char c ) { |
83 |
new Exception( "Depricated call to setEchoChar,. Use JPasswordField instead." ). |
84 |
printStackTrace(); |
85 |
} |
86 |
|
87 |
/** STATIC: Faster setText-Handling - if true, the Text is only set, if it differs from the current value. */
|
88 |
public static boolean fastSetText = true; |
89 |
|
90 |
/** Default for auto-selection-handling. */
|
91 |
public static boolean defaultAutoSelection = false; |
92 |
|
93 |
/** Lokal auto-selektion-mode. */
|
94 |
protected Boolean autoSelection = null; |
95 |
|
96 |
/** Focus-Handling. Stores "temporary"-flag of the last lostFocus. */
|
97 |
protected boolean lastFTemp = false; |
98 |
|
99 |
/** Keys to replace on input. STILL REQUIRED??? Don't know... */
|
100 |
protected String keys = ""; // "@"; |
101 |
|
102 |
/** Replacements for keys on input. STILL REQUIRED??? Don't know... */
|
103 |
protected String replace = ""; // "@"; |
104 |
|
105 |
/** A String with valid input characters. If null, all characters are valid. */
|
106 |
protected String charSet = null; |
107 |
|
108 |
/** Info-Text for this field. */
|
109 |
protected String infoText = null; |
110 |
|
111 |
/** Auto-Format-Flag. */
|
112 |
protected boolean autoFormat = true; |
113 |
|
114 |
/** Keep-Focus-On-Illegal-Value-Flag. */
|
115 |
protected boolean keepFocus = false; // FIXME: should be: true; |
116 |
|
117 |
/** Disable-Lock for setEnabled(). */
|
118 |
protected boolean lockDisable = false; |
119 |
|
120 |
|
121 |
/**
|
122 |
* Constructor.
|
123 |
*/
|
124 |
public JIoSTextField() {
|
125 |
super();
|
126 |
initListener(); |
127 |
} |
128 |
|
129 |
|
130 |
/**
|
131 |
* Constructor.
|
132 |
*/
|
133 |
public JIoSTextField( int cols ) { |
134 |
super(cols);
|
135 |
initListener(); |
136 |
} |
137 |
|
138 |
|
139 |
/**
|
140 |
* Constructor.
|
141 |
* This constructor is temporary of type 'package', cause it's of the same
|
142 |
* type like a deprecated constructor, but with changed behaviour.
|
143 |
*/
|
144 |
JIoSTextField( String _charSet ) {
|
145 |
super();
|
146 |
setCharSet( _charSet ); |
147 |
initListener(); |
148 |
} |
149 |
|
150 |
|
151 |
/**
|
152 |
* Constructor defining also the Charset.
|
153 |
*/
|
154 |
public JIoSTextField( int cols, String _charSet ) { |
155 |
super(cols);
|
156 |
setCharSet( _charSet ); |
157 |
initListener(); |
158 |
} |
159 |
|
160 |
|
161 |
/**
|
162 |
* Constructor defining the charset, auto-formating and illegal-value-focus-keeping.
|
163 |
*/
|
164 |
public JIoSTextField( int cols, String _charSet, boolean _autoFormat, boolean _keepFocus ) { |
165 |
super(cols);
|
166 |
setCharSet( _charSet ); |
167 |
setAutoFormat( _autoFormat ); |
168 |
setKeepFocusOnIllegalValue( _keepFocus ); |
169 |
initListener(); |
170 |
} |
171 |
|
172 |
|
173 |
// /**
|
174 |
// * Add an ActionListener, set a predefined color for 'Action'-Fields.
|
175 |
// */
|
176 |
// public void addActionListener(ActionListener l) {
|
177 |
// super.addActionListener( l );
|
178 |
// String c = Parameters.getParameter( "jtextfield.action.color" );
|
179 |
// if ( c != null )
|
180 |
// setBackground( new Color( Integer.parseInt( c, 16 ) ) );
|
181 |
// }
|
182 |
|
183 |
|
184 |
/**
|
185 |
* Disable the field and lock it against any setEnable().
|
186 |
*/
|
187 |
public void lockDisable( boolean disable ) { |
188 |
if (!isDisableLocked())
|
189 |
super.setEnabled( false ); |
190 |
lockDisable |= disable; |
191 |
} |
192 |
|
193 |
|
194 |
/**
|
195 |
* Unlock a the lock on the disable of the field (does not directly enable the field, but realows enabling).
|
196 |
*/
|
197 |
public void unlockDisabled() { |
198 |
lockDisable = false;
|
199 |
} |
200 |
|
201 |
|
202 |
/**
|
203 |
* Check, if the field is locked disabled.
|
204 |
*/
|
205 |
public boolean isDisableLocked() { |
206 |
return lockDisable;
|
207 |
} |
208 |
|
209 |
|
210 |
/**
|
211 |
* setEnabled() protected with the disable-lock.
|
212 |
*/
|
213 |
public void setEnabled( boolean en ) { |
214 |
if (!isDisableLocked())
|
215 |
super.setEnabled( en );
|
216 |
} |
217 |
|
218 |
|
219 |
/**
|
220 |
* Set the displayed text withoput any furhter checks.
|
221 |
*/
|
222 |
protected void setRawText( String text ) { |
223 |
if (text == null) |
224 |
text = "";
|
225 |
if ( fastSetText ? (text.compareTo( super.getText() )!=0) : true ) |
226 |
super.setText( text );
|
227 |
} |
228 |
|
229 |
|
230 |
/**
|
231 |
* Sets the text. Watches the maximal size of the field,
|
232 |
* so that the leading part of the text is always visible,
|
233 |
* handles null-Values.
|
234 |
* (the default-implementation show the trailing part of the text, because
|
235 |
* always the caret is set to the end of the textfield.).
|
236 |
*/
|
237 |
public void setText( String text ) { |
238 |
setRawText( text ); |
239 |
if ( text != null ) |
240 |
if ( text.length() >= getColumns())
|
241 |
if (fastSetText ? (getCaretPosition() != 0) : true) |
242 |
setCaretPosition( 0 );
|
243 |
} |
244 |
|
245 |
|
246 |
/**
|
247 |
* Gets the text.
|
248 |
* We couldn't give that functionality to getText(),
|
249 |
* cause it's internally used by Super-Classes not expecting null-Values.
|
250 |
*/
|
251 |
public String getNullText() { |
252 |
String text = getText();
|
253 |
return (text.length() == 0) ? null : text; |
254 |
} |
255 |
|
256 |
|
257 |
/**
|
258 |
* Get the text after formating (primary for internal use at inheritances).
|
259 |
* Does (and MUST) not request the focus.
|
260 |
*/
|
261 |
public String getFormatedText() { |
262 |
if (!formatValue())
|
263 |
setText( null );
|
264 |
return getNullText();
|
265 |
} |
266 |
|
267 |
|
268 |
/**
|
269 |
* Request-Method for formating the Data (only for redefinition by inheritances).
|
270 |
* Default-Implementation does nothing.
|
271 |
* @return false, if formating fails due to an illegal Value
|
272 |
* (results in keeping the Focus, if requested by setKeepFocusOnIllegalValue).
|
273 |
*/
|
274 |
public boolean formatValue() { |
275 |
return true; |
276 |
} |
277 |
|
278 |
|
279 |
/**
|
280 |
* Set a "special" character set. All other characters are ignored on input.
|
281 |
* @see #keyTyped()
|
282 |
* @param s A string with valid characters.
|
283 |
*/
|
284 |
public void setCharSet(String s) { |
285 |
charSet = s; |
286 |
} |
287 |
|
288 |
|
289 |
/**
|
290 |
* Get the "special" character set.
|
291 |
* @return The "special" character set.
|
292 |
*/
|
293 |
public String getCharSet() { |
294 |
return charSet;
|
295 |
} |
296 |
|
297 |
|
298 |
// /**
|
299 |
// * Implements InfoTopic.
|
300 |
// */
|
301 |
// public void setInfoText( String text ) {
|
302 |
// infoText = text;
|
303 |
// InfoTopicViewer.watchInfoTopic( this );
|
304 |
// }
|
305 |
|
306 |
|
307 |
/**
|
308 |
* Implements InfoTopic.
|
309 |
*/
|
310 |
public String getInfoText( ) { |
311 |
return infoText;
|
312 |
} |
313 |
|
314 |
|
315 |
/**
|
316 |
* Enable/disable the Auto-Selection-Mode.
|
317 |
*/
|
318 |
public void setAutoSelection( boolean _autoSelection ) { |
319 |
autoSelection = new Boolean( _autoSelection ); |
320 |
} |
321 |
|
322 |
|
323 |
/**
|
324 |
* Set the autoformat-mode of this field.
|
325 |
* If autoformat is activated the field is automaticaly formated if the focus
|
326 |
* is lost permanently.
|
327 |
*/
|
328 |
public void setAutoFormat( boolean _autoFormat ) { |
329 |
autoFormat = _autoFormat; |
330 |
} |
331 |
|
332 |
|
333 |
/**
|
334 |
* Defines, if to keep the Focus on illegal Values (only on active AutoFormat).
|
335 |
*/
|
336 |
public void setKeepFocusOnIllegalValue( boolean _keepFocus ) { |
337 |
keepFocus = _keepFocus; |
338 |
} |
339 |
|
340 |
|
341 |
/**
|
342 |
* Define the Keys to be handled manually
|
343 |
*/
|
344 |
public synchronized JIoSTextField manuallyHandle(String _keys, String _replace) { |
345 |
keys=_keys; |
346 |
replace=_replace; |
347 |
if ( keys == null ) |
348 |
keys="";
|
349 |
if ( replace == null ) |
350 |
replace="";
|
351 |
return this; |
352 |
} |
353 |
|
354 |
|
355 |
/////// Internal Stuff ///////
|
356 |
|
357 |
|
358 |
/**
|
359 |
* Init the listeners.
|
360 |
*/
|
361 |
protected void initListener() { |
362 |
getDocument().addDocumentListener( this );
|
363 |
addFocusListener( this );
|
364 |
addMouseListener( this );
|
365 |
} |
366 |
|
367 |
|
368 |
// Events //
|
369 |
|
370 |
|
371 |
/**
|
372 |
* Implements FocusListener
|
373 |
* Invoked when the component gains the keyboard focus.
|
374 |
* Marks the complet input (but leaves the caret-position).
|
375 |
*/
|
376 |
public void focusGained( FocusEvent evt ) { |
377 |
if ( ((autoSelection == null) ? defaultAutoSelection : autoSelection.booleanValue()) |
378 |
&& isEditable() && !lastFTemp) { |
379 |
int p = getCaretPosition();
|
380 |
selectAll(); |
381 |
setCaretPosition( p ); |
382 |
} |
383 |
} |
384 |
|
385 |
|
386 |
/**
|
387 |
* Implements FocusListener
|
388 |
* Invoked when a component loses the keyboard focus.
|
389 |
* Stores the temporary-flag of the event and
|
390 |
* resets selection if autoselection is enabled.
|
391 |
*/
|
392 |
public void focusLost( FocusEvent evt ) { |
393 |
lastFTemp = evt.isTemporary(); |
394 |
if (!lastFTemp) {
|
395 |
if ( autoFormat && isEditable() )
|
396 |
if ( !formatValue() ) {
|
397 |
if ( keepFocus )
|
398 |
requestFocus(); |
399 |
//else
|
400 |
// setText( null );
|
401 |
getToolkit().beep(); |
402 |
} |
403 |
if ( (autoSelection == null) ? defaultAutoSelection : autoSelection.booleanValue() ) { |
404 |
int p = getCaretPosition();
|
405 |
select(0,0); |
406 |
setCaretPosition( p ); |
407 |
} |
408 |
} |
409 |
} |
410 |
|
411 |
|
412 |
/**
|
413 |
* Implements MouseListener
|
414 |
* Invoked when the mouse has been clicked on a component.
|
415 |
* Defaultimplementation, does nothing
|
416 |
*/
|
417 |
public void mouseClicked(MouseEvent evt) { |
418 |
} |
419 |
|
420 |
|
421 |
/**
|
422 |
* Implements MouseListener
|
423 |
* Invoked when the mouse enters a component.
|
424 |
* Defaultimplementation, does nothing
|
425 |
*/
|
426 |
public void mouseEntered(MouseEvent evt) { |
427 |
} |
428 |
|
429 |
|
430 |
/**
|
431 |
* Implements MouseListener
|
432 |
* Invoked when the mouse exits a component.
|
433 |
* Defaultimplementation, does nothing
|
434 |
*/
|
435 |
public void mouseExited(MouseEvent evt) { |
436 |
} |
437 |
|
438 |
|
439 |
/**
|
440 |
* Implements MouseListener
|
441 |
* Invoked when a mouse button has been pressed on a component.
|
442 |
* Subpress autoselection for the following focus-event.
|
443 |
*/
|
444 |
public void mousePressed(MouseEvent evt ) { |
445 |
lastFTemp = true;
|
446 |
} |
447 |
|
448 |
|
449 |
/**
|
450 |
* Implements MouseListener
|
451 |
* Invoked when a mouse button has been released on a component.
|
452 |
* Defaultimplementation, does nothing
|
453 |
*/
|
454 |
public void mouseReleased(MouseEvent evt ) { |
455 |
} |
456 |
|
457 |
/**
|
458 |
* Implements DocumentListener.
|
459 |
* @see #handleDocumentEvent
|
460 |
*/
|
461 |
public void changedUpdate( DocumentEvent e ) { |
462 |
handleDocumentEvent( e ); |
463 |
} |
464 |
|
465 |
/**
|
466 |
* Implements DocumentListener.
|
467 |
* @see #handleDocumentEvent
|
468 |
*/
|
469 |
public void insertUpdate( DocumentEvent e ) { |
470 |
handleDocumentEvent( e ); |
471 |
} |
472 |
|
473 |
/**
|
474 |
* Implements DocumentListener.
|
475 |
* @see #handleDocumentEvent
|
476 |
*/
|
477 |
public void removeUpdate( DocumentEvent e ) { |
478 |
handleDocumentEvent( e ); |
479 |
} |
480 |
|
481 |
/**
|
482 |
* Flag that indicated that a text-check is already involed at the end of the actual event-processing.
|
483 |
*/
|
484 |
boolean checkInvoked = false; |
485 |
|
486 |
/**
|
487 |
* Handle a document-event.
|
488 |
*/
|
489 |
protected void handleDocumentEvent( final DocumentEvent evt ) { |
490 |
if (checkInvoked)
|
491 |
return;
|
492 |
checkInvoked = true;
|
493 |
SwingUtilities.invokeLater( new Runnable() { |
494 |
|
495 |
public void run() { |
496 |
try {
|
497 |
|
498 |
String text = getNullText();
|
499 |
String rtext;
|
500 |
|
501 |
if (text == null) |
502 |
return;
|
503 |
|
504 |
int idx;
|
505 |
char c;
|
506 |
|
507 |
int firstRemovePos = -1; |
508 |
|
509 |
int n = text.length();
|
510 |
StringBuffer sb = new StringBuffer( n ); |
511 |
|
512 |
// Check actual input.
|
513 |
for (int i=0 ; i<n ; i++) { |
514 |
c = text.charAt( i ); |
515 |
idx = keys.indexOf( c ); |
516 |
if ( idx >= 0 ) |
517 |
c = replace.charAt(idx); |
518 |
if ( (charSet == null) || (charSet.indexOf(c) >= 0)) |
519 |
sb.append( c ); |
520 |
else {
|
521 |
if (firstRemovePos<0) |
522 |
firstRemovePos = idx; |
523 |
} |
524 |
} |
525 |
|
526 |
String newText = sb.toString();
|
527 |
int newN = newText.length();
|
528 |
if ((newN != n) || (newText.compareTo( text )!=0) ) { |
529 |
int cpos = getCaretPosition();
|
530 |
setRawText( newText ); |
531 |
|
532 |
if ((cpos > firstRemovePos) && (n>newN))
|
533 |
cpos -= (n-newN); |
534 |
if (cpos < 0) |
535 |
cpos = 0;
|
536 |
if (cpos > newN)
|
537 |
cpos = newN; |
538 |
setCaretPosition( cpos ); |
539 |
getToolkit().beep(); |
540 |
} |
541 |
} finally {
|
542 |
checkInvoked = false;
|
543 |
} |
544 |
} |
545 |
} ); |
546 |
} |
547 |
} |