Refactoring: extract common code with infinite cycled long running task
This commit is contained in:
@@ -27,6 +27,7 @@ import com.intellij.openapi.editor.ScrollType;
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager;
|
||||
import com.intellij.openapi.progress.ProcessCanceledException;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.wm.ToolWindow;
|
||||
@@ -45,6 +46,7 @@ import org.jetbrains.jet.lang.types.lang.InlineUtil;
|
||||
import org.jetbrains.jet.plugin.JetPluginUtil;
|
||||
import org.jetbrains.jet.plugin.internal.Location;
|
||||
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
|
||||
import org.jetbrains.jet.plugin.util.InfinitePeriodicalTask;
|
||||
import org.jetbrains.jet.plugin.util.LongRunningReadTask;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -172,12 +174,9 @@ public class BytecodeToolWindow extends JPanel implements Disposable {
|
||||
}
|
||||
|
||||
private final Editor myEditor;
|
||||
private final Alarm myUpdateAlarm;
|
||||
private final Project myProject;
|
||||
private final ToolWindow toolWindow;
|
||||
|
||||
private UpdateBytecodeToolWindowTask currentTask = null;
|
||||
|
||||
public BytecodeToolWindow(Project project, ToolWindow toolWindow) {
|
||||
super(new BorderLayout());
|
||||
myProject = project;
|
||||
@@ -187,20 +186,12 @@ public class BytecodeToolWindow extends JPanel implements Disposable {
|
||||
EditorFactory.getInstance().createDocument(""), project, JavaFileType.INSTANCE, true);
|
||||
add(myEditor.getComponent());
|
||||
|
||||
myUpdateAlarm = new Alarm(Alarm.ThreadToUse.SWING_THREAD, this);
|
||||
myUpdateAlarm.addRequest(new Runnable() {
|
||||
new InfinitePeriodicalTask(UPDATE_DELAY, Alarm.ThreadToUse.SWING_THREAD, this, new Computable<LongRunningReadTask>() {
|
||||
@Override
|
||||
public void run() {
|
||||
myUpdateAlarm.addRequest(this, UPDATE_DELAY);
|
||||
UpdateBytecodeToolWindowTask task = new UpdateBytecodeToolWindowTask();
|
||||
task.init();
|
||||
|
||||
if (task.shouldStart(currentTask)) {
|
||||
currentTask = task;
|
||||
currentTask.run();
|
||||
}
|
||||
public LongRunningReadTask compute() {
|
||||
return new UpdateBytecodeToolWindowTask();
|
||||
}
|
||||
}, UPDATE_DELAY);
|
||||
}).start();
|
||||
|
||||
setText(DEFAULT_TEXT);
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.EditorFactory;
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.wm.ToolWindow;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
@@ -48,6 +49,7 @@ import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.plugin.JetFileType;
|
||||
import org.jetbrains.jet.plugin.internal.Location;
|
||||
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
|
||||
import org.jetbrains.jet.plugin.util.InfinitePeriodicalTask;
|
||||
import org.jetbrains.jet.plugin.util.LongRunningReadTask;
|
||||
import org.jetbrains.jet.util.slicedmap.ReadOnlySlice;
|
||||
|
||||
@@ -154,9 +156,7 @@ public class ResolveToolWindow extends JPanel implements Disposable {
|
||||
}
|
||||
}
|
||||
|
||||
private final Alarm myUpdateAlarm;
|
||||
private final Editor myEditor;
|
||||
private UpdateResolveToolWindowTask currentTask = null;
|
||||
|
||||
private final Project myProject;
|
||||
private final ToolWindow toolWindow;
|
||||
@@ -169,20 +169,12 @@ public class ResolveToolWindow extends JPanel implements Disposable {
|
||||
.createEditor(EditorFactory.getInstance().createDocument(""), project, JetFileType.INSTANCE, true);
|
||||
add(myEditor.getComponent());
|
||||
|
||||
myUpdateAlarm = new Alarm(Alarm.ThreadToUse.SWING_THREAD, this);
|
||||
myUpdateAlarm.addRequest(new Runnable() {
|
||||
new InfinitePeriodicalTask(UPDATE_DELAY, Alarm.ThreadToUse.SWING_THREAD, this, new Computable<LongRunningReadTask>() {
|
||||
@Override
|
||||
public void run() {
|
||||
myUpdateAlarm.addRequest(this, UPDATE_DELAY);
|
||||
UpdateResolveToolWindowTask task = new UpdateResolveToolWindowTask();
|
||||
task.init();
|
||||
|
||||
if (task.shouldStart(currentTask)) {
|
||||
currentTask = task;
|
||||
currentTask.run();
|
||||
}
|
||||
public LongRunningReadTask compute() {
|
||||
return new UpdateResolveToolWindowTask();
|
||||
}
|
||||
}, UPDATE_DELAY);
|
||||
}).start();
|
||||
|
||||
setText(DEFAULT_TEXT);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2010-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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.plugin.util;
|
||||
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.util.Alarm;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class InfinitePeriodicalTask {
|
||||
private final Alarm myUpdateAlarm;
|
||||
private final long delay;
|
||||
private final Computable<? extends LongRunningReadTask> taskProvider;
|
||||
private LongRunningReadTask currentTask;
|
||||
|
||||
public InfinitePeriodicalTask(
|
||||
long delay, @NotNull Alarm.ThreadToUse threadToUse, Disposable parentDisposable,
|
||||
Computable<? extends LongRunningReadTask> taskProvider
|
||||
) {
|
||||
myUpdateAlarm = new Alarm(threadToUse, parentDisposable);
|
||||
this.delay = delay;
|
||||
this.taskProvider = taskProvider;
|
||||
}
|
||||
|
||||
public InfinitePeriodicalTask start() {
|
||||
myUpdateAlarm.addRequest(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
myUpdateAlarm.addRequest(this, delay);
|
||||
LongRunningReadTask task = taskProvider.compute();
|
||||
task.init();
|
||||
|
||||
//noinspection unchecked
|
||||
if (task.shouldStart(currentTask)) {
|
||||
currentTask = task;
|
||||
currentTask.run();
|
||||
}
|
||||
}
|
||||
}, delay);
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user