cancel find usages on usage view close

This commit is contained in:
Alexey Kudravtsev
2013-10-01 14:54:47 +04:00
parent ded351bcab
commit 069d1720b2
2 changed files with 30 additions and 49 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import com.intellij.openapi.ide.CopyPasteManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.progress.util.ProgressWrapper;
import com.intellij.openapi.progress.util.TooManyUsagesStatus;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
@@ -140,6 +141,7 @@ public class UsageViewImpl implements UsageView, UsageModelTracker.UsageModelTra
private final UsageViewTreeModelBuilder myModel;
private final Object lock = new Object();
private Splitter myPreviewSplitter;
private volatile ProgressIndicator associatedProgress; // the progress that current find usages is running under
UsageViewImpl(@NotNull final Project project,
@NotNull UsageViewPresentation presentation,
@@ -256,10 +258,16 @@ public class UsageViewImpl implements UsageView, UsageModelTracker.UsageModelTra
}
protected boolean searchHasBeenCancelled() {
return false;
ProgressIndicator progress = associatedProgress;
return progress != null && progress.isCanceled();
}
protected void setCurrentSearchCancelled(boolean flag){}
protected void cancelCurrentSearch() {
ProgressIndicator progress = associatedProgress;
if (progress != null) {
ProgressWrapper.unwrap(progress).cancel();
}
}
private void setupCentralPanel() {
myCentralPanel.removeAll();
@@ -696,6 +704,10 @@ public class UsageViewImpl implements UsageView, UsageModelTracker.UsageModelTra
return ActionManager.getInstance().getKeyboardShortcut("ShowSettingsAndFindUsages");
}
void associateProgress(ProgressIndicator indicator) {
associatedProgress = indicator;
}
private class CloseAction extends CloseTabToolbarAction {
@Override
public void update(AnActionEvent e) {
@@ -758,8 +770,7 @@ public class UsageViewImpl implements UsageView, UsageModelTracker.UsageModelTra
public void run(@NotNull final ProgressIndicator indicator) {
final TooManyUsagesStatus tooManyUsagesStatus = TooManyUsagesStatus.createFor(indicator);
setSearchInProgress(true);
setCurrentSearchCancelled(false);
associateProgress(indicator);
myChangesDetected = false;
UsageSearcher usageSearcher = myUsageSearcherFactory.create();
@@ -1016,6 +1027,7 @@ public class UsageViewImpl implements UsageView, UsageModelTracker.UsageModelTra
@Override
public void close() {
cancelCurrentSearch();
UsageViewManager.getInstance(myProject).closeContent(myContent);
}
@@ -135,7 +135,7 @@ public class UsageViewManagerImpl extends UsageViewManager {
final boolean showNotFoundMessage,
@NotNull final UsageViewPresentation presentation,
@Nullable final UsageViewStateListener listener) {
final AtomicReference<UsageViewImpl> usageView = new AtomicReference<UsageViewImpl>();
final AtomicReference<UsageViewImpl> usageViewRef = new AtomicReference<UsageViewImpl>();
final FindUsagesProcessPresentation processPresentation = new FindUsagesProcessPresentation();
processPresentation.setShowNotFoundMessage(showNotFoundMessage);
@@ -144,7 +144,7 @@ public class UsageViewManagerImpl extends UsageViewManager {
Task.Backgroundable task = new Task.Backgroundable(myProject, getProgressTitle(presentation), true, new SearchInBackgroundOption()) {
@Override
public void run(@NotNull final ProgressIndicator indicator) {
new SearchForUsagesRunnable(UsageViewManagerImpl.this.myProject, usageView, presentation, searchFor, searcherFactory,
new SearchForUsagesRunnable(UsageViewManagerImpl.this.myProject, usageViewRef, presentation, searchFor, searcherFactory,
processPresentation, listener).run();
}
@@ -156,12 +156,12 @@ public class UsageViewManagerImpl extends UsageViewManager {
@Override
@Nullable
public NotificationInfo getNotificationInfo() {
String notification = usageView.get() != null ? usageView.get().getUsagesCount() + " Usage(s) Found" : "No Usages Found";
String notification = usageViewRef.get() != null ? usageViewRef.get().getUsagesCount() + " Usage(s) Found" : "No Usages Found";
return new NotificationInfo("Find Usages", "Find Usages Finished", notification);
}
};
ProgressManager.getInstance().run(task);
return usageView.get();
return usageViewRef.get();
}
@Override
@@ -249,7 +249,7 @@ public class UsageViewManagerImpl extends UsageViewManager {
String message = UsageViewBundle.message("find.excessive.usage.count.prompt", usageCount);
UsageLimitUtil.Result ret = UsageLimitUtil.showTooManyUsagesWarning(project, message);
if (ret == UsageLimitUtil.Result.ABORT && usageView != null) {
usageView.setCurrentSearchCancelled(true);
usageView.cancelCurrentSearch();
indicator.cancel();
}
tooManyUsagesStatus.userResponded();
@@ -267,31 +267,30 @@ public class UsageViewManagerImpl extends UsageViewManager {
private final Factory<UsageSearcher> mySearcherFactory;
private final FindUsagesProcessPresentation myProcessPresentation;
private final UsageViewStateListener myListener;
private volatile boolean mySearchHasBeenCancelled;
private SearchForUsagesRunnable(@NotNull Project project,
@NotNull AtomicReference<UsageViewImpl> usageView,
@NotNull AtomicReference<UsageViewImpl> usageViewRef,
@NotNull UsageViewPresentation presentation,
@NotNull UsageTarget[] searchFor,
@NotNull Factory<UsageSearcher> searcherFactory,
@NotNull FindUsagesProcessPresentation processPresentation,
@Nullable UsageViewStateListener listener) {
myProject = project;
myUsageViewRef = usageView;
myUsageViewRef = usageViewRef;
myPresentation = presentation;
mySearchFor = searchFor;
mySearcherFactory = searcherFactory;
myProcessPresentation = processPresentation;
myListener = listener;
mySearchHasBeenCancelled = false;
}
private UsageViewImpl getUsageView() {
private UsageViewImpl getUsageView(ProgressIndicator indicator) {
UsageViewImpl usageView = myUsageViewRef.get();
if (usageView != null) return usageView;
int usageCount = myUsageCountWithoutDefinition.get();
if (usageCount >= 2 || usageCount == 1 && myProcessPresentation.isShowPanelIfOnlyOneUsage()) {
usageView = new MyUsageViewImpl(myProject);
usageView = new UsageViewImpl(myProject, myPresentation, mySearchFor, mySearcherFactory);
usageView.associateProgress(indicator);
if (myUsageViewRef.compareAndSet(null, usageView)) {
openView(usageView);
final Usage firstUsage = myFirstUsage.get();
@@ -352,7 +351,7 @@ public class UsageViewManagerImpl extends UsageViewManager {
@Override
public boolean process(final Usage usage) {
ProgressIndicator indicator = ProgressWrapper.unwrap(ProgressManager.getInstance().getProgressIndicator());
if (searchHasBeenCancelled() || indicator != null && indicator.isCanceled()) return false;
if (indicator != null && indicator.isCanceled()) return false;
TooManyUsagesStatus tooManyUsagesStatus = TooManyUsagesStatus.getFrom(indicator);
tooManyUsagesStatus.pauseProcessingIfTooManyUsages();
boolean incrementCounter = !isSelfUsage(usage, mySearchFor);
@@ -363,7 +362,7 @@ public class UsageViewManagerImpl extends UsageViewManager {
myFirstUsage.compareAndSet(null, usage);
}
final UsageViewImpl usageView = getUsageView();
final UsageViewImpl usageView = getUsageView(indicator);
if (usageCount > UsageLimitUtil.USAGES_LIMIT) {
if (tooManyUsagesStatus.switchTooManyUsagesStatus()) {
@@ -383,7 +382,7 @@ public class UsageViewManagerImpl extends UsageViewManager {
return indicator == null || !indicator.isCanceled();
}
});
if (getUsageView() != null) {
if (getUsageView(indicator) != null) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
@@ -405,14 +404,6 @@ public class UsageViewManagerImpl extends UsageViewManager {
}, myProject.getDisposed());
}
public void setCurrentSearchCancelled(boolean cancelled) {
mySearchHasBeenCancelled = cancelled;
}
public boolean searchHasBeenCancelled() {
return mySearchHasBeenCancelled;
}
private void endSearchForUsages(@NotNull final AtomicBoolean findStartedBalloonShown) {
assert !ApplicationManager.getApplication().isDispatchThread() : Thread.currentThread();
int usageCount = myUsageCountWithoutDefinition.get();
@@ -485,28 +476,6 @@ public class UsageViewManagerImpl extends UsageViewManager {
myListener.findingUsagesFinished(myUsageViewRef.get());
}
}
private class MyUsageViewImpl extends UsageViewImpl {
private MyUsageViewImpl(@NotNull Project project) {
super(project, SearchForUsagesRunnable.this.myPresentation, mySearchFor, mySearcherFactory);
}
@Override
public void close() {
setCurrentSearchCancelled(true);
super.close();
}
@Override
public boolean searchHasBeenCancelled() {
return SearchForUsagesRunnable.this.searchHasBeenCancelled();
}
@Override
public void setCurrentSearchCancelled(boolean cancelled) {
SearchForUsagesRunnable.this.setCurrentSearchCancelled(cancelled);
}
}
}
private static void notifyByFindBalloon(final HyperlinkListener listener,