1 33 34 package com.nqadmin.swingSet.formatting.helpers; 35 36 import com.nqadmin.swingSet.datasources.SSConnection; 37 import com.nqadmin.swingSet.datasources.SSRowSet; 38 import com.nqadmin.swingSet.formatting.SSFormattedTextField; 39 import java.awt.BorderLayout ; 40 import java.awt.event.ActionListener ; 41 import java.awt.event.FocusListener ; 42 import java.awt.event.KeyEvent ; 43 import java.awt.event.KeyListener ; 44 import java.awt.event.MouseListener ; 45 import java.sql.SQLException ; 46 import javax.swing.BoxLayout ; 47 import javax.swing.DefaultListSelectionModel ; 48 import javax.swing.DefaultListModel ; 49 import javax.swing.JButton ; 50 import javax.swing.JPanel ; 51 import javax.swing.JPopupMenu ; 52 import javax.swing.JScrollPane ; 53 import javax.swing.JTextField ; 54 55 import javax.swing.event.ListSelectionListener ; 56 import javax.swing.event.PopupMenuListener ; 57 58 62 public class RowSetHelperPopup extends JPopupMenu implements MouseListener , KeyListener , ActionListener , ListSelectionListener , PopupMenuListener , FocusListener { 63 64 private JPanel spane; 65 private JPanel buttons; 66 private JPanel tpane; 67 private JButton searchButton; 68 private JButton closeButton; 69 private JButton refreshButton; 70 private JButton helpButton; 71 private JTextField searchText; 72 73 private String table = null; 74 private String dataColumn = null; 75 private String listColumn = null; 76 private String orderBy = null; 77 private String query = null; 78 79 private SSFormattedTextField target = null; 80 private JScrollPane sc; 81 private SSConnection connection; 82 private DefaultListModel model = null; 83 private SelectorList lista; 84 85 private SSRowSet rowset = null; 86 87 88 public RowSetHelperPopup() { 89 90 spane = new JPanel (); 92 spane.setLayout(new BorderLayout ()); 93 spane.setBorder(new javax.swing.border.TitledBorder (" RowSet Helper ")); 94 95 tpane = new JPanel (); 97 tpane.setLayout(new BorderLayout ()); 98 99 buttons = new JPanel (); 101 buttons.setLayout(new BoxLayout (buttons, BoxLayout.X_AXIS)); 102 103 searchButton = new JButton ("Search"); 104 searchButton.addActionListener(this); 105 106 closeButton = new JButton ("Close"); 107 closeButton.addActionListener(this); 108 109 refreshButton = new JButton ("Refresh"); 110 refreshButton.addActionListener(this); 111 112 helpButton = new JButton ("Help"); 113 helpButton.addActionListener(this); 114 115 searchText = new JTextField (); 116 searchText.setColumns(20); 117 searchText.addActionListener(this); 118 searchText.addFocusListener(this); 119 tpane.add(searchText, BorderLayout.NORTH); 120 121 buttons.add(searchButton); 122 buttons.add(closeButton); 123 buttons.add(refreshButton); 124 buttons.add(helpButton); 125 126 tpane.add(buttons, BorderLayout.SOUTH); 127 128 lista = new SelectorList(); 129 lista.addKeyListener(this); 130 lista.addMouseListener(this); 131 lista.setVisibleRowCount(10); 132 133 sc = new JScrollPane (lista); 134 135 spane.add(tpane, BorderLayout.NORTH); 136 spane.add(sc , BorderLayout.CENTER); 137 138 this.add(spane); 139 this.addPopupMenuListener(this); 140 this.setEnabled(true); 141 this.setFocusable(true); 142 this.addFocusListener(this); 143 this.pack(); 144 } 145 146 public void setRowSet(SSRowSet rowset) { 147 this.rowset = rowset; 148 } 149 150 public void setModel(DefaultListModel model) { 151 this.model = model; 152 lista.setModel(model); 153 } 154 155 public void setTarget(SSFormattedTextField target) { 156 this.target = target; 157 158 160 } 161 162 public void setTable(String table) { 163 this.table = table; 164 } 165 166 public void setDataColumn(String dataColumn) { 167 this.dataColumn = dataColumn; 168 } 169 170 public void setListColumn(String listColumn) { 171 this.listColumn = listColumn; 172 } 173 174 public void setOrderBy(String orderBy) { 175 this.orderBy = orderBy; 176 } 177 178 public void setConnection(SSConnection connection) { 179 this.connection = connection; 180 181 try { 182 connection.createConnection(); 183 } catch(java.lang.ClassNotFoundException nfe) { 184 185 } catch(java.lang.Exception ex) { 186 187 } 188 } 189 190 public void createHelper() { 191 200 model = new DefaultListModel (); 201 202 try { 203 System.out.println("beforeFirst();"); 204 System.out.println("dataColumn = " + dataColumn); 205 System.out.println("listColumn = " + listColumn); 206 207 rowset.beforeFirst(); 208 while (rowset.next()) { 209 System.out.println("rowset.next() =" + rowset.getString(listColumn)); 210 String s1 = rowset.getString(dataColumn); 211 String s2 = rowset.getString(listColumn); 212 model.addElement(new SelectorElement(s1,s2)); 213 } 214 } catch (SQLException se) { 215 System.out.println("rowset.next()" + se); 216 } catch (java.lang.NullPointerException np) { 217 System.out.println(np); 218 } 219 220 lista.setModel(model); 221 lista.getSelectionModel().addListSelectionListener(this); 222 lista.setVisibleRowCount(10); 223 pack(); 224 } 225 226 public void keyPressed(java.awt.event.KeyEvent e) { 227 System.out.println("keyPressed"); 228 System.out.println("KeyCode = " + KeyEvent.getKeyText(e.getKeyCode())); 229 230 if (e.getKeyCode() == KeyEvent.VK_ENTER) { 231 this.setVisible(false); 232 } 233 } 234 235 public void keyTyped(java.awt.event.KeyEvent e) { 236 System.out.println("keyTyped"); 237 } 238 239 public void keyReleased(java.awt.event.KeyEvent e) { 240 System.out.println("keyReleased"); 241 } 242 243 public void mouseReleased(java.awt.event.MouseEvent e) { 244 } 245 246 public void mousePressed(java.awt.event.MouseEvent e) { 247 } 248 249 public void mouseExited(java.awt.event.MouseEvent e) { 250 } 251 252 public void mouseEntered(java.awt.event.MouseEvent e) { 253 } 254 255 public void mouseClicked(java.awt.event.MouseEvent e) { 256 System.out.println("mouseClicked"); 257 258 if (e.getClickCount() == 2) { 259 this.setVisible(false); 260 } 261 262 if (e.getClickCount() == 321) { 263 this.setVisible(false); 264 } 265 } 266 267 public void actionPerformed(java.awt.event.ActionEvent e) { 268 269 if (e.getSource().equals(searchButton)) { 270 System.out.println("searchButton"); 271 search(); 272 } 273 if (e.getSource().equals(closeButton)) { 274 System.out.println("closeButton"); 275 this.setVisible(false); 276 } 277 if (e.getSource().equals(refreshButton)) { 278 System.out.println("refreshButton"); 279 createHelper(); 280 } 281 282 if (e.getSource().equals(helpButton)) { 283 int index = lista.getSelectedIndex(); 284 lista.ensureIndexIsVisible(index); 285 } 286 287 if (e.getSource().equals(searchText)) { 288 search(); 289 } 290 } 291 292 293 private void search() { 294 295 int j, n; 296 297 String toFind = searchText.getText().toUpperCase().trim(); 299 searchText.setText(toFind); 300 System.out.println("Texto a buscar : " + toFind); 301 n = lista.getModel().getSize(); 302 303 308 j = lista.getSelectedIndex() + 1; 309 310 System.out.println("j = " + j + " n = " + n); 311 for (; j < n; j++) { 312 String texto = lista.getModel().getElementAt(j).toString().toUpperCase(); 313 System.out.println("Comparando con " + texto); 314 315 if (texto.indexOf(toFind) != -1) { lista.setSelectedIndex(j); 317 lista.ensureIndexIsVisible(j); 318 lista.requestFocus(); 319 break; 320 } 321 } 322 } 323 324 public void valueChanged(javax.swing.event.ListSelectionEvent e) { 325 int desde; 326 int hasta; 327 int selected; 328 329 331 if (e.getValueIsAdjusting() == false) { 332 desde = e.getFirstIndex(); 333 hasta = e.getLastIndex(); 334 336 DefaultListSelectionModel lm = ((DefaultListSelectionModel )e.getSource()); 337 338 selected = lm.getLeadSelectionIndex(); 339 340 System.out.println("--------------------- desde --------------------------------------"); 341 SelectorElement se1 = (SelectorElement) (lista.getModel().getElementAt(desde)); 342 345 System.out.println("--------------------- hasta --------------------------------------"); 346 SelectorElement se2 = (SelectorElement) (lista.getModel().getElementAt(hasta)); 347 350 System.out.println("--------------------- selected --------------------------------------"); 351 SelectorElement se3 = (SelectorElement) (lista.getModel().getElementAt(selected)); 352 355 } 356 } 357 358 public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent e) { 359 360 int index = -1; 361 362 try { 363 index = rowset.getRow() -1; 364 } 365 catch(java.sql.SQLException se) { 366 367 } 368 369 lista.setSelectedIndex(index); 370 lista.ensureIndexIsVisible(index); 371 searchText.requestFocusInWindow(); 372 searchText.selectAll(); 373 } 374 375 public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent e) { 376 377 System.out.println("popupMenuWillBecomeInvisible();"); 378 int index = lista.getSelectedIndex() + 1; 379 380 if (index > -1 && target != null) { 381 try { 382 rowset.absolute(index); 383 } 384 catch(java.sql.SQLException se) { 385 386 } 387 } 388 } 389 390 public void popupMenuCanceled(javax.swing.event.PopupMenuEvent e) { 391 System.out.println("popupMenuCanceled();"); 392 } 393 394 public void show(java.awt.Component invoker, int x, int y) { 395 System.out.println("show(" + x + "," + y + ")"); 396 this.setSize(target.getWidth(), searchText.getHeight() * 15); 397 searchText.requestFocusInWindow(); 398 super.show(invoker, x, y); 399 } 400 401 public void focusLost(java.awt.event.FocusEvent e) { 402 System.out.println("focusLost"); 403 } 404 405 public void focusGained(java.awt.event.FocusEvent e) { 406 System.out.println("focusGained"); 407 } 408 } 409 | Popular Tags |