1 19 package org.netbeans.modules.xml.tax.beans.editor; 20 21 import java.util.*; 22 import java.awt.event.KeyAdapter ; 23 import java.awt.event.KeyEvent ; 24 import javax.swing.*; 25 import javax.swing.table.AbstractTableModel ; 26 import javax.swing.event.ListSelectionListener ; 27 import javax.swing.event.ListSelectionEvent ; 28 29 import org.openide.util.HelpCtx; 30 import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor; 31 32 import org.netbeans.tax.*; 33 import org.netbeans.tax.traversal.TreeNodeFilter; 34 import org.netbeans.modules.xml.tax.util.TAXUtil; 35 36 41 public class TreeNodeFilterCustomEditor extends JPanel implements EnhancedCustomPropertyEditor { 42 43 private static final long serialVersionUID = 1767193347881681541L; 44 45 46 47 private static final Map publicNodeTypeNamesMap = new HashMap(); 48 49 50 54 static { 55 publicNodeTypeNamesMap.put (TreeNode.class, Util.THIS.getString ("NAME_Any_Node_Type")); 56 publicNodeTypeNamesMap.put (TreeParentNode.class, Util.THIS.getString ("NAME_Any_Parent_Node_Type")); 57 publicNodeTypeNamesMap.put (TreeCharacterData.class, Util.THIS.getString ("NAME_Any_Character_Data_Node_Type")); 58 publicNodeTypeNamesMap.put (TreeReference.class, Util.THIS.getString ("NAME_Any_Reference_Node_Type")); 59 publicNodeTypeNamesMap.put (TreeNodeDecl.class, Util.THIS.getString ("NAME_Any_Declaration_Node_Type")); 61 62 publicNodeTypeNamesMap.put (TreeComment.class, Util.THIS.getString ("NAME_Comment_Node_Type")); 63 publicNodeTypeNamesMap.put (TreeProcessingInstruction.class, Util.THIS.getString ("NAME_Processing_Instruction_Node_Type")); 64 publicNodeTypeNamesMap.put (TreeText.class, Util.THIS.getString ("NAME_Text_Node_Type")); 65 publicNodeTypeNamesMap.put (TreeCDATASection.class, Util.THIS.getString ("NAME_CDATA_Section_Node_Type")); 66 publicNodeTypeNamesMap.put (TreeElement.class, Util.THIS.getString ("NAME_Element_Node_Type")); 67 publicNodeTypeNamesMap.put (TreeAttribute.class, Util.THIS.getString ("NAME_Attribute_Node_Type")); 68 publicNodeTypeNamesMap.put (TreeConditionalSection.class, Util.THIS.getString ("NAME_Conditional_Section_Node_Type")); 71 publicNodeTypeNamesMap.put (TreeDocumentType.class, Util.THIS.getString ("NAME_Document_Type_Node_Type")); 72 publicNodeTypeNamesMap.put (TreeGeneralEntityReference.class, Util.THIS.getString ("NAME_General_Entity_Reference_Node_Type")); 73 publicNodeTypeNamesMap.put (TreeParameterEntityReference.class, Util.THIS.getString ("NAME_Parameter_Entity_Reference_Node_Type")); 74 publicNodeTypeNamesMap.put (TreeElementDecl.class, Util.THIS.getString ("NAME_Element_Declaration_Node_Type")); 75 publicNodeTypeNamesMap.put (TreeEntityDecl.class, Util.THIS.getString ("NAME_Entity_Declaration_Node_Type")); 76 publicNodeTypeNamesMap.put (TreeAttlistDecl.class, Util.THIS.getString ("NAME_Attlist_Declaration_Node_Type")); 77 publicNodeTypeNamesMap.put (TreeNotationDecl.class, Util.THIS.getString ("NAME_Notation_Declaration_Node_Type")); 78 } 79 80 81 82 private final TreeNodeFilter filter; 83 84 85 private final List nodeTypesList; 86 87 88 private NodeTypesTableModel tableModel; 89 90 91 95 96 97 public TreeNodeFilterCustomEditor (TreeNodeFilter filter) { 98 this.filter = filter; 99 this.nodeTypesList = new LinkedList (Arrays.asList (filter.getNodeTypes())); 100 101 initComponents(); 102 ownInitComponents(); 103 initAccessibility(); 104 105 HelpCtx.setHelpIDString (this, this.getClass().getName()); 106 } 107 108 109 111 private void ownInitComponents () { 112 tableModel = (NodeTypesTableModel)nodeTypesTable.getModel(); 113 114 ListSelectionModel selModel = nodeTypesTable.getSelectionModel(); 115 selModel.addListSelectionListener (new ListSelectionListener () { 116 public void valueChanged (ListSelectionEvent e) { 117 if (e.getValueIsAdjusting()) 118 return; 119 ListSelectionModel lsm = (ListSelectionModel)e.getSource(); 120 if (lsm.isSelectionEmpty()) { 121 removeButton.setEnabled (false); 122 } else { 123 removeButton.setEnabled (true); 124 } 125 } 126 }); 127 128 135 JComboBox cb = new JComboBox (getPublicNodeTypesInheritanceTree()); 136 cb.setEditable (false); 137 DefaultCellEditor dce = new DefaultCellEditor (cb); 138 nodeTypesTable.getColumnModel().getColumn (0).setCellEditor (dce); 140 } 141 142 143 148 public Object getPropertyValue () throws IllegalStateException { 149 short acceptPolicy = acceptRadioButton.isSelected() ? 150 TreeNodeFilter.ACCEPT_TYPES : 151 TreeNodeFilter.REJECT_TYPES; 152 Class [] nodeTypes = (Class [])nodeTypesList.toArray (new Class [0]); 153 154 return new TreeNodeFilter (nodeTypes, acceptPolicy); 155 } 156 157 158 163 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 165 166 acceptPolicyGroup = new javax.swing.ButtonGroup (); 167 acceptPolicyPanel = new javax.swing.JPanel (); 168 acceptRejectLabel = new javax.swing.JLabel (); 169 rbPanel = new javax.swing.JPanel (); 170 acceptRadioButton = new javax.swing.JRadioButton (); 171 acceptRadioButton.setSelected (filter.getAcceptPolicy() == TreeNodeFilter.ACCEPT_TYPES); 172 rejectRadioButton = new javax.swing.JRadioButton (); 173 rejectRadioButton.setSelected (filter.getAcceptPolicy() == TreeNodeFilter.REJECT_TYPES); 174 tablePanel = new javax.swing.JPanel (); 175 tableScrollPane = new javax.swing.JScrollPane (); 176 nodeTypesTable = new javax.swing.JTable (); 177 nodeTypesTable.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); 178 addButton = new javax.swing.JButton (); 179 removeButton = new javax.swing.JButton (); 180 181 setLayout(new java.awt.BorderLayout ()); 182 183 acceptPolicyPanel.setLayout(new java.awt.GridBagLayout ()); 184 185 acceptRejectLabel.setText(Util.THIS.getString ("LBL_acceptReject")); 186 acceptRejectLabel.setLabelFor(rbPanel); 187 gridBagConstraints = new java.awt.GridBagConstraints (); 188 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 189 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 190 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 0); 191 acceptPolicyPanel.add(acceptRejectLabel, gridBagConstraints); 192 193 rbPanel.setLayout(new java.awt.GridBagLayout ()); 194 195 acceptRadioButton.setText(Util.THIS.getString ("LBL_showItRadioButton")); 196 acceptPolicyGroup.add(acceptRadioButton); 197 gridBagConstraints = new java.awt.GridBagConstraints (); 198 gridBagConstraints.gridx = 0; 199 gridBagConstraints.gridy = 0; 200 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 201 gridBagConstraints.weightx = 1.0; 202 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 203 rbPanel.add(acceptRadioButton, gridBagConstraints); 204 205 rejectRadioButton.setText(Util.THIS.getString ("LBL_hideItRadioButton")); 206 acceptPolicyGroup.add(rejectRadioButton); 207 gridBagConstraints = new java.awt.GridBagConstraints (); 208 gridBagConstraints.gridx = 0; 209 gridBagConstraints.gridy = 1; 210 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 211 gridBagConstraints.weightx = 1.0; 212 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 213 rbPanel.add(rejectRadioButton, gridBagConstraints); 214 215 gridBagConstraints = new java.awt.GridBagConstraints (); 216 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 217 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 218 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 219 gridBagConstraints.weightx = 1.0; 220 gridBagConstraints.weighty = 1.0; 221 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 11); 222 acceptPolicyPanel.add(rbPanel, gridBagConstraints); 223 224 add(acceptPolicyPanel, java.awt.BorderLayout.NORTH); 225 226 tablePanel.setLayout(new java.awt.GridBagLayout ()); 227 228 nodeTypesTable.setModel(new NodeTypesTableModel()); 229 nodeTypesTable.setPreferredScrollableViewportSize(new java.awt.Dimension (300, 200)); 230 tableScrollPane.setViewportView(nodeTypesTable); 231 232 gridBagConstraints = new java.awt.GridBagConstraints (); 233 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 234 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 235 gridBagConstraints.weightx = 1.0; 236 gridBagConstraints.weighty = 1.0; 237 gridBagConstraints.insets = new java.awt.Insets (11, 12, 0, 0); 238 tablePanel.add(tableScrollPane, gridBagConstraints); 239 240 addButton.setText(Util.THIS.getString ("LBL_addButton")); 241 addButton.addActionListener(new java.awt.event.ActionListener () { 242 public void actionPerformed(java.awt.event.ActionEvent evt) { 243 addButtonActionPerformed(evt); 244 } 245 }); 246 247 gridBagConstraints = new java.awt.GridBagConstraints (); 248 gridBagConstraints.gridx = 1; 249 gridBagConstraints.gridy = 0; 250 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 251 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 252 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 253 gridBagConstraints.insets = new java.awt.Insets (11, 11, 0, 11); 254 tablePanel.add(addButton, gridBagConstraints); 255 256 removeButton.setText(Util.THIS.getString ("LBL_removeButton")); 257 removeButton.setEnabled(false); 258 removeButton.addActionListener(new java.awt.event.ActionListener () { 259 public void actionPerformed(java.awt.event.ActionEvent evt) { 260 removeButtonActionPerformed(evt); 261 } 262 }); 263 264 gridBagConstraints = new java.awt.GridBagConstraints (); 265 gridBagConstraints.gridx = 1; 266 gridBagConstraints.gridy = 1; 267 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 268 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 269 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 270 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 271 gridBagConstraints.insets = new java.awt.Insets (5, 11, 11, 11); 272 tablePanel.add(removeButton, gridBagConstraints); 273 274 add(tablePanel, java.awt.BorderLayout.CENTER); 275 276 } 278 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { int sel = nodeTypesTable.getSelectedRow(); 281 if (sel != -1) { 282 tableModel.removeRow (sel); 283 284 int numRows = nodeTypesTable.getModel().getRowCount(); 285 if (numRows > 0) { 286 sel = Math.min (sel, numRows - 1); 287 nodeTypesTable.getSelectionModel().setSelectionInterval (sel, sel); 288 } 289 } 290 } 292 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { nodeTypesList.add (TreeNode.class); 295 tableModel.fireTableDataChanged(); 296 } 298 299 private javax.swing.JRadioButton rejectRadioButton; 301 private javax.swing.JPanel rbPanel; 302 private javax.swing.JPanel acceptPolicyPanel; 303 private javax.swing.JButton addButton; 304 private javax.swing.JLabel acceptRejectLabel; 305 private javax.swing.JScrollPane tableScrollPane; 306 private javax.swing.JTable nodeTypesTable; 307 private javax.swing.JRadioButton acceptRadioButton; 308 private javax.swing.JPanel tablePanel; 309 private javax.swing.ButtonGroup acceptPolicyGroup; 310 private javax.swing.JButton removeButton; 311 313 314 315 319 320 private class RowKeyListener extends KeyAdapter { 321 322 323 private JTable table; 324 325 326 330 public RowKeyListener (JTable table) { 331 this.table = table; 332 } 333 334 335 339 341 public void keyReleased (KeyEvent e) { 342 if (e.getKeyCode() == KeyEvent.VK_DELETE) { 343 tableModel.removeRow (table.getSelectedRow()); 344 } 345 } 346 } 347 348 349 353 356 private class NodeTypesTableModel extends AbstractTableModel { 357 358 private static final long serialVersionUID =-1438087942670592779L; 359 360 362 public void removeRow (int row) { 363 nodeTypesList.remove (row); 364 fireTableDataChanged(); 365 } 366 367 368 369 public int getRowCount () { 370 return nodeTypesList.size(); 371 } 372 373 374 public int getColumnCount () { 375 return 1; 376 } 377 378 379 public Class getColumnClass (int index) { 380 return Class .class; 381 } 382 383 385 public Object getValueAt (int row, int column) { 386 Object retVal = new Item (new NamedClass ((Class )nodeTypesList.get (row))); 387 388 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("<-- getValue: row = " + row); if ( Util.THIS.isLoggable() ) Util.THIS.debug ("<-- getValue: column = " + column); if ( Util.THIS.isLoggable() ) Util.THIS.debug ("<-- getValue: " + retVal.getClass().getName() + " -- '" + retVal + "'"); 392 return retVal; 393 } 394 395 397 public void setValueAt (Object val, int row, int column) { 398 if ( row >= nodeTypesList.size() ) { 399 return; 406 } 407 408 Class type = null; 409 410 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("--> setValue: " + val.getClass().getName() + " -- '" + val + "'"); if ( Util.THIS.isLoggable() ) Util.THIS.debug ("--> setValue: row = " + row); if ( Util.THIS.isLoggable() ) Util.THIS.debug ("--> setValue: column = " + column); 414 if ( val instanceof String ) { 415 try { 416 type = Class.forName (val.toString()); 417 } catch (ClassNotFoundException exc) { 418 } 420 } else if ( val instanceof Item ) { 421 type = ((Item)val).clazz.clazz; 422 } else if ( val instanceof NamedClass ) { 423 type = ((NamedClass)val).clazz; 424 } 425 426 if ( ( type == null ) || 427 ( TreeNodeFilter.isValidNodeType (type) == false ) ) { 428 TAXUtil.notifyWarning (Util.THIS.getString ("MSG_invalidNodeType", val.toString())); 429 return; 430 } 431 nodeTypesList.set (row, type); 432 } 433 434 436 public String getColumnName (int column) { 437 return Util.THIS.getString ("LBL_nodeType"); 438 } 439 440 443 public boolean isCellEditable (int rowIndex, int columnIndex) { 444 return true; 445 } 446 447 } 449 450 454 457 private static class NamedClass { 458 459 460 private final Class clazz; 461 462 463 public NamedClass (Class clazz) { 464 this.clazz = clazz; 465 } 466 467 468 470 public String toString () { 471 String name = (String )publicNodeTypeNamesMap.get (clazz); 472 473 if ( name == null ) { 474 name = clazz.getName(); 475 } 476 477 return name; 478 } 479 480 482 public boolean equals (Object obj) { 483 if ( (obj instanceof NamedClass) == false ) { 484 return false; 485 } 486 NamedClass peer = (NamedClass)obj; 487 return clazz.equals (peer.clazz); 488 } 489 490 } 492 493 497 500 private static class NamedClassComparator implements Comparator { 501 502 504 public int compare (Object obj1, Object obj2) throws ClassCastException { 505 return (obj1.toString().compareTo (obj2.toString())); 506 } 507 508 510 public boolean equals (Object obj) { 511 return ( obj instanceof NamedClassComparator ); 512 } 513 514 } 516 517 518 522 523 private static Vector publicNodeTypesInheritanceTree; 524 525 526 528 private static Vector getPublicNodeTypesInheritanceTree () { 529 if ( publicNodeTypesInheritanceTree == null ) { 530 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Init Set"); 532 Item rootItem = new Item(); 533 Object [] array = publicNodeTypeNamesMap.keySet().toArray(); 534 for (int i = 0; i < array.length; i++) { 535 Class clazz = (Class )array[i]; 536 Item.insertItemIntoLayer (rootItem.layer, Item.getItem (clazz)); 537 538 if ( clazz.isInterface() ) { 539 for (int j = 0; j < i; j++) { 540 Item.insertItemIntoLayer (rootItem.layer, Item.getItem ((Class )array[j])); 541 } 542 } 543 } 544 545 publicNodeTypesInheritanceTree = new Vector(); 546 fillPublicNodeTypesInheritanceTree (rootItem.layer, ""); 548 Item.itemMap.clear(); 549 Item.itemMap = null; 550 rootItem = null; 551 } 552 553 return publicNodeTypesInheritanceTree; 554 } 555 556 558 private static void fillPublicNodeTypesInheritanceTree (Set layer, String prefix) { 559 Iterator it = layer.iterator(); 560 while ( it.hasNext() ) { 561 Item item = (Item) it.next(); 562 String itemPrefix = ""; if ( prefix.length() != 0 ) { 564 if ( it.hasNext() ) { 565 itemPrefix = prefix + "|- "; } else { 567 itemPrefix = prefix + "`- "; } 569 } 570 Item newItem = new Item (item, itemPrefix); 571 572 if ( Util.THIS.isLoggable() ) Util.THIS.debug (">>" + newItem.toString() + "<<"); 574 publicNodeTypesInheritanceTree.add (newItem); 575 576 String newPrefix; 577 if ( prefix.length() == 0 ) { 578 newPrefix = " "; } else { 580 if ( it.hasNext() ) { 581 newPrefix = prefix + "| "; } else { 583 newPrefix = prefix + " "; } 585 } 586 fillPublicNodeTypesInheritanceTree (item.layer, newPrefix); 587 } 588 } 589 590 591 594 private static class Item { 595 596 private static Map itemMap; 597 598 599 private final NamedClass clazz; 600 601 private final Set layer; 602 603 private final String prefix; 604 605 606 private Item (NamedClass clazz, Set layer, String prefix) { 607 this.clazz = clazz; 608 this.layer = layer; 609 this.prefix = prefix; 610 } 611 612 613 private Item (Item item, String prefix) { 614 this (item.clazz, null, prefix); 615 } 616 617 618 private Item (NamedClass clazz) { 619 this (clazz, new TreeSet (new NamedClassComparator()), new String ()); 620 } 621 622 623 private Item () { 624 this (new NamedClass (null)); 625 } 626 627 628 630 public String toString () { 631 return prefix + clazz.toString(); 632 } 633 634 635 637 public boolean equals (Object obj) { 638 if ( (obj instanceof Item) == false ) { 639 return false; 640 } 641 642 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("___ Item::equals: this = " + this); if ( Util.THIS.isLoggable() ) Util.THIS.debug ("___ ::equals: obj = " + obj); 645 Item peer = (Item)obj; 646 return clazz.equals (peer.clazz); 647 } 648 649 650 652 private static Item getItem (Class clazz) { 653 if ( itemMap == null ) { 654 itemMap = new HashMap(); 655 } 656 657 Item item = (Item) itemMap.get (clazz); 658 if ( item == null ) { 659 itemMap.put (clazz, item = new Item (new NamedClass (clazz))); 660 } 661 return item; 662 } 663 664 666 private static void insertItemIntoLayer (Set layer, Item newItem) { 667 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("\n\nInsert newItem : " + newItem); if ( Util.THIS.isLoggable() ) Util.THIS.debug (" Item : set = " + layer); 670 boolean inserted = false; 671 672 Object [] array = layer.toArray(); 673 for (int i = 0; i < array.length; i++) { 674 Item item = (Item) array[i]; 675 676 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" Item : item [" + i + "] = " + item); 678 if ( item.clazz.clazz == newItem.clazz.clazz ) { if ( Util.THIS.isLoggable() ) Util.THIS.debug (" Item : #1 -= [ ITEM.clazz.clazz == NEW_ITEM.clazz.clazz => IGNORE insert ]=-"); inserted = true; 682 } else if ( item.clazz.clazz.isAssignableFrom (newItem.clazz.clazz) ) { if ( Util.THIS.isLoggable() ) Util.THIS.debug (" Item : #2 -= [ NEW_ITEM is subclass of actual ITEM => insert to ITEM.LAYER ]=-"); 685 insertItemIntoLayer (item.layer, newItem); 686 inserted = true; 687 } else if ( newItem.clazz.clazz.isAssignableFrom (item.clazz.clazz) ) { if ( Util.THIS.isLoggable() ) Util.THIS.debug (" Item : #3 -= [ actual ITEM '" + item + "' is subclass of NEW_ITEM => item REMOVED & insert to NEW_ITEM.LAYER ]=-"); 690 if ( newItem.clazz.clazz.isInterface() == false ) { 691 layer.remove (item); 692 insertItemIntoLayer (newItem.layer, item); 693 } 694 } 695 } 696 697 if ( inserted == false ) { if ( Util.THIS.isLoggable() ) Util.THIS.debug (" Item : #4 -= [ item '" + newItem + "' INSERTED into " + layer + " ] =-"); 700 layer.add (newItem); 701 } 702 } 703 704 } 705 706 708 public void initAccessibility(){ 709 710 this.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_TreeNodeFilterCustomEditor")); 711 712 acceptRadioButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_acceptRejectLabel")); 713 acceptRadioButton.setMnemonic((Util.THIS.getString ("LBL_showItRadioButton_Mnem")).charAt(0)); 714 715 rejectRadioButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_rejectRadioButton")); 716 rejectRadioButton.setMnemonic((Util.THIS.getString ("LBL_hideItRadioButton_Mnem")).charAt(0)); 717 718 addButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_addButton1")); 719 addButton.setMnemonic((Util.THIS.getString ("LBL_addButton_Mnem")).charAt(0)); 720 721 removeButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_removeButton1")); 722 removeButton.setMnemonic((Util.THIS.getString ("LBL_removeButton_Mnem")).charAt(0)); 723 724 nodeTypesTable.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_nodeTypesTable")); 725 nodeTypesTable.getAccessibleContext().setAccessibleName(Util.THIS.getString("ACSN_nodeTypesTable")); 726 } 727 728 729 public static final void main (String [] args) throws Exception { 731 Vector vector = getPublicNodeTypesInheritanceTree(); 732 733 } 739 740 } 741 | Popular Tags |