vcs: cleanup
authorAleksey Pivovarov <AMPivovarov@gmail.com>
Mon, 10 Aug 2015 12:22:07 +0000 (15:22 +0300)
committerAleksey Pivovarov <AMPivovarov@gmail.com>
Wed, 19 Aug 2015 12:06:56 +0000 (15:06 +0300)
platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchAction.java

index d86b54d7c5c19439d9e2dd139d8d9db4c57255a3..a18adc2e1498800a05ffe5c42632a8ca9a086085 100644 (file)
@@ -70,6 +70,18 @@ import java.util.List;
 public class ApplyPatchAction extends DumbAwareAction {
   private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.vcs.changes.patch.ApplyPatchAction");
 
+  @Override
+  public void update(AnActionEvent e) {
+    Project project = e.getData(CommonDataKeys.PROJECT);
+    if (e.getPlace().equals(ActionPlaces.PROJECT_VIEW_POPUP)) {
+      VirtualFile vFile = e.getData(CommonDataKeys.VIRTUAL_FILE);
+      e.getPresentation().setVisible(project != null && vFile != null && vFile.getFileType() == StdFileTypes.PATCH);
+    }
+    else {
+      e.getPresentation().setEnabled(project != null);
+    }
+  }
+
   public void actionPerformed(AnActionEvent e) {
     final Project project = e.getData(CommonDataKeys.PROJECT);
     if (project == null) return;
@@ -196,11 +208,6 @@ public class ApplyPatchAction extends DumbAwareAction {
     return status;
   }
 
-  private static <T extends FilePatch> void showIOException(Project project, String name, IOException e) {
-    Messages.showErrorDialog(project, VcsBundle.message("patch.apply.error", name, e.getMessage()),
-                             VcsBundle.message("patch.apply.dialog.title"));
-  }
-
   @Nullable
   private static CharSequence getBaseContents(final FilePatch patchBase, final CommitContext commitContext, final Project project) {
     final BaseRevisionTextPatchEP baseRevisionTextPatchEP = Extensions.findExtension(PatchEP.EP_NAME, project, BaseRevisionTextPatchEP.class);
@@ -211,67 +218,6 @@ public class ApplyPatchAction extends DumbAwareAction {
     return null;
   }
 
-  public static ApplyPatchStatus showBadDiffDialog(final Project project, final VirtualFile file, final ApplyPatchForBaseRevisionTexts texts, final boolean readonly)
-    throws IOException {
-    if (texts.getLocal() == null) {
-      return ApplyPatchStatus.FAILURE;
-    }
-    final DiffTool tool = DiffManager.getInstance().getDiffTool();
-
-    final SimpleDiffRequest simpleRequest = createBadDiffRequest(project, file, texts, readonly);
-    tool.show(simpleRequest);
-
-    return ApplyPatchStatus.SUCCESS;
-  }
-
-  public static SimpleDiffRequest createBadDiffRequest(final Project project,
-                                                        final VirtualFile file,
-                                                        ApplyPatchForBaseRevisionTexts texts,
-                                                        boolean readonly) {
-    final SimpleDiffRequest simpleRequest =
-      new SimpleDiffRequest(project, "Result Of Patch Apply To " + file.getName() + " (" +
-                                     (file.getParent() == null ? file.getPath() : file.getParent().getPath()) +
-                                     ")");
-    final DocumentImpl patched = new DocumentImpl(texts.getPatched());
-    patched.setReadOnly(false);
-
-    final DocumentContent mergedDocument = new DocumentContent(project, patched, file.getFileType());
-    mergedDocument.getDocument().setReadOnly(readonly);
-    simpleRequest.setContents(new SimpleContent(texts.getLocal().toString(), file.getFileType()),
-                              mergedDocument);
-    simpleRequest.setContentTitles(VcsBundle.message("diff.title.local"), "Patched (with problems)");
-    simpleRequest.addHint(DiffTool.HINT_SHOW_MODAL_DIALOG);
-    simpleRequest.addHint(DiffTool.HINT_DIFF_IS_APPROXIMATE);
-
-    if (! readonly) {
-      simpleRequest.setOnOkRunnable(new Runnable() {
-        @Override
-        public void run() {
-          ApplicationManager.getApplication().runWriteAction(new Runnable() {
-            @Override
-            public void run() {
-              final String resultText = mergedDocument.getDocument().getText();
-              final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
-              final Document document = fileDocumentManager.getDocument(file);
-              if (document == null) {
-                try {
-                  VfsUtil.saveText(file, resultText);
-                }
-                catch (IOException e) {
-                  showIOException(project, file.getName(), e);          // todo bad: we had already returned success by now
-                }
-              } else {
-                document.setText(resultText);
-                fileDocumentManager.saveDocument(document);
-              }
-            }
-          });
-        }
-      });
-    }
-    return simpleRequest;
-  }
-
   private static ApplyPatchStatus showMergeDialog(Project project,
                                                   VirtualFile file,
                                                   CharSequence content,
@@ -311,18 +257,6 @@ public class ApplyPatchAction extends DumbAwareAction {
     return ApplyPatchStatus.FAILURE;
   }
 
-  @Override
-  public void update(AnActionEvent e) {
-    Project project = e.getData(CommonDataKeys.PROJECT);
-    if (e.getPlace().equals(ActionPlaces.PROJECT_VIEW_POPUP)) {
-      VirtualFile vFile = e.getData(CommonDataKeys.VIRTUAL_FILE);
-      e.getPresentation().setVisible(project != null && vFile != null && vFile.getFileType() == StdFileTypes.PATCH);
-    }
-    else {
-      e.getPresentation().setEnabled(project != null);
-    }
-  }
-
   public static class ApplyPatchMergeRequestFactory implements PatchMergeRequestFactory {
     private final boolean myReadOnly;
 
@@ -356,4 +290,70 @@ public class ApplyPatchAction extends DumbAwareAction {
       return request;
     }
   }
+
+  private static ApplyPatchStatus showBadDiffDialog(final Project project, final VirtualFile file, final ApplyPatchForBaseRevisionTexts texts, final boolean readonly)
+    throws IOException {
+    if (texts.getLocal() == null) {
+      return ApplyPatchStatus.FAILURE;
+    }
+    final DiffTool tool = DiffManager.getInstance().getDiffTool();
+
+    final SimpleDiffRequest simpleRequest = createBadDiffRequest(project, file, texts, readonly);
+    tool.show(simpleRequest);
+
+    return ApplyPatchStatus.SUCCESS;
+  }
+
+  public static SimpleDiffRequest createBadDiffRequest(final Project project,
+                                                       final VirtualFile file,
+                                                       ApplyPatchForBaseRevisionTexts texts,
+                                                       boolean readonly) {
+    final SimpleDiffRequest simpleRequest =
+      new SimpleDiffRequest(project, "Result Of Patch Apply To " + file.getName() + " (" +
+                                     (file.getParent() == null ? file.getPath() : file.getParent().getPath()) +
+                                     ")");
+    final DocumentImpl patched = new DocumentImpl(texts.getPatched());
+    patched.setReadOnly(false);
+
+    final DocumentContent mergedDocument = new DocumentContent(project, patched, file.getFileType());
+    mergedDocument.getDocument().setReadOnly(readonly);
+    simpleRequest.setContents(new SimpleContent(texts.getLocal().toString(), file.getFileType()),
+                              mergedDocument);
+    simpleRequest.setContentTitles(VcsBundle.message("diff.title.local"), "Patched (with problems)");
+    simpleRequest.addHint(DiffTool.HINT_SHOW_MODAL_DIALOG);
+    simpleRequest.addHint(DiffTool.HINT_DIFF_IS_APPROXIMATE);
+
+    if (! readonly) {
+      simpleRequest.setOnOkRunnable(new Runnable() {
+        @Override
+        public void run() {
+          ApplicationManager.getApplication().runWriteAction(new Runnable() {
+            @Override
+            public void run() {
+              final String resultText = mergedDocument.getDocument().getText();
+              final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
+              final Document document = fileDocumentManager.getDocument(file);
+              if (document == null) {
+                try {
+                  VfsUtil.saveText(file, resultText);
+                }
+                catch (IOException e) {
+                  showIOException(project, file.getName(), e);          // todo bad: we had already returned success by now
+                }
+              } else {
+                document.setText(resultText);
+                fileDocumentManager.saveDocument(document);
+              }
+            }
+          });
+        }
+      });
+    }
+    return simpleRequest;
+  }
+
+  private static <T extends FilePatch> void showIOException(Project project, String name, IOException e) {
+    Messages.showErrorDialog(project, VcsBundle.message("patch.apply.error", name, e.getMessage()),
+                             VcsBundle.message("patch.apply.dialog.title"));
+  }
 }