1 19 package org.netbeans.modules.db.sql.visualeditor.querybuilder; 20 21 import java.util.ArrayList ; 22 import java.util.List ; 23 24 import javax.swing.JTable ; 25 import javax.swing.JComboBox ; 26 import javax.swing.JCheckBox ; 27 import javax.swing.DefaultCellEditor ; 28 import javax.swing.table.TableCellEditor ; 29 import javax.swing.table.TableCellRenderer ; 30 import javax.swing.JPopupMenu ; 31 import javax.swing.JMenu ; 32 import javax.swing.JMenuItem ; 33 import javax.swing.JTextField ; 34 import javax.swing.table.TableColumn ; 35 import javax.swing.table.DefaultTableModel ; 36 37 import java.awt.*; 38 39 import java.awt.event.*; 40 41 import org.openide.ErrorManager; 42 import org.openide.util.NbBundle; 43 import org.openide.NotifyDescriptor; 44 import org.openide.DialogDisplayer; 45 46 import org.netbeans.modules.db.sql.visualeditor.Log; 47 48 import org.netbeans.modules.db.sql.visualeditor.querymodel.Column; 49 import org.netbeans.modules.db.sql.visualeditor.querymodel.Where; 50 import org.netbeans.modules.db.sql.visualeditor.querymodel.Predicate; 51 import org.netbeans.modules.db.sql.visualeditor.querymodel.Value; 52 import org.netbeans.modules.db.sql.visualeditor.querymodel.SortSpecification; 53 import org.netbeans.modules.db.sql.visualeditor.querymodel.SQLQueryFactory; 54 import org.netbeans.modules.db.sql.visualeditor.querymodel.OrderBy; 55 import org.netbeans.modules.db.sql.visualeditor.querymodel.Expression; 56 import org.netbeans.modules.db.sql.visualeditor.querymodel.And; 57 import org.netbeans.modules.db.sql.visualeditor.querymodel.ExpressionList; 58 import org.netbeans.modules.db.sql.visualeditor.querymodel.Literal; 59 60 64 65 72 public class QueryBuilderInputTable extends JTable 73 implements ActionListener, ItemListener, KeyListener 74 { 76 79 public static final int Column_COLUMN = 0; 80 public static final int Alias_COLUMN = 1; 81 public static final int Table_COLUMN = 2; 82 public static final int Output_COLUMN = 3; 83 public static final int SortType_COLUMN = 4; 84 public static final int SortOrder_COLUMN = 5; 85 public static final int Criteria_COLUMN = 6; 86 public static final int CriteriaOrder_COLUMN = 7; 87 88 89 public static final int Column_COLUMN_WIDTH = 140; 90 public static final int Alias_COLUMN_WIDTH = 70; 91 public static final int Table_COLUMN_WIDTH = 180; 92 public static final int SortType_COLUMN_WIDTH = 80; 95 public static final int SortOrder_COLUMN_WIDTH = 80; 96 public static final int Criteria_COLUMN_WIDTH = 100; 97 public static final int CriteriaOrder_COLUMN_WIDTH = 40; 98 99 public static final String Criteria_Uneditable_String = "*****"; 100 public static final String CriteriaOrder_Uneditable_String = "*"; 101 103 private static final boolean DEBUG = false; 104 private QueryBuilder _queryBuilder; 105 private JPopupMenu _inputTablePopup; 106 private int _inputTablePopupRow; 107 private int _inputTablePopupColumn; 108 private JComboBox _sortOrderComboBox; private JComboBox _criteriaOrderComboBox; 110 111 AddQueryParameterDlg _addQueryParameterDlg = null; 112 113 114 116 public QueryBuilderInputTable(QueryBuilder queryBuilder) { 117 118 super(); 119 120 Log.err.log(ErrorManager.INFORMATIONAL, "Entering QueryBuilderInputTable ctor"); 122 _queryBuilder = queryBuilder; 123 124 QueryBuilderInputTableModel queryBuilderInputTableModel = new QueryBuilderInputTableModel(); 125 126 super.setModel( queryBuilderInputTableModel ); 127 128 TableColumn column = this.getColumnModel().getColumn(Column_COLUMN); 129 column.setPreferredWidth(Column_COLUMN_WIDTH); 130 131 column = this.getColumnModel().getColumn(Alias_COLUMN); 132 column.setPreferredWidth(Alias_COLUMN_WIDTH); 133 column.setCellEditor(new FocusCellEditor(new JTextField ())); 134 135 column = this.getColumnModel().getColumn(Table_COLUMN); 136 column.setPreferredWidth(Table_COLUMN_WIDTH); 137 138 column = this.getColumnModel().getColumn(SortType_COLUMN); 139 column.setPreferredWidth(SortType_COLUMN_WIDTH); 140 141 column = this.getColumnModel().getColumn(SortOrder_COLUMN); 142 column.setPreferredWidth(SortOrder_COLUMN_WIDTH); 143 144 column = this.getColumnModel().getColumn(Criteria_COLUMN); 145 column.setPreferredWidth(Criteria_COLUMN_WIDTH); 146 column.setCellEditor(new FocusCellEditor(new JTextField ())); 147 148 column = this.getColumnModel().getColumn(CriteriaOrder_COLUMN); 149 column.setPreferredWidth(CriteriaOrder_COLUMN_WIDTH); 150 151 this.getColumnModel().getColumn(0).setCellEditor( 152 new FocusCellEditor(new JTextField ())); 153 154 final Object [] sortTypeItems = { 155 "", 156 NbBundle.getMessage(QueryBuilderInputTable.class, "ASCENDING"), NbBundle.getMessage(QueryBuilderInputTable.class, "DESCENDING") }; 159 TableColumn sortTypeColumn = this.getColumnModel().getColumn(SortType_COLUMN); 160 JComboBox sortTypeComboBox = new JComboBox (sortTypeItems); 161 sortTypeColumn.setCellEditor(new DefaultCellEditor (sortTypeComboBox)); 162 sortTypeComboBox.addItemListener(this); 163 164 final Object [] sortOrderItems = {""}; TableColumn sortOrderColumn = this.getColumnModel().getColumn(SortOrder_COLUMN); 166 _sortOrderComboBox = new JComboBox (sortOrderItems); 167 sortOrderColumn.setCellEditor(new DefaultCellEditor (_sortOrderComboBox)); 168 _sortOrderComboBox.addItemListener(this); 169 170 final Object [] criteriaOrderItems = {""}; TableColumn criteriaOrderColumn = this.getColumnModel().getColumn(CriteriaOrder_COLUMN); 172 _criteriaOrderComboBox = new JComboBox (criteriaOrderItems); 173 criteriaOrderColumn.setCellEditor(new DefaultCellEditor (_criteriaOrderComboBox)); 174 176 this.setAutoResizeMode (JTable.AUTO_RESIZE_OFF); 177 _inputTablePopup = createInputTablePopup(); 178 MouseListener inputTablePopupListener = new InputTablePopupListener(); 179 super.addMouseListener(inputTablePopupListener); 180 this.setMinimumSize(new Dimension (200, 200) ); 181 this.setBackground(Color.white); 182 this.getTableHeader().setReorderingAllowed (false); 183 184 addKeyListener(this); 185 186 188 } 194 195 private class FocusCellEditor extends DefaultCellEditor { 198 Component c; 199 public FocusCellEditor(JTextField jtf) { 200 super(jtf); 201 addFocusListener(jtf); 202 } 203 private void addFocusListener(Component C) { 204 super.getComponent().addFocusListener(new java.awt.event.FocusAdapter () { 205 public void focusLost(java.awt.event.FocusEvent fe) { lostFocus(); } 206 }); 207 } 208 public void lostFocus() { 209 stopCellEditing(); 210 } 211 } 212 213 214 215 public void keyTyped(KeyEvent e) { 216 } 217 218 219 public void keyReleased(KeyEvent e) { 220 } 221 222 224 public void keyPressed(KeyEvent e) { 225 if( e.isShiftDown() ) { 226 int code = e.getKeyCode(); 227 switch(code) { 228 case KeyEvent.VK_F10: 230 JTable source = (JTable )(e.getSource()); 231 232 if (DEBUG) 233 System.out.println( "QBIT : keyPressed called Shift+F10 Down source.isEnabled() returns : " + source.isEnabled() + "\n" ); 234 235 if ( ! source.isEnabled () ) return; 236 237 _inputTablePopupRow = source.getSelectedRow(); 239 _inputTablePopupColumn = source.getEditingColumn(); 240 if (_inputTablePopupColumn == (Criteria_COLUMN-1)) { 241 source.setEditingColumn(Column_COLUMN); 242 } 243 if (DEBUG) 244 System.out.println( "QBIT : keyPressed called\n" 245 + " inputTablePopupRow = " + _inputTablePopupRow + " inputTablePopupColumn == Criteria_COLUMN " + (_inputTablePopupRow == Criteria_COLUMN ) + " inputTablePopupColumn = " + _inputTablePopupColumn ); if (_inputTablePopupRow != -1) { 250 source.setRowSelectionInterval (_inputTablePopupRow, 251 _inputTablePopupRow); 252 } 253 _inputTablePopup.show ( source, source.getWidth() / 2, 254 source.getHeight() / 2 ); 255 break; 256 } 257 } 258 _queryBuilder.handleKeyPress(e); 259 } 260 261 JComboBox getSortOrderComboBox () { 262 return _sortOrderComboBox; } 264 265 JComboBox getCriteriaOrderComboBox () { 266 return _criteriaOrderComboBox; 267 } 268 269 JPopupMenu createInputTablePopup() 270 { 271 JPopupMenu inputTablePopup; 272 JMenu menu, subMenu; 273 JMenuItem menuItem; 274 JMenuItem subMenuItem; 275 276 inputTablePopup = new JPopupMenu (); 278 279 285 286 menuItem = new JMenuItem (NbBundle.getMessage(QueryBuilderInputTable.class, "ADD_QUERY_CRITERIA")); menuItem.addActionListener(this); 288 inputTablePopup.add(menuItem); 289 290 299 300 304 334 338 return ( inputTablePopup ); 339 } 340 341 342 344 345 class InputTablePopupListener extends MouseAdapter { 347 public void mousePressed(MouseEvent e) { 348 JTable source = (JTable )(e.getSource()); 349 350 maybeShowPopup(e); 351 } 352 353 public void mouseReleased(MouseEvent e) { 354 mousePressed(e); 355 } 356 357 private void maybeShowPopup(MouseEvent e) { 358 if (e.isPopupTrigger()) { 359 JTable source = (JTable )(e.getSource()); 360 361 if ( ! source.isEnabled () ) return; 362 363 _inputTablePopupRow = 364 source.rowAtPoint(new Point (e.getX(), e.getY())); 365 _inputTablePopupColumn = 366 source.columnAtPoint(new Point (e.getX(), e.getY())); 367 if (_inputTablePopupRow != -1) { 369 source.setRowSelectionInterval (_inputTablePopupRow, 370 _inputTablePopupRow); 371 } 372 378 _inputTablePopup.show(e.getComponent(), e.getX(), e.getY()); 379 } 380 } 381 } 382 383 384 387 public void actionPerformed(ActionEvent e) { 388 389 JMenuItem source = (JMenuItem )(e.getSource()); 390 391 if (source.getText().equals(NbBundle.getMessage(QueryBuilderInputTable.class, "ADD_QUERY_CRITERIA"))) { 393 _addQueryParameterDlg = 395 new AddQueryParameterDlg(true, (String )(this.getValueAt(getSelectedRow(), Column_COLUMN))); 396 397 if (DEBUG) 398 System.out.println( "QBIT : actionPerformed called\n_addQueryParameterDlg returns : " + (_addQueryParameterDlg.getReturnStatus() == AddQueryParameterDlg.RET_OK) ); 400 if ( _addQueryParameterDlg.getReturnStatus() == AddQueryParameterDlg.RET_OK ) 401 { 402 _queryBuilder._updateText=false; 404 try { 405 406 String result = (String )(_addQueryParameterDlg.getCriteria()); 408 if (DEBUG) 409 System.out.println( "QBIT : actionPerformed called\nresult " + result + " length : " + (result.trim().length() ) + " inputTablePopupRow = " + _inputTablePopupRow + " inputTablePopupColumn = " + _inputTablePopupColumn ); 411 if ( result.trim().length() == 0 ) 412 return; 413 414 this.getCellEditor ( _inputTablePopupRow, Criteria_COLUMN ).stopCellEditing(); 415 416 this.setValueAt ( result, _inputTablePopupRow, Criteria_COLUMN ); 418 } 419 finally { 420 _queryBuilder._updateText=true; 421 } 422 423 _queryBuilder.generateText(); 425 } 426 } 427 else if (source.getText().equals(NbBundle.getMessage(QueryBuilderInputTable.class, "ADD_AND_QUERY_CRITERIA"))) { 429 int row=getSelectedRow(); 430 String tableSpec=(String )this.getValueAt(row,Table_COLUMN); 431 String columnName=(String )this.getValueAt(row,Column_COLUMN); 432 String result0 = (String ) this.getValueAt ( _inputTablePopupRow, _inputTablePopupColumn ); 433 if ( _queryBuilder._queryModel.getWhere() == null ) { 434 String msg = NbBundle.getMessage(QueryBuilderInputTable.class, "EMPTY_QUERY_CRITERIA"); 435 NotifyDescriptor d = 436 new NotifyDescriptor.Message(msg + "\n\n", NotifyDescriptor.ERROR_MESSAGE); DialogDisplayer.getDefault().notify(d); 438 return; 439 } 440 441 Column col = _queryBuilder._queryModel.findSelectColumn(tableSpec, columnName); 442 String derColName = col.getDerivedColName(); 443 444 Object [] rowData = { 446 (columnName.equals("* { All Columns }")) ? "*" : columnName, (derColName==null) ? "" : derColName, tableSpec, Boolean.TRUE, "", "", "", "", }; 456 457 _addQueryParameterDlg = 458 new AddQueryParameterDlg(true, (String )(this.getValueAt(getSelectedRow(), Column_COLUMN))); 459 460 if ( _addQueryParameterDlg.getReturnStatus() == AddQueryParameterDlg.RET_OK ) 461 { 462 ((DefaultTableModel )this.getModel()).insertRow ( row+1, rowData ); 463 this.setRowSelectionInterval (row+1, row+1); 465 String result = (String )(_addQueryParameterDlg.getCriteria()); 467 468 if ( result.trim().length() == 0 ) return; 469 470 if (DEBUG) 471 System.out.println( 472 " Table Row = " + row + 473 " Table = " + tableSpec + 474 " Column = " + columnName + 475 " Result = " + result 476 + "\n" ); 479 this.setValueAt ( result, getSelectedRow(), Criteria_COLUMN ); 480 481 int i = result.indexOf(" "); String op = result.substring(0, i); 483 String val = result.substring(i+1); 484 485 487 Column col1 = SQLQueryFactory.createColumn(tableSpec, columnName); 488 Literal lit2 = new Literal(val); 489 Predicate pred = SQLQueryFactory.createPredicate(col1, lit2, op); 490 491 _queryBuilder._queryModel.addOrCreateAndExpression(pred); 492 493 _queryBuilder.generateText(); 495 } 496 } 497 else if (source.getText().equals(NbBundle.getMessage(QueryBuilderInputTable.class, "ADD_OR_QUERY_CRITERIA"))) { 499 int row=getSelectedRow(); 500 String tableSpec=(String )this.getValueAt(row,Table_COLUMN); 501 String columnName=(String )this.getValueAt(row,Column_COLUMN); 502 String result0 = (String ) this.getValueAt ( _inputTablePopupRow, _inputTablePopupColumn ); 503 if ( _queryBuilder._queryModel.getWhere() == null ) { 504 String msg = NbBundle.getMessage(QueryBuilderInputTable.class, "EMPTY_QUERY_CRITERIA"); 505 NotifyDescriptor d = 506 new NotifyDescriptor.Message(msg + "\n\n", NotifyDescriptor.ERROR_MESSAGE); DialogDisplayer.getDefault().notify(d); 508 return; 509 } 510 511 Column col = _queryBuilder._queryModel.findSelectColumn(tableSpec, columnName); 512 String derColName = col.getDerivedColName(); 513 514 Object [] rowData = { 516 (columnName.equals("* { All Columns }")) ? "*" : columnName, (derColName==null) ? "" : derColName, tableSpec, Boolean.TRUE, "", "", "", "" }; 526 527 _addQueryParameterDlg = 528 new AddQueryParameterDlg(true, (String )(this.getValueAt(getSelectedRow(), Column_COLUMN))); 529 530 if ( _addQueryParameterDlg.getReturnStatus() == AddQueryParameterDlg.RET_OK ) 531 { 532 ((DefaultTableModel )this.getModel()).addColumn ("Or..." ); 533 int orColumn = ((DefaultTableModel )this.getModel()).getColumnCount (); 534 ((DefaultTableModel )this.getModel()).addColumn ("Order" ); 535 String result = (String )(_addQueryParameterDlg.getCriteria()); 538 539 if ( result.trim().length() == 0 ) return; 540 541 if (DEBUG) 542 System.out.println( 543 " OR called \n " + 544 " Table Row = " + row + 545 " Table = " + tableSpec + 546 " Column = " + columnName + 547 " orColumn = " + orColumn + 548 " Result = " + result 549 + "\n" ); 552 ((DefaultTableModel )this.getModel()).setValueAt ( result, row, orColumn-1); 553 554 int i = result.indexOf(" "); String op = result.substring(0, i); 556 String val = result.substring(i+1); 557 558 560 Column col1 = SQLQueryFactory.createColumn(tableSpec, columnName); 561 Literal lit2 = new Literal(val); 562 Predicate pred = SQLQueryFactory.createPredicate(col1, lit2, op); 563 564 _queryBuilder._queryModel.addOrCreateOrExpression(pred); 565 566 _queryBuilder.generateText(); 568 } 569 } 570 } 571 572 573 577 public void itemStateChanged (ItemEvent e) { 578 579 int row=getSelectedRow(); 580 581 if (DEBUG) 582 System.out.println( "QBIT : itemStateChanged called\nTable Row: " + row + " Item: " + e.getItem() + " Item Selectable: " + e.getItemSelectable()); 585 if (_queryBuilder._updateModel && 586 (row!=-1) && 587 (e.getStateChange() == ItemEvent.SELECTED)) 588 { 589 boolean updateText = _queryBuilder._updateText; 591 _queryBuilder._updateText=false; 592 try { 593 String item= (String )e.getItem(); 594 String tableSpec= (String )(getValueAt(row, Table_COLUMN)); 595 String columnName= (String )(getValueAt(row, Column_COLUMN)); 596 int sortCount= _queryBuilder._queryModel.getSortCount(); 597 598 if (item.equals("")) { setValueAt("", row, SortType_COLUMN); setValueAt("", row, SortOrder_COLUMN); _queryBuilder._queryModel. 604 removeSortSpecification(tableSpec, columnName); 605 } 606 607 else if ( item.equals("ASC") || item.equals ( NbBundle.getMessage(QueryBuilderInputTable.class, "ASCENDING") ) ) { 610 String order = (String )getValueAt(row, SortOrder_COLUMN); 612 int orderNum = order.equals("") ? sortCount+1 : Integer.parseInt(order); _queryBuilder._queryModel. 614 addSortSpecification(tableSpec, columnName, "ASC", orderNum); 615 } 616 else if ( item.equals("DESC") || item.equals ( NbBundle.getMessage(QueryBuilderInputTable.class, "DESCENDING") ) ) { 619 String order = (String )getValueAt(row, SortOrder_COLUMN); 621 int orderNum = order.equals("") ? sortCount+1 : Integer.parseInt(order); _queryBuilder._queryModel. 623 addSortSpecification(tableSpec, columnName, "DESC", orderNum); 624 } else { 625 String type=(String )getValueAt(row, SortType_COLUMN); 627 int orderNum = Integer.parseInt(item); 628 if (!type.equals("")) { String sortType = new String ("ASC"); 632 if (type.equals ( NbBundle.getMessage(QueryBuilderInputTable.class, "DESCENDING") ) ) { sortType = new String ("DESC"); 634 } 635 if ( orderNum <= sortCount ) { 641 _queryBuilder._queryModel. 642 addSortSpecification(tableSpec, columnName, sortType, orderNum); 643 } 644 } 645 else { 648 _queryBuilder._queryModel. 649 addSortSpecification(tableSpec, columnName, "ASC", orderNum); } 651 } 652 653 clearSelection(); 655 656 generateTableOrderBy(_queryBuilder._queryModel); 659 } 660 finally { 661 _queryBuilder._updateText=updateText; 663 } 664 665 if (_queryBuilder._updateText) 667 _queryBuilder.generateText(); 668 } 669 } 670 671 672 void selectColumn(String tableSpec, String columnName, Boolean select) { 673 int row = findRow(tableSpec, columnName); 674 if (row == -1) 675 return; 676 if ((select==Boolean.TRUE) && 677 (getValueAt(row,Output_COLUMN)!=Boolean.TRUE)) 678 setValueAt(Boolean.TRUE,row,Output_COLUMN); 679 else if ((select==Boolean.FALSE) && 680 (getValueAt(row,Output_COLUMN)!=Boolean.FALSE)) 681 setValueAt(Boolean.FALSE,row,Output_COLUMN); 682 } 683 684 686 void addRow(String tableSpec, String columnName) { 687 688 if (DEBUG) 689 System.out.println("QBIT.addRow, tableSpec: "+tableSpec + " columnName: "+columnName); 691 694 int i = findRow(tableSpec, columnName); 696 if (i!=-1) { 697 698 if (this.getValueAt(i, Output_COLUMN)!=Boolean.TRUE) 700 this.setValueAt(Boolean.TRUE, i, Output_COLUMN); 701 702 } else { 703 704 Column col = _queryBuilder._queryModel.findSelectColumn(tableSpec, columnName); 707 String derColName = null; 708 if ( col != null ) 709 derColName = col.getDerivedColName(); 710 711 Object [] rowData = { 715 (columnName.equals("* { All Columns }")) ? "*" : columnName, (derColName==null) ? "" : derColName, tableSpec, Boolean.TRUE, "", "", "", "" }; 725 ((DefaultTableModel )dataModel).addRow(rowData); 727 728 } 729 } 730 731 732 void removeRow(String tableSpec, String columnName) { 736 737 for (int i=0; i < this.getRowCount(); i++) { 740 741 if (this.getValueAt(i, Table_COLUMN).equals(tableSpec)) { 743 744 if (this.getValueAt(i, Column_COLUMN).equals(columnName)) 746 { 747 749 boolean sortOrderRow = 752 ! (((String )this.getValueAt(i,SortType_COLUMN)).trim().equals("") && ((String )this.getValueAt(i,SortOrder_COLUMN)).trim().equals("")); 755 if (sortOrderRow) { 757 setValueAt("", i, SortType_COLUMN); setValueAt("", i, SortOrder_COLUMN); 759 } 761 if (((String )this.getValueAt(i,CriteriaOrder_COLUMN)).trim().equals("") && ((String )this.getValueAt(i,Criteria_COLUMN)).trim().equals("")) { 765 ((DefaultTableModel )this.getModel()).removeRow(i); 766 } 767 else { 768 if (this.getValueAt(i, Output_COLUMN)!=Boolean.FALSE) 770 this.setValueAt(Boolean.FALSE, i, Output_COLUMN); 771 } 772 773 if (sortOrderRow) 775 generateTableOrderBy(_queryBuilder._queryModel); 776 } 777 797 } 798 } 799 } 800 801 802 804 void removeRows(String tableSpec) { 805 806 Log.err.log(ErrorManager.INFORMATIONAL, "Entering removeRows, tableSpec: " + tableSpec); 808 for (int i=this.getRowCount()-1; i>=0; i--) { 809 if (this.getValueAt(i, Table_COLUMN).equals(tableSpec)) { 810 ((DefaultTableModel )this.getModel()).removeRow(i); 811 } 812 } 813 } 814 815 816 818 void addCriterion(String tableSpec, String columnName, String val, String order) { 819 820 if (DEBUG) { 821 System.out.println( 822 "Entering QueryBuilderInputTable.addCriterion, tableSpec: " + tableSpec + " columnName: "+columnName + " val: "+val + "Order: " + order + "\n"); } 825 Log.err.log(ErrorManager.INFORMATIONAL, 826 "Entering QueryBuilderInputTable.addCriterion, tableSpec: " + tableSpec + " columnName: " + columnName + " val: " + val); 829 834 boolean foundIt=false; 836 int row = 0; 837 for (int i=0; i<this.getRowCount(); i++) { 838 if (((String )this.getValueAt(i, Table_COLUMN)).equals(tableSpec) && 839 ((String )this.getValueAt(i, Column_COLUMN)).equals(columnName)) 840 { 842 foundIt=true; 844 row = i; 845 if ( ! this.getValueAt( i, Criteria_COLUMN ).equals ( 846 Criteria_Uneditable_String ) ) { 847 this.setValueAt(val, i, Criteria_COLUMN ); 848 this.setValueAt(order, i, CriteriaOrder_COLUMN ); 849 break; 850 } 851 } 852 858 } 859 860 Object [] rowData = { columnName, 861 "", tableSpec, 863 Boolean.FALSE, "", "", val, order 866 }; 868 if (!foundIt) { 869 ((DefaultTableModel )this.getModel()).addRow(rowData); 871 } 872 878 } 879 880 881 884 void generateTableWhere (QueryModel query) { 885 886 if (DEBUG) 887 System.out.println( 888 "Entering QueryBuilderInputTable.generateTableWhere"); 890 boolean needsCriteriaOrder = false; 891 int criteriaCount=0; 892 String [] criteriaOrderItems = null; 893 Where where=query.getWhere(); 894 if (where!=null) { 895 896 Expression expr = where.getExpression(); 902 if (expr != null) { 903 criteriaCount = 1; if (expr instanceof Predicate) { 905 needsCriteriaOrder = true; 906 generatePredicateInTableWhere((Predicate)expr, 0, needsCriteriaOrder); 907 } 908 else if (expr instanceof And) { 909 needsCriteriaOrder = true; 910 ExpressionList andExpr = (ExpressionList)expr; 911 criteriaCount=andExpr.size(); 912 for (int i=0; i<criteriaCount; i++) { 913 expr = andExpr.getExpression(i); 914 if (expr instanceof Predicate) { 915 Predicate pred = (Predicate)expr; 916 generatePredicateInTableWhere(pred, i, needsCriteriaOrder); 917 } 918 } 919 } 920 } 921 } 922 923 criteriaOrderItems = new String [criteriaCount+1]; 926 criteriaOrderItems[0]=""; TableColumn criteriaOrderColumn = this.getColumnModel().getColumn(CriteriaOrder_COLUMN); 928 if (needsCriteriaOrder) { 929 for (int i=0; i<criteriaCount; i++) 930 criteriaOrderItems[i+1]=String.valueOf(i+1); 931 } 932 _criteriaOrderComboBox = new JComboBox (criteriaOrderItems); 933 criteriaOrderColumn.setCellEditor(new DefaultCellEditor (_criteriaOrderComboBox)); 934 } 937 938 private void generatePredicateInTableWhere (Predicate pred, int order, boolean needsCriteriaOrder) { 939 Value val1 = pred.getVal1(); 940 Value val2 = pred.getVal2(); 941 942 String marker = pred.getVal2().toString(); 945 946 949 if ( (val1 instanceof Column ) && !(val2 instanceof Column ) ) { 952 Column col = (Column )val1; 953 String tableSpec=col.getTableSpec(); 954 String columnName=col.getColumnName(); 955 String val = pred.getOp() + " " + marker; 958 int row = findRow(tableSpec, columnName); 959 if (row!=-1) { 960 setValueAt(val, row, Criteria_COLUMN); 961 if (needsCriteriaOrder) 962 setValueAt(String.valueOf(order+1), row, CriteriaOrder_COLUMN); 963 } 964 } 965 } 966 967 969 void generateTableOrderBy (QueryModel query) { 970 971 OrderBy orderBy=query.getOrderBy(); 972 if (orderBy!=null) { 973 974 int size=orderBy.getSortSpecificationCount(); 980 for (int i=0; i<size; i++) { 981 SortSpecification sortSpec = orderBy.getSortSpecification(i); 982 int row = findRow(sortSpec.getColumn().getTableSpec(), sortSpec.getColumn().getColumnName()); 983 if (row!=-1) { 984 if ( sortSpec.getDirection().equals("ASC") ) { 988 setValueAt ( 989 ( NbBundle.getMessage(QueryBuilderInputTable.class, 990 "ASCENDING") ) , row, SortType_COLUMN ); 992 } 993 else if ( sortSpec.getDirection().equals("DESC") ) { 994 setValueAt ( 995 ( NbBundle.getMessage(QueryBuilderInputTable.class, 996 "DESCENDING") ) , row, SortType_COLUMN ); 998 999 } 1000 setValueAt(String.valueOf(i+1), row, SortOrder_COLUMN); 1001 } 1002 } 1003 } 1004 1005 int sortCount=_queryBuilder._queryModel.getSortCount(); 1010 if ( sortCount < this.getRowCount() ) { 1011 sortCount += 1; 1012 } 1013 if (_sortOrderComboBox.getItemCount() != sortCount+1) { 1014 final String [] sortOrderItems = new String [sortCount+1]; 1015 sortOrderItems[0]=""; for (int i=1; i<sortCount+1; i++) 1017 sortOrderItems[i]=String.valueOf(i); 1018 TableColumn sortOrderColumn = this.getColumnModel().getColumn(SortOrder_COLUMN); 1019 _sortOrderComboBox = new JComboBox (sortOrderItems); 1020 sortOrderColumn.setCellEditor(new DefaultCellEditor (_sortOrderComboBox)); 1021 _sortOrderComboBox.addItemListener(this); 1022 } 1023 } 1024 1025 1026 private int findRow(String tableSpec, String columnName) 1028 { 1029 for (int i=0; i<this.getRowCount(); i++) { 1030 if ((this.getValueAt(i, Table_COLUMN).equals(tableSpec)) && 1031 (this.getValueAt(i, Column_COLUMN).equals(columnName))) 1032 return i; 1033 } 1034 return -1; 1035 } 1036 1037 1038 1040 protected String getClassName(Object o) { 1041 String classString = o.getClass().getName(); 1042 int dotIndex = classString.lastIndexOf("."); return classString.substring(dotIndex+1); 1044 } 1045 1046 1047 1050 void clearModel() { 1051 ((DefaultTableModel )this.getModel()).setRowCount(0); 1052 } 1053 1054} 1055 | Popular Tags |