use of com.intellij.openapi.command.undo.GlobalUndoableAction in project intellij-community by JetBrains.
the class DynamicDialog method doOKAction.
@Override
protected void doOKAction() {
super.doOKAction();
mySettings.setContainingClassName((String) myClassComboBox.getSelectedItem());
mySettings.setStatic(myStaticCheckBox.isSelected());
GrTypeElement typeElement = getEnteredTypeName();
if (typeElement == null) {
mySettings.setType(CommonClassNames.JAVA_LANG_OBJECT);
} else {
PsiType type = typeElement.getType();
if (type instanceof PsiPrimitiveType) {
type = TypesUtil.boxPrimitiveType(type, typeElement.getManager(), ProjectScope.getAllScope(myProject));
}
final String typeQualifiedName = type.getCanonicalText();
if (typeQualifiedName != null) {
mySettings.setType(typeQualifiedName);
} else {
mySettings.setType(type.getPresentableText());
}
}
final Document document = PsiDocumentManager.getInstance(myProject).getDocument(myContext.getContainingFile());
CommandProcessor.getInstance().executeCommand(myProject, () -> {
UndoManager.getInstance(myProject).undoableActionPerformed(new GlobalUndoableAction(document) {
@Override
public void undo() throws UnexpectedUndoException {
final DItemElement itemElement;
if (mySettings.isMethod()) {
final List<ParamInfo> myPairList = mySettings.getParams();
final String[] argumentsTypes = QuickfixUtil.getArgumentsTypes(myPairList);
itemElement = myDynamicManager.findConcreteDynamicMethod(mySettings.getContainingClassName(), mySettings.getName(), argumentsTypes);
} else {
itemElement = myDynamicManager.findConcreteDynamicProperty(mySettings.getContainingClassName(), mySettings.getName());
}
if (itemElement == null) {
Messages.showWarningDialog(myProject, GroovyInspectionBundle.message("Cannot.perform.undo.operation"), GroovyInspectionBundle.message("Undo.disable"));
return;
}
final DClassElement classElement = myDynamicManager.getClassElementByItem(itemElement);
if (classElement == null) {
Messages.showWarningDialog(myProject, GroovyInspectionBundle.message("Cannot.perform.undo.operation"), GroovyInspectionBundle.message("Undo.disable"));
return;
}
removeElement(itemElement);
if (classElement.getMethods().isEmpty() && classElement.getProperties().isEmpty()) {
myDynamicManager.removeClassElement(classElement);
}
}
@Override
public void redo() throws UnexpectedUndoException {
addElement(mySettings);
}
});
addElement(mySettings);
}, "Add dynamic element", null);
}
use of com.intellij.openapi.command.undo.GlobalUndoableAction in project intellij-community by JetBrains.
the class PsiPackageImplementationHelperImpl method handleQualifiedNameChange.
@Override
public void handleQualifiedNameChange(@NotNull final PsiPackage psiPackage, @NotNull final String newQualifiedName) {
ApplicationManager.getApplication().assertWriteAccessAllowed();
final String oldQualifedName = psiPackage.getQualifiedName();
final boolean anyChanged = changePackagePrefixes(psiPackage, oldQualifedName, newQualifiedName);
if (anyChanged) {
UndoManager.getInstance(psiPackage.getProject()).undoableActionPerformed(new GlobalUndoableAction() {
@Override
public void undo() {
changePackagePrefixes(psiPackage, newQualifiedName, oldQualifedName);
}
@Override
public void redo() {
changePackagePrefixes(psiPackage, oldQualifedName, newQualifiedName);
}
});
}
}
use of com.intellij.openapi.command.undo.GlobalUndoableAction in project intellij-community by JetBrains.
the class ChangeFileEncodingAction method chosen.
// returns true if charset was changed, false if failed
protected boolean chosen(final Document document, final Editor editor, @Nullable final VirtualFile virtualFile, byte[] bytes, @NotNull final Charset charset) {
if (virtualFile == null)
return false;
String text = document.getText();
EncodingUtil.Magic8 isSafeToConvert = EncodingUtil.isSafeToConvertTo(virtualFile, text, bytes, charset);
EncodingUtil.Magic8 isSafeToReload = EncodingUtil.isSafeToReloadIn(virtualFile, text, bytes, charset);
final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile);
final Charset oldCharset = virtualFile.getCharset();
final Runnable undo;
final Runnable redo;
if (isSafeToConvert == EncodingUtil.Magic8.ABSOLUTELY && isSafeToReload == EncodingUtil.Magic8.ABSOLUTELY) {
//change and forget
undo = () -> EncodingManager.getInstance().setEncoding(virtualFile, oldCharset);
redo = () -> EncodingManager.getInstance().setEncoding(virtualFile, charset);
} else {
IncompatibleEncodingDialog dialog = new IncompatibleEncodingDialog(virtualFile, charset, isSafeToReload, isSafeToConvert);
dialog.show();
if (dialog.getExitCode() == IncompatibleEncodingDialog.RELOAD_EXIT_CODE) {
undo = () -> EncodingUtil.reloadIn(virtualFile, oldCharset);
redo = () -> EncodingUtil.reloadIn(virtualFile, charset);
} else if (dialog.getExitCode() == IncompatibleEncodingDialog.CONVERT_EXIT_CODE) {
undo = () -> EncodingUtil.saveIn(document, editor, virtualFile, oldCharset);
redo = () -> EncodingUtil.saveIn(document, editor, virtualFile, charset);
} else {
return false;
}
}
final UndoableAction action = new GlobalUndoableAction(virtualFile) {
@Override
public void undo() {
// invoke later because changing document inside undo/redo is not allowed
Application application = ApplicationManager.getApplication();
application.invokeLater(undo, ModalityState.NON_MODAL, (project == null ? application : project).getDisposed());
}
@Override
public void redo() {
// invoke later because changing document inside undo/redo is not allowed
Application application = ApplicationManager.getApplication();
application.invokeLater(redo, ModalityState.NON_MODAL, (project == null ? application : project).getDisposed());
}
};
redo.run();
CommandProcessor.getInstance().executeCommand(project, () -> {
UndoManager undoManager = project == null ? UndoManager.getGlobalInstance() : UndoManager.getInstance(project);
undoManager.undoableActionPerformed(action);
}, "Change encoding for '" + virtualFile.getName() + "'", null, UndoConfirmationPolicy.REQUEST_CONFIRMATION);
return true;
}
use of com.intellij.openapi.command.undo.GlobalUndoableAction in project intellij-community by JetBrains.
the class Configuration method replaceInjectionsWithUndo.
public static <T> void replaceInjectionsWithUndo(final Project project, final T add, final T remove, final List<? extends PsiElement> psiElementsToRemove, final PairProcessor<T, T> actualProcessor) {
final UndoableAction action = new GlobalUndoableAction() {
@Override
public void undo() {
actualProcessor.process(remove, add);
}
@Override
public void redo() {
actualProcessor.process(add, remove);
}
};
final List<PsiFile> psiFiles = ContainerUtil.mapNotNull(psiElementsToRemove, o -> o instanceof PsiCompiledElement ? null : o.getContainingFile());
new WriteCommandAction.Simple(project, "Language Injection Configuration Update", PsiUtilCore.toPsiFileArray(psiFiles)) {
@Override
public void run() {
for (PsiElement annotation : psiElementsToRemove) {
if (!annotation.isValid())
continue;
annotation.delete();
}
actualProcessor.process(add, remove);
UndoManager.getInstance(project).undoableActionPerformed(action);
}
@Override
protected UndoConfirmationPolicy getUndoConfirmationPolicy() {
return UndoConfirmationPolicy.REQUEST_CONFIRMATION;
}
}.execute();
}
use of com.intellij.openapi.command.undo.GlobalUndoableAction in project intellij-community by JetBrains.
the class ClearContextAction method actionPerformed.
public void actionPerformed(final AnActionEvent e) {
final Project project = getProject(e);
GlobalUndoableAction action = new GlobalUndoableAction() {
public void undo() throws UnexpectedUndoException {
}
public void redo() throws UnexpectedUndoException {
WorkingContextManager.getInstance(project).clearContext();
}
};
UndoableCommand.execute(project, action, "Clear context", "Context");
}
Aggregations