1 19 package org.netbeans.modules.refactoring.api; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import org.netbeans.api.queries.SharabilityQuery; 27 import org.netbeans.modules.refactoring.api.impl.APIAccessor; 28 import org.netbeans.modules.refactoring.api.impl.ProgressSupport; 29 import org.netbeans.modules.refactoring.api.impl.SPIAccessor; 30 import org.netbeans.modules.refactoring.spi.GuardedBlockHandler; 31 import org.netbeans.modules.refactoring.spi.GuardedBlockHandlerFactory; 32 import org.netbeans.modules.refactoring.spi.ProgressProvider; 33 import org.netbeans.modules.refactoring.spi.ReadOnlyFilesHandler; 34 import org.netbeans.modules.refactoring.spi.RefactoringPlugin; 35 import org.netbeans.modules.refactoring.spi.RefactoringPluginFactory; 36 import org.openide.ErrorManager; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.loaders.DataObject; 40 import org.openide.loaders.DataObjectNotFoundException; 41 import org.openide.util.Lookup; 42 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 43 import org.openide.util.lookup.InstanceContent; 44 45 46 60 public abstract class AbstractRefactoring { 61 62 static { 63 APIAccessor.DEFAULT = new AccessorImpl(); 64 } 65 66 69 public static final int INIT = 0; 70 71 public static final int PRE_CHECK = 1; 72 73 public static final int PARAMETERS_CHECK = 2; 74 75 public static final int PREPARE = 3; 76 77 private int currentState = INIT; 78 79 private static final int PLUGIN_STEPS = 30; 80 81 private ArrayList plugins; 82 83 private ArrayList pluginsWithProgress; 84 85 private ArrayList gbHandlers; 86 87 private ProgressListener progressListener = new ProgressL(); 88 89 private ProgressSupport progressSupport; 90 AbstractRefactoring caller; 91 92 Lookup refactoringSource; 93 94 protected AbstractRefactoring(Lookup refactoringSource) { 95 this.refactoringSource = refactoringSource; 96 } 97 98 private Collection getPlugins() { 99 if (plugins == null) { 100 plugins = new ArrayList (); 101 Lookup.Result result = Lookup.getDefault().lookup(new Lookup.Template(RefactoringPluginFactory.class)); 103 for (Iterator it = result.allInstances().iterator(); it.hasNext();) { 104 RefactoringPluginFactory factory = (RefactoringPluginFactory) it.next(); 105 RefactoringPlugin plugin = factory.createInstance(this); 106 if (plugin != null) { 107 if (caller == null || factory.getClass().getClassLoader().equals(getClass().getClassLoader()) || factory.createInstance(caller)==null) { 108 plugins.add(plugin); 119 } 120 } 121 } 122 } 123 return plugins; 124 } 125 126 Collection getGBHandlers() { 127 if (gbHandlers == null) { 128 gbHandlers = new ArrayList (); 129 Lookup.Result result = Lookup.getDefault().lookup(new Lookup.Template(GuardedBlockHandlerFactory.class)); 131 for (Iterator it = result.allInstances().iterator(); it.hasNext();) { 132 GuardedBlockHandler handler = ((GuardedBlockHandlerFactory) it.next()).createInstance(this); 133 if (handler != null) gbHandlers.add(handler); 134 } 135 } 136 return gbHandlers; 137 } 138 139 144 public final Problem preCheck() { 145 currentState = PRE_CHECK; 154 return pluginsPreCheck(null); 155 } 156 157 165 public final Problem prepare(RefactoringSession session) { 166 Problem p = null; 167 if (currentState < PARAMETERS_CHECK) { 168 p = checkParameters(); 169 } 170 if (p != null && p.isFatal()) 171 return p; 172 173 p = pluginsPrepare(p, session); 174 175 return p; 176 } 177 178 182 public final Problem checkParameters() { 183 Problem p = fastCheckParameters(); 194 if (p != null && p.isFatal()) 195 return p; 196 currentState = PARAMETERS_CHECK; 197 return pluginsCheckParams(p); 198 } 199 200 205 public final Problem fastCheckParameters() { 206 Problem p = null; 210 if (currentState < PRE_CHECK) { 211 p = preCheck(); 212 } 213 if (p != null && p.isFatal()) 214 return p; 215 return pluginsFastCheckParams(p); 216 } 217 218 222 public final synchronized void addProgressListener(ProgressListener listener) { 223 if (progressSupport == null ) { 224 progressSupport = new ProgressSupport(); 225 } 226 progressSupport.addProgressListener(listener); 227 228 if (pluginsWithProgress == null) { 229 pluginsWithProgress = new ArrayList (); 230 Iterator pIt=getPlugins().iterator(); 231 while(pIt.hasNext()) { 232 RefactoringPlugin plugin=(RefactoringPlugin)pIt.next(); 233 if (plugin instanceof ProgressProvider) { 234 ((ProgressProvider) plugin).addProgressListener(progressListener); 235 pluginsWithProgress.add(plugin); 236 } 237 } 238 } 239 } 240 241 245 public final synchronized void removeProgressListener(ProgressListener listener) { 246 if (progressSupport != null ) { 247 progressSupport.removeProgressListener(listener); 248 } 249 250 if (pluginsWithProgress != null) { 251 Iterator pIt=pluginsWithProgress.iterator(); 252 253 while(pIt.hasNext()) { 254 ProgressProvider plugin=(ProgressProvider)pIt.next(); 255 plugin.removeProgressListener(progressListener); 256 } 257 pluginsWithProgress.clear(); 258 pluginsWithProgress = null; 259 } 260 } 261 262 267 public final Context getContext() { 268 if (this.scope == null) { 269 this.scope=new Context(new InstanceContent()); 270 } 271 return this.scope; 272 } 273 274 278 public final Lookup getRefactoringSource() { 279 return refactoringSource; 280 } 281 282 private Context scope; 283 284 287 public final void cancelRequest() { 288 Iterator pIt=getPlugins().iterator(); 289 290 while(pIt.hasNext()) { 291 RefactoringPlugin plugin=(RefactoringPlugin)pIt.next(); 292 plugin.cancelRequest(); 293 } 294 } 295 296 private Problem pluginsPreCheck(Problem problem) { 297 Iterator pIt=getPlugins().iterator(); 298 299 while(pIt.hasNext()) { 300 RefactoringPlugin plugin=(RefactoringPlugin)pIt.next(); 301 302 problem=chainProblems(plugin.preCheck(),problem); 303 if (problem!=null && problem.isFatal()) 304 return problem; 305 } 306 return problem; 307 } 308 309 private Problem pluginsPrepare(Problem problem, RefactoringSession session) { 310 RefactoringElementsBag elements = session.getElementsBag(); 311 Iterator pIt=getPlugins().iterator(); 312 313 while(pIt.hasNext()) { 314 RefactoringPlugin plugin=(RefactoringPlugin)pIt.next(); 315 316 problem=chainProblems(plugin.prepare(elements),problem); 317 if (problem!=null && problem.isFatal()) 318 return problem; 319 } 320 321 if (!(this instanceof WhereUsedQuery)) { 327 ReadOnlyFilesHandler handler = getROHandler(); 328 if (handler!=null) { 329 Collection files = SPIAccessor.DEFAULT.getReadOnlyFiles(elements); 330 Collection allFiles = new HashSet (); 331 for (Iterator i = files.iterator(); i.hasNext();) { 332 FileObject f = (FileObject) i.next(); 333 DataObject dob; 334 try { 335 dob = DataObject.find(f); 336 for (Iterator j = dob.files().iterator(); j.hasNext();) { 337 FileObject file = (FileObject) j.next(); 338 if (SharabilityQuery.getSharability(FileUtil.toFile(file)) == SharabilityQuery.SHARABLE) { 339 allFiles.add(file); 340 } 341 } 342 } catch (DataObjectNotFoundException e) { 343 allFiles.add(f); 344 } 345 } 346 problem = chainProblems(handler.createProblem(session, allFiles), problem); 347 } 348 } 349 350 return problem; 351 } 352 353 private ReadOnlyFilesHandler getROHandler() { 354 Lookup.Result result = Lookup.getDefault().lookup(new Lookup.Template(ReadOnlyFilesHandler.class)); 355 List handlers = (List ) result.allInstances(); 356 if (handlers.size() == 0) { 357 return null; 358 } 359 if (handlers.size() > 1) { 360 ErrorManager.getDefault().log(ErrorManager.WARNING, "Multiple instances of ReadOnlyFilesHandler found in Lookup; only using first one: " + handlers); } 362 return (ReadOnlyFilesHandler) handlers.get(0); 363 } 364 365 private Problem pluginsCheckParams(Problem problem) { 366 Iterator pIt=getPlugins().iterator(); 367 368 while(pIt.hasNext()) { 369 RefactoringPlugin plugin=(RefactoringPlugin)pIt.next(); 370 371 problem=chainProblems(plugin.checkParameters(),problem); 372 if (problem!=null && problem.isFatal()) 373 return problem; 374 } 375 return problem; 376 } 377 378 private Problem pluginsFastCheckParams(Problem problem) { 379 Iterator pIt=getPlugins().iterator(); 380 381 while(pIt.hasNext()) { 382 RefactoringPlugin plugin=(RefactoringPlugin)pIt.next(); 383 384 problem=chainProblems(plugin.fastCheckParameters(),problem); 385 if (problem!=null && problem.isFatal()) 386 return problem; 387 } 388 return problem; 389 } 390 391 static Problem chainProblems(Problem p,Problem p1) { 392 Problem problem; 393 394 if (p==null) return p1; 395 if (p1==null) return p; 396 problem=p; 397 while(problem.getNext()!=null) { 398 problem=problem.getNext(); 399 } 400 problem.setNext(p1); 401 return p; 402 } 403 404 405 private class ProgressL implements ProgressListener { 406 407 private float progressStep; 408 private float current; 409 public void start(ProgressEvent event) { 410 progressStep = (float) PLUGIN_STEPS / event.getCount(); 411 412 if (pluginsWithProgress.indexOf(event.getSource()) == 0) { 413 current = 0; 416 if (event.getCount()==-1) { 417 fireProgressListenerStart(event.getOperationType(), -1); 418 } else { 419 fireProgressListenerStart(event.getOperationType(), PLUGIN_STEPS*pluginsWithProgress.size()); 420 } 421 } else { 422 current = pluginsWithProgress.indexOf(event.getSource())*PLUGIN_STEPS; 423 fireProgressListenerStep((int) current); 424 } 425 } 426 427 public void step(ProgressEvent event) { 428 current = current + progressStep; 429 fireProgressListenerStep((int) current) ; 430 } 431 432 public void stop(ProgressEvent event) { 433 if (pluginsWithProgress.indexOf(event.getSource()) == pluginsWithProgress.size()-1) { 434 fireProgressListenerStop(); 435 } 436 } 437 443 private void fireProgressListenerStart(int type, int count) { 444 if (progressSupport != null) 445 progressSupport.fireProgressListenerStart(this, type, count); 446 } 447 448 450 private void fireProgressListenerStep() { 451 if (progressSupport != null) 452 progressSupport.fireProgressListenerStep(this); 453 } 454 455 459 private void fireProgressListenerStep(int count) { 460 if (progressSupport != null) 461 progressSupport.fireProgressListenerStep(this, count); 462 } 463 464 466 private void fireProgressListenerStop() { 467 if (progressSupport != null) 468 progressSupport.fireProgressListenerStop(this); 469 } 470 } 471 } 472 | Popular Tags |