VCS: diff in shelved changes, apply patch: lazy load contents
authorirengrig <Irina.Chernushina@jetbrains.com>
Mon, 15 Mar 2010 10:38:09 +0000 (13:38 +0300)
committerirengrig <Irina.Chernushina@jetbrains.com>
Mon, 15 Mar 2010 10:38:09 +0000 (13:38 +0300)
platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/DiffRequestPresentableProxy.java [new file with mode: 0644]
platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/FilePatchInProgress.java
platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/DiffShelvedChangesAction.java

diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/DiffRequestPresentableProxy.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/DiffRequestPresentableProxy.java
new file mode 100644 (file)
index 0000000..74c6886
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2000-2010 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.openapi.vcs.changes.actions;
+
+import com.intellij.openapi.actionSystem.AnAction;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.List;
+
+public abstract class DiffRequestPresentableProxy implements DiffRequestPresentable {
+  private DiffRequestPresentable myDelegate;
+  private List<? extends AnAction> myCachedActions;
+  private MyResult myStepResult;
+
+  @Nullable
+  protected abstract DiffRequestPresentable init();
+
+  @Nullable
+  private DiffRequestPresentable initRequest() {
+    if (myDelegate == null) {
+      myDelegate = init();
+    }
+    return myDelegate;
+  }
+
+  public List<? extends AnAction> createActions(ShowDiffAction.DiffExtendUIFactory uiFactory) {
+    if (myCachedActions == null) {
+      myCachedActions = initRequest().createActions(uiFactory);
+    }
+    return myCachedActions;
+  }
+
+  public MyResult step() {
+    final DiffRequestPresentable request = initRequest();
+    if (request == null) {
+      return new MyResult(null, DiffPresentationReturnValue.removeFromList);
+    }
+    myStepResult = request.step();
+    return myStepResult;
+  }
+
+  public boolean haveStuff() {
+    final DiffRequestPresentable request = initRequest();
+    if (request == null) {
+      return false;
+    }
+    return request.haveStuff();
+  }
+}
index a785c66343d6758f8b48c8eb9567815a50e39788..780784f9e2871c5bbf166f48433f8d062c619265 100644 (file)
@@ -32,6 +32,7 @@ import com.intellij.openapi.vcs.changes.CurrentContentRevision;
 import com.intellij.openapi.vcs.changes.SimpleContentRevision;
 import com.intellij.openapi.vcs.changes.actions.ChangeDiffRequestPresentable;
 import com.intellij.openapi.vcs.changes.actions.DiffRequestPresentable;
+import com.intellij.openapi.vcs.changes.actions.DiffRequestPresentableProxy;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.vcsUtil.VcsUtil;
 import org.jetbrains.annotations.NotNull;
