1 19 package org.netbeans.modules.xml.refactoring; 20 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.HashSet ; 25 import java.util.List ; 26 import java.util.Set ; 27 import java.util.concurrent.ExecutionException ; 28 import java.util.concurrent.Future ; 29 import java.util.concurrent.TimeUnit ; 30 import java.util.concurrent.TimeoutException ; 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 48 public class FindUsageResult implements Cancellable, Future <UsageSet> { 49 private List <SourceGroup> sourceGroups; 50 private Referenceable target; 51 private Set <Component> searchRoots; 52 private UsageSet usageSet; 53 private boolean done; 54 private boolean cancelled; 55 56 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 <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 unit) throws InterruptedException , 78 ExecutionException , TimeoutException { 79 throw new UnsupportedOperationException ("Timeout not supported."); } 81 82 public boolean cancel(boolean mayInterruptIfRunning) { 83 if (done || cancelled) { 84 return false; 85 } 86 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 , ExecutionException { 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 <Component> searchRoots) { 117 for (RefactoringEngine engine : RefactoringManager.getInstance().getEngines()) { 118 for (Component root : searchRoots) { 119 if (isCancelled()) return; 120 List <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 <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 <SourceGroup> getSourceGroups() { 159 if (sourceGroups == null) { 160 sourceGroups = new ArrayList <SourceGroup>(); 161 Project project = RefactoringUtil.findCurrentProject(target); 162 if (project != null) { 163 sourceGroups.addAll(RefactoringUtil.findSourceRoots(project)); 164 Set <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 <FileObject> getSearchFiles() { 174 HashSet <FileObject> files = new HashSet <FileObject>(); 175 for (SourceGroup sourceGroup : getSourceGroups()) { 176 files.addAll(RefactoringUtil.findSourceFiles(sourceGroup.getRootFolder())); 177 } 178 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 <Component> getSearchRoots() { 187 if (searchRoots != null) { 188 return searchRoots; 189 } 190 searchRoots = new HashSet <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 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 |