KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > refactoring > ui > readers > WhereUsedReader


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * WhereUsedReader.java
22  *
23  * Created on October 26, 2005, 11:27 AM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.refactoring.ui.readers;
30
31 import java.awt.Color JavaDoc;
32 import java.awt.Image JavaDoc;
33 import java.beans.BeanInfo JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.concurrent.ExecutionException JavaDoc;
36 import javax.swing.ImageIcon JavaDoc;
37 import javax.swing.tree.DefaultTreeModel JavaDoc;
38 import java.text.MessageFormat JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Collection JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Map JavaDoc;
44 import java.util.Map.Entry;
45 import java.util.Set JavaDoc;
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 /**
83  *
84  * @author Jeri Lockhart
85  *
86  * Reader for Where Used query and Complex Type Derivations query
87  */

88 public class WhereUsedReader {
89     
90     private static final ImageIcon JavaDoc FIND_USAGES_ICON = new ImageIcon JavaDoc(
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(); // prefuse graph schema
97
static {
98         FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.LABEL,
99                 String JavaDoc.class, AnalysisConstants.EMPTY_STRING);
100         FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.ID,
101                 String JavaDoc.class, AnalysisConstants.EMPTY_STRING);
102         FIND_USAGES_NODES_SCHEMA.addColumn(
103                 AnalysisConstants.COMPONENT_TYPE_NAME,
104                 String JavaDoc.class, AnalysisConstants.EMPTY_STRING);
105         FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.ELEMENT_TYPE,
106                 String JavaDoc.class, AnalysisConstants.EMPTY_STRING);
107         // type of a GE, LE, GA, or LA
108

109         FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.XML_FILENAME,
110                 String JavaDoc.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 JavaDoc.class, AnalysisConstants.EMPTY_STRING);
115         FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.TOOLTIP,
116                 String JavaDoc.class, AnalysisConstants.EMPTY_STRING); // name
117
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 JavaDoc.class);
123         
124         // used to set node visible or not visible, depending on whether the
125
// file node is expanded or collapsed
126
// -1 = always visible, any other value matches a
127
// FILE_NODE_FILE_GROUP
128
// the file node itself has a FILE_GROUP of -1 because it is always
129
// visible
130
FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.FILE_GROUP,
131                 int.class, -1);
132         // a File Node has a positive value for this column, other nodes
133
// have a -1 value
134
FIND_USAGES_NODES_SCHEMA.addColumn(
135                 AnalysisConstants.FILE_NODE_FILE_GROUP, int.class, -1);
136         // assigned number of schema file group
137

138         FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.IS_PRIMITIVE,
139                 boolean.class, false); // is builtin type
140
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(); // prefuse graph schema
156
static {
157         FIND_USAGES_EDGES_SCHEMA.addColumn(AnalysisConstants.LABEL,
158                 String JavaDoc.class, AnalysisConstants.EMPTY_STRING);
159         FIND_USAGES_EDGES_SCHEMA.addColumn(AnalysisConstants.EDGE_TYPE,
160                 String JavaDoc.class, AnalysisConstants.EMPTY_STRING);
161         // "file-edge-type" "generalization", "reference" "composition"
162

163         FIND_USAGES_EDGES_SCHEMA.addColumn(AnalysisConstants.TOOLTIP,
164                 String JavaDoc.class, AnalysisConstants.EMPTY_STRING);
165         // used to set edge visible or not visible, depending on whether the
166
// file node is expanded or collapsed
167
// -1 = always visible, any other value matches a
168
// FILE_NODE_FILE_GROUP
169
// the file node itself has a FILE_GROUP of -1 because it is
170
// always visible
171
FIND_USAGES_EDGES_SCHEMA.addColumn(AnalysisConstants.FILE_GROUP,
172                 int.class, -1);
173     }
174     
175     /**
176      * graph for WhereUsedView
177      * @param buildTreeModel true, if the logical DefaultTreeModel
178      * for the Find Usages output window is to be built
179      * at the same time that the graph is being created
180      *
181      * Logical View of Explorer Tree
182      * Root Node "Usages of Xyz [3 occurrences]"
183      * Project Node
184      * Source Root Node
185      * Folder Node
186      * Schema File Node
187      * Usage Node
188      *
189      *
190      * @param queryComponent the Component that is the query target
191      * @param buildTreeModel build a CheckNode tree model for the explorer view
192      * @param isPrimitive is the query component a primitive type
193      * @param model the Model of this Multiview's schema file
194      *
195      */

