1 19 package org.openide.explorer.view; 20 21 import org.openide.DialogDescriptor; 22 import org.openide.DialogDisplayer; 23 import org.openide.nodes.Node; 24 import org.openide.nodes.Node.Property; 25 import org.openide.util.NbBundle; 26 27 import java.awt.*; 28 29 import java.beans.PropertyChangeEvent ; 30 import java.beans.PropertyChangeListener ; 31 32 import java.util.ArrayList ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.TreeMap ; 36 37 import javax.swing.*; 38 import javax.swing.table.AbstractTableModel ; 39 40 41 51 public class NodeTableModel extends AbstractTableModel { 52 private static final String ATTR_INVISIBLE = "InvisibleInTreeTableView"; static final String ATTR_COMPARABLE_COLUMN = "ComparableColumnTTV"; static final String ATTR_SORTING_COLUMN = "SortingColumnTTV"; static final String ATTR_DESCENDING_ORDER = "DescendingOrderTTV"; private static final String ATTR_ORDER_NUMBER = "OrderNumberTTV"; private static final String ATTR_TREE_COLUMN = "TreeColumnTTV"; private static final String ATTR_MNEMONIC_CHAR = "ColumnMnemonicCharTTV"; 60 61 ArrayColumn[] allPropertyColumns = new ArrayColumn[] { }; 62 63 64 private int[] propertyColumns = new int[] { }; 65 66 67 private Node[] nodeRows = new Node[] { }; 68 69 70 private int sortColumn = -1; 71 72 73 private boolean existsComparableColumn = false; 74 private Property treeColumnProperty = null; 75 76 77 private PropertyChangeListener pcl = new PropertyChangeListener () { 78 public void propertyChange(PropertyChangeEvent evt) { 79 int row = rowForNode((Node) evt.getSource()); 81 82 if (row == -1) { 83 return; 84 } 85 86 int column = columnForProperty(evt.getPropertyName()); 87 88 if (column == -1) { 89 fireTableRowsUpdated(row, row); 90 } else { 91 fireTableCellUpdated(row, column); 92 } 93 } 94 }; 95 96 99 public void setNodes(Node[] nodes) { 100 for (int i = 0; i < nodeRows.length; i++) 101 nodeRows[i].removePropertyChangeListener(pcl); 102 103 nodeRows = nodes; 104 105 for (int i = 0; i < nodeRows.length; i++) 106 nodeRows[i].addPropertyChangeListener(pcl); 107 108 fireTableDataChanged(); 109 } 110 111 114 public void setProperties(Property[] props) { 115 int size = props.length; 116 sortColumn = -1; 117 118 int treePosition = -1; 119 120 for (int i = 0; i < props.length; i++) { 121 Object o = props[i].getValue(ATTR_TREE_COLUMN); 122 boolean x; 123 124 if ((o != null) && o instanceof Boolean ) { 125 if (((Boolean ) o).booleanValue()) { 126 treeColumnProperty = props[i]; 127 size--; 128 treePosition = i; 129 } 130 } 131 } 132 133 allPropertyColumns = new ArrayColumn[size]; 134 135 int visibleCount = 0; 136 existsComparableColumn = false; 137 138 TreeMap <Double , Integer > sort = new TreeMap <Double , Integer >(); 139 int i = 0; 140 int ia = 0; 141 142 while (i < props.length) { 143 if (i != treePosition) { 144 allPropertyColumns[ia] = new ArrayColumn(); 145 allPropertyColumns[ia].setProperty(props[i]); 146 147 if (isVisible(props[i])) { 148 visibleCount++; 149 150 Object o = props[i].getValue(ATTR_ORDER_NUMBER); 151 152 if ((o != null) && o instanceof Integer ) { 153 sort.put(new Double (((Integer ) o).doubleValue()), Integer.valueOf(ia)); 154 } else { 155 sort.put(new Double (ia + 0.1), new Integer (ia)); 156 } 157 } else { 158 allPropertyColumns[ia].setVisibleIndex(-1); 159 160 Object o = props[i].getValue(ATTR_SORTING_COLUMN); 161 162 if ((o != null) && o instanceof Boolean ) { 163 props[i].setValue(ATTR_SORTING_COLUMN, Boolean.FALSE); 164 } 165 } 166 167 if (!existsComparableColumn) { 168 Object o = props[i].getValue(ATTR_COMPARABLE_COLUMN); 169 170 if ((o != null) && o instanceof Boolean ) { 171 existsComparableColumn = ((Boolean ) o).booleanValue(); 172 } 173 } 174 175 ia++; 176 } 177 178 i++; 179 } 180 181 propertyColumns = new int[visibleCount]; 183 184 int j = 0; 185 Iterator it = sort.values().iterator(); 186 187 while (it.hasNext()) { 188 i = ((Integer ) it.next()).intValue(); 189 allPropertyColumns[i].setVisibleIndex(j); 190 propertyColumns[j] = i; 191 j++; 192 } 193 194 fireTableStructureChanged(); 195 } 196 197 199 private void computeVisiblePorperties(int visCount) { 200 propertyColumns = new int[visCount]; 201 202 TreeMap <Double , Integer > sort = new TreeMap <Double , Integer >(); 203 204 for (int i = 0; i < allPropertyColumns.length; i++) { 205 int vi = allPropertyColumns[i].getVisibleIndex(); 206 207 if (vi == -1) { 208 sort.put(new Double (i - 0.1), Integer.valueOf(i)); 209 } else { 210 sort.put(new Double (vi), Integer.valueOf(i)); 211 } 212 } 213 214 int j = 0; 215 Iterator <Integer > it = sort.values().iterator(); 216 217 while (it.hasNext()) { 218 int i = it.next().intValue(); 219 Property p = allPropertyColumns[i].getProperty(); 220 221 if (isVisible(p)) { 222 propertyColumns[j] = i; 223 allPropertyColumns[i].setVisibleIndex(j); 224 j++; 225 } else { 226 allPropertyColumns[i].setVisibleIndex(-1); 227 228 Object o = p.getValue(ATTR_SORTING_COLUMN); 229 230 if ((o != null) && o instanceof Boolean ) { 231 if (((Boolean ) o).booleanValue()) { 232 p.setValue(ATTR_SORTING_COLUMN, Boolean.FALSE); 233 p.setValue(ATTR_DESCENDING_ORDER, Boolean.FALSE); 234 } 235 } 236 } 237 } 238 239 fireTableStructureChanged(); 240 } 241 242 246 int getVisibleColumnWidth(int column) { 247 return allPropertyColumns[propertyColumns[column]].getWidth(); 248 } 249 250 254 int getArrayColumnWidth(int column) { 255 return allPropertyColumns[column].getWidth(); 256 } 257 258 262 void setVisibleColumnWidth(int column, int width) { 263 allPropertyColumns[propertyColumns[column]].setWidth(width); 264 } 265 266 270 void setArrayColumnWidth(int column, int width) { 271 allPropertyColumns[column].setWidth(width); 272 } 273 274 278 int getVisibleIndex(int arrayIndex) { 279 return allPropertyColumns[arrayIndex].getVisibleIndex(); 280 } 281 282 286 int getArrayIndex(int visibleIndex) { 287 for (int i = 0; i < allPropertyColumns.length; i++) { 288 if (allPropertyColumns[i].getVisibleIndex() == visibleIndex) { 289 return i; 290 } 291 } 292 293 return -1; 294 } 295 296 300 boolean isComparableColumn(int column) { 301 return isComparableColumnEx(propertyColumns[column]); 302 } 303 304 308 boolean isComparableColumnEx(int column) { 309 Property p = allPropertyColumns[column].getProperty(); 310 Object o = p.getValue(ATTR_COMPARABLE_COLUMN); 311 312 if ((o != null) && o instanceof Boolean ) { 313 return ((Boolean ) o).booleanValue(); 314 } 315 316 return false; 317 } 318 319 323 boolean isVisibleColumnEx(int column) { 324 for (int i = 0; i < propertyColumns.length; i++) { 325 if (column == propertyColumns[i]) { 326 return true; 327 } 328 } 329 330 return false; 331 } 332 333 335 boolean existsComparableColumn() { 336 return existsComparableColumn; 337 } 338 339 343 boolean isSortingColumnEx(int column) { 344 Property p = allPropertyColumns[column].getProperty(); 345 Object o = p.getValue(ATTR_SORTING_COLUMN); 346 347 if ((o != null) && o instanceof Boolean ) { 348 return ((Boolean ) o).booleanValue(); 349 } 350 351 return false; 352 } 353 354 358 void setSortingColumnEx(int column) { 359 if (sortColumn != -1) { 360 Property p = allPropertyColumns[sortColumn].getProperty(); 361 p.setValue(ATTR_SORTING_COLUMN, Boolean.FALSE); 362 p.setValue(ATTR_DESCENDING_ORDER, Boolean.FALSE); 363 } 364 365 if (column != -1) { 366 sortColumn = column; 368 Property p = allPropertyColumns[sortColumn].getProperty(); 369 p.setValue(ATTR_SORTING_COLUMN, Boolean.TRUE); 370 } else { 371 sortColumn = -1; 372 } 373 } 374 375 int translateVisibleColumnIndex(int index) { 376 if (index < 0) { 377 return index; 378 } 379 380 return propertyColumns[index]; 381 } 382 383 386 int getVisibleSortingColumn() { 387 if (sortColumn == -1) { 388 for (int i = 0; i < propertyColumns.length; i++) { 389 if (isSortingColumnEx(propertyColumns[i])) { 390 sortColumn = propertyColumns[i]; 391 392 return i; 393 } 394 } 395 } else { 396 if (isVisible(allPropertyColumns[sortColumn].getProperty())) { 397 return getVisibleIndex(sortColumn); 398 } 399 } 400 401 return -1; 402 } 403 404 407 int getSortingColumn() { 408 if (sortColumn == -1) { 409 for (int i = 0; i < allPropertyColumns.length; i++) { 410 if (isSortingColumnEx(i)) { 411 sortColumn = i; 412 413 return i; 414 } 415 } 416 } else { 417 return sortColumn; 418 } 419 420 return -1; 421 } 422 423 425 boolean isSortOrderDescending() { 426 if (sortColumn == -1) { 427 return false; 428 } 429 430 Property p = allPropertyColumns[sortColumn].getProperty(); 431 Object o = p.getValue(ATTR_DESCENDING_ORDER); 432 433 if ((o != null) && o instanceof Boolean ) { 434 return ((Boolean ) o).booleanValue(); 435 } 436 437 return false; 438 } 439 440 442 void setSortOrderDescending(boolean descending) { 443 if (sortColumn != -1) { 444 Property p = allPropertyColumns[sortColumn].getProperty(); 445 p.setValue(ATTR_DESCENDING_ORDER, descending ? Boolean.TRUE : Boolean.FALSE); 446 } 447 } 448 449 455 protected Property getPropertyFor(Node node, Property prop) { 456 Node.PropertySet[] propSets = node.getPropertySets(); 457 458 for (int i = 0; i < propSets.length; i++) { 459 Node.Property[] props = propSets[i].getProperties(); 460 461 for (int j = 0; j < props.length; j++) { 462 if (prop.equals(props[j])) { 463 return props[j]; 464 } 465 } 466 } 467 468 return null; 469 } 470 471 473 Node nodeForRow(int row) { 474 return nodeRows[row]; 475 } 476 477 481 Property propertyForColumn(int column) { 482 if (column >= 0) { 483 column = propertyColumns[column]; 484 } 485 486 return propertyForColumnEx(column); 487 } 488 489 493 Property propertyForColumnEx(int column) { 494 if (column == -1) { 495 return treeColumnProperty; 496 } else { 497 return allPropertyColumns[column].getProperty(); 498 } 499 } 500 501 504 int getColumnCountEx() { 505 return allPropertyColumns.length; 506 } 507 508 private int rowForNode(Node node) { 509 for (int i = 0; i < nodeRows.length; i++) { 510 if (node.equals(nodeRows[i])) { 511 return i; 512 } 513 } 514 515 return -1; 516 } 517 518 private int columnForProperty(String propName) { 519 for (int i = 0; i < propertyColumns.length; i++) { 520 if (allPropertyColumns[propertyColumns[i]].getProperty().getName().equals(propName)) { 521 return i; 522 } 523 } 524 525 return -1; 526 } 527 528 531 private boolean isVisible(Property p) { 532 Object o = p.getValue(ATTR_INVISIBLE); 533 534 if ((o != null) && o instanceof Boolean ) { 535 return !((Boolean ) o).booleanValue(); 536 } 537 538 return true; 539 } 540 541 543 private void setVisible(Property p, boolean visible) { 544 p.setValue(ATTR_INVISIBLE, (!visible) ? Boolean.TRUE : Boolean.FALSE); 545 } 546 547 551 554 public int getRowCount() { 555 return nodeRows.length; 556 } 557 558 561 public int getColumnCount() { 562 return propertyColumns.length; 563 } 564 565 570 public Object getValueAt(int row, int column) { 571 return getPropertyFor(nodeRows[row], allPropertyColumns[propertyColumns[column]].getProperty()); 572 } 573 574 579 public boolean isCellEditable(int row, int column) { 580 return getValueAt(row, column) != null; 581 } 582 583 587 public Class getColumnClass(int column) { 588 return Node.Property.class; 589 } 590 591 595 public String getColumnName(int column) { 596 return getColumnNameEx(propertyColumns[column]); 597 } 598 599 603 String getColumnNameEx(int column) { 604 return allPropertyColumns[column].getProperty().getDisplayName(); 605 } 606 607 609 boolean selectVisibleColumns(String viewName, String treeColumnName, String treeColumnDesc) { 610 boolean changed = false; 611 612 javax.swing.JPanel panel = new javax.swing.JPanel (); 613 panel.setLayout(new GridBagLayout()); 614 615 panel.getAccessibleContext().setAccessibleName( 616 NbBundle.getBundle(NodeTableModel.class).getString("ACSN_ColumnDialog") ); 617 panel.getAccessibleContext().setAccessibleDescription( 618 NbBundle.getBundle(NodeTableModel.class).getString("ACSD_ColumnDialog") ); 619 620 ArrayList <JCheckBox> boxes = new ArrayList <JCheckBox>(allPropertyColumns.length); 621 boolean[] oldvalues = new boolean[allPropertyColumns.length]; 622 int[] sortpointer = new int[allPropertyColumns.length]; 623 GridBagConstraints gridBagConstraints = new GridBagConstraints(); 624 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 625 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 626 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 12); 627 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 628 gridBagConstraints.weightx = 1.0; 629 630 GridBagConstraints labelConstraints = new GridBagConstraints(); 631 labelConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 632 labelConstraints.anchor = java.awt.GridBagConstraints.WEST; 633 labelConstraints.insets = new java.awt.Insets (12, 12, 0, 12); 634 labelConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 635 labelConstraints.weightx = 1.0; 636 637 JLabel desc = new JLabel(NbBundle.getBundle(NodeTableModel.class).getString("LBL_ColumnDialogDesc")); 638 panel.add(desc, labelConstraints); 639 640 GridBagConstraints firstConstraints = new GridBagConstraints(); 641 firstConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 642 firstConstraints.anchor = java.awt.GridBagConstraints.WEST; 643 firstConstraints.insets = new java.awt.Insets (12, 12, 0, 12); 644 firstConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 645 firstConstraints.weightx = 1.0; 646 647 JCheckBox first = new JCheckBox(treeColumnName + ": " + treeColumnDesc, true); first.setEnabled(false); 649 panel.add(first, firstConstraints); 650 651 String boxtext; 652 TreeMap <String , Integer > sort = new TreeMap <String , Integer >(); 653 654 for (int i = 0; i < allPropertyColumns.length; i++) { 655 oldvalues[i] = isVisible(allPropertyColumns[i].getProperty()); 656 boxtext = allPropertyColumns[i].getProperty().getDisplayName() + ": " + 657 allPropertyColumns[i].getProperty().getShortDescription(); sort.put(boxtext, Integer.valueOf(i)); 659 } 660 661 Iterator <String > it = sort.keySet().iterator(); 662 int j = 0; 663 664 while (it.hasNext()) { 665 boxtext = it.next(); 666 667 int i = sort.get(boxtext).intValue(); 668 JCheckBox b = new JCheckBox(boxtext, oldvalues[i]); 669 makeAccessibleCheckBox(b, allPropertyColumns[i].getProperty()); 670 sortpointer[j] = i; 671 panel.add(b, gridBagConstraints); 672 boxes.add(b); 673 j++; 674 } 675 676 String title = NbBundle.getMessage(NodeTableModel.class, "LBL_ColumnDialogTitle"); 677 678 if ((viewName != null) && (viewName.length() > 0)) { 679 title = viewName + " - " + title; } 681 682 DialogDescriptor dlg = new DialogDescriptor( 683 panel, title, true, DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION, 684 DialogDescriptor.DEFAULT_ALIGN, null, null 685 ); 686 687 final Dialog dialog = DialogDisplayer.getDefault().createDialog(dlg); 688 dialog.setVisible(true); 689 690 if (dlg.getValue().equals(DialogDescriptor.OK_OPTION)) { 691 int num = boxes.size(); 692 int nv = 0; 693 694 for (int i = 0; i < num; i++) { 695 JCheckBox b = boxes.get(i); 696 697 j = sortpointer[i]; 698 699 if (b.isSelected() != oldvalues[j]) { 700 setVisible(allPropertyColumns[j].getProperty(), b.isSelected()); 701 changed = true; 702 } 703 704 if (b.isSelected()) { 705 nv++; 706 } 707 } 708 709 711 717 if (changed) { 718 computeVisiblePorperties(nv); 719 } 720 } 721 722 return changed; 723 } 724 725 void makeAccessibleCheckBox(JCheckBox box, Property p) { 726 box.getAccessibleContext().setAccessibleName(p.getDisplayName()); 727 box.getAccessibleContext().setAccessibleDescription(p.getShortDescription()); 728 729 Object mnemonicChar = p.getValue(ATTR_MNEMONIC_CHAR); 730 731 if ((null != mnemonicChar) && (mnemonicChar.toString().length() > 0)) { 732 box.setMnemonic(mnemonicChar.toString().charAt(0)); 733 } 734 } 735 736 void moveColumn(int from, int to) { 737 int i = propertyColumns[from]; 738 int j = propertyColumns[to]; 739 740 propertyColumns[from] = j; 741 propertyColumns[to] = i; 742 743 allPropertyColumns[i].setVisibleIndex(to); 744 allPropertyColumns[j].setVisibleIndex(from); 745 746 sortColumn = -1; 747 } 748 749 751 static class ArrayColumn { 752 753 private Property property; 754 755 756 private int width; 757 758 759 private int visibleIndex; 760 761 ArrayColumn() { 762 } 763 764 767 public Property getProperty() { 768 return this.property; 769 } 770 771 774 public void setProperty(Property property) { 775 this.property = property; 776 } 777 778 781 public int getWidth() { 782 return this.width; 783 } 784 785 788 public void setWidth(int width) { 789 this.width = width; 790 } 791 792 795 public int getVisibleIndex() { 796 return this.visibleIndex; 797 } 798 799 802 public void setVisibleIndex(int visibleIndex) { 803 this.visibleIndex = visibleIndex; 804 property.setValue(ATTR_ORDER_NUMBER, new Integer (visibleIndex)); 805 } 806 } 807 } 808 | Popular Tags |