001/* 002 * Copyright (c) 2003-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: welker $' 006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 007 * '$Revision: 24234 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.ecoinformatics.seek.querybuilder; 031 032import java.awt.BorderLayout; 033import java.awt.Dimension; 034import java.awt.GridBagConstraints; 035import java.awt.GridBagLayout; 036import java.awt.GridLayout; 037import java.awt.event.ActionEvent; 038import java.awt.event.KeyAdapter; 039import java.awt.event.KeyEvent; 040import java.awt.event.KeyListener; 041import java.util.Enumeration; 042 043import javax.swing.AbstractAction; 044import javax.swing.JButton; 045import javax.swing.JComboBox; 046import javax.swing.JEditorPane; 047import javax.swing.JLabel; 048import javax.swing.JPanel; 049import javax.swing.JScrollPane; 050import javax.swing.JTextField; 051import javax.swing.SwingConstants; 052import javax.swing.SwingUtilities; 053import javax.swing.border.TitledBorder; 054import javax.swing.event.ListDataEvent; 055import javax.swing.event.ListDataListener; 056import javax.swing.event.ListSelectionEvent; 057import javax.swing.event.ListSelectionListener; 058import javax.swing.event.TableModelListener; 059 060import org.kepler.objectmanager.data.DataType; 061import org.kepler.objectmanager.data.db.DSSchemaIFace; 062import org.kepler.objectmanager.data.db.DSTableFieldIFace; 063 064/** 065 * This class is a JPanel that contains the list of Where OPerator and Condition 066 * objects. NOTE: The list display the operator and its children in reverse 067 * polish notation. It also contains<br> 068 * 1) a sub-panel with some UI for adding and removing items.<br> 069 * 2) A subpanel inspector for the editting of the COndition or Operator items<br> 070 * 3) A text control for displaying the results of the where conidtion 071 */ 072public class DBWherePanel extends JPanel implements ListSelectionListener, 073 ListDataListener { 074 protected DBWhereList mList = null; 075 protected DBWhereModel mModel = null; 076 protected DSSchemaIFace mSchema = null; 077 078 protected JButton mAddCondBtn = null; 079 protected JButton mAddAndOperBtn = null; 080 protected JButton mAddOROperBtn = null; 081 protected JButton mRemoveBtn = null; 082 protected JTextField mNameField = new JTextField(); 083 protected JTextField mCriteriaField = new JTextField(); 084 protected JEditorPane mEditorPane = null; 085 protected JScrollPane mTextScrollPane = null; 086 087 protected JComboBox mTablesCombobox = new JComboBox(); 088 protected JComboBox mFieldsCombobox = new JComboBox(); 089 protected JComboBox mCondCombobox = new JComboBox(); 090 protected JComboBox mOperCombobox = new JComboBox(); 091 092 protected JPanel mCondInspPanel = null; 093 protected JPanel mOperInspPanel = null; 094 protected JPanel mCurrInspPanel = null; 095 protected JPanel mInspContainer = null; 096 097 protected boolean mRejectChanges = false; 098 protected TableModelListener mModelListener = null; 099 100 /** 101 * Constructor 102 * 103 * @param aSchema 104 * the schema 105 * 106 */ 107 public DBWherePanel(DSSchemaIFace aSchema) { 108 super(new BorderLayout()); 109 mSchema = aSchema; 110 111 mModel = new DBWhereModel(); 112 mList = new DBWhereList(aSchema, mModel); 113 114 DBUIUtils.fillTableCombobox(mSchema, mTablesCombobox); 115 mTablesCombobox.setSelectedIndex(0); 116 DBUIUtils.fillFieldCombobox(mSchema, (String) mTablesCombobox 117 .getSelectedItem(), mFieldsCombobox, false); 118 119 mList.getSelectionModel().addListSelectionListener(this); 120 mModel.addListDataListener(this); 121 122 mOperInspPanel = createOperInspector(); 123 mCondInspPanel = createCondInspector(); 124 125 mInspContainer = new JPanel(new BorderLayout()); 126 JScrollPane scrollPane = new JScrollPane(); 127 scrollPane.getViewport().setView(mList); 128 mInspContainer.add(scrollPane, BorderLayout.CENTER); 129 130 mEditorPane = new JEditorPane("text/text", "\n\n\n\n\n"); 131 mEditorPane.setMinimumSize(new Dimension(200, 200)); 132 mEditorPane.setFocusable(false); 133 mEditorPane.setEditable(false); 134 135 mTextScrollPane = new JScrollPane(); 136 mTextScrollPane.getViewport().setView(mEditorPane); 137 mTextScrollPane 138 .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 139 mTextScrollPane 140 .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 141 142 add(createControlPanel(), BorderLayout.EAST); 143 add(mInspContainer, BorderLayout.CENTER); 144 add(mTextScrollPane, BorderLayout.SOUTH); 145 146 } 147 148 /** 149 * Set a single model listener 150 * 151 * @param aTblModelListener 152 * the listener 153 */ 154 public void setModelListener(TableModelListener aTblModelListener) { 155 mModelListener = aTblModelListener; 156 } 157 158 /** 159 * Returns the model 160 * 161 * @return the model 162 */ 163 public DBWhereModel getModel() { 164 return mModel; 165 } 166 167 /** 168 * Creates the inspector panel for a condition 169 * 170 * @return the panel 171 */ 172 protected JPanel createCondInspector() { 173 JPanel panel = new JPanel(new GridLayout(2, 4)); 174 panel.add(new JLabel("Table", SwingConstants.CENTER)); 175 panel.add(new JLabel("Field", SwingConstants.CENTER)); 176 panel.add(new JLabel("Compartor", SwingConstants.CENTER)); 177 panel.add(new JLabel("Value", SwingConstants.CENTER)); 178 179 panel.add(mTablesCombobox); 180 panel.add(mFieldsCombobox); 181 182 panel.add(mCondCombobox); 183 for (int i = 1; i < DBSelectTableUIStd.OPERS_TXT.length; i++) { // skip 184 // first 185 // item 186 mCondCombobox.addItem(DBSelectTableUIStd.OPERS_TXT[i]); 187 } 188 panel.add(mCriteriaField); 189 190 mTablesCombobox.addActionListener(new AbstractAction() { 191 public void actionPerformed(ActionEvent e) { 192 // populateFieldComboboxFromTableName((String)mTablesCombobox.getSelectedItem()); 193 DBUIUtils.fillFieldCombobox(mSchema, (String) mTablesCombobox 194 .getSelectedItem(), mFieldsCombobox, false); 195 doUpdate(false); 196 generateAndSetWhereText(); 197 } 198 }); 199 200 mFieldsCombobox.addActionListener(new AbstractAction() { 201 public void actionPerformed(ActionEvent e) { 202 doUpdate(false); 203 generateAndSetWhereText(); 204 } 205 }); 206 207 mCondCombobox.addActionListener(new AbstractAction() { 208 public void actionPerformed(ActionEvent e) { 209 doUpdate(false); 210 generateAndSetWhereText(); 211 } 212 }); 213 214 KeyListener keyListener = new KeyAdapter() { 215 public void keyPressed(KeyEvent e) { 216 doUpdate(true); 217 generateAndSetWhereText(); 218 } 219 220 public void keyReleased(KeyEvent e) { 221 keyPressed(e); 222 } 223 224 public void keyTyped(KeyEvent e) { 225 keyPressed(e); 226 } 227 }; 228 mCriteriaField.addKeyListener(keyListener); 229 // mCriteriaField.addKeyListener(this); 230 231 return panel; 232 } 233 234 /** 235 * Creates the inspector panel for the Operator 236 * 237 * @return the panel 238 */ 239 protected JPanel createOperInspector() { 240 JPanel mainPanel = new JPanel(new BorderLayout()); 241 JPanel panel = new JPanel(new BorderLayout()); 242 mainPanel.add(panel, BorderLayout.WEST); 243 panel.add(new JLabel("Operator: ", SwingConstants.CENTER), 244 BorderLayout.WEST); 245 246 panel.add(mOperCombobox, BorderLayout.CENTER); 247 mOperCombobox.addItem(DBWhereOperator.AND_OPER); 248 mOperCombobox.addItem(DBWhereOperator.OR_OPER); 249 250 mOperCombobox.addActionListener(new AbstractAction() { 251 public void actionPerformed(ActionEvent e) { 252 doUpdate(false); 253 generateAndSetWhereText(); 254 } 255 }); 256 257 return mainPanel; 258 } 259 260 /** 261 * Creates and adds a button to the control panel used for editting the list 262 * of operators and conditions 263 * 264 * @param aPanel 265 * the parent 266 * @param name 267 * the name/text on the button 268 * @param gridbag 269 * gridbag 270 * @param c 271 * constraint 272 * @return the new button 273 */ 274 protected JButton makeButton(JPanel aPanel, String name, 275 GridBagLayout gridbag, GridBagConstraints c) { 276 JButton button = new JButton(name); 277 gridbag.setConstraints(button, c); 278 aPanel.add(button); 279 return button; 280 } 281 282 /** 283 * Helper class to add new item to main list 284 * 285 * @param aItem 286 * item to be added 287 */ 288 protected void addNewItem(DBWhereIFace aParent, DBWhereIFace aItem) { 289 DBWhereIFace afterItem = null; 290 int newInx = mList.getSelectedIndex(); 291 if (newInx != -1) { 292 afterItem = (DBWhereIFace) mModel.getElementAt(newInx); 293 } 294 295 newInx++; 296 mModel.add(aItem, newInx); 297 298 if (aParent != null) { 299 ((DBWhereOperator) aParent).addAfter(aItem, afterItem); 300 } 301 302 mModel.fireContentsChanged(); 303 mList.setSelectedIndex(newInx); 304 305 } 306 307 /** 308 * Return the appropriate DBWhereIFace object that the new item will be 309 * parented to 310 * 311 * @return the parent 312 */ 313 protected DBWhereOperator getParentForInsert() { 314 DBWhereIFace parent = null; 315 int inx = mList.getSelectedIndex(); 316 if (inx != -1) { 317 parent = (DBWhereIFace) mModel.getElementAt(inx); 318 if (!parent.isOperator() || ((DBWhereOperator) parent).isClosure()) { 319 parent = parent.getParent(); 320 } 321 } 322 return (DBWhereOperator) parent; 323 } 324 325 /** 326 * Create a new operator and two children 327 * 328 * @param aOperName 329 */ 330 protected void addOperator(String aOperName) { 331 DBWhereOperator parent = getParentForInsert(); 332 if (parent != null || mModel.getSize() == 0) { 333 DBWhereOperator operObj = new DBWhereOperator(parent, false); 334 operObj.setName(aOperName); 335 addNewItem(parent, operObj); 336 String tableName = (String) mTablesCombobox.getItemAt(0); 337 String fieldName = (String) mFieldsCombobox.getItemAt(0); 338 DSTableFieldIFace fieldIFace = DBUIUtils.getFieldByName(mSchema, 339 tableName, fieldName); 340 addNewItem(operObj, new DBWhereCondition(operObj, tableName, 341 fieldName, fieldIFace != null ? fieldIFace.getDataType() 342 : "")); 343 addNewItem(operObj, new DBWhereCondition(operObj, tableName, 344 fieldName, fieldIFace != null ? fieldIFace.getDataType() 345 : "")); 346 generateAndSetWhereText(); 347 } 348 } 349 350 /** 351 * Creates the control panel of button for adding and removing items in the 352 * "where clause" 353 * 354 * */ 355 protected JPanel createControlPanel() { 356 JPanel mainPanel = new JPanel(new BorderLayout()); 357 358 GridBagLayout gridbag = new GridBagLayout(); 359 GridBagConstraints c = new GridBagConstraints(); 360 c.gridwidth = GridBagConstraints.REMAINDER; // end row 361 c.fill = GridBagConstraints.HORIZONTAL; 362 363 JPanel panel = new JPanel(gridbag); 364 panel.setBorder(new TitledBorder("Control")); 365 366 mAddAndOperBtn = makeButton(panel, "Add AND", gridbag, c); 367 mAddOROperBtn = makeButton(panel, "Add OR", gridbag, c); 368 mAddCondBtn = makeButton(panel, "Add Condition", gridbag, c); 369 mRemoveBtn = makeButton(panel, "Remove", gridbag, c); 370 mainPanel.add(panel, BorderLayout.NORTH); 371 372 mAddCondBtn.setEnabled(true); 373 mAddAndOperBtn.setEnabled(true); 374 mAddOROperBtn.setEnabled(true); 375 mRemoveBtn.setEnabled(false); 376 377 mAddCondBtn.addActionListener(new AbstractAction() { 378 public void actionPerformed(ActionEvent e) { 379 DBWhereOperator parent = getParentForInsert(); 380 // if (parent != null || mModel.getSize() == 0) { 381 if (mModel.getSize() == 0) { 382 String tableName = (String) mTablesCombobox.getItemAt(0); 383 String fieldName = (String) mFieldsCombobox.getItemAt(0); 384 DBWhereCondition cond = new DBWhereCondition(parent, 385 tableName, fieldName, DataType.STR); 386 DSTableFieldIFace fieldIFace = DBUIUtils.getFieldByName( 387 mSchema, tableName, fieldName); 388 if (fieldIFace != null) { 389 cond.setDataType(fieldIFace.getDataType()); 390 } 391 addNewItem(parent, cond); 392 generateAndSetWhereText(); 393 } 394 } 395 }); 396 397 mAddAndOperBtn.addActionListener(new AbstractAction() { 398 public void actionPerformed(ActionEvent e) { 399 addOperator(DBWhereOperator.AND_OPER); 400 } 401 }); 402 403 mAddOROperBtn.addActionListener(new AbstractAction() { 404 public void actionPerformed(ActionEvent e) { 405 addOperator(DBWhereOperator.OR_OPER); 406 } 407 }); 408 409 mRemoveBtn.addActionListener(new AbstractAction() { 410 public void actionPerformed(ActionEvent e) { 411 int inx = mList.getSelectedIndex(); 412 if (inx != -1) { 413 DBWhereIFace item = (DBWhereIFace) mModel.getElementAt(inx); 414 415 // First remove the item from its parent 416 DBWhereIFace parent = item.getParent(); 417 if (parent != null) { 418 if (parent.isOperator() 419 && parent instanceof DBWhereOperator) // safety 420 // checks 421 // (should 422 // NEVER 423 // fail) 424 { 425 ((DBWhereOperator) parent).remove(item); 426 if (item.isOperator() 427 && item instanceof DBWhereOperator) { 428 ((DBWhereOperator) parent) 429 .remove(((DBWhereOperator) item) 430 .getClosure()); 431 } 432 } 433 } 434 435 // Now remove it from the List Model 436 mModel.remove(item); 437 mList.clearSelection(); 438 generateAndSetWhereText(); 439 440 SwingUtilities.invokeLater(new Runnable() { 441 public void run() { 442 // valueChanged(null); 443 } 444 }); 445 446 } 447 } 448 }); 449 450 return mainPanel; 451 } 452 453 /** 454 * Updates the inspector UI depending on the type of object being editted 455 * 456 * @param aDoTextOnly 457 */ 458 protected void doUpdate(boolean aDoTextOnly) { 459 if (mRejectChanges) 460 return; 461 462 int inx = mList.getSelectedIndex(); 463 if (inx != -1) { 464 DBWhereIFace item = (DBWhereIFace) mModel.getElementAt(inx); 465 if (!item.isOperator()) { 466 if (item instanceof DBWhereCondition) { 467 DBWhereCondition cond = (DBWhereCondition) item; 468 if (!aDoTextOnly) { 469 String tableName = (String) mTablesCombobox 470 .getSelectedItem(); 471 String fieldName = (String) mFieldsCombobox 472 .getSelectedItem(); 473 cond.setTableName(tableName); 474 cond.setName(fieldName); 475 cond.setOperator((String) mCondCombobox 476 .getSelectedItem()); 477 DSTableFieldIFace fieldIFace = DBUIUtils 478 .getFieldByName(mSchema, tableName, fieldName); 479 if (fieldIFace != null) { 480 cond.setDataType(fieldIFace.getDataType()); 481 } 482 483 } 484 cond.setCriteria(mCriteriaField.getText()); 485 } 486 } else if (item instanceof DBWhereOperator) { 487 ((DBWhereOperator) item).setName((String) mOperCombobox 488 .getSelectedItem()); 489 } 490 mModel.fireContentsChanged(); 491 } 492 } 493 494 /** 495 * Recurses through the "tree" of operators and creates a textual rendering 496 * of the operators and conditions 497 * 498 * @param aOper 499 * @param aStrBuf 500 * @param aLevel 501 * */ 502 protected int recurseList(DBWhereOperator aOper, StringBuffer aStrBuf, 503 int aLevel, boolean aUseSymbols) { 504 int retDepth = aLevel; 505 // Check number of Children 506 if (aOper.getNumChildern() < 2) 507 return 0; 508 509 int numChildren = 0; 510 for (Enumeration e = aOper.getEnumeration(); e.hasMoreElements();) { 511 DBWhereIFace item = (DBWhereIFace) e.nextElement(); 512 if (item instanceof DBWhereOperator) { 513 DBWhereOperator oper = (DBWhereOperator) item; 514 if (!oper.isClosure() && oper.getNumChildern() > 1) { 515 numChildren++; 516 } 517 } else { 518 numChildren++; 519 } 520 } 521 if (numChildren < 2) 522 return 0; 523 524 if (aLevel > 0) { 525 aStrBuf.append("\n"); 526 for (int i = 0; i < aLevel; i++) { 527 aStrBuf.append(" "); 528 } 529 } 530 531 int cnt = 0; 532 aStrBuf.append("("); 533 for (Enumeration e = aOper.getEnumeration(); e.hasMoreElements();) { 534 DBWhereIFace item = (DBWhereIFace) e.nextElement(); 535 if (item instanceof DBWhereOperator) { 536 DBWhereOperator oper = (DBWhereOperator) item; 537 if (!oper.isClosure() && oper.getNumChildern() > 1) { 538 if (cnt > 0) { 539 aStrBuf.append(" " 540 + DBSelectTableUIStd.getBoolOperSymbol(aOper 541 .getName()) + " "); 542 } 543 int depth = recurseList(oper, aStrBuf, aLevel + 1, 544 aUseSymbols); 545 if (depth > retDepth) 546 retDepth = depth; 547 } 548 } else { 549 if (cnt > 0) { 550 aStrBuf.append(" " 551 + DBSelectTableUIStd.getBoolOperSymbol(aOper 552 .getName()) + " "); 553 } 554 aStrBuf.append(item.toString(aUseSymbols)); 555 cnt++; 556 } 557 } 558 aStrBuf.append(")"); 559 560 return retDepth; 561 } 562 563 /** 564 * Generates a textual representation of the "where" clause 565 * 566 */ 567 public String generateWhereSQL(boolean aUseSymbols) { 568 StringBuffer strBuf = new StringBuffer(""); 569 int depth = 0; 570 if (mModel.getSize() > 0) { 571 if (mModel.getElementAt(0) instanceof DBWhereOperator) { 572 DBWhereOperator oper = (DBWhereOperator) mModel.getElementAt(0); 573 depth = recurseList(oper, strBuf, 0, aUseSymbols); 574 } else { 575 strBuf.append(((DBWhereCondition) mModel.getElementAt(0)) 576 .toString(aUseSymbols)); 577 } 578 for (int i = 0; i < (5 - depth); i++) { 579 strBuf.append("\n"); 580 } 581 } 582 return strBuf.toString(); 583 584 } 585 586 /** 587 * Recurses through the "tree" of operators and creates a textual rendering 588 * of the operators and conditions 589 * 590 * @param aOper 591 * @param aStrBuf 592 * @param aLevel 593 * */ 594 protected boolean isComplexRecurse(DBWhereOperator aParent, String aName) { 595 if (!aParent.getName().equals(aName)) { 596 return true; 597 } 598 599 // Check number of Children 600 if (aParent.getNumChildern() < 2) 601 return false; 602 603 for (Enumeration e = aParent.getEnumeration(); e.hasMoreElements();) { 604 DBWhereIFace item = (DBWhereIFace) e.nextElement(); 605 if (item instanceof DBWhereOperator) { 606 DBWhereOperator oper = (DBWhereOperator) item; 607 if (!oper.isClosure() && oper.getNumChildern() > 1) { 608 boolean isComplex = isComplexRecurse(oper, aName); 609 if (isComplex) { 610 return true; 611 } 612 } 613 } 614 } 615 616 return false; 617 } 618 619 /** 620 * Generates a textual representation of the "where" clause 621 * 622 */ 623 public boolean isComplex() { 624 if (mModel.getSize() > 0) { 625 if (mModel.getElementAt(0) instanceof DBWhereOperator) { 626 DBWhereOperator oper = (DBWhereOperator) mModel.getElementAt(0); 627 return isComplexRecurse(oper, oper.getName()); 628 } 629 } 630 return false; 631 632 } 633 634 /** 635 * Generates a textual representation of the "where" clause and displays it 636 * 637 */ 638 protected void generateAndSetWhereText() { 639 mEditorPane.setText(generateWhereSQL(false)); 640 } 641 642 /** 643 * Populates the table combobox with the table names from the schema 644 * 645 */ 646 /* 647 * protected void populateTableCombobox() { 648 * mTablesCombobox.removeAllItems(); Vector tables = mSchema.getTables(); if 649 * (tables != null && tables.size() > 0) { for (Enumeration et = 650 * tables.elements(); et.hasMoreElements();) { DSTableIFace table = 651 * (DSTableIFace)et.nextElement(); mTablesCombobox.addItem(table.getName()); 652 * } } } 653 */ 654 655 /** 656 * Add a filed name to the combo for each field in the table 657 * 658 * @param aTableName 659 * The table to use to fill the field combo 660 */ 661 /* 662 * protected void populateFieldComboboxFromTableName(String aTableName) { 663 * Vector tables = mSchema.getTables(); if (tables != null && tables.size() 664 * > 0) { for (Enumeration et = tables.elements(); et.hasMoreElements();) { 665 * DSTableIFace table = (DSTableIFace)et.nextElement(); if 666 * (table.getName().equals(aTableName)) { mFieldsCombobox.removeAllItems(); 667 * Vector fields = table.getFields(); for (Enumeration ef = 668 * fields.elements(); ef.hasMoreElements();) { DSTableFieldIFace field = 669 * (DSTableFieldIFace)ef.nextElement(); if (!field.getName().equals("*")) { 670 * mFieldsCombobox.addItem(field.getName()); } } } } } } 671 */ 672 673 /** 674 * Given a selection in the "main" list it populates the Field Combobox from 675 * the table 676 * 677 */ 678 protected void populateFieldComboboxFromMainList() { 679 int inx = mList.getSelectedIndex(); 680 if (inx != -1) { 681 DBWhereIFace item = (DBWhereIFace) mModel.getElementAt(inx); 682 if (!item.isOperator()) { 683 if (item instanceof DBWhereCondition) { 684 DBWhereCondition cond = (DBWhereCondition) item; 685 String selectedTableName = (String) mTablesCombobox 686 .getSelectedItem(); 687 String tableName = cond.getTableName(); 688 if (!tableName.equals(selectedTableName)) { 689 // populateFieldComboboxFromTableName(tableName); 690 DBUIUtils.fillFieldCombobox(mSchema, tableName, 691 mFieldsCombobox, false); 692 693 } 694 } 695 } 696 } 697 } 698 699 /** 700 * Returns the index of a String item in a combobox 701 * 702 * @param aCBX 703 * the combobox 704 * @param aName 705 * the string name of the item 706 * @return the index in the combobox 707 */ 708 protected int getIndexForName(JComboBox aCBX, String aName) { 709 for (int i = 0; i < aCBX.getItemCount(); i++) { 710 String name = (String) aCBX.getItemAt(i); 711 if (name.equals(aName)) { 712 return i; 713 } 714 } 715 return -1; 716 } 717 718 /** 719 * Fills QueryDef from Model 720 */ 721 public void fillQueryDef(DBQueryDef aQueryDef) { 722 mModel.fillQueryDef(aQueryDef); 723 } 724 725 // -------------------------------------------- 726 // -------- ListSelectionListener ------------- 727 // -------------------------------------------- 728 729 /** 730 * Upates the "inspector" ui when an item in the list is clicked on 731 */ 732 public void valueChanged(ListSelectionEvent e) { 733 mRejectChanges = true; 734 if (e == null || !e.getValueIsAdjusting()) { 735 if (mCurrInspPanel != null) { 736 mInspContainer.remove(mCurrInspPanel); 737 } 738 boolean enable = false; 739 int selectedInx = mList.getSelectedIndex(); 740 if (selectedInx != -1 && mModel.getSize() > 0) { 741 DBWhereIFace item = (DBWhereIFace) mModel 742 .getElementAt(selectedInx); 743 if (!item.isOperator()) { 744 mCriteriaField.setText(""); 745 mCurrInspPanel = mCondInspPanel; 746 747 if (item instanceof DBWhereCondition) { 748 DBWhereCondition whereItem = (DBWhereCondition) item; 749 mTablesCombobox.setSelectedIndex(getIndexForName( 750 mTablesCombobox, whereItem.getTableName())); 751 populateFieldComboboxFromMainList(); 752 mFieldsCombobox.setSelectedIndex(getIndexForName( 753 mFieldsCombobox, item.getName())); 754 mCondCombobox.setSelectedIndex(getIndexForName( 755 mCondCombobox, whereItem.getOperator())); 756 mCriteriaField.setText(whereItem.getCriteria()); 757 if (mCriteriaField.getText().length() == 0) { 758 SwingUtilities.invokeLater(new Runnable() { 759 public void run() { 760 mCriteriaField.requestFocus(); 761 mTablesCombobox.repaint(); 762 mFieldsCombobox.repaint(); 763 mCondCombobox.repaint(); 764 } 765 }); 766 } 767 } 768 } else { 769 mCurrInspPanel = mOperInspPanel; 770 mOperCombobox.setSelectedIndex(getIndexForName( 771 mOperCombobox, item.getName())); 772 } 773 mInspContainer.add(mCurrInspPanel, BorderLayout.SOUTH); 774 enable = selectedInx != mModel.getSize() - 1 775 || selectedInx == 0 && mModel.getSize() == 1; 776 } 777 this.validate(); 778 this.repaint(); 779 mRemoveBtn.setEnabled(enable); 780 781 enable = (selectedInx != (mModel.getSize() - 1) && selectedInx != -1) 782 || mModel.getSize() == 0; 783 mAddCondBtn.setEnabled(enable); 784 mAddAndOperBtn.setEnabled(enable); 785 mAddOROperBtn.setEnabled(enable); 786 } 787 mRejectChanges = false; 788 } 789 790 // -------------------------------------------- 791 // ------------ ListDataListener -------------- 792 // -------------------------------------------- 793 public void contentsChanged(ListDataEvent e) { 794 if (mModelListener != null) { 795 mModelListener.tableChanged(null); 796 } 797 } 798 799 public void intervalAdded(ListDataEvent e) { 800 } 801 802 public void intervalRemoved(ListDataEvent e) { 803 } 804}