public boolean execute(boolean drop, boolean isInsideStartFinishGroup) {
    if (!myUndoableGroup.isUndoable()) {
      reportCannotUndo(
          CommonBundle.message("cannot.undo.error.contains.nonundoable.changes.message"),
          myUndoableGroup.getAffectedDocuments());
      return false;
    }

    Set<DocumentReference> clashing = getStackHolder().collectClashingActions(myUndoableGroup);
    if (!clashing.isEmpty()) {
      reportCannotUndo(
          CommonBundle.message("cannot.undo.error.other.affected.files.changed.message"), clashing);
      return false;
    }

    if (!isInsideStartFinishGroup && myUndoableGroup.shouldAskConfirmation(isRedo())) {
      if (!askUser()) return false;
    } else {
      if (restore(getBeforeState())) {
        setBeforeState(new EditorAndState(myEditor, myEditor.getState(FileEditorStateLevel.UNDO)));
        return true;
      }
    }

    Collection<VirtualFile> readOnlyFiles = collectReadOnlyAffectedFiles();
    if (!readOnlyFiles.isEmpty()) {
      final Project project = myManager.getProject();
      final VirtualFile[] files = VfsUtil.toVirtualFileArray(readOnlyFiles);

      if (project == null) {
        return false;
      }

      final ReadonlyStatusHandler.OperationStatus operationStatus =
          ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(files);
      if (operationStatus.hasReadonlyFiles()) {
        return false;
      }
    }

    Collection<Document> readOnlyDocuments = collectReadOnlyDocuments();
    if (!readOnlyDocuments.isEmpty()) {
      for (Document document : readOnlyDocuments) {
        document.fireReadOnlyModificationAttempt();
      }
      return false;
    }

    getStackHolder().removeFromStacks(myUndoableGroup);
    if (!drop) {
      getReverseStackHolder().addToStacks(myUndoableGroup);
    }

    performAction();

    restore(getAfterState());

    return true;
  }
 private void reportCannotUndo(String message, Collection<DocumentReference> problemFiles) {
   if (ApplicationManager.getApplication().isUnitTestMode()) {
     throw new RuntimeException(
         message
             + "\n"
             + StringUtil.join(
                 problemFiles, StringUtil.createToStringFunction(DocumentReference.class), "\n"));
   }
   new CannotUndoReportDialog(myManager.getProject(), message, problemFiles).show();
 }
  private boolean askUser() {
    String actionText = getActionName(myUndoableGroup.getCommandName());

    if (actionText.length() > 80) {
      actionText = actionText.substring(0, 80) + "... ";
    }

    return Messages.showOkCancelDialog(
            myManager.getProject(), actionText + "?", getActionName(), Messages.getQuestionIcon())
        == Messages.OK;
  }
Exemple #4
0
 private static DocumentReference createReferenceOrGetOriginal(Document d) {
   Document original = UndoManagerImpl.getOriginal(d);
   return DocumentReferenceManager.getInstance().create(original);
 }
 private Set<DocumentReference> getDecRefs() {
   return myEditor == null
       ? Collections.<DocumentReference>emptySet()
       : UndoManagerImpl.getDocumentReferences(myEditor);
 }