diff: allow to set custom notifications
authorAleksey Pivovarov <AMPivovarov@gmail.com>
Fri, 14 Aug 2015 14:41:20 +0000 (17:41 +0300)
committerAleksey Pivovarov <AMPivovarov@gmail.com>
Wed, 19 Aug 2015 17:36:13 +0000 (20:36 +0300)
platform/diff-api/src/com/intellij/diff/util/DiffUserDataKeys.java
platform/diff-impl/src/com/intellij/diff/tools/fragmented/UnifiedDiffViewer.java
platform/diff-impl/src/com/intellij/diff/tools/util/base/DiffPanelBase.java
platform/diff-impl/src/com/intellij/diff/tools/util/side/OnesideDiffViewer.java
platform/diff-impl/src/com/intellij/diff/tools/util/side/ThreesideDiffViewer.java
platform/diff-impl/src/com/intellij/diff/tools/util/side/TwosideDiffViewer.java
platform/diff-impl/src/com/intellij/diff/util/DiffUtil.java

index 9457b0030e803ee002ea0551bd1ce8331d50fe78..8583f1e342fcfd1856d1bfdb8a8906704e25c66a 100644 (file)
@@ -20,6 +20,7 @@ import com.intellij.openapi.actionSystem.DataProvider;
 import com.intellij.openapi.util.Key;
 import com.intellij.openapi.util.Pair;
 
+import javax.swing.*;
 import java.util.List;
 
 public interface DiffUserDataKeys {
@@ -52,6 +53,7 @@ public interface DiffUserDataKeys {
   // Both data from DiffContext / DiffRequest will be used. Data from DiffRequest will be used first.
   //
 
+  Key<List<JComponent>> NOTIFICATIONS = Key.create("Diff.Notifications");
   Key<List<AnAction>> CONTEXT_ACTIONS = Key.create("Diff.ContextActions");
   Key<DataProvider> DATA_PROVIDER = Key.create("Diff.DataProvider");
   Key<Boolean> GO_TO_SOURCE_DISABLE = Key.create("Diff.GoToSourceDisable");
index 87b8f6ab111d472642169fb1a3f4e4326deea539..950fb11fb26a982dc00a3417a6c1c44129a51fca 100644 (file)
@@ -135,6 +135,7 @@ public class UnifiedDiffViewer extends ListenerDiffViewerBase {
     installEditorListeners();
     installTypingSupport();
     myPanel.setLoadingContent(); // We need loading panel only for initial rediff()
+    myPanel.setPersistentNotifications(DiffUtil.getCustomNotifications(myContext, myRequest));
   }
 
   @Override
index 1698e1cdc02bc882784912a53441e92859703250..d1c300c8c0b4273dddcd1bfd3d0e1fd3ff5e5a9f 100644 (file)
@@ -25,12 +25,16 @@ import org.jetbrains.annotations.Nullable;
 
 import javax.swing.*;
 import java.awt.*;
+import java.util.ArrayList;
+import java.util.List;
 
 public abstract class DiffPanelBase extends JPanel implements DataProvider {
   @Nullable protected final Project myProject;
   @NotNull private final DataProvider myDataProvider;
   @NotNull protected final DiffContext myContext;
 
+  @NotNull private final List<JComponent> myPersistentNotifications = new ArrayList<JComponent>();
+
   @NotNull protected final JPanel myContentPanel;
   @NotNull protected final JPanel myNotificationsPanel;
 
@@ -95,8 +99,25 @@ public abstract class DiffPanelBase extends JPanel implements DataProvider {
   // Notifications
   //
 
+  public void setPersistentNotifications(@NotNull List<JComponent> components) {
+    for (JComponent notification : myPersistentNotifications) {
+      myNotificationsPanel.remove(notification);
+    }
+
+    myPersistentNotifications.clear();
+    myPersistentNotifications.addAll(components);
+
+    for (JComponent notification : myPersistentNotifications) {
+      myNotificationsPanel.add(notification);
+    }
+    myNotificationsPanel.revalidate();
+  }
+
   public void resetNotifications() {
     myNotificationsPanel.removeAll();
+    for (JComponent notification : myPersistentNotifications) {
+      myNotificationsPanel.add(notification);
+    }
     myNotificationsPanel.revalidate();
   }
 
index cade7e21675df469b553883452c16624bffc3af2..56addf42632bbc5e56c9bbf43bc50fad91e2851e 100644 (file)
@@ -57,6 +57,12 @@ public abstract class OnesideDiffViewer<T extends EditorHolder> extends Listener
     myPanel = new SimpleDiffPanel(myContentPanel, this, context);
   }
 
+  @Override
+  protected void onInit() {
+    super.onInit();
+    myPanel.setPersistentNotifications(DiffUtil.getCustomNotifications(myContext, myRequest));
+  }
+
   @Override
   @CalledInAwt
   protected void onDispose() {
index 2d5dee2aeff11ab83c49f00733b2cccde3adf693..e1a4cb5cf491bf2330f7c6b78cd7a8d5bcba7ee5 100644 (file)
@@ -66,6 +66,12 @@ public abstract class ThreesideDiffViewer<T extends EditorHolder> extends Listen
     myPanel = new SimpleDiffPanel(myContentPanel, this, context);
   }
 
+  @Override
+  protected void onInit() {
+    super.onInit();
+    myPanel.setPersistentNotifications(DiffUtil.getCustomNotifications(myContext, myRequest));
+  }
+
   @Override
   @CalledInAwt
   protected void onDispose() {
index ea4cee2fd76177041b0bf7af305571dbc0cf8b55..c78d0fb610d2e60e7db909cedde21ab0aa7fced0 100644 (file)
@@ -59,6 +59,12 @@ public abstract class TwosideDiffViewer<T extends EditorHolder> extends Listener
     myPanel = new SimpleDiffPanel(myContentPanel, this, context);
   }
 
+  @Override
+  protected void onInit() {
+    super.onInit();
+    myPanel.setPersistentNotifications(DiffUtil.getCustomNotifications(myContext, myRequest));
+  }
+
   @Override
   @CalledInAwt
   protected void onDispose() {
index bfb3fd0ff52ff575f7803a649b90dd48c94b8c6a..6e8f8ae5365fdeb44380c5ce6410fd084a52bbce 100644 (file)
@@ -895,6 +895,22 @@ public class DiffUtil {
     return null;
   }
 
+  public static void addNotification(@NotNull JComponent component, @NotNull UserDataHolder holder) {
+    List<JComponent> components = holder.getUserData(DiffUserDataKeys.NOTIFICATIONS);
+    if (components == null) {
+      holder.putUserData(DiffUserDataKeys.NOTIFICATIONS, Collections.singletonList(component));
+    }
+    else {
+      holder.putUserData(DiffUserDataKeys.NOTIFICATIONS, ContainerUtil.append(components, component));
+    }
+  }
+
+  public static List<JComponent> getCustomNotifications(@NotNull DiffContext context, @NotNull DiffRequest request) {
+    List<JComponent> requestComponents = request.getUserData(DiffUserDataKeys.NOTIFICATIONS);
+    List<JComponent> contextComponents = context.getUserData(DiffUserDataKeys.NOTIFICATIONS);
+    return ContainerUtil.concat(ContainerUtil.notNullize(contextComponents), ContainerUtil.notNullize(requestComponents));
+  }
+
   //
   // DataProvider
   //