KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: SSMemoField.java,v 1.4 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 import java.awt.KeyboardFocusManager JavaDoc;
39 import java.awt.event.FocusEvent JavaDoc;
40 import java.awt.event.FocusListener JavaDoc;
41 import java.awt.event.KeyEvent JavaDoc;
42 import java.awt.event.KeyListener JavaDoc;
43 import java.util.HashSet JavaDoc;
44 import java.util.Set JavaDoc;
45 import javax.sql.RowSetListener JavaDoc;
46 import javax.swing.InputVerifier JavaDoc;
47 import javax.swing.JComponent JavaDoc;
48 import javax.swing.JTextArea JavaDoc;
49 import javax.swing.KeyStroke JavaDoc;
50 import javax.swing.SwingUtilities JavaDoc;
51
52 /**
53  *
54  * @author dags
55  */

56 public class SSMemoField extends JTextArea JavaDoc implements RowSetListener JavaDoc, KeyListener JavaDoc, FocusListener JavaDoc {
57     
58     private java.awt.Color JavaDoc std_color = null;
59     private String JavaDoc columnName = null;
60     private int colType = -99;
61     private SSRowSet rowset = null;
62     private SSDataNavigator navigator = null;
63     
64     /** Creates a new instance of SSBooleanField */
65     public SSMemoField() {
66         super();
67         
68         Set forwardKeys = getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
69         Set newForwardKeys = new HashSet JavaDoc(forwardKeys);
70         newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
71         newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, java.awt.event.InputEvent.SHIFT_MASK ));
72         setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,newForwardKeys);
73         
74         Set backwardKeys = getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
75         Set newBackwardKeys = new HashSet JavaDoc(backwardKeys);
76         newBackwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_UP, java.awt.event.InputEvent.SHIFT_MASK ));
77         setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,newBackwardKeys);
78         
79         addKeyListener(this);
80         addFocusListener(this);
81         this.setInputVerifier(new internalVerifier());
82     }
83     
84     public void setColumnName(String JavaDoc columnName) {
85         this.columnName = columnName;
86         bind();
87     }
88     
89     public void setRowSet(SSRowSet rowset) {
90         this.rowset = rowset;
91         bind();
92     }
93     
94     public void setNavigator(SSDataNavigator navigator) {
95         this.navigator = navigator;
96         setRowSet(navigator.getSSRowSet());
97     }
98     
99     public SSDataNavigator getNavigator() {
100         return this.navigator;
101     }
102     
103     
104     private void DbToFm() {
105         
106         try {
107             
108             switch(colType) {
109                 
110                 case java.sql.Types.VARCHAR://-7
111
case java.sql.Types.LONGVARCHAR://-7
112
case java.sql.Types.CHAR://-7
113
this.setText(rowset.getString(columnName));
114                     break;
115                     
116                 default:
117                     break;
118             }
119         } catch (java.sql.SQLException JavaDoc sqe) {
120             System.out.println("Error in DbToFm() = " + sqe);
121         }
122     }
123     
124     private void bind() {
125         
126         if (this.columnName == null) return;
127         if (this.rowset == null) return;
128         
129         try {
130             colType = rowset.getColumnType(columnName);
131         } catch(java.sql.SQLException JavaDoc sqe) {
132             System.out.println("bind error = " + sqe);
133         }
134         rowset.addRowSetListener(this);
135         DbToFm();
136     }
137     
138     public void rowSetChanged(javax.sql.RowSetEvent JavaDoc event) {
139         
140     }
141     
142     public void rowChanged(javax.sql.RowSetEvent JavaDoc event) {
143         
144     }
145     
146     public void cursorMoved(javax.sql.RowSetEvent JavaDoc event) {
147         DbToFm();
148     }
149     
150     public void keyTyped(KeyEvent JavaDoc e) {
151     }
152     
153     public void keyReleased(KeyEvent JavaDoc e) {
154     }
155     
156     /**
157      * Catch severals keys, to implement some forms functionality (To be done).
158      *
159      *
160      *
161      *
162      */