196     public Object JavaDoc[] loadGraph(FindUsageResult fuResult,
197             Referenceable queryComponent,
198             boolean buildTreeModel,
199             boolean isPrimitive,
200             Model JavaDoc model)
201     {
202         if (fuResult == null) {
203             return new Object JavaDoc[0];
204         }
205         
206         List JavaDoc<Component> componentsInGraph = new ArrayList JavaDoc<Component>();
207         int fileGroupNumber = 0;
208         int usagesCount = 0;
209 // System.out.println("loadGraph start " + start);// Get current time
210

211         Graph graph = new Graph(true); // isDirected
212
graph.getNodeTable().addColumns(FIND_USAGES_NODES_SCHEMA);
213         graph.getEdgeTable().addColumns(FIND_USAGES_EDGES_SCHEMA);
214         
215         
216         // Get collection of nodes where the global is used
217
// FindUsageVisitor usage = new FindUsageVisitor();
218
// Preview preview = null;
219

220         // **********************************************
221
// IF BUILD TREE MODEL - START
222
// **********************************************
223
DefaultTreeModel JavaDoc treeModel = null;
224         if (buildTreeModel){
225             treeModel = new DefaultTreeModel JavaDoc(
226                 new CheckNode(AnalysisUtilities.getName(queryComponent), FIND_USAGES_ICON));
227         } // end if buildTreeModel
228
// **********************************************
229
// IF BUILD TREE MODEL - END
230
// **********************************************
231

232         
233         // get usages for the current schema file
234
// get project from SC
235
// Get source roots in project
236
// for each treeModelRoot, get schema model
237
// find usages in each model
238
// - create file node
239
// - hide nodes from other files
240

241         UsageSet usageSet = null;
242         try {
243             usageSet = fuResult.get();
244         } catch (InterruptedException JavaDoc ex) {
245             ErrorManager.getDefault().notify(ex);
246         } catch (ExecutionException JavaDoc 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 of SourceGroups and their packages
259
Map JavaDoc<SourceGroup, Map JavaDoc<FileObject,Set JavaDoc<UsageGroup>>> sortedUses =
260                 usageSet.getSortedUsages();
261         Set JavaDoc<Entry<SourceGroup,Map JavaDoc<FileObject,Set JavaDoc<UsageGroup>>>> sgUses =
262                 sortedUses.entrySet();
263         LogicalViewProvider viewProvider = null;
264         Project project = null;
265         CheckNode projectNode = null;
266         for (Entry<SourceGroup, Map JavaDoc<FileObject, Set JavaDoc<UsageGroup>>> sgUse:sgUses){
267             Set JavaDoc<Entry<FileObject,Set JavaDoc<UsageGroup>>> pkgUses = sgUse.getValue().entrySet();
268             for (Entry<FileObject, Set JavaDoc<UsageGroup>> pkgUse:pkgUses){
269                 Set JavaDoc<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 JavaDoc[] {
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 JavaDoc[] {Integer.valueOf(usagesCount),
311                     queryNode.getString(AnalysisConstants.LABEL)
312             }
313             ));
314         }
315         
316 // GraphUtilities.dumpGraph(graph);
317

318         return new Object JavaDoc[] {graph, treeModel};
319         
320     }
321
322     private static UIHelper getUIHelper(Referenceable component) {
323         return RefactoringManager.getInstance().getTargetComponentUIHelper(component);
324     }
325     
326     private static String JavaDoc createRootTreeNodeLabel(String JavaDoc name, int usagesCount){
327         
328         String JavaDoc description = MessageFormat.format(NbBundle.getMessage(
329                 WhereUsedReader.class,
330                 "DSC_WhereUsed"),
331                 new Object JavaDoc[] {name} );
332         
333         StringBuilder JavaDoc desc = new StringBuilder JavaDoc(" [" + usagesCount); // NOI18N
334
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     /**
345      *
346      * Get usages in one schema file
347      * @param usage collection of Items found in one model
348      * @param graph the prefuse Graph
349      * @param isPrimitive is the query component a primitive type
350      * @param componentsInGraph the list of SchemaComponents that are already in the graph
351      * @param queryComponent the query component
352      * @param queryNode prefuse node representing queryComponent
353      * @param fileGroupNumber number of fileGrp
354      *
355      */

356     private void addUsagesToGraph(
357             UsageGroup usage,
358             Graph graph,
359             boolean isPrimitive,
360             List JavaDoc<Component> componentsInGraph,
361             Referenceable queryComponent,
362             Node queryNode,
363             int fileGroupNumber) {
364 // Map<Component, List<Component>> um =
365
Collection JavaDoc<Usage> items = usage.getItems();
366         UIHelper uiHelper = usage.getEngine().getUIHelper();
367         
368         // *****************************
369
// *** FILE NODE
370
// *****************************
371
// create file node and attach it to the query node
372
Node fileNode = createFileNode(graph,
373                 queryComponent,
374                 (FileObject)usage.getModel().getModelSource().getLookup().lookup(FileObject.class),
375                 queryNode,
376                 fileGroupNumber);
377         
378         // create a special edge from the file node to the query node
379
// that will be visible when the file node is collapsed
380
// This edge uses the default prefuse renderer, e.g.,
381
// a small solid arrow head
382
// When the file node is expanded, this edge will be
383
// hidden. See FindUsagesFocusControl (double click)
384
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 JavaDoc<Component> aPath = uiHelper.getRelevantPathFromRoot(item.getComponent());
393             Node parent = fileNode;
394             // if queryComponent is primitive skip the top level (schema)element
395
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                     // there's already a Node for this Component,
402
// find it in Graph
403
pn = findDup(graph, sc);
404                     assert pn != null:"Cannot find Node for Component "
405                             +
406                             sc; //NOI18N
407
}
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                  // Usage node is last
421
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 JavaDoc[] {queryNode.getString(
428                                     AnalysisConstants.LABEL) })),
429                             100,
430                             Color.BLACK.getRGB(),
431                             AnalysisUtilities.ToolTipLine.
432                             HorizontalAlignment.CENTER);
433                     // Connect this usage node to the Query Node
434
// with the appropriate edge (composition or reference)
435
addApppropriateEdge(graph,
436                             pn,
437                             queryNode,
438                             fileGroupNumber,
439                             item.getType());
440                     
441                 }// END if (i == aPath.size()-1)
442

