KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nqadmin > swingSet > formatting > SSFormattedTextField


1 /* $Id: SSFormattedTextField.java,v 1.12 2005/02/22 15:14:34 yoda2 Exp $
2  *
3  * Tab Spacing = 4
4  *
5  * Copyright (c) 2004-2005, The Pangburn Company, Prasanth R. Pasala and
6  * Diego Gil
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * Redistributions of source code must retain the above copyright notice, this
13  * list of conditions and the following disclaimer. Redistributions in binary
14  * form must reproduce the above copyright notice, this list of conditions and
15  * the following disclaimer in the documentation and/or other materials
16  * provided with the distribution. The names of its contributors may not be
17  * used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  */

33
34 package com.nqadmin.swingSet.formatting;
35
36 import com.nqadmin.swingSet.SSDataNavigator;
37 import com.nqadmin.swingSet.datasources.SSRowSet;
38
39 import java.awt.*;
40 import java.awt.event.*;
41 import javax.swing.*;
42
43 import javax.sql.RowSetListener JavaDoc;
44 import javax.swing.JFormattedTextField JavaDoc;
45 import java.util.Set JavaDoc;
46 import java.util.HashSet JavaDoc;
47
48 import com.nqadmin.swingSet.SSDataNavigator;
49 import com.nqadmin.swingSet.formatting.helpers.*;
50
51
52 /**
53  * SSFormattedTextField.java
54  *<p>
55  * SwingSet - Open Toolkit For Making Swing Controls Database-Aware
56  *<p><pre>
57  * SSFormattedTextField extends the JFormattedTextField.
58  *</pre><p>
59  * @author $Author: yoda2 $
60  * @version $Revision: 1.12 $
61  */

62 public class SSFormattedTextField extends JFormattedTextField JavaDoc implements RowSetListener JavaDoc, KeyListener, FocusListener, MouseListener {
63     
64     private JPopupMenu menu = null;
65     private JPopupMenu calculator = null;
66     private JPopupMenu helper = null;
67     
68     private java.awt.Color JavaDoc std_color = null;
69     private String JavaDoc columnName = null;
70     private int colType = -99;
71     private SSDataNavigator navigator = null;
72     private SSRowSet rowset = null;
73     
74     
75     /** Creates a new instance of SSFormattedTextField */
76     public SSFormattedTextField() {
77         super();
78         
79         /*
80          *
81          *
82          */

83         
84         Set forwardKeys = getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
85         Set newForwardKeys = new HashSet JavaDoc(forwardKeys);
86         newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
87         newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, java.awt.event.InputEvent.SHIFT_MASK ));
88         setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,newForwardKeys);
89         
90         Set backwardKeys = getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
91         Set newBackwardKeys = new HashSet JavaDoc(backwardKeys);
92         newBackwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_UP, java.awt.event.InputEvent.SHIFT_MASK ));
93         setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,newBackwardKeys);
94         
95         /*
96          * add this as a self KeyListener
97          *
98          */

99         addKeyListener(this);
100         
101         /*
102          * add this as a self FocusListener
103          *
104          */

105         addFocusListener(this);
106         
107         /*
108          * add this as a self MouseListener
109          *
110          */

111         addMouseListener(this);
112
113
114         /*
115          *
116          */

117         menu = new JPopupMenu();
118         menu.add("Opcion 1");
119         menu.add("Opcion 2");
120         
121         /*
122          * set InputVerifier. Rowset's updates are handled by this class. Is the preferred method instead of focus change.
123          *
124          */

125         setInputVerifier(new internalVerifier());
126     }
127     
128     public SSFormattedTextField(javax.swing.JFormattedTextField.AbstractFormatterFactory factory) {
129         this();
130         this.setFormatterFactory(factory);
131     }
132     
133     public void setColumnName(String JavaDoc columnName) {
134         this.columnName = columnName;
135         bind();
136     }
137     
138     public String JavaDoc getColumnName() {
139         return columnName;
140     }
141     
142     /**
143      *
144      * @deprecated
145      * @see #setSSRowSet
146      *
147      */

