1 19 20 28 29 package org.netbeans.modules.xml.refactoring.ui.readers; 30 31 import java.awt.Color ; 32 import java.awt.Image ; 33 import java.beans.BeanInfo ; 34 import java.util.HashMap ; 35 import java.util.concurrent.ExecutionException ; 36 import javax.swing.ImageIcon ; 37 import javax.swing.tree.DefaultTreeModel ; 38 import java.text.MessageFormat ; 39 import java.util.ArrayList ; 40 import java.util.Collection ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.Map ; 44 import java.util.Map.Entry; 45 import java.util.Set ; 46 47 import org.openide.filesystems.FileUtil; 48 import org.openide.loaders.DataObject; 49 import org.openide.loaders.DataObjectNotFoundException; 50 import org.openide.util.Utilities; 51 import org.openide.ErrorManager; 52 import org.openide.awt.StatusDisplayer; 53 import org.openide.filesystems.FileObject; 54 import org.openide.util.NbBundle; 55 56 import org.netbeans.api.project.Project; 57 import org.netbeans.api.project.SourceGroup; 58 import org.netbeans.api.project.FileOwnerQuery; 59 import org.netbeans.spi.project.ui.LogicalViewProvider; 60 61 import org.netbeans.modules.xml.xam.Component; 62 import org.netbeans.modules.xml.xam.Model; 63 import org.netbeans.modules.xml.xam.Referenceable; 64 import org.netbeans.modules.xml.xam.Named; 65 import org.netbeans.modules.xml.xam.NamedReferenceable; 66 import org.netbeans.modules.xml.xam.dom.DocumentModel; 67 68 import org.netbeans.modules.xml.nbprefuse.AnalysisConstants; 69 import org.netbeans.modules.xml.refactoring.FindUsageResult; 70 import org.netbeans.modules.xml.refactoring.UsageGroup; 71 import org.netbeans.modules.xml.refactoring.ui.j.ui.CheckNode; 72 import org.netbeans.modules.xml.refactoring.ui.util.AnalysisUtilities; 73 import org.netbeans.modules.xml.refactoring.RefactoringManager; 74 import org.netbeans.modules.xml.refactoring.Usage; 75 import org.netbeans.modules.xml.refactoring.UsageSet; 76 import org.netbeans.modules.xml.refactoring.spi.UIHelper; 77 78 import prefuse.data.Node; 79 import prefuse.data.Edge; 80 import prefuse.data.Graph; 81 82 88 public class WhereUsedReader { 89 90 private static final ImageIcon FIND_USAGES_ICON = new ImageIcon ( 91 Utilities.loadImage( 92 "org/netbeans/modules/refactoring/api/resources/"+ 93 "findusages.png")); 94 95 public static final prefuse.data.Schema FIND_USAGES_NODES_SCHEMA = 96 new prefuse.data.Schema(); static { 98 FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.LABEL, 99 String .class, AnalysisConstants.EMPTY_STRING); 100 FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.ID, 101 String .class, AnalysisConstants.EMPTY_STRING); 102 FIND_USAGES_NODES_SCHEMA.addColumn( 103 AnalysisConstants.COMPONENT_TYPE_NAME, 104 String .class, AnalysisConstants.EMPTY_STRING); 105 FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.ELEMENT_TYPE, 106 String .class, AnalysisConstants.EMPTY_STRING); 107 109 FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.XML_FILENAME, 110 String .class, AnalysisConstants.EMPTY_STRING); 111 FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.FILE_OBJECT, 112 FileObject.class); 113 FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.FILE_TYPE, 114 String .class, AnalysisConstants.EMPTY_STRING); 115 FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.TOOLTIP, 116 String .class, AnalysisConstants.EMPTY_STRING); FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.XAM_COMPONENT, 118 Component.class); 119 FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.OPENIDE_NODE, 120 org.openide.nodes.Node.class); 121 FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.JAVA_AWT_IMAGE, 122 Image .class); 123 124 FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.FILE_GROUP, 131 int.class, -1); 132 FIND_USAGES_NODES_SCHEMA.addColumn( 135 AnalysisConstants.FILE_NODE_FILE_GROUP, int.class, -1); 136 138 FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.IS_PRIMITIVE, 139 boolean.class, false); FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.IS_QUERY_NODE, 141 boolean.class, false); 142 FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.IS_USAGE_NODE, 143 boolean.class, false); 144 FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.IS_FILE_NODE, 145 boolean.class, false); 146 FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.IS_EXPANDED, 147 boolean.class, false); 148 FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.MOUSEOVER, 149 boolean.class, false); 150 } 151 152 153 154 public static final prefuse.data.Schema FIND_USAGES_EDGES_SCHEMA = 155 new prefuse.data.Schema(); static { 157 FIND_USAGES_EDGES_SCHEMA.addColumn(AnalysisConstants.LABEL, 158 String .class, AnalysisConstants.EMPTY_STRING); 159 FIND_USAGES_EDGES_SCHEMA.addColumn(AnalysisConstants.EDGE_TYPE, 160 String .class, AnalysisConstants.EMPTY_STRING); 161 163 FIND_USAGES_EDGES_SCHEMA.addColumn(AnalysisConstants.TOOLTIP, 164 String .class, AnalysisConstants.EMPTY_STRING); 165 FIND_USAGES_EDGES_SCHEMA.addColumn(AnalysisConstants.FILE_GROUP, 172 int.class, -1); 173 } 174 175 196 public Object [] loadGraph(FindUsageResult fuResult, 197 Referenceable queryComponent, 198 boolean buildTreeModel, 199 boolean isPrimitive, 200 Model model) 201 { 202 if (fuResult == null) { 203 return new Object [0]; 204 } 205 206 List <Component> componentsInGraph = new ArrayList <Component>(); 207 int fileGroupNumber = 0; 208 int usagesCount = 0; 209 211 Graph graph = new Graph(true); graph.getNodeTable().addColumns(FIND_USAGES_NODES_SCHEMA); 213 graph.getEdgeTable().addColumns(FIND_USAGES_EDGES_SCHEMA); 214 215 216 220 DefaultTreeModel treeModel = null; 224 if (buildTreeModel){ 225 treeModel = new DefaultTreeModel ( 226 new CheckNode(AnalysisUtilities.getName(queryComponent), FIND_USAGES_ICON)); 227 } 232 233 241 UsageSet usageSet = null; 242 try { 243 usageSet = fuResult.get(); 244 } catch (InterruptedException ex) { 245 ErrorManager.getDefault().notify(ex); 246 } catch (ExecutionException ex) { 247 ErrorManager.getDefault().notify(ex); 248 } 249 250 Node queryNode = createQueryNode(graph, 251 queryComponent, 252 getUIHelper(queryComponent), 253 false, 254 componentsInGraph, 255 false, 256 fileGroupNumber); 257 258 Map <SourceGroup, Map <FileObject,Set <UsageGroup>>> sortedUses = 260 usageSet.getSortedUsages(); 261 Set <Entry<SourceGroup,Map <FileObject,Set <UsageGroup>>>> sgUses = 262 sortedUses.entrySet(); 263 LogicalViewProvider viewProvider = null; 264 Project project = null; 265 CheckNode projectNode = null; 266 for (Entry<SourceGroup, Map <FileObject, Set <UsageGroup>>> sgUse:sgUses){ 267 Set <Entry<FileObject,Set <UsageGroup>>> pkgUses = sgUse.getValue().entrySet(); 268 for (Entry<FileObject, Set <UsageGroup>> pkgUse:pkgUses){ 269 Set <UsageGroup> usages = pkgUse.getValue(); 270 for (UsageGroup usage:usages){ 271 int count = usage.getItems().size(); 272 if (count < 1){ 273 continue; 274 } 275 usagesCount += count; 276 addUsagesToGraph( 277 usage, 278 graph, 279 isPrimitive, 280 componentsInGraph, 281 queryComponent, 282 queryNode, 283 ++fileGroupNumber); 284 if (buildTreeModel){ 285 addUsagesToTreeModel(usage, treeModel); 286 } 287 } 288 } 289 } 290 291 if (buildTreeModel){ 292 ((CheckNode)treeModel.getRoot()).setLabel( 293 createRootTreeNodeLabel( 294 AnalysisUtilities.getName(queryComponent), usagesCount)); 295 } 296 if (usagesCount == 1) { 297 StatusDisplayer.getDefault().setStatusText( 298 MessageFormat.format(NbBundle.getMessage( 299 WhereUsedReader.class, 300 "LBL_Usage_Found"), 301 new Object [] { 302 queryNode.getString(AnalysisConstants.LABEL) 303 } 304 )); 305 } else { 306 StatusDisplayer.getDefault().setStatusText( 307 MessageFormat.format(NbBundle.getMessage( 308 WhereUsedReader.class, 309 "LBL_Usages_Found"), 310 new Object [] {Integer.valueOf(usagesCount), 311 queryNode.getString(AnalysisConstants.LABEL) 312 } 313 )); 314 } 315 316 318 return new Object [] {graph, treeModel}; 319 320 } 321 322 private static UIHelper getUIHelper(Referenceable component) { 323 return RefactoringManager.getInstance().getTargetComponentUIHelper(component); 324 } 325 326 private static String createRootTreeNodeLabel(String name, int usagesCount){ 327 328 String description = MessageFormat.format(NbBundle.getMessage( 329 WhereUsedReader.class, 330 "DSC_WhereUsed"), 331 new Object [] {name} ); 332 333 StringBuilder desc = new StringBuilder (" [" + usagesCount); desc.append(' '); 335 desc.append(usagesCount == 1 ? 336 NbBundle.getMessage(WhereUsedReader.class, "LBL_Occurrence") : 337 NbBundle.getMessage(WhereUsedReader.class, "LBL_Occurrences") 338 ); 339 desc.append(']'); 340 return description + desc.toString(); 341 } 342 343 344 356 private void addUsagesToGraph( 357 UsageGroup usage, 358 Graph graph, 359 boolean isPrimitive, 360 List <Component> componentsInGraph, 361 Referenceable queryComponent, 362 Node queryNode, 363 int fileGroupNumber) { 364 Collection <Usage> items = usage.getItems(); 366 UIHelper uiHelper = usage.getEngine().getUIHelper(); 367 368 Node fileNode = createFileNode(graph, 373 queryComponent, 374 (FileObject)usage.getModel().getModelSource().getLookup().lookup(FileObject.class), 375 queryNode, 376 fileGroupNumber); 377 378 Edge fileEdge = graph.addEdge(fileNode,queryNode); 385 fileEdge.setString(AnalysisConstants.EDGE_TYPE, 386 AnalysisConstants.FILE_EDGE_TYPE); 387 fileEdge.setInt(AnalysisConstants.FILE_GROUP, 388 fileGroupNumber); 389 390 for (Usage item:items){ 391 392 List <Component> aPath = uiHelper.getRelevantPathFromRoot(item.getComponent()); 393 Node parent = fileNode; 394 int i = isPrimitive?1:0; 396 for (; i < aPath.size();i++){ 397 Component sc = aPath.get(i); 398 Node pn = null; 399 400 if (componentsInGraph.contains(sc)){ 401 pn = findDup(graph, sc); 404 assert pn != null:"Cannot find Node for Component " 405 + 406 sc; } 408 409 if (pn == null){ 410 pn = createNode(graph, 411 sc, 412 uiHelper, 413 isPrimitive, 414 componentsInGraph, 415 queryComponent, 416 fileGroupNumber); 417 } 418 419 AnalysisUtilities.ToolTipLine topLine = null; 420 if (i == aPath.size()-1){ 422 pn.setBoolean(AnalysisConstants.IS_USAGE_NODE, true); 423 424 topLine = new AnalysisUtilities.ToolTipLine(( 425 MessageFormat.format(NbBundle.getMessage( 426 WhereUsedReader.class, "LBL_Uses_Component"), 427 new Object [] {queryNode.getString( 428 AnalysisConstants.LABEL) })), 429 100, 430 Color.BLACK.getRGB(), 431 AnalysisUtilities.ToolTipLine. 432 HorizontalAlignment.CENTER); 433 addApppropriateEdge(graph, 436 pn, 437 queryNode, 438 fileGroupNumber, 439 item.getType()); 440 441 } 443 AnalysisUtilities.ToolTipLine typeLine = 444 new AnalysisUtilities.ToolTipLine(getCompTypeDisplayName(pn), 445 100, 446 Color.BLACK.getRGB(), 447 AnalysisUtilities.ToolTipLine. 448 HorizontalAlignment.CENTER); 449 String toolTip = AnalysisUtilities.createHTMLToolTip( 450 new AnalysisUtilities.ToolTipLine[] {topLine, typeLine}); 451 pn.setString(AnalysisConstants.TOOLTIP, toolTip); 452 453 454 addApppropriateEdge(graph, 456 pn, 457 parent, 458 fileGroupNumber, 459 null); 460 parent = pn; 461 462 463 } 464 } 465 466 } 467 468 490 private void addUsagesToTreeModel(UsageGroup usageGroup, DefaultTreeModel treeModel) { 491 assert usageGroup != null:"usageGroup cannot be null"; 492 CheckNode fileTreeNode = createFileTreeNode(usageGroup.getFileObject(),treeModel); 493 Map <Component,CheckNode> componentsInModel = 494 new HashMap <Component,CheckNode>(); 495 496 UIHelper uiHelper = usageGroup.getEngine().getUIHelper(); 497 org.openide.nodes.Node helperNode = null; 498 Collection <Usage> items = usageGroup.getItems(); 499 for (Usage item:items){ 500 List <Component> aPath = uiHelper.getRelevantPathFromRoot(item.getComponent()); assert aPath.size() > 0: 502 "There should be at least a usage component"; for (int i = 0; i< aPath.size(); i++) { 504 Component comp = aPath.get(i); 505 helperNode = uiHelper.getDisplayNode(comp); 506 CheckNode node = null; 507 if (componentsInModel.containsKey(comp)) { 508 node = componentsInModel.get(comp); 509 } else { 510 if (comp == item.getComponent()) { 511 node = new CheckNode(item, helperNode); 512 } else { 513 node = new CheckNode(comp, helperNode); 514 } 515 componentsInModel.put(comp, node); 516 } 517 if (i==0){ 518 fileTreeNode.add(node); 520 } else { 521 CheckNode parentNode =null; 522 Component parent = aPath.get(i-1); 523 parentNode = componentsInModel.get(Component.class.cast(parent)); 524 assert parentNode != null:"The relevantPath is invalid"; 525 parentNode.add(node); 526 } 527 if (i == aPath.size()-1){ 528 String htmlDisplayName = helperNode.getHtmlDisplayName(); 530 String usageTreeNodeLabel = 531 MessageFormat.format( 532 NbBundle.getMessage( 533 WhereUsedReader.class, 534 "LBL_Usage_Node"), 535 new Object [] { 536 helperNode.getName(), 537 helperNode.getShortDescription(), htmlDisplayName==null?AnalysisConstants.EMPTY_STRING:htmlDisplayName 539 }); 540 node.setLabel(usageTreeNodeLabel); 541 } 542 } 543 } 544 } 545 546 private static String getCompTypeDisplayName(final Node pn) { 547 String compType = null; 548 if (pn.canGetBoolean(AnalysisConstants.IS_PRIMITIVE) && 549 pn.getBoolean(AnalysisConstants.IS_PRIMITIVE)){ 550 compType = NbBundle.getMessage(WhereUsedReader.class, 551 "LBL_Primitive_Type"); 552 } else { 553 compType = pn.getString(AnalysisConstants.COMPONENT_TYPE_NAME); 554 } 555 return compType; 556 } 557 558 564 private static void addApppropriateEdge(Graph graph, Node from, Node queryNode, 565 int fileGroupNumber, Usage.Type edgeType){ 566 Edge edge = graph.addEdge(from, queryNode); 567 edge.setInt(AnalysisConstants.FILE_GROUP, Integer.valueOf(fileGroupNumber)); 569 if ( edgeType == Usage.Type.GENERALIZATION){ 570 edge.setString(AnalysisConstants.EDGE_TYPE, AnalysisConstants.GENERALIZATION); 571 } else if ( edgeType == Usage.Type.REFERENCE){ 572 edge.setString(AnalysisConstants.EDGE_TYPE, 573 AnalysisConstants.REFERENCE); 574 from.setString(AnalysisConstants.LABEL, 575 MessageFormat.format(NbBundle.getMessage( 576 WhereUsedReader.class, 577 "LBL_References_Ref"), 578 new Object [] 579 {queryNode.getString(AnalysisConstants.LABEL)})); 580 } else { 581 edge.setString(AnalysisConstants.EDGE_TYPE, AnalysisConstants.COMPOSITION); 582 } 583 } 584 585 private static Node findDup(Graph graph, Component sc){ 586 Iterator it = graph.nodes(); 587 while (it.hasNext()){ 588 Node n= Node.class.cast(it.next()); 589 Object nodeSC = n.get(AnalysisConstants.XAM_COMPONENT); 590 if ((n.canGetBoolean(AnalysisConstants.IS_FILE_NODE) && 591 n.getBoolean(AnalysisConstants.IS_FILE_NODE) == false) 592 && nodeSC != null){ 593 if (nodeSC == sc){ 594 return n; 595 } 596 } 597 } 598 return null; 599 } 600 601 private static Node createQueryNode(Graph graph, 602 Referenceable query, 603 UIHelper uiHelper, 604 boolean isPrimitive, 605 List <Component> componentsInGraph, 606 boolean showOnlyDerivations, 607 int fileGroupNumber 608 ) { 609 Node queryNode = null; 611 Component queryComponent = null; 612 if (query instanceof Component) { 613 queryComponent = (Component) query; 614 } else { 615 if (query instanceof DocumentModel) { 616 queryComponent = ((DocumentModel) query).getRootComponent(); 617 } 618 } 619 if (uiHelper == null){ 620 String name = ""; if (query instanceof Named){ 622 name = ((Named)Named.class.cast(query)).getName(); 623 } 624 queryNode = graph.addNode(); 625 queryNode.setBoolean(AnalysisConstants.IS_PRIMITIVE, isPrimitive); 627 queryNode.setString(AnalysisConstants.LABEL, name); 628 queryNode.setString(AnalysisConstants.COMPONENT_TYPE_NAME, "" ); queryNode.set(AnalysisConstants.XAM_COMPONENT, query ); 630 queryNode.setInt(AnalysisConstants.FILE_GROUP, fileGroupNumber); 631 632 } else { 633 queryNode = createNode(graph, queryComponent, uiHelper, isPrimitive, componentsInGraph, query, fileGroupNumber); 634 } 635 636 queryNode.setBoolean(AnalysisConstants.IS_QUERY_NODE, true); 637 queryNode.setInt(AnalysisConstants.FILE_GROUP,-1); 639 queryNode.setBoolean(AnalysisConstants.IS_PRIMITIVE, isPrimitive); 641 642 AnalysisUtilities.ToolTipLine topLine = new AnalysisUtilities.ToolTipLine(( 643 showOnlyDerivations? 644 NbBundle.getMessage(WhereUsedReader.class, 645 "LBL_Base_Complex_Type"): 646 NbBundle.getMessage(WhereUsedReader.class, 647 "LBL_Query_Component")), 648 100, 649 Color.BLACK.getRGB(), 650 AnalysisUtilities.ToolTipLine.HorizontalAlignment.CENTER); 651 String compType = getCompTypeDisplayName(queryNode); 652 AnalysisUtilities.ToolTipLine typeLine = new AnalysisUtilities.ToolTipLine( 653 compType, 654 100, 655 Color.BLACK.getRGB(), 656 AnalysisUtilities.ToolTipLine.HorizontalAlignment.CENTER); 657 String toolTip = AnalysisUtilities.createHTMLToolTip(new AnalysisUtilities.ToolTipLine[] { 658 topLine, typeLine}); 659 660 queryNode.setString(AnalysisConstants.TOOLTIP, toolTip); 661 return queryNode; 662 } 663 664 665 private static Node createFileNode(Graph graph, Referenceable queryComp, 666 FileObject fobj, Node queryNode, int fileGroupNumber){ 667 if (queryComp == null || fobj == null){ 668 return null; 669 } 670 String fileType = AnalysisUtilities.getXmlFileType(fobj); 671 Node n = graph.addNode(); 672 n.setString(AnalysisConstants.FILE_TYPE,fileType); 673 n.setInt(AnalysisConstants.FILE_NODE_FILE_GROUP, fileGroupNumber); 674 n.setBoolean(AnalysisConstants.IS_EXPANDED, false); 675 n.setBoolean(AnalysisConstants.IS_FILE_NODE, true); 676 n.set(AnalysisConstants.JAVA_AWT_IMAGE, AnalysisUtilities.getImage(fobj)); 677 if (fobj != null){ 678 n.setString( 679 AnalysisConstants.LABEL, fobj.getName()+"."+fobj.getExt()); n.setString( 681 AnalysisConstants.XML_FILENAME, 682 fobj.getName()+"."+fobj.getExt()); n.set(AnalysisConstants.FILE_OBJECT, fobj); 684 685 AnalysisUtilities.ToolTipLine topLine = new AnalysisUtilities.ToolTipLine( 687 MessageFormat.format( 688 NbBundle.getMessage(WhereUsedReader.class, 689 "LBL_Xml_File_With_Usages"),new Object []{ 690 AnalysisUtilities.getXmlFileTypeDisplayName(fileType), 691 queryNode.getString(AnalysisConstants.LABEL)}), 692 100, 693 Color.BLACK.getRGB(), 694 AnalysisUtilities.ToolTipLine.HorizontalAlignment.CENTER); 695 AnalysisUtilities.ToolTipLine typeLine = 696 new AnalysisUtilities.ToolTipLine( 697 FileUtil.getFileDisplayName(fobj), 698 100, 699 Color.BLACK.getRGB(), 700 AnalysisUtilities.ToolTipLine.HorizontalAlignment.CENTER); 701 String toolTip = AnalysisUtilities.createHTMLToolTip( 702 new AnalysisUtilities.ToolTipLine[] {topLine, typeLine}); 703 704 n.setString(AnalysisConstants.TOOLTIP, 705 toolTip); 706 } 707 return n; 708 } 709 713 private static Node createNode(Graph graph, 714 Component comp, 715 UIHelper uiHelper, 716 boolean isPrimitive, 717 List <Component> componentsInGraph, 718 Referenceable queryComponent, 719 int fileGroupNumber 720 ){ 721 Node n = graph.addNode(); 722 if (componentsInGraph != null){ 723 componentsInGraph.add(comp); 724 } 725 org.openide.nodes.Node displayNode = uiHelper.getDisplayNode(comp); 727 728 if (isPrimitive){ 732 if (comp.getModel() == 733 ((Component) queryComponent).getModel()) { 734 n.setBoolean(AnalysisConstants.IS_PRIMITIVE, true); 735 } 736 } 737 n.setBoolean(AnalysisConstants.IS_PRIMITIVE, false); 738 n.setString(AnalysisConstants.LABEL, displayNode.getName()); 739 n.setString(AnalysisConstants.COMPONENT_TYPE_NAME, displayNode.getShortDescription() ); 740 n.set(AnalysisConstants.XAM_COMPONENT, comp ); 741 n.set(AnalysisConstants.OPENIDE_NODE, displayNode ); 742 n.setInt(AnalysisConstants.FILE_GROUP, fileGroupNumber); 743 n.set(AnalysisConstants.JAVA_AWT_IMAGE, displayNode.getIcon(BeanInfo.ICON_COLOR_16x16)); 744 745 return n; 746 } 747 748 749 755 public static Object [] createDeletePreviewModels(NamedReferenceable target){ 756 Object [] models = null; 757 Graph graph = new Graph(true); 759 graph.getNodeTable().addColumns(WhereUsedReader.FIND_USAGES_NODES_SCHEMA); 760 graph.getEdgeTable().addColumns(WhereUsedReader.FIND_USAGES_EDGES_SCHEMA); 761 createQueryNode( 762 graph, 763 target, 764 getUIHelper(target), 765 false, null, false, 0); 770 CheckNode root = new CheckNode(target.getName(),FIND_USAGES_ICON); 771 772 models = new Object [] {graph, new DefaultTreeModel (root)}; 773 insertTargetComponent(target, models, NbBundle.getMessage(WhereUsedReader.class, "LBL_Delete")); 774 return models; 775 } 776 777 784 public static Object [] insertTargetComponent(Referenceable ref, Object [] models, String nodeLabel){ 785 assert (ref instanceof Named) || (ref instanceof Model ) : 786 "Target is not NamedComponent or DocumentModel"; 787 DefaultTreeModel treeModel = null; 789 for (Object obj:models){ 790 if (obj instanceof DefaultTreeModel ){ 791 treeModel = DefaultTreeModel .class.cast(obj); 792 } 793 } 794 assert treeModel != null:"There is no tree model"; 795 796 CheckNode targetNode = AnalysisUtilities.findCheckNode(treeModel, ref); 797 if (targetNode == null){ 798 Component targetComponent = ref instanceof Component ? (Component) ref : 799 ref instanceof DocumentModel ? ((DocumentModel) ref).getRootComponent() :null; 800 assert targetComponent != null : "target is not Component or DocumentModel"; 801 FileObject targetFobj = AnalysisUtilities.getFileObject(targetComponent); 802 CheckNode targetFileNode = createFileTreeNode(targetFobj, treeModel); 803 804 UIHelper helper = RefactoringManager.getInstance().getTargetComponentUIHelper(ref); 805 if (helper == null){ 806 helper = new UIHelper(); 807 } 808 org.openide.nodes.Node displayNode = AnalysisUtilities.getDisplayNode(ref); 809 String name = AnalysisUtilities.getName(ref); 810 targetNode = new CheckNode(ref, displayNode); 811 targetNode.setLabel(nodeLabel + " <b>" + name + "</b>"); 812 targetFileNode.add(targetNode); 813 } 814 targetNode.setCheckEnabled(false); 815 816 return models; 817 } 818 819 private static CheckNode createFileTreeNode(FileObject targetFobj, DefaultTreeModel treeModel) { 820 CheckNode root = (CheckNode)treeModel.getRoot(); 821 assert root != null:"There is no root in the tree model"; 822 CheckNode targetFileNode = null; 823 DataObject dobj = null; 824 try { 825 dobj = DataObject.find(targetFobj); 826 } catch (DataObjectNotFoundException ex) { 827 } 828 assert dobj != null:"No data object found for "+targetFobj; 829 Project project = FileOwnerQuery.getOwner(targetFobj); 830 831 if (project == null) { 832 CheckNode ret = new CheckNode(dobj.getPrimaryFile(), dobj.getNodeDelegate()); 833 root.add(ret); 834 return ret; 835 } 836 837 LogicalViewProvider viewProvider = (LogicalViewProvider)project. 838 getLookup().lookup(LogicalViewProvider.class); 839 840 CheckNode projectNode = AnalysisUtilities.findCheckNode(treeModel,project); 841 if(projectNode==null) { 842 projectNode = new CheckNode(project,viewProvider.createLogicalView()); 843 root.add(projectNode); 844 } 845 846 org.openide.nodes.Node projNode = projectNode.getOrigNode(); 847 org.openide.nodes.Node logicalNode = viewProvider.findPath(projNode, targetFobj); 848 if(logicalNode==null) { 849 logicalNode = viewProvider.findPath(projNode, dobj); 850 } 851 if(logicalNode!=null) { 852 List <org.openide.nodes.Node> path = 853 new ArrayList <org.openide.nodes.Node>(); 854 org.openide.nodes.Node parent = logicalNode; 855 path.add(logicalNode); 856 while((parent=parent.getParentNode())!=null) 857 path.add(0,parent); 858 assert path.remove(0) == projNode:"given fobj must be under project"; 859 CheckNode parentNode = projectNode; 860 for(org.openide.nodes.Node node:path) { 861 DataObject o = (DataObject) node.getLookup().lookup(DataObject.class); 862 if (o == null) continue; 863 targetFileNode = AnalysisUtilities.findCheckNode(treeModel, o.getPrimaryFile()); 864 if(targetFileNode==null) { 865 targetFileNode = new CheckNode(o.getPrimaryFile(), node); 866 parentNode.add(targetFileNode); 867 } 868 parentNode = targetFileNode; 869 } 870 } else { 871 targetFileNode = new CheckNode(dobj.getPrimaryFile(), dobj.getNodeDelegate()); 872 projectNode.add(targetFileNode); 873 } 874 return targetFileNode; 875 } 876 877 } 878 | Popular Tags |