use of com.intellij.openapi.command.UndoConfirmationPolicy 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.UndoConfirmationPolicy in project android by JetBrains.
the class AndroidExtractStyleAction method doExtractStyle.
@Nullable
public static String doExtractStyle(@NotNull Module module, @NotNull final XmlTag viewTag, final boolean addStyleAttributeToTag, @Nullable MyTestConfig testConfig) {
final PsiFile file = viewTag.getContainingFile();
if (file == null) {
return null;
}
final String dialogTitle = AndroidBundle.message("android.extract.style.title");
final String fileName = AndroidResourceUtil.getDefaultResourceFileName(ResourceType.STYLE);
assert fileName != null;
final List<String> dirNames = Collections.singletonList(ResourceFolderType.VALUES.getName());
final List<XmlAttribute> extractableAttributes = getExtractableAttributes(viewTag);
final Project project = module.getProject();
if (extractableAttributes.size() == 0) {
AndroidUtils.reportError(project, "The tag doesn't contain any attributes that can be extracted", dialogTitle);
return null;
}
final LayoutViewElement viewElement = getLayoutViewElement(viewTag);
assert viewElement != null;
final ResourceValue parentStyleValue = viewElement.getStyle().getValue();
final String parentStyle;
boolean supportImplicitParent = false;
if (parentStyleValue != null) {
parentStyle = parentStyleValue.getResourceName();
if (ResourceType.STYLE != parentStyleValue.getType() || parentStyle == null || parentStyle.length() == 0) {
AndroidUtils.reportError(project, "Invalid parent style reference " + parentStyleValue.toString(), dialogTitle);
return null;
}
supportImplicitParent = parentStyleValue.getNamespace() == null;
} else {
parentStyle = null;
}
final String styleName;
final List<XmlAttribute> styledAttributes;
final VirtualFile chosenDirectory;
final boolean searchStyleApplications;
if (testConfig == null) {
final ExtractStyleDialog dialog = new ExtractStyleDialog(module, fileName, supportImplicitParent ? parentStyle : null, dirNames, extractableAttributes);
dialog.setTitle(dialogTitle);
if (!dialog.showAndGet()) {
return null;
}
searchStyleApplications = dialog.isToSearchStyleApplications();
chosenDirectory = dialog.getResourceDirectory();
if (chosenDirectory == null) {
AndroidUtils.reportError(project, AndroidBundle.message("check.resource.dir.error", module.getName()));
return null;
}
styledAttributes = dialog.getStyledAttributes();
styleName = dialog.getStyleName();
} else {
testConfig.validate(extractableAttributes);
chosenDirectory = testConfig.getResourceDirectory();
styleName = testConfig.getStyleName();
final Set<String> attrsToExtract = new HashSet<String>(Arrays.asList(testConfig.getAttributesToExtract()));
styledAttributes = new ArrayList<XmlAttribute>();
for (XmlAttribute attribute : extractableAttributes) {
if (attrsToExtract.contains(attribute.getName())) {
styledAttributes.add(attribute);
}
}
searchStyleApplications = false;
}
final boolean[] success = { false };
final Ref<Style> createdStyleRef = Ref.create();
final boolean finalSupportImplicitParent = supportImplicitParent;
new WriteCommandAction(project, "Extract Android Style '" + styleName + "'", file) {
@Override
protected void run(@NotNull final Result result) throws Throwable {
final List<XmlAttribute> attributesToDelete = new ArrayList<XmlAttribute>();
if (!AndroidResourceUtil.createValueResource(project, chosenDirectory, styleName, null, ResourceType.STYLE, fileName, dirNames, new Processor<ResourceElement>() {
@Override
public boolean process(ResourceElement element) {
assert element instanceof Style;
final Style style = (Style) element;
createdStyleRef.set(style);
for (XmlAttribute attribute : styledAttributes) {
if (SdkConstants.NS_RESOURCES.equals(attribute.getNamespace())) {
final StyleItem item = style.addItem();
item.getName().setStringValue("android:" + attribute.getLocalName());
item.setStringValue(attribute.getValue());
attributesToDelete.add(attribute);
}
}
if (parentStyleValue != null && (!finalSupportImplicitParent || !styleName.startsWith(parentStyle + "."))) {
final String aPackage = parentStyleValue.getNamespace();
style.getParentStyle().setStringValue((aPackage != null ? aPackage + ":" : "") + parentStyle);
}
return true;
}
})) {
return;
}
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
for (XmlAttribute attribute : attributesToDelete) {
attribute.delete();
}
if (addStyleAttributeToTag) {
final LayoutViewElement viewElement = getLayoutViewElement(viewTag);
assert viewElement != null;
viewElement.getStyle().setStringValue("@style/" + styleName);
}
}
});
success[0] = true;
}
@Override
protected UndoConfirmationPolicy getUndoConfirmationPolicy() {
return UndoConfirmationPolicy.REQUEST_CONFIRMATION;
}
}.execute();
if (!success[0]) {
return null;
}
final Style createdStyle = createdStyleRef.get();
final XmlTag createdStyleTag = createdStyle != null ? createdStyle.getXmlTag() : null;
if (createdStyleTag != null) {
final AndroidFindStyleApplicationsAction.MyStyleData createdStyleData = AndroidFindStyleApplicationsAction.getStyleData(createdStyleTag);
if (createdStyleData != null && searchStyleApplications) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
AndroidFindStyleApplicationsAction.doRefactoringForTag(createdStyleTag, createdStyleData, file, null);
}
});
}
}
return styleName;
}
use of com.intellij.openapi.command.UndoConfirmationPolicy in project android by JetBrains.
the class AndroidInlineUtil method doInlineStyleDeclaration.
static void doInlineStyleDeclaration(@NotNull Project project, @NotNull MyStyleData data, @Nullable final StyleUsageData usageData, @NotNull ErrorReporter errorReporter, @Nullable AndroidInlineTestConfig testConfig) {
final Style style = data.myStyleElement;
final Map<AndroidAttributeInfo, String> attributeValues = AndroidRefactoringUtil.computeAttributeMap(style, errorReporter, AndroidBundle.message("android.inline.style.title"));
if (attributeValues == null) {
return;
}
final StyleRefData parentStyleRef = AndroidRefactoringUtil.getParentStyle(style);
boolean inlineThisOnly;
if (testConfig != null) {
inlineThisOnly = testConfig.isInlineThisOnly();
} else {
final boolean invokedOnReference = usageData != null;
final AndroidInlineStyleDialog dialog = new AndroidInlineStyleDialog(project, data.myReferredElement, style.getXmlTag(), data.myStyleName, attributeValues, parentStyleRef, invokedOnReference, invokedOnReference);
if (!dialog.showAndGet()) {
return;
}
inlineThisOnly = dialog.isInlineThisOnly();
}
if (inlineThisOnly) {
assert usageData != null;
final PsiFile file = usageData.getFile();
if (file == null) {
return;
}
new WriteCommandAction(project, AndroidBundle.message("android.inline.style.command.name", data.myStyleName), file) {
@Override
protected void run(@NotNull final Result result) throws Throwable {
usageData.inline(attributeValues, parentStyleRef);
}
@Override
protected UndoConfirmationPolicy getUndoConfirmationPolicy() {
return UndoConfirmationPolicy.REQUEST_CONFIRMATION;
}
}.execute();
} else if (testConfig != null) {
final AndroidInlineAllStyleUsagesProcessor processor = new AndroidInlineAllStyleUsagesProcessor(project, data.myReferredElement, style.getXmlTag(), data.myStyleName, attributeValues, parentStyleRef, testConfig);
processor.setPreviewUsages(false);
processor.run();
}
}
use of com.intellij.openapi.command.UndoConfirmationPolicy in project android by JetBrains.
the class AndroidDbManager method processAddOrRemove.
public void processAddOrRemove(final AndroidDataSource dataSource, final boolean add) {
final Project project = myDbFacade.getProject();
final UndoableAction action = new GlobalUndoableAction() {
public void undo() throws UnexpectedUndoException {
if (add) {
removeDataSourceInner(project, dataSource);
} else {
addDataSourceInner(project, dataSource);
}
}
public void redo() throws UnexpectedUndoException {
if (add) {
addDataSourceInner(project, dataSource);
} else {
removeDataSourceInner(project, dataSource);
}
}
};
final String commandName = add ? DatabaseMessages.message("command.name.add.data.source") : DatabaseMessages.message("command.name.remove.data.source");
new WriteCommandAction(project, commandName) {
protected void run(@NotNull final Result result) throws Throwable {
action.redo();
UndoManager.getInstance(project).undoableActionPerformed(action);
}
@Override
protected UndoConfirmationPolicy getUndoConfirmationPolicy() {
return UndoConfirmationPolicy.REQUEST_CONFIRMATION;
}
}.execute();
}
Aggregations