148     public void setRowSet(SSRowSet rowset) {
149         this.rowset = rowset;
150         bind();
151     }
152     
153     public void setSSRowSet(SSRowSet rowset) {
154         this.rowset = rowset;
155     }
156     
157     public SSRowSet getSSRowSet() {
158         return rowset;
159     }
160     
161     public void setNavigator(SSDataNavigator navigator) {
162         this.navigator = navigator;
163         setSSRowSet(navigator.getSSRowSet());
164         bind();
165     }
166     
167     public SSDataNavigator getNavigator() {
168         return this.navigator;
169     }
170     
171     private void bind() {
172     
173         if (columnName == null || rowset == null) return;
174         
175         try {
176             colType = rowset.getColumnType(columnName);
177         } catch(java.sql.SQLException JavaDoc sqe) {
178             System.out.println("bind error = " + sqe);
179         }
180         
181         rowset.addRowSetListener(this);
182         
183         DbToFm();
184     }
185     
186     public void rowSetChanged(javax.sql.RowSetEvent JavaDoc event) {
187 // System.out.println("rowSetChanged");
188

189     }
190     
191     public void rowChanged(javax.sql.RowSetEvent JavaDoc event) {
192 // System.out.println("rowChanged " + event);
193

194     }
195     
196     public void cursorMoved(javax.sql.RowSetEvent JavaDoc event) {
197         /*
198          *
199          *
200          */

201         DbToFm();
202     }
203     
204     public void keyTyped(KeyEvent e) {
205         
206     }
207     
208     public void keyReleased(KeyEvent e) {
209         
210     }
211     
212     /**
213      * Catch severals keys, to implement some forms functionality (To be done).
214      * This is to mimic console legacy systems behavior.
215      *
216      */

217     public void keyPressed(KeyEvent e) {
218         
219         if (e.getKeyCode() == KeyEvent.VK_F1) {
220             showHelper(e);
221         }
222         
223         if (e.getKeyCode() == KeyEvent.VK_F2) {
224             
225         }
226         
227         if (e.getKeyCode() == KeyEvent.VK_F3) {
228             
229             System.out.println("F3 ");
230             calculator = new javax.swing.JPopupMenu JavaDoc();
231             //calculator.add(new com.nqadmin.swingSet.formatting.utils.JCalculator());
232
JFormattedTextField JavaDoc ob = (JFormattedTextField JavaDoc)(e.getSource());
233             java.awt.Dimension JavaDoc d = ob.getSize();
234             calculator.show(ob, 0, d.height);
235         }
236         
237         if (e.getKeyCode() == KeyEvent.VK_F4) {
238             System.out.println("F4 ");
239         }
240         
241         if (e.getKeyCode() == KeyEvent.VK_F5) {
242             System.out.println("F5 = PROCESS");
243             navigator.doCommitButtonClick();
244         }
245         
246         if (e.getKeyCode() == KeyEvent.VK_F6) {
247             System.out.println("F6 = DELETE");
248             navigator.doDeleteButtonClick();
249         }
250         
251         if (e.getKeyCode() == KeyEvent.VK_F8) {
252             System.out.println("F8 ");
253             navigator.doUndoButtonClick();
254         }
255         
256         if (e.getKeyCode() == KeyEvent.VK_END) {
257             System.out.println("END ");
258         }
259         
260         if (e.getKeyCode() == KeyEvent.VK_DELETE) {
261             System.out.println("DELETE ");
262         }
263         
264         if (e.getKeyCode() == KeyEvent.VK_HOME) {
265             System.out.println("HOME ");
266         }
267     }
268     
269     public void focusLost(FocusEvent e) {
270         /**
271          * some code to highlight the component with the focus
272          *
273          */

274         setBackground(std_color);
275     }
276     
277     public void focusGained(FocusEvent e) {
278         
279         /**
280          * some code to highlight the component with the focus
281          *
282          */

283         java.awt.Color JavaDoc col = new java.awt.Color JavaDoc(204,255,255);
284         std_color = getBackground();
285         setBackground(col);
286         
287         
288         /**
289          * This is a bug workaround
290          * see : http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4740914
291          *
292          */

293         SwingUtilities.invokeLater(new Runnable JavaDoc() {
294             public void run() {
295                 selectAll();
296             }
297         });
298     }
299     
300     /**
301      * This method perform the actual data transfer from rowset to this object Value field.
302      * depending on the column Type.
303      *
304      */