@@ -220,19 +221,24 @@ public class FilePatchInProgress implements Strippable {
 
     @Nullable
     public DiffRequestPresentable createDiffRequestPresentable(final Project project) {
-      if (myPatchInProgress.isConflictingChange()) {
-        final ApplyPatchForBaseRevisionTexts texts = ApplyPatchForBaseRevisionTexts
-          .create(project, myPatchInProgress.getCurrentBase(), new FilePathImpl(myPatchInProgress.getCurrentBase()),
-                  myPatchInProgress.getPatch());
-        if ((texts != null) &&
-            (ApplyPatchStatus.SUCCESS.equals(texts.getStatus()) || ApplyPatchStatus.ALREADY_APPLIED.equals(texts.getStatus()))) {
-          return new MergedDiffRequestPresentable(project, texts,
-                  myPatchInProgress.getCurrentBase(), myPatchInProgress.getPatch().getAfterVersionId());
+      return new DiffRequestPresentableProxy() {
+        @Override
+        protected DiffRequestPresentable init() {
+          if (myPatchInProgress.isConflictingChange()) {
+            final ApplyPatchForBaseRevisionTexts texts = ApplyPatchForBaseRevisionTexts
+              .create(project, myPatchInProgress.getCurrentBase(), new FilePathImpl(myPatchInProgress.getCurrentBase()),
+                      myPatchInProgress.getPatch());
+            if ((texts != null) &&
+                (ApplyPatchStatus.SUCCESS.equals(texts.getStatus()) || ApplyPatchStatus.ALREADY_APPLIED.equals(texts.getStatus()))) {
+              return new MergedDiffRequestPresentable(project, texts,
+                      myPatchInProgress.getCurrentBase(), myPatchInProgress.getPatch().getAfterVersionId());
+            }
+            return null;
+          } else {
+            return new ChangeDiffRequestPresentable(project, PatchChange.this);
+          }
         }
-        return null;
-      } else {
-        return new ChangeDiffRequestPresentable(project, this);
-      }
+      };
     }
   }
 
index 8133ecc3c6b2ea77f3ab86010d64a6e2baf72574..850be87689997a2957ffed920e545c4015924786 100644 (file)
@@ -29,6 +29,7 @@ import com.intellij.openapi.vcs.changes.Change;
 import com.intellij.openapi.vcs.changes.ContentRevision;
 import com.intellij.openapi.vcs.changes.actions.ChangeDiffRequestPresentable;
 import com.intellij.openapi.vcs.changes.actions.DiffRequestPresentable;
+import com.intellij.openapi.vcs.changes.actions.DiffRequestPresentableProxy;
 import com.intellij.openapi.vcs.changes.actions.ShowDiffAction;
 import com.intellij.openapi.vcs.changes.patch.ApplyPatchForBaseRevisionTexts;
 import com.intellij.openapi.vcs.changes.patch.MergedDiffRequestPresentable;
@@ -51,7 +52,7 @@ public class DiffShelvedChangesAction extends AnAction implements DumbAware {
   }
 
   public static void showShelvedChangesDiff(final DataContext dc) {
-    Project project = PlatformDataKeys.PROJECT.getData(dc);
+    final Project project = PlatformDataKeys.PROJECT.getData(dc);
     ShelvedChangeList[] changeLists = ShelvedChangesViewManager.SHELVED_CHANGELIST_KEY.getData(dc);
     if (changeLists == null) {
       changeLists = ShelvedChangesViewManager.SHELVED_RECYCLED_CHANGELIST_KEY.getData(dc);
@@ -68,7 +69,7 @@ public class DiffShelvedChangesAction extends AnAction implements DumbAware {
 
     int toSelectIdx = 0;
     final ArrayList<DiffRequestPresentable> diffRequestPresentables = new ArrayList<DiffRequestPresentable>();
-    ApplyPatchContext context = new ApplyPatchContext(project.getBaseDir(), 0, false, false);
+    final ApplyPatchContext context = new ApplyPatchContext(project.getBaseDir(), 0, false, false);
     final PatchesPreloader preloader = new PatchesPreloader();
 
     final List<String> missing = new LinkedList<String>();
@@ -76,7 +77,7 @@ public class DiffShelvedChangesAction extends AnAction implements DumbAware {
       final Change change = shelvedChange.getChange(project);
       final String beforePath = shelvedChange.getBeforePath();
       try {
-        VirtualFile f = FilePatch.findPatchTarget(context, beforePath, shelvedChange.getAfterPath(), beforePath == null);
+        final VirtualFile f = FilePatch.findPatchTarget(context, beforePath, shelvedChange.getAfterPath(), beforePath == null);
         if ((f == null) || (! f.exists())) {
           if (beforePath != null) {
             missing.add(beforePath);
@@ -84,22 +85,27 @@ public class DiffShelvedChangesAction extends AnAction implements DumbAware {
           continue;
         }
 
-        if (isConflictingChange(change)) {
-          TextFilePatch patch = preloader.getPatch(shelvedChange);
-          if (patch == null) continue;
-
-          final FilePath pathBeforeRename = context.getPathBeforeRename(f);
-
-          final ApplyPatchForBaseRevisionTexts threeTexts = ApplyPatchForBaseRevisionTexts.create(project, f, pathBeforeRename, patch);
-          if ((threeTexts == null) || (threeTexts.getStatus() == null) || (ApplyPatchStatus.FAILURE.equals(threeTexts.getStatus()))) {
-            continue;
+        diffRequestPresentables.add(new DiffRequestPresentableProxy() {
+          @Override
+          protected DiffRequestPresentable init() {
+            if (isConflictingChange(change)) {
+              TextFilePatch patch = preloader.getPatch(shelvedChange);
+              if (patch == null) return null;
+
+              final FilePath pathBeforeRename = context.getPathBeforeRename(f);
+
+              final ApplyPatchForBaseRevisionTexts threeTexts = ApplyPatchForBaseRevisionTexts.create(project, f, pathBeforeRename, patch);
+              if ((threeTexts == null) || (threeTexts.getStatus() == null) || (ApplyPatchStatus.FAILURE.equals(threeTexts.getStatus()))) {
+                return null;
+              }
+
+              return new MergedDiffRequestPresentable(project, threeTexts, f, "Shelved Version");
+            }
+            else {
+              return new ChangeDiffRequestPresentable(project, change);
+            }
           }
-
-          diffRequestPresentables.add(new MergedDiffRequestPresentable(project, threeTexts, f, "Shelved Version"));
-        }
-        else {
-          diffRequestPresentables.add(new ChangeDiffRequestPresentable(project, change));
-        }
+        });
       }
       catch (IOException e) {
         continue;
@@ -114,7 +120,7 @@ public class DiffShelvedChangesAction extends AnAction implements DumbAware {
       ChangesViewBalloonProblemNotifier.showMe(project, "Show Diff: Cannot find base for: " + StringUtil.join(missing, ",\n"), MessageType.WARNING);
     }
 
-    ShowDiffAction.showDiffImpl(project, diffRequestPresentables, toSelectIdx, ShowDiffAction.DiffExtendUIFactory.NONE, true);
+    ShowDiffAction.showDiffImpl(project, diffRequestPresentables, toSelectIdx, ShowDiffAction.DiffExtendUIFactory.NONE, false);
   }
 
   private static class PatchesPreloader {