diff: add listeners for DiffViewerBase
authorAleksey Pivovarov <AMPivovarov@gmail.com>
Mon, 22 Jun 2015 12:48:18 +0000 (15:48 +0300)
committerAleksey Pivovarov <AMPivovarov@gmail.com>
Thu, 3 Sep 2015 16:28:05 +0000 (19:28 +0300)
platform/diff-impl/src/com/intellij/diff/tools/util/base/DiffViewerBase.java
platform/diff-impl/src/com/intellij/diff/tools/util/base/DiffViewerListener.java [new file with mode: 0644]

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,
+  }
 }
diff --git a/platform/diff-impl/src/com/intellij/diff/tools/util/base/DiffViewerListener.java b/platform/diff-impl/src/com/intellij/diff/tools/util/base/DiffViewerListener.java
new file mode 100644 (file)
index 0000000..0455c95
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2000-2015 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 com.intellij.diff.tools.util.base;
+
+import org.jetbrains.annotations.CalledInAwt;
+
+public class DiffViewerListener {
+  @CalledInAwt
+  protected void onInit() {
+  }
+
+  @CalledInAwt
+  protected void onDispose() {
+  }
+
+  @CalledInAwt
+  protected void onBeforeRediff() {
+  }
+
+  @CalledInAwt
+  protected void onAfterRediff() {
+  }
+
+  @CalledInAwt
+  protected void onRediffAborted() {
+  }
+}