305     
306     private void DbToFm() {
307         
308         try {
309             
310             switch(colType) {
311                 
312                 case java.sql.Types.ARRAY://2003
313
break;
314                     
315                 case java.sql.Types.BINARY://-2
316
break;
317                     
318                 case java.sql.Types.BIT://-7
319
case java.sql.Types.BOOLEAN://16
320
this.setValue(new Boolean JavaDoc(rowset.getBoolean(columnName)));
321                     break;
322                     
323                 case java.sql.Types.BLOB://2004
324
break;
325
326                 case java.sql.Types.CLOB://2005
327
break;
328                     
329                 case java.sql.Types.DATALINK://70
330
break;
331                     
332                 case java.sql.Types.DATE://91
333
this.setValue(new java.util.Date JavaDoc(rowset.getDate(columnName).getTime()));
334                     break;
335                     
336                 case java.sql.Types.DECIMAL://3
337
break;
338                     
339                 case java.sql.Types.DISTINCT://2001
340
break;
341                     
342                 case java.sql.Types.DOUBLE://8
343
break;
344                     
345                 case java.sql.Types.FLOAT://6
346
break;
347                     
348                 case java.sql.Types.INTEGER://4
349
case java.sql.Types.BIGINT://-5
350
case java.sql.Types.SMALLINT://5
351
case java.sql.Types.TINYINT://-6
352
this.setValue(new Integer JavaDoc(rowset.getInt(columnName)));
353                     break;
354                     
355                 case java.sql.Types.JAVA_OBJECT://2000
356
break;
357                     
358                 case java.sql.Types.LONGVARBINARY://-4
359
case java.sql.Types.VARBINARY://-3
360
break;
361                     
362                 case java.sql.Types.VARCHAR://
363
case java.sql.Types.LONGVARCHAR://-1
364
case java.sql.Types.CHAR://1
365
this.setValue(rowset.getString(columnName));
366                     break;
367                     
368                 case java.sql.Types.NULL://0
369
break;
370                     
371                 case java.sql.Types.NUMERIC://2
372
break;
373                     
374                 case java.sql.Types.OTHER://1111
375
break;
376                     
377                 case java.sql.Types.REAL://7
378
break;
379                     
380                 case java.sql.Types.REF://2006
381
break;
382                     
383                 case java.sql.Types.STRUCT://2002
384
break;
385                     
386                 case java.sql.Types.TIME://92
387
break;
388                     
389                 case java.sql.Types.TIMESTAMP://93
390
break;
391                     
392                 default:
393                     break;
394             }
395         } catch (java.sql.SQLException JavaDoc sqe) {
396             System.out.println("Error in DbToFm() = " + sqe);
397         }
398     }
399     
400     public void mouseExited(MouseEvent e) {
401 // System.out.println("mouseExited");
402
}
403     
404     public void mouseEntered(MouseEvent e) {
405 // System.out.println("mouseEntered");
406
}
407     
408     public void mouseClicked(MouseEvent e) {
409 // System.out.println("mouseClicked");
410
// System.out.println("x= " + e.getComponent().getX() + " y= " + e.getComponent().getY());
411
}
412     
413     public void mousePressed(MouseEvent evt) {
414 // System.out.println("mousePressed");
415
if (evt.isPopupTrigger()) {
416 // System.out.println("isPopupTrigger");
417
menu.show(evt.getComponent(), evt.getX(), evt.getY());
418         }
419     }
420     
421     public void mouseReleased(MouseEvent evt) {
422 // System.out.println("mouseReleased");
423
if (evt.isPopupTrigger()) {
424 // System.out.println("isPopupTrigger");
425
menu.show(evt.getComponent(), evt.getX(), evt.getY());
426         }
427     }
428     
429     public void setHelper(JPopupMenu helper) {
430         this.helper = helper;
431
432         if (helper instanceof HelperPopup)
433             ((HelperPopup)this.helper).setTarget(this);
434   
435         if (helper instanceof RowSetHelperPopup)
436             ((RowSetHelperPopup)this.helper).setTarget(this);
437         
438     }
439     
440     public void showHelper(KeyEvent e) {
441         if (helper == null) return;
442         
443         JFormattedTextField JavaDoc ob = (JFormattedTextField JavaDoc)(e.getSource());
444         java.awt.Dimension JavaDoc d = ob.getSize();
445         helper.requestFocusInWindow();
446         helper.show(ob, 0, d.height);
447     }
448     
449     /**
450      * This method should implements validation AND, most important for our purposes
451      * implements actual rowset's fields updates.
452      *
453      */

