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