KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > refactoring > FindUsageResult


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 package org.netbeans.modules.xml.refactoring;
20
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.concurrent.ExecutionException JavaDoc;
28 import java.util.concurrent.Future JavaDoc;
29 import java.util.concurrent.TimeUnit JavaDoc;
30 import java.util.concurrent.TimeoutException JavaDoc;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.api.project.SourceGroup;
33 import org.netbeans.modules.xml.refactoring.impl.RefactoringUtil;
34 import org.netbeans.modules.xml.refactoring.spi.RefactoringEngine;
35 import org.netbeans.modules.xml.xam.Component;
36 import org.netbeans.modules.xml.xam.Model;
37 import org.netbeans.modules.xml.xam.Referenceable;
38 import org.openide.ErrorManager;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileUtil;
41 import org.openide.loaders.DataObject;
42 import org.openide.util.Cancellable;
43
44 /**
45  *
46  * @author Nam Nguyen
47  */

48 public class FindUsageResult implements Cancellable, Future JavaDoc<UsageSet> {
49     private List JavaDoc<SourceGroup> sourceGroups;
50     private Referenceable target;
51     private Set JavaDoc<Component> searchRoots;
52     private UsageSet usageSet;
53     private boolean done;
54     private boolean cancelled;
55     
56     /** Creates a new instance of RefactorResult */
57     public FindUsageResult(Referenceable target) {
58         this.target = target;
59         cancelled = false;
60     }
61     
62     public FindUsageResult(Referenceable target, Component searchRoot) {
63         this(target, Collections.singleton(searchRoot));
64     }
65     
66     public FindUsageResult(Referenceable target, Set JavaDoc<Component> searchRoots) {
67         this(target);
68         this.searchRoots = searchRoots;
69     }
70     
71     public FindUsageResult(Referenceable target, UsageSet usages) {
72         this(target);
73         this.usageSet = usages;
74         done = true;
75     }
76     
77     public UsageSet get(long timeout, TimeUnit JavaDoc unit) throws InterruptedException JavaDoc,
78         ExecutionException JavaDoc, TimeoutException JavaDoc {
79         throw new UnsupportedOperationException JavaDoc("Timeout not supported."); //NOI18N
80
}
81     
82     public boolean cancel(boolean mayInterruptIfRunning) {
83         if (done || cancelled) {
84             return false;
85         }
86         //TODO something with mayInterruptIfRunning
87
setCancelled(true);
88         return true;
89     }
90     
91     void setDone(boolean v) {
92         done = v;
93     }
94     
95     public boolean isDone() {
96         return done;
97     }
98     
99     void setCancelled(boolean v) {
100         cancelled = v;
101     }
102     
103     public boolean isCancelled() {
104         return cancelled;
105     }
106     
107     public UsageSet get() throws InterruptedException JavaDoc, ExecutionException JavaDoc {
108         if (usageSet == null) {
109             usageSet = new UsageSet(target);
110             doSearch(getSearchRoots());
111             done = true;
112         }
113         return usageSet;
114     }
115     
116     protected void doSearch(Set JavaDoc<Component> searchRoots) {
117         for (RefactoringEngine engine : RefactoringManager.getInstance().getEngines()) {
118             for (Component root : searchRoots) {
119                 if (isCancelled()) return;
120                 List JavaDoc<UsageGroup> founds = findUsages(engine, target, root);
121                 if (founds != null) {
122                     for (UsageGroup ug : founds) {
123                         usageSet.addUsage(ug);
124                         ug.setSourceGroup(getSourceGroup(root));
125                     }
126                 }
127             }
128         }
129     }
130     
131     private List JavaDoc<UsageGroup> findUsages(RefactoringEngine engine, Referenceable target, Component searchRoot) {
132         if (target instanceof Model) {
133             return engine.findUsages((Model) target, searchRoot);
134         } else if (target instanceof Component) {
135             return engine.findUsages((Component) target, searchRoot);
136         } else {
137             return null;
138         }
139     }
140     
141     public SourceGroup getSourceGroup(Component component) {
142         if (component == null) return null;
143         FileObject fobj = (FileObject)
144             component.getModel().getModelSource().getLookup().lookup(FileObject.class);
145         if (fobj == null) return null;
146         for (SourceGroup sg : getSourceGroups()) {
147             FileObject rootFolder = sg.getRootFolder();
148             if (fobj != rootFolder && !FileUtil.isParentOf(rootFolder, fobj)) {
149                 continue;
150             }
151             if (sg.contains(fobj)) {
152                 return sg;
153             }
154         }
155         return null;
156     }
157     
158     private List JavaDoc<SourceGroup> getSourceGroups() {
159         if (sourceGroups == null) {
160             sourceGroups = new ArrayList JavaDoc<SourceGroup>();
161             Project project = RefactoringUtil.findCurrentProject(target);
162             if (project != null) {
163                 sourceGroups.addAll(RefactoringUtil.findSourceRoots(project));
164                 Set JavaDoc<Project> referencings = RefactoringUtil.getReferencingProjects(project);
165                 for (Project p : referencings) {
166                     sourceGroups.addAll(RefactoringUtil.findSourceRoots(p));
167                 }
168             }
169         }
170         return sourceGroups;
171     }
172     
173     protected Set JavaDoc<FileObject> getSearchFiles() {
174         HashSet JavaDoc<FileObject> files = new HashSet JavaDoc<FileObject>();
175         for (SourceGroup sourceGroup : getSourceGroups()) {
176             files.addAll(RefactoringUtil.findSourceFiles(sourceGroup.getRootFolder()));
177         }
178         // make sure target source is also included in search in case outside projects
179
DataObject dobj = RefactoringUtil.getDataObject(RefactorRequest.getModel(target));
180         if (dobj != null) {
181             files.add(dobj.getPrimaryFile());
182         }
183         return files;
184     }
185     
186     protected Set JavaDoc<Component> getSearchRoots() {
187         if (searchRoots != null) {
188             return searchRoots;
189         }
190         searchRoots = new HashSet JavaDoc<Component>();
191         for (FileObject file : getSearchFiles()) {
192             for (RefactoringEngine engine : RefactoringManager.getInstance().getEngines()) {
193                 try {
194                     if (isCancelled()) {
195                         searchRoots.clear();
196                         break;
197                     }
198                     Component root = engine.getSearchRoot(file);
199                     searchRoots.add(root);
200                 } catch (IOException JavaDoc ex) {
201                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage());
202                 }
203             }
204         }
205         return searchRoots;
206     }
207     
208     public boolean cancel() {
209         return cancel(true);
210     }
211 }
212
Popular Tags