1 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 ; 39 import java.awt.event.FocusEvent ; 40 import java.awt.event.FocusListener ; 41 import java.awt.event.KeyEvent ; 42 import java.awt.event.KeyListener ; 43 import java.util.HashSet ; 44 import java.util.Set ; 45 import javax.sql.RowSetListener ; 46 import javax.swing.InputVerifier ; 47 import javax.swing.JComponent ; 48 import javax.swing.JTextArea ; 49 import javax.swing.KeyStroke ; 50 import javax.swing.SwingUtilities ; 51 52 56 public class SSMemoField extends JTextArea implements RowSetListener , KeyListener , FocusListener { 57 58 private java.awt.Color std_color = null; 59 private String columnName = null; 60 private int colType = -99; 61 private SSRowSet rowset = null; 62 private SSDataNavigator navigator = null; 63 64 65 public SSMemoField() { 66 super(); 67 68 Set forwardKeys = getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS); 69 Set newForwardKeys = new HashSet (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 (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 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: case java.sql.Types.LONGVARCHAR: case java.sql.Types.CHAR: this.setText(rowset.getString(columnName)); 114 break; 115 116 default: 117 break; 118 } 119 } catch (java.sql.SQLException 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 sqe) { 132 System.out.println("bind error = " + sqe); 133 } 134 rowset.addRowSetListener(this); 135 DbToFm(); 136 } 137 138 public void rowSetChanged(javax.sql.RowSetEvent event) { 139 140 } 141 142 public void rowChanged(javax.sql.RowSetEvent event) { 143 144 } 145 146 public void cursorMoved(javax.sql.RowSetEvent event) { 147 DbToFm(); 148 } 149 150 public void keyTyped(KeyEvent e) { 151 } 152 153 public void keyReleased(KeyEvent e) { 154 } 155 156 163 public void keyPressed(KeyEvent e) { 164 165 if (e.getKeyCode() == KeyEvent.VK_F1) { 166 } 168 169 if (e.getKeyCode() == KeyEvent.VK_F2) { 170 } 171 172 if (e.getKeyCode() == KeyEvent.VK_F3) { 173 System.out.println("F3 "); 174 180 } 182 183 if (e.getKeyCode() == KeyEvent.VK_F4) { 184 System.out.println("F4 "); 185 } 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 navigator.doUndoButtonClick(); 204 } 205 206 if (e.getKeyCode() == KeyEvent.VK_END) { 207 System.out.println("END "); 208 } 210 211 if (e.getKeyCode() == KeyEvent.VK_DELETE) { 212 System.out.println("DELETE "); 213 } 215 216 if (e.getKeyCode() == KeyEvent.VK_HOME) { 217 System.out.println("HOME "); 218 } 220 221 } 222 223 224 public void focusLost(FocusEvent e) { 225 229 setBackground(std_color); 230 } 231 232 public void focusGained(FocusEvent e) { 233 234 238 java.awt.Color col = new java.awt.Color (204,255,255); 239 std_color = getBackground(); 240 setBackground(col); 241 242 243 248 SwingUtilities.invokeLater(new Runnable () { 249 public void run() { 250 selectAll(); 251 } 252 }); 253 } 254 255 260 261 class internalVerifier extends InputVerifier { 262 263 public boolean verify(JComponent input) { 264 265 String 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: case java.sql.Types.LONGVARCHAR: case java.sql.Types.CHAR: rowset.updateString(columnName, aux); 290 break; 291 292 default: 293 break; 294 } 295 rowset.addRowSetListener(tf); 296 } catch (java.sql.SQLException se) { 297 System.out.println("---> SQLException -----------> " + se); 298 } catch(java.lang.NullPointerException np) { 299 System.out.println("---> NullPointerException ---> " + np); 300 } 301 return true; 302 } else { 303 307 308 setBackground(java.awt.Color.RED); 309 return false; 310 } 311 } 312 } 313 } 314 | Popular Tags |