1 19 20 25 26 package org.netbeans.modules.db.sql.visualeditor.querybuilder; 27 28 import java.awt.Component ; 29 import java.awt.Dialog ; 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.ActionListener ; 32 33 import javax.swing.DefaultCellEditor ; 34 import javax.swing.JDialog ; 35 import javax.swing.JTable ; 36 import javax.swing.JTextField ; 37 38 import javax.swing.table.*; 39 40 import org.openide.DialogDescriptor; 41 import org.openide.DialogDisplayer; 42 import org.openide.util.HelpCtx; 43 import org.openide.util.NbBundle; 44 45 public class ParameterizedQueryDialog extends javax.swing.JPanel { 46 47 48 public static final int RETURNED_CANCEL = 0; 49 50 51 public static final int RETURNED_OK = 1; 52 53 public static final int PARAMETER_COLUMN = 0; 54 55 public static final int VALUE_COLUMN = 1; 56 57 private int returnStatus = RETURNED_CANCEL; 58 59 private Dialog dialog; 60 61 private DialogDescriptor dlg = null; 62 63 ParameterizedTableModel _pTableModel; 64 65 public ParameterizedQueryDialog() { 66 this(null, true); 67 } 68 69 70 public ParameterizedQueryDialog(String [] parameters, boolean modal) { 71 _pTableModel = new ParameterizedTableModel(); 72 initComponents(); 73 74 setParameters(parameters); 75 76 TableColumn column = parameterValueTable.getColumnModel().getColumn( 77 VALUE_COLUMN); 78 column.setCellEditor(new FocusCellEditor(new JTextField ())); 79 parameterValueTable.setRowSelectionAllowed(true); 80 parameterValueTable.setColumnSelectionAllowed(true); 81 parameterValueTable.setColumnSelectionInterval(VALUE_COLUMN,VALUE_COLUMN); 82 parameterValueTable.changeSelection(0, VALUE_COLUMN, false, false); 83 parameterValueTable.setRowSelectionInterval(0,0); 84 85 DefaultCellEditor dce = 86 (DefaultCellEditor )parameterValueTable.getDefaultEditor(Object .class); 87 dce.setClickCountToStart(1); 88 89 final JTable fTable = parameterValueTable; 90 Runnable r = new Runnable () { 91 public void run() { 92 fTable.requestFocusInWindow(); 94 } 95 }; 96 97 javax.swing.SwingUtilities.invokeLater(r); 99 100 ActionListener listener = new ActionListener () { 101 public void actionPerformed(ActionEvent evt) { 102 Object o = evt.getSource(); 103 104 Object [] option = dlg.getOptions(); 105 106 if (o == option[1]) { 107 returnStatus = RETURNED_CANCEL; 108 dialog.dispose(); 109 } 110 if (o == option[0]) { 111 returnStatus = RETURNED_OK; 112 dialog.dispose(); 113 } 114 } 115 }; 116 117 dlg = new DialogDescriptor(this, 118 NbBundle.getMessage(ParameterizedQueryDialog.class, 120 "SPECIFY_PARAMETER_VALUES"), modal, listener); 122 123 dlg.setOptions(new Object [] { okButton, cancelButton }); 124 dlg.setClosingOptions(new Object [] { okButton, cancelButton }); 125 126 dlg.setHelpCtx ( 127 new HelpCtx( "projrave_ui_elements_editors_about_query_editor" ) ); 129 dialog = (JDialog ) DialogDisplayer.getDefault().createDialog(dlg); 130 131 dialog.setResizable(true); 132 dialog.pack(); 134 dialog.show(); 135 } 136 137 140 public int getReturnStatus() { 141 return returnStatus; 142 } 143 144 147 public String [] getParameterValues() { 148 int rows = _pTableModel.getRowCount(); 149 String [] values = new String [rows]; 150 for (int i = 0; i < rows; i++) { 151 values[i] = (String ) _pTableModel.getValueAt(i, VALUE_COLUMN); 152 } 153 return values; 154 } 155 156 159 public void setParameterValues(String [] values) { 160 for (int i = 0; i < values.length; i++) { 161 _pTableModel.setValueAt(values[i], i, VALUE_COLUMN); 162 } 163 } 164 165 168 public String [] getParameters() { 169 int rows = _pTableModel.getRowCount(); 170 String [] parameters = new String [rows]; 171 for (int i = 0; i < rows; i++) { 172 parameters[i] = (String ) _pTableModel.getValueAt(i, 173 PARAMETER_COLUMN); 174 } 175 return parameters; 176 } 177 178 public void setParameters(String [] parameters) { 179 if (parameters == null || parameters.length == 0) 180 return; 181 _pTableModel.setRowCount(0); 182 for (int i = 0; i < parameters.length; i++) { 183 Object [] rowData = { parameters[i], "" }; _pTableModel.addRow(rowData); 186 } 187 } 188 189 class ParameterizedTable extends JTable { 190 public boolean getSurrendersFocusOnKeystroke() { 194 return true; 195 } 196 197 public void changeSelection(int row,int col,boolean toggle, 214 boolean expand) { 215 if (!this.getModel().isCellEditable(row,col)) { 221 int currentRow = getEditingRow(); 223 int currentCol = getEditingColumn(); 224 if (currentRow == -1) { 225 currentRow = getSelectedRow(); 226 } 227 if (currentCol == -1) { 228 currentCol = getSelectedColumn(); 229 } 230 231 int numberOfRows = getRowCount(); 233 int numberOfCols = getColumnCount(); 234 235 int nextRow = row; 237 int nextCol = col; 238 239 if (col==currentCol) { 240 int direction = row-currentRow; 242 if (direction>1) { 243 direction=1; 244 } 245 if (direction<-1) { 246 direction=-1; 247 } 248 nextRow = getNextEditableRow(row,col,direction, 249 numberOfRows,numberOfCols); 250 } else if (row == currentRow) { 251 int direction = col-currentCol; 252 if (direction>1) { 253 direction=1; 254 } 255 if (direction<-1) { 256 direction=-1; 257 } 258 int[] nextCell = getNextEditableCell(row,col,direction, 259 numberOfRows,numberOfCols); 260 nextRow = nextCell[0]; 261 nextCol = nextCell[1]; 262 } else { 263 int direction = row-currentRow; 264 if (direction>1) { 265 direction=1; 266 } 267 if (direction<-1) { 268 direction=-1; 269 } 270 if ((row==0) && (currentRow==numberOfRows-1)) { 271 direction=1; 272 } 273 int[] nextCell = getNextEditableCell(row,col, 274 direction,numberOfRows,numberOfCols); 275 nextRow = nextCell[0]; 276 nextCol = nextCell[1]; 277 } 278 super.changeSelection(nextRow,nextCol,toggle,expand); 280 } else { 281 super.changeSelection(row,col,toggle,expand); 283 } 284 } 285 286 int[] getNextEditableCell(int row,int col, 289 int direction,int numberOfRows,int numberOfCols) { 290 int originalRow=row; 291 int originalCol=col; 292 do { 294 col = col+direction; 295 if (col>=numberOfCols) { 296 col = 0; 297 row += direction; 298 } 299 if (col<0) { 300 col = numberOfCols-1; 301 row += direction; 302 } 303 if (row>=numberOfRows) { 304 row = 0; 305 } 306 if (row<0) { 307 row = numberOfRows-1; 308 } 309 if (isCellEditable(row,col)) { 310 return new int[]{row,col}; 311 } 312 } while (!((row==originalRow)&&(col==originalCol))); 313 314 return new int[]{originalRow,originalCol}; 316 317 } 318 319 int getNextEditableRow(int row,int col,int direction,int numberOfRows,int numberOfCols) { 321 int originalRow = row; 322 do { 324 row = row+direction; 325 if (row<0) { 326 row = numberOfRows-1; 327 } 328 if (row>=numberOfRows) { 329 row=0; 330 } 331 if (isCellEditable(row,col)) { 332 return row; 333 } 334 } while (row != originalRow); 335 return originalRow; 337 } 338 } 339 340 class ParameterizedTableModel extends DefaultTableModel { 341 342 final String [] columnNames = { 343 NbBundle.getMessage(ParameterizedQueryDialog.class, 345 "PARAMETER"), NbBundle.getMessage(ParameterizedQueryDialog.class, 348 "VALUE") }; 350 351 Object [][] data = { { "", "" } }; 352 353 public ParameterizedTableModel() { 354 super(0, 2); 355 setColumnIdentifiers(columnNames); 356 } 357 358 public boolean isCellEditable(int row, int col) { 359 if (col < 1) 360 return false; 361 else 362 return true; 363 } 364 } 365 366 private class FocusCellEditor extends DefaultCellEditor { 369 Component c; 370 371 public FocusCellEditor(JTextField jtf) { 372 super(jtf); 373 addFocusListener(jtf); 374 } 375 376 private void addFocusListener(Component C) { 377 C.getClass(); 378 super.getComponent().addFocusListener( 379 new java.awt.event.FocusAdapter () { 380 public void focusLost(java.awt.event.FocusEvent fe) { 381 lostFocus(); 382 } 383 }); 384 } 385 386 public void lostFocus() { 387 stopCellEditing(); 388 } 389 } 390 391 396 private void initComponents() { 398 mainPanel = new javax.swing.JPanel (); 399 messageAreaTablePanel = new javax.swing.JPanel (); 400 messageAreaPanel = new javax.swing.JPanel (); 401 messageArea = new javax.swing.JTextArea (); 402 parameterValueTablePanel = new javax.swing.JPanel (); 403 parameterValueTableScrollPane = new javax.swing.JScrollPane (); 404 parameterValueTable = new ParameterizedTable(); 405 406 setLayout(new java.awt.GridLayout ()); 408 409 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (20, 20, 20, 20))); 410 mainPanel.setLayout(new java.awt.BorderLayout ()); 411 412 mainPanel.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (0, 0, 0, 0))); 413 414 okButton = new javax.swing.JButton (); 415 cancelButton = new javax.swing.JButton (); 416 helpButton = new javax.swing.JButton (); 417 418 mainPanel.setLayout(new java.awt.BorderLayout ()); 419 420 mainPanel.setBorder(new javax.swing.border.EmptyBorder ( 421 new java.awt.Insets (0, 0, 0, 0))); 422 messageAreaTablePanel.setLayout(new java.awt.BorderLayout ()); 423 424 messageAreaTablePanel.setBorder(new javax.swing.border.EmptyBorder ( 425 new java.awt.Insets (0, 0, 0, 0))); 426 messageAreaPanel.setLayout(new java.awt.BorderLayout ()); 427 428 messageAreaPanel.setBorder(new javax.swing.border.EmptyBorder ( 429 new java.awt.Insets (0, 0, 0, 0))); 430 messageArea.setBackground(new java.awt.Color (212, 208, 200)); 432 messageArea.setEditable(false); 433 messageArea.setLineWrap(true); 434 messageArea 435 .setText( NbBundle.getMessage(ParameterizedQueryDialog.class, 436 "PAREMETERIZED_QURY_MESSAGE") ); messageArea.setWrapStyleWord(true); 438 messageArea.setBorder(new javax.swing.border.EmptyBorder ( 439 new java.awt.Insets (10, 10, 10, 10))); 440 messageAreaPanel.add(messageArea, java.awt.BorderLayout.CENTER); 441 442 messageAreaTablePanel 443 .add(messageAreaPanel, java.awt.BorderLayout.NORTH); 444 445 parameterValueTablePanel.setLayout(new java.awt.BorderLayout ()); 446 447 parameterValueTablePanel.setBorder(new javax.swing.border.EmptyBorder ( 448 new java.awt.Insets (0, 0, 0, 0))); 449 parameterValueTableScrollPane 450 .setBorder(new javax.swing.border.EmptyBorder ( 451 new java.awt.Insets (0, 0, 0, 0))); 452 parameterValueTableScrollPane 453 .setViewportBorder(new javax.swing.border.EmptyBorder ( 454 new java.awt.Insets (0, 0, 0, 0))); 455 parameterValueTable.setModel(_pTableModel); 456 457 parameterValueTableScrollPane.setViewportView(parameterValueTable); 458 parameterValueTable.setPreferredScrollableViewportSize ( 459 new java.awt.Dimension (450,100)); 460 parameterValueTable.setRowHeight(24); 461 java.awt.Dimension dim = parameterValueTable.getTableHeader() 462 .getPreferredSize(); 463 java.awt.Dimension newDim = new java.awt.Dimension ( 464 (int) dim.getWidth(), 25); 465 467 parameterValueTableScrollPane.setViewportView(parameterValueTable); 468 469 parameterValueTablePanel.add(parameterValueTableScrollPane, 470 java.awt.BorderLayout.CENTER); 471 472 messageAreaTablePanel.add(parameterValueTablePanel, 473 java.awt.BorderLayout.CENTER); 474 475 mainPanel.add(messageAreaTablePanel, java.awt.BorderLayout.CENTER); 476 477 okButton.setText( NbBundle.getMessage(ParameterizedQueryDialog.class, 478 "OK") ); 481 cancelButton.setText(NbBundle.getMessage(ParameterizedQueryDialog.class, 482 "CANCEL") ); 485 helpButton.setText(NbBundle.getMessage(ParameterizedQueryDialog.class, 486 "HELP") ); 489 add(mainPanel); 490 491 } 493 496 public static void main(String args[]) { 497 java.awt.EventQueue.invokeLater(new Runnable () { 498 public void run() { 499 String [] parameters = new String [5]; 500 String [] values = new String [5]; 501 for (int i = 0; i < parameters.length; i++) { 502 parameters[i] = new String ("Table.column" + i); 503 } 504 ParameterizedQueryDialog pqDlg = new ParameterizedQueryDialog( 505 parameters, true); 506 System.out.println(pqDlg.getReturnStatus()); 507 if (pqDlg.getReturnStatus() == ParameterizedQueryDialog.RETURNED_OK) { 508 values = pqDlg.getParameterValues(); 509 510 for (int i = 0; i < values.length; i++) { 511 System.out.println(values[i] + "\n"); 512 } 513 } 514 } 515 }); 516 } 517 518 private javax.swing.JPanel mainPanel; 520 521 private javax.swing.JButton helpButton; 522 523 private javax.swing.JButton cancelButton; 524 525 private javax.swing.JTextArea messageArea; 526 527 private javax.swing.JPanel messageAreaPanel; 528 529 private javax.swing.JPanel messageAreaTablePanel; 530 531 private javax.swing.JButton okButton; 532 533 private ParameterizedTable parameterValueTable; 534 535 private javax.swing.JPanel parameterValueTablePanel; 536 537 private javax.swing.JScrollPane parameterValueTableScrollPane; 538 539 541 } 542 | Popular Tags |