454     
455     class internalVerifier extends InputVerifier {
456         
457         public boolean verify(JComponent input) {
458             
459             Object JavaDoc aux = null;
460             boolean passed = true;
461             
462             /**
463              * field to be validated and updated
464              */

465             
466             SSFormattedTextField tf = (SSFormattedTextField) input;
467             aux = tf.getValue();
468             
469             /**
470              * future NULL validation ....
471              *
472              * test null
473              */

474             //if (tf.isNullable() == false && tf.getValue().equals(null)) {
475
// passed = false;
476
//}
477

478             /**
479              *
480              * future test of numeric ranges
481              *
482              * properties to add:
483              * minValue
484              * maxValue
485              *
486              * If value is outside range, returns false and focus transfer is canceled (stay in same field, background set to RED).
487              */

488             
489             //int val = ((Integer)tf.getValue()).intValue();
490
//if (val < minValue) passed = false;
491
//if (val > maxValue) passed = false;
492

493             System.out.println("inputVerifier(): " + columnName);
494             
495             // if (aux == null) {
496
// passed = false;
497
// }
498

499             if (passed == true) {
500                 
501                 setBackground(java.awt.Color.WHITE);
502                 
503                 try {
504                     tf.commitEdit();
505                     System.out.println("Committed();");
506                 } catch (java.text.ParseException JavaDoc pe) {
507                     System.out.println("ParseException");
508                 }
509                 
510                 try {
511                     rowset.removeRowSetListener(tf);
512                     
513                     aux = tf.getValue();
514                     if (aux == null) {
515                         return false;
516                     }
517                     
518                     switch(colType) {
519                         
520                         case java.sql.Types.ARRAY://2003
521
break;
522                             
523                         case java.sql.Types.BINARY://-2
524
break;
525                             
526                         case java.sql.Types.BIT://-7
527
System.out.println("BIT - Set");
528                             rowset.updateBoolean(columnName, ((Boolean JavaDoc)tf.getValue()).booleanValue());
529                             break;
530                             
531                         case java.sql.Types.BLOB://2004
532
break;
533                             
534                         case java.sql.Types.BOOLEAN://16
535
System.out.println("BOOLEAN - Set");
536                             break;
537                             
538                         case java.sql.Types.CLOB://2005
539
break;
540                             
541                         case java.sql.Types.DATALINK://70
542
break;
543                             
544                         case java.sql.Types.DATE://91
545
rowset.updateDate(columnName, new java.sql.Date JavaDoc(((java.util.Date JavaDoc) aux).getTime()));
546                             break;
547                             
548                         case java.sql.Types.DECIMAL://3
549
System.out.println("DECIMAL");
550                             break;
551                             
552                         case java.sql.Types.DISTINCT://2001
553
break;
554                             
555                         case java.sql.Types.FLOAT://6
556
case java.sql.Types.DOUBLE://8
557
System.out.println("DOUBLE");
558                             if (aux instanceof Double JavaDoc) {
559                                 System.out.println("Double = columnName => " + columnName);
560                                 System.out.println("getValue() = " + aux);
561                                 rowset.updateDouble(columnName, ((Double JavaDoc)aux).intValue());
562                             } else if (aux instanceof Float JavaDoc) {
563                                 System.out.println("Float = columnName => " + columnName);
564                                 System.out.println("getValue() = " + aux);
565                                 rowset.updateFloat(columnName, ((Float JavaDoc)aux).intValue());
566                             } else {
567                                 System.out.println("ELSE ???");
568                             }
569                             System.out.println("getValue() = " + aux);
570                             break;
571                             
572                         case java.sql.Types.INTEGER: //4
573

574                         case java.sql.Types.BIGINT: //-5
575

576                         case java.sql.Types.SMALLINT: //5
577

578                         case java.sql.Types.TINYINT: //-6
579

580                             if (aux instanceof Integer JavaDoc) {
581                                 System.out.println("Integer = columnName => " + columnName);
582                                 System.out.println("getValue() = " + aux);
583                                 rowset.updateInt(columnName, ((Integer JavaDoc)aux).intValue());
584                             } else if (aux instanceof Long JavaDoc) {
585                                 System.out.println("Long = columnName => " + columnName);
586                                 System.out.println("getValue() = " + aux);
587                                 rowset.updateLong(columnName, ((Long JavaDoc)aux).intValue());
588                             } else {
589                                 System.out.println("ELSE ???");
590                             }
591                             System.out.println("getValue() = " + aux);
592                             break;
593                             
594                         case java.sql.Types.JAVA_OBJECT://2000
595
break;
596                             
597                         case java.sql.Types.LONGVARBINARY://-4
598
case java.sql.Types.VARBINARY://-3
599
break;
600                             
601                         case java.sql.Types.VARCHAR://
602
case java.sql.Types.LONGVARCHAR://-1
603
case java.sql.Types.CHAR://1
604
System.out.println("CHAR = columnName => " + columnName);
605                             System.out.println("getValue() = " + aux);
606                             rowset.updateString(columnName, aux.toString());
607                             break;
608                             
609                         case java.sql.Types.NULL://0
610
break;
611                             
612                         case java.sql.Types.NUMERIC://2
613
break;
614                             
615                         case java.sql.Types.OTHER://1111
616
break;
617                             
618                         case java.sql.Types.REAL://7
619
break;
620                             
621                         case java.sql.Types.REF://2006
622
break;
623                             
624                         case java.sql.Types.STRUCT://2002
625
break;
626                             
627                         case java.sql.Types.TIME://92
628
break;
629                             
630                         case java.sql.Types.TIMESTAMP://93
631
break;
632                             
633                         default:
634                             System.out.println("============================================================================");
635                             System.out.println("default = " + colType);
636                             System.out.println("columnName = " + columnName);
637                             System.out.println("============================================================================");
638                             
639                             if (aux instanceof java.lang.Double JavaDoc
640                                     && ((java.lang.Double JavaDoc) aux).doubleValue() < 0.0) {
641                                 tf.setForeground(Color.RED);
642                             } else {
643                                 tf.setForeground(Color.BLACK);
644                             }
645                             break;
646                     }
647                     rowset.addRowSetListener(tf);
648                 } catch (java.sql.SQLException JavaDoc se) {
649                     System.out.println("---> SQLException -----------> " + se);
650                 } catch(java.lang.NullPointerException JavaDoc np) {
651                     System.out.println("---> NullPointerException ---> " + np);
652                 }
653                 return true;
654             } else {
655                 /*
656                  * Validation fails.
657                  *
658                  */

659                 
660                 setBackground(java.awt.Color.RED);
661                 return false;
662             }
663         }
664     }
665 }
666
667 /*
668  * $Log: SSFormattedTextField.java,v $
669  * Revision 1.12 2005/02/22 15:14:34 yoda2
670  * Fixed some JavaDoc & deprecation errors/warnings.
671  *
672  * Revision 1.11 2005/02/04 22:42:06 yoda2
673  * Updated Copyright info.
674  *
675  * Revision 1.10 2005/01/24 14:04:16 dags
676  * bean preparation
677  *
678  * Revision 1.9 2005/01/19 19:33:50 dags
679  * bind refactoring
680  *
681  * Revision 1.8 2005/01/19 19:12:40 dags
682  * bind refactoring
683  *
684  * Revision 1.7 2005/01/18 22:43:08 dags
685  * Eliminate JCalculator test code, due to licence issues
686  *
687  * Revision 1.6 2005/01/18 22:36:55 dags
688  * Helpers refactoring
689  *
690  * Revision 1.5 2005/01/18 22:34:30 dags
691  * sincronization update
692  *
693  * Revision 1.4 2004/12/13 20:58:49 dags
694  * Added some javadoc tags
695  *
696  * Revision 1.3 2004/12/13 20:50:16 dags
697  * Fix package name
698  *
699  * Revision 1.2 2004/12/13 18:46:13 prasanth
700  * Added License.
701  *
702  */

703
Popular Tags