diff: add listeners for DiffViewerBase
[idea/community.git] / platform / diff-impl / src / com / intellij / diff / tools / util / base / DiffViewerBase.java
index a17deb7f07dfc57f6935c3946200bb69e1e28f80..ce2708b91770627e4543774fc6e0444465071a59 100644 (file)
@@ -32,6 +32,7 @@ import com.intellij.openapi.util.Disposer;
 import com.intellij.util.Alarm;
 import com.intellij.util.Function;
 import com.intellij.util.containers.ContainerUtil;
+import com.intellij.util.SmartList;
 import com.intellij.util.ui.UIUtil;
 import org.jetbrains.annotations.*;
 
@@ -42,6 +43,8 @@ import java.util.List;
 public abstract class DiffViewerBase implements DiffViewer, DataProvider {
   protected static final Logger LOG = Logger.getInstance(DiffViewerBase.class);
 
+  @NotNull private final List<DiffViewerListener> myListeners = new SmartList<DiffViewerListener>();
+
   @Nullable protected final Project myProject;
   @NotNull protected final DiffContext myContext;
   @NotNull protected final ContentDiffRequest myRequest;
@@ -66,6 +69,8 @@ public abstract class DiffViewerBase implements DiffViewer, DataProvider {
     components.popupActions = createPopupActions();
     components.statusPanel = getStatusPanel();
 
+    fireEvent(EventType.INIT);
+
     rediff(true);
     return components;
   }
@@ -84,6 +89,8 @@ public abstract class DiffViewerBase implements DiffViewer, DataProvider {
         abortRediff();
         updateContextHints();
 
+        fireEvent(EventType.DISPOSE);
+
         onDispose();
       }
     };
@@ -117,6 +124,7 @@ public abstract class DiffViewerBase implements DiffViewer, DataProvider {
   public final void abortRediff() {
     myTaskExecutor.abort();
     myTaskAlarm.cancelAllRequests();
+    fireEvent(EventType.REDIFF_ABORTED);
   }
 
   @CalledInAwt
@@ -129,6 +137,7 @@ public abstract class DiffViewerBase implements DiffViewer, DataProvider {
     if (isDisposed()) return;
     abortRediff();
 
+    fireEvent(EventType.BEFORE_REDIFF);
     onBeforeRediff();
 
     // most of performRediff implementations take ReadLock inside. If EDT is holding write lock - this will never happen,
@@ -140,8 +149,16 @@ public abstract class DiffViewerBase implements DiffViewer, DataProvider {
     myTaskExecutor.executeAndTryWait(
       new Function<ProgressIndicator, Runnable>() {
         @Override
-        public Runnable fun(ProgressIndicator indicator) {
-          return performRediff(indicator);
+        public Runnable fun(final ProgressIndicator indicator) {
+          final Runnable callback = performRediff(indicator);
+          return new Runnable() {
+            @Override
+            public void run() {
+              callback.run();
+              onAfterRediff();
+              fireEvent(EventType.AFTER_REDIFF);
+            }
+          };
         }
       },
       new Runnable() {
@@ -215,6 +232,10 @@ public abstract class DiffViewerBase implements DiffViewer, DataProvider {
   protected void onBeforeRediff() {
   }
 
+  @CalledInAwt
+  protected void onAfterRediff() {
+  }
+
   @CalledInBackground
   @NotNull
   protected abstract Runnable performRediff(@NotNull ProgressIndicator indicator);
@@ -229,6 +250,48 @@ public abstract class DiffViewerBase implements DiffViewer, DataProvider {
     return null;
   }
 
+  //
+  // Listeners
+  //
+
+  @CalledInAwt
+  public void addListener(@NotNull DiffViewerListener listener) {
+    myListeners.add(listener);
+  }
+
+  @CalledInAwt
+  public void removeListener(@NotNull DiffViewerListener listener) {
+    myListeners.remove(listener);
+  }
+
+  @NotNull
+  @CalledInAwt
+  protected List<DiffViewerListener> getListeners() {
+    return myListeners;
+  }
+
+  @CalledInAwt
+  private void fireEvent(@NotNull EventType type) {
+    for (DiffViewerListener listener : myListeners) {
+      switch (type) {
+        case INIT:
+          listener.onInit();
+          break;
+        case DISPOSE:
+          listener.onDispose();
+          break;
+        case BEFORE_REDIFF:
+          listener.onBeforeRediff();
+          break;
+        case AFTER_REDIFF:
+          listener.onAfterRediff();
+          break;
+        case REDIFF_ABORTED:
+          listener.onRediffAborted();
+          break;
+      }
+    }
+  }
 
   //
   // Helpers
@@ -250,4 +313,8 @@ public abstract class DiffViewerBase implements DiffViewer, DataProvider {
       return null;
     }
   }
+
+  private enum EventType {
+    INIT, DISPOSE, BEFORE_REDIFF, AFTER_REDIFF, REDIFF_ABORTED,
+  }
 }