163     public void keyPressed(KeyEvent JavaDoc e) {
164         
165         if (e.getKeyCode() == KeyEvent.VK_F1) {
166             //showHelper(e);
167
}
168         
169         if (e.getKeyCode() == KeyEvent.VK_F2) {
170         }
171         
172         if (e.getKeyCode() == KeyEvent.VK_F3) {
173             System.out.println("F3 ");
174             //calculator = new javax.swing.JPopupMenu();
175
//calculator.add(new com.nqadmin.swingSet.formatting.utils.JCalculator());
176
//JFormattedTextField ob = (JFormattedTextField)(e.getSource());
177
//java.awt.Dimension d = ob.getSize();
178
//calculator.show(ob, 0, d.height);
179

180             //((Component)e.getSource()).transferFocus();
181
}
182         
183         if (e.getKeyCode() == KeyEvent.VK_F4) {
184             System.out.println("F4 ");
185             //((Component)e.getSource()).transferFocus();
186
}
187         
188         if (e.getKeyCode() == KeyEvent.VK_F5) {
189             System.out.println("F5 = PROCESS");
190             if (navigator.updatePresentRow()==true) {
191                 System.out.println("Update Sucessfully");
192             }
193         }
194         
195         if (e.getKeyCode() == KeyEvent.VK_F6) {
196             System.out.println("F6 = DELETE");
197             navigator.doDeleteButtonClick();
198         }
199         
200         if (e.getKeyCode() == KeyEvent.VK_F8) {
201             System.out.println("F8 ");
202             //((Component)e.getSource()).transferFocus();
203
navigator.doUndoButtonClick();
204         }
205         
206         if (e.getKeyCode() == KeyEvent.VK_END) {
207             System.out.println("END ");
208             //((Component)e.getSource()).transferFocus();
209
}
210         
211         if (e.getKeyCode() == KeyEvent.VK_DELETE) {
212             System.out.println("DELETE ");
213             //((Component)e.getSource()).transferFocus();
214
}
215         
216         if (e.getKeyCode() == KeyEvent.VK_HOME) {
217             System.out.println("HOME ");
218             //((Component)e.getSource()).transferFocus();
219
}
220         
221     }
222     
223     
224     public void focusLost(FocusEvent JavaDoc e) {
225         /**
226          * some code to highlight the component with the focus
227          *
228          */

229         setBackground(std_color);
230     }
231     
232     public void focusGained(FocusEvent JavaDoc e) {
233         
234         /**
235          * some code to highlight the component with the focus
236          *
237          */

238         java.awt.Color JavaDoc col = new java.awt.Color JavaDoc(204,255,255);
239         std_color = getBackground();
240         setBackground(col);
241         
242         
243         /**
244          * This is a bug workaround
245          * see : http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4740914
246          *
247          */

248         SwingUtilities.invokeLater(new Runnable JavaDoc() {
249             public void run() {
250                 selectAll();
251             }
252         });
253     }
254     
255     /**
256      * This method should implements validation AND, most important for our purposes
257      * implements actual rowset fields updates.
258      *
259      */

260     
261     class internalVerifier extends InputVerifier JavaDoc {
262         
263         public boolean verify(JComponent JavaDoc input) {
264             
265             String JavaDoc aux = null;
266             boolean passed = true;
267             
268             SSMemoField tf = (SSMemoField) input;
269             aux = tf.getText();
270             
271             System.out.println("inputVerifier():");
272             
273             if (aux == null) {
274                 passed = false;
275             }
276             
277             if (passed == true) {
278                 
279                 setBackground(java.awt.Color.WHITE);
280                 
281                 try {
282                     rowset.removeRowSetListener(tf);
283                     
284                     switch(colType) {
285                         
286                         case java.sql.Types.VARCHAR://-7
287
case java.sql.Types.LONGVARCHAR://-7
288
case java.sql.Types.CHAR://-7
289
rowset.updateString(columnName, aux);
290                             break;
291                             
292                         default:
293                             break;
294                     }
295                     rowset.addRowSetListener(tf);
296                 } catch (java.sql.SQLException JavaDoc se) {
297                     System.out.println("---> SQLException -----------> " + se);
298                 } catch(java.lang.NullPointerException JavaDoc np) {
299                     System.out.println("---> NullPointerException ---> " + np);
300                 }
301                 return true;
302             } else {
303                 /*
304                  * Validation fails.
305                  *
306                  */

307                 
308                 setBackground(java.awt.Color.RED);
309                 return false;
310             }
311         }
312     }
313 }
314
Popular Tags