ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / changes / ControlledCycle.java
blob434975b2a7493136165355d0e337fea3c2e23d3c
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.vcs.changes;
18 import com.intellij.lifecycle.AtomicSectionsAware;
19 import com.intellij.lifecycle.ControlledAlarmFactory;
20 import com.intellij.lifecycle.SlowlyClosingAlarm;
21 import com.intellij.openapi.diagnostic.Logger;
22 import com.intellij.openapi.progress.ProcessCanceledException;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.util.Alarm;
25 import org.jetbrains.annotations.NotNull;
27 import java.util.concurrent.atomic.AtomicBoolean;
29 public class ControlledCycle implements Runnable {
30 private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.vcs.changes.ControlledCycle");
32 private final Alarm mySimpleAlarm;
33 private final SlowlyClosingAlarm myControlledAlarm;
34 // this interval is also to check for not initialized paths, so it is rather small
35 private static final int ourRefreshInterval = 10000;
36 private final int myRefreshInterval;
37 private final Runnable myRunnable;
39 private final AtomicBoolean myActive;
41 public ControlledCycle(final Project project, final MyCallback callback, @NotNull final String name) {
42 this(project, callback, name, -1);
45 public ControlledCycle(final Project project, final MyCallback callback, @NotNull final String name, final int refreshInterval) {
46 myRefreshInterval = (refreshInterval <= 0) ? ourRefreshInterval : refreshInterval;
47 myActive = new AtomicBoolean(false);
48 myRunnable = new Runnable() {
49 boolean shouldBeContinued = true;
50 public void run() {
51 try {
52 shouldBeContinued = callback.call(myControlledAlarm);
53 } catch (ProcessCanceledException e) {
54 return;
55 } catch (RuntimeException e) {
56 LOG.info(e);
58 if (! shouldBeContinued) {
59 myActive.set(false);
60 } else {
61 mySimpleAlarm.addRequest(ControlledCycle.this, myRefreshInterval);
65 mySimpleAlarm = new Alarm(Alarm.ThreadToUse.SHARED_THREAD, project);
66 myControlledAlarm = ControlledAlarmFactory.createOnApplicationPooledThread(project, name);
69 public void start() {
70 final boolean wasSet = myActive.compareAndSet(false, true);
71 if (wasSet) {
72 mySimpleAlarm.addRequest(this, myRefreshInterval);
76 public void run() {
77 try {
78 myControlledAlarm.checkShouldExit();
79 myControlledAlarm.addRequest(myRunnable);
80 } catch (ProcessCanceledException e) {
85 public interface MyCallback {
86 boolean call(final AtomicSectionsAware atomicSectionsAware);