443                AnalysisUtilities.ToolTipLine typeLine =
444                         new AnalysisUtilities.ToolTipLine(getCompTypeDisplayName(pn),
445                         100,
446                         Color.BLACK.getRGB(),
447                         AnalysisUtilities.ToolTipLine.
448                         HorizontalAlignment.CENTER);
449                 String JavaDoc toolTip = AnalysisUtilities.createHTMLToolTip(
450                         new AnalysisUtilities.ToolTipLine[] {topLine, typeLine});
451                 pn.setString(AnalysisConstants.TOOLTIP, toolTip);
452                 
453                 
454                 // connect it to its parent
455
addApppropriateEdge(graph,
456                         pn,
457                         parent,
458                         fileGroupNumber,
459                         null);
460                 parent = pn;
461                 
462                 
463             }
464         }
465         
466     }
467     
468     /**
469      *
470      * Get usages in one schema file
471      * Project
472      * Source Group
473      * Folder
474      * File1
475      * ParentComp1
476      * Usage1
477      * Usage2
478      * ParentComp2
479      * ParentComp3
480      * Usage3
481      * Usage4
482      * SubFolder
483      * File3
484      * Usage6
485      * File2
486      * ParentComp4
487      * Usage5
488      *
489      */

490     private void addUsagesToTreeModel(UsageGroup usageGroup, DefaultTreeModel JavaDoc treeModel) {
491         assert usageGroup != null:"usageGroup cannot be null";
492         CheckNode fileTreeNode = createFileTreeNode(usageGroup.getFileObject(),treeModel);
493         Map JavaDoc<Component,CheckNode> componentsInModel =
494                 new HashMap JavaDoc<Component,CheckNode>();
495         
496         UIHelper uiHelper = usageGroup.getEngine().getUIHelper();
497         org.openide.nodes.Node helperNode = null;
498         Collection JavaDoc<Usage> items = usageGroup.getItems();
499         for (Usage item:items){
500             List JavaDoc<Component> aPath = uiHelper.getRelevantPathFromRoot(item.getComponent());// item.getPathFromRoot();
501
assert aPath.size() > 0:
502                 "There should be at least a usage component";//NOI18N
503
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                     // the first Component in the path is a child of the file node
519
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                     // this is the usage node
529
String JavaDoc htmlDisplayName = helperNode.getHtmlDisplayName();
530                     String JavaDoc usageTreeNodeLabel =
531                             MessageFormat.format(
532                             NbBundle.getMessage(
533                             WhereUsedReader.class,
534                             "LBL_Usage_Node"),
535                             new Object JavaDoc[] {
536                         helperNode.getName(),
537                         helperNode.getShortDescription(), // comp type
538
htmlDisplayName==null?AnalysisConstants.EMPTY_STRING:htmlDisplayName
539                     });
540                     node.setLabel(usageTreeNodeLabel);
541                 }
542             }
543         }
544     }
545     
546     private static String JavaDoc getCompTypeDisplayName(final Node pn) {
547         String JavaDoc 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     /**
559      * Adds a Reference edge or Generalization edge from
560      * "from" node to the queryNode
561      * or adds composition edge if edgeType is null
562      *
563      */

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.setBoolean(AnalysisConstants.SHOW, isVisible);
568
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 JavaDoc[]
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 JavaDoc it = graph.nodes();
587         while (it.hasNext()){
588             Node n= Node.class.cast(it.next());
589             Object JavaDoc 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 JavaDoc<Component> componentsInGraph,
606             boolean showOnlyDerivations,
607             int fileGroupNumber
608             ) {
609         // TODO remove this temporary hack when UIHelper for query Component is available
610
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 JavaDoc name = ""; //NOI18N
621
if (query instanceof Named){
622                 name = ((Named)Named.class.cast(query)).getName();
623             }
624             queryNode = graph.addNode();
625             //componentsInGraph.add(queryComponent);
626
queryNode.setBoolean(AnalysisConstants.IS_PRIMITIVE, isPrimitive);
627             queryNode.setString(AnalysisConstants.LABEL, name);
628             queryNode.setString(AnalysisConstants.COMPONENT_TYPE_NAME, "" ); //NOI18N
629
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         // unset the FILE_GROUP because this node is always visible
638
queryNode.setInt(AnalysisConstants.FILE_GROUP,-1);
639         // reset IS_PRIMITIVE in case it is
640
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 JavaDoc 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 JavaDoc 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 JavaDoc 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()); // NOI18N
680
n.setString(
681                     AnalysisConstants.XML_FILENAME,
682                     fobj.getName()+"."+fobj.getExt()); // NOI18N
683
n.set(AnalysisConstants.FILE_OBJECT, fobj);
684             
685             // "Schema file containing usages of XYZ"
686
AnalysisUtilities.ToolTipLine topLine = new AnalysisUtilities.ToolTipLine(
687                     MessageFormat.format(
688                     NbBundle.getMessage(WhereUsedReader.class,
689                     "LBL_Xml_File_With_Usages"),new Object JavaDoc[]{
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 JavaDoc toolTip = AnalysisUtilities.createHTMLToolTip(
702                     new AnalysisUtilities.ToolTipLine[] {topLine, typeLine});
703             
704             n.setString(AnalysisConstants.TOOLTIP,
705                     toolTip);
706         }
707         return n;
708     }
709     /**
710      *
711      *
712      */

713     private static Node createNode(Graph graph,
714             Component comp,
715             UIHelper uiHelper,
716             boolean isPrimitive,
717             List JavaDoc<Component> componentsInGraph,
718             Referenceable queryComponent,
719             int fileGroupNumber
720             ){
721         Node n = graph.addNode();
722         if (componentsInGraph != null){
723             componentsInGraph.add(comp);
724         }
725 // DisplayInfo dInfo = div.getDisplayInfo(comp);
726
org.openide.nodes.Node displayNode = uiHelper.getDisplayNode(comp);
727         
728         // if the queryComponent node is a primitive,
729
// check if the usage node is also a primitive, i.e.,
730
// from the same model (the W3c Schema model)
731
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     /**
750      * Create a tree model and a graph that contain only the
751      * target Component
752      *
753      *
754      */

755     public static Object JavaDoc[] createDeletePreviewModels(NamedReferenceable target){
756         Object JavaDoc[] models = null;
757         // create prefuse graph with target as only node
758
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, // is primitive
766
null, // components in graph - empty
767
false, // show only derivations
768
0); // file group number
769

770         CheckNode root = new CheckNode(target.getName(),FIND_USAGES_ICON);
771         
772         models = new Object JavaDoc[] {graph, new DefaultTreeModel JavaDoc(root)};
773         insertTargetComponent(target, models, NbBundle.getMessage(WhereUsedReader.class, "LBL_Delete"));
774         return models;
775     }
776     
777     /**
778      * Insert nodes for the target component into the usages Tree Model
779      * @param ref the target to insert
780      * @param models an array containing the prefuse graph and a DefaultTreeModel
781      * @param nodeLabel "Delete" or "Rename"
782      *
783      */

784     public static Object JavaDoc[] insertTargetComponent(Referenceable ref, Object JavaDoc[] models, String JavaDoc nodeLabel){
785         assert (ref instanceof Named) || (ref instanceof Model JavaDoc) :
786             "Target is not NamedComponent or DocumentModel";
787         // don't do anything with the graph
788
DefaultTreeModel JavaDoc treeModel = null;
789         for (Object JavaDoc obj:models){
790             if (obj instanceof DefaultTreeModel JavaDoc){
791                 treeModel = DefaultTreeModel JavaDoc.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 JavaDoc 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 JavaDoc 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 JavaDoc<org.openide.nodes.Node> path =
853                     new ArrayList JavaDoc<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