use of com.intellij.openapi.command.WriteCommandAction in project android by JetBrains.
the class ResizeInteraction method moveTo.
private void moveTo(@SwingCoordinate int x, @SwingCoordinate int y, @InputEventMask final int modifiers, boolean commit) {
if (myResizeHandler == null) {
return;
}
final int ax = Coordinates.getAndroidX(myScreenView, x);
final int ay = Coordinates.getAndroidY(myScreenView, y);
final int deltaX = Coordinates.getAndroidDimension(myScreenView, x - myStartX);
final int deltaY = Coordinates.getAndroidDimension(myScreenView, y - myStartY);
final Rectangle newBounds = getNewBounds(new Rectangle(myComponent.x, myComponent.y, myComponent.w, myComponent.h), deltaX, deltaY);
myResizeHandler.update(ax, ay, modifiers, newBounds);
if (commit) {
NlModel model = myScreenView.getModel();
Project project = model.getFacet().getModule().getProject();
XmlFile file = model.getFile();
String label = "Resize";
WriteCommandAction action = new WriteCommandAction(project, label, file) {
@Override
protected void run(@NotNull Result result) throws Throwable {
myResizeHandler.commit(ax, ay, modifiers, newBounds);
}
};
action.execute();
model.notifyModified(NlModel.ChangeType.RESIZE_COMMIT);
}
myScreenView.getSurface().repaint();
}
use of com.intellij.openapi.command.WriteCommandAction in project android by JetBrains.
the class LayoutMetadata method setProperty.
/**
* Sets the given property of the given DOM node to a given value, or if null clears
* the property.
*/
public static void setProperty(@NotNull final Project project, @Nullable String title, @NotNull final XmlFile file, @NotNull final XmlTag element, @NotNull final String name, @Nullable final String namespace, @Nullable final String value) {
String capitalizedName = StringUtil.capitalize(name);
if (title == null) {
title = String.format(value != null ? "Set %1$s" : "Clear %1$s", capitalizedName);
}
WriteCommandAction<Void> action = new WriteCommandAction<Void>(project, title, file) {
@Override
protected void run(@NotNull Result<Void> result) throws Throwable {
if (value == null) {
// Clear attribute
XmlAttribute attribute;
if (namespace != null) {
attribute = element.getAttribute(name, namespace);
} else {
attribute = element.getAttribute(name);
}
if (attribute != null) {
attribute.delete();
}
} else {
if (namespace != null) {
AndroidResourceUtil.ensureNamespaceImported(file, namespace, null);
element.setAttribute(name, namespace, value);
} else {
element.setAttribute(name, value);
}
}
}
};
action.execute();
// Also set the values on the same elements in any resource variations
// of the same layout
// TODO: This should be done after a brief delay, say 50ms
final List<XmlTag> list = ApplicationManager.getApplication().runReadAction(new Computable<List<XmlTag>>() {
@Override
@Nullable
public List<XmlTag> compute() {
// Look up the id of the element, if any
String id = stripIdPrefix(element.getAttributeValue(ATTR_ID, ANDROID_URI));
if (id.isEmpty()) {
return null;
}
VirtualFile layoutFile = file.getVirtualFile();
if (layoutFile != null) {
final List<VirtualFile> variations = ResourceHelper.getResourceVariations(layoutFile, false);
if (variations.isEmpty()) {
return null;
}
PsiManager manager = PsiManager.getInstance(project);
List<XmlTag> list = Lists.newArrayList();
for (VirtualFile file : variations) {
PsiFile psiFile = manager.findFile(file);
if (psiFile == null) {
continue;
}
for (XmlTag tag : PsiTreeUtil.findChildrenOfType(psiFile, XmlTag.class)) {
XmlAttribute attribute = tag.getAttribute(ATTR_ID, ANDROID_URI);
if (attribute == null || attribute.getValue() == null) {
continue;
}
if (attribute.getValue().endsWith(id) && id.equals(stripIdPrefix(attribute.getValue()))) {
list.add(tag);
break;
}
}
}
return list;
}
return null;
}
});
if (list != null && !list.isEmpty()) {
List<PsiFile> affectedFiles = Lists.newArrayList();
for (XmlTag tag : list) {
PsiFile psiFile = tag.getContainingFile();
if (psiFile != null) {
affectedFiles.add(psiFile);
}
}
action = new WriteCommandAction<Void>(project, title, affectedFiles.toArray(new PsiFile[affectedFiles.size()])) {
@Override
protected void run(@NotNull Result<Void> result) throws Throwable {
for (XmlTag tag : list) {
if (value == null) {
// Clear attribute
XmlAttribute attribute;
if (namespace != null) {
attribute = tag.getAttribute(name, namespace);
} else {
attribute = tag.getAttribute(name);
}
if (attribute != null) {
attribute.delete();
}
} else {
if (namespace != null) {
AndroidResourceUtil.ensureNamespaceImported(file, namespace, null);
tag.setAttribute(name, namespace, value);
} else {
tag.setAttribute(name, value);
}
}
}
}
};
action.execute();
}
}
use of com.intellij.openapi.command.WriteCommandAction in project android by JetBrains.
the class NewAndroidComponentDialog method addInflateStatement.
private static void addInflateStatement(final PsiCodeBlock body, final String layoutFieldRef) {
final Project project = body.getProject();
final PsiStatement[] statements = body.getStatements();
if (statements.length == 1) {
final PsiStatement statement = statements[0];
new WriteCommandAction(project, body.getContainingFile()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
final PsiStatement newStatement = PsiElementFactory.SERVICE.getInstance(project).createStatementFromText("return inflater.inflate(" + layoutFieldRef + ", container, false);", body);
statement.replace(newStatement);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(body);
CodeStyleManager.getInstance(project).reformat(body);
}
}.execute();
}
}
use of com.intellij.openapi.command.WriteCommandAction in project android by JetBrains.
the class BaseNamedConfigurable method apply.
@Override
public void apply() throws ConfigurationException {
if (myModule.isModified()) {
GradleBuildModel parsedModel = myModule.getParsedModel();
if (parsedModel != null && parsedModel.isModified()) {
String name = String.format("Applying changes to module '%1$s'", myModule.getName());
new WriteCommandAction(myModule.getParent().getResolvedModel(), name) {
@Override
protected void run(@NotNull Result result) throws Throwable {
parsedModel.applyChanges();
myModule.setModified(false);
}
}.execute();
}
}
}
use of com.intellij.openapi.command.WriteCommandAction in project android by JetBrains.
the class InferSupportAnnotationsAction method applyRunnable.
private static Runnable applyRunnable(Project project, Computable<UsageInfo[]> computable) {
return () -> {
LocalHistoryAction action = LocalHistory.getInstance().startAction(INFER_SUPPORT_ANNOTATIONS);
try {
new WriteCommandAction(project, INFER_SUPPORT_ANNOTATIONS) {
@Override
protected void run(@NotNull Result result) throws Throwable {
UsageInfo[] infos = computable.compute();
if (infos.length > 0) {
Set<PsiElement> elements = new LinkedHashSet<>();
for (UsageInfo info : infos) {
PsiElement element = info.getElement();
if (element != null) {
PsiFile containingFile = element.getContainingFile();
// Skip results in .class files; these are typically from extracted AAR files
VirtualFile virtualFile = containingFile.getVirtualFile();
if (virtualFile.getFileType().isBinary()) {
continue;
}
ContainerUtil.addIfNotNull(elements, containingFile);
}
}
if (!FileModificationService.getInstance().preparePsiElementsForWrite(elements))
return;
SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, INFER_SUPPORT_ANNOTATIONS, false);
progressTask.setMinIterationTime(200);
progressTask.setTask(new AnnotateTask(project, progressTask, infos));
ProgressManager.getInstance().run(progressTask);
} else {
InferSupportAnnotations.nothingFoundMessage(project);
}
}
}.execute();
} finally {
action.finish();
}
};
}
Aggregations