use of com.intellij.openapi.command.WriteCommandAction in project android by JetBrains.
the class NlDropListener method morphReceiverIntoViewGroup.
/**
* Morph the receiver into a constraint layout and add the dragged component to it.
*
* @param model {@link NlComponentTree#getDesignerModel()}
*/
private void morphReceiverIntoViewGroup(@NotNull NlModel model) {
final AttributesTransaction transaction = myDragReceiver.startAttributeTransaction();
for (AttributeSnapshot attribute : myDragReceiver.getAttributes()) {
if (!TOOLS_PREFIX.equals(attribute.prefix) && !ourCopyableAttributes.contains(attribute.name) && attribute.namespace != null) {
transaction.removeAttribute(attribute.namespace, attribute.name);
}
}
new WriteCommandAction(model.getProject(), model.getFile()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
final XmlTag tag = myDragReceiver.getTag();
tag.setName(CONSTRAINT_LAYOUT);
myDragReceiver.setTag(tag);
transaction.commit();
}
}.execute();
}
use of com.intellij.openapi.command.WriteCommandAction in project android by JetBrains.
the class GuidelineCycleTarget method mouseRelease.
@Override
public void mouseRelease(int x, int y, @Nullable Target closestTarget) {
AttributesTransaction attributes = myComponent.getNlComponent().startAttributeTransaction();
String begin = attributes.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_BEGIN);
String end = attributes.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_END);
String percent = attributes.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_PERCENT);
SceneComponent parent = myComponent.getParent();
int value = myComponent.getDrawY() - parent.getDrawY();
int dimension = parent.getDrawHeight();
if (!myIsHorizontal) {
value = myComponent.getDrawX() - parent.getDrawX();
dimension = parent.getDrawWidth();
}
if (begin != null) {
setEnd(attributes, dimension - value);
} else if (end != null) {
setPercent(attributes, value / (float) dimension);
} else if (percent != null) {
setBegin(attributes, value);
}
attributes.apply();
NlModel nlModel = myComponent.getNlComponent().getModel();
Project project = nlModel.getProject();
XmlFile file = nlModel.getFile();
String label = "Cycle Guideline";
WriteCommandAction action = new WriteCommandAction(project, label, file) {
@Override
protected void run(@NotNull Result result) throws Throwable {
attributes.commit();
}
};
action.execute();
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-plugins by JetBrains.
the class DartIntroduceHandler method performReplace.
@Nullable
private SmartPsiElementPointer<PsiElement> performReplace(@NotNull final PsiElement declaration, final DartIntroduceOperation operation) {
final DartExpression expression = operation.getInitializer();
final Project project = operation.getProject();
return new WriteCommandAction<SmartPsiElementPointer<PsiElement>>(project, expression.getContainingFile()) {
protected void run(@NotNull final Result<SmartPsiElementPointer<PsiElement>> result) throws Throwable {
final PsiElement createdDeclaration = addDeclaration(operation, declaration);
if (createdDeclaration != null) {
result.setResult(SmartPointerManager.getInstance(project).createSmartPsiElementPointer(createdDeclaration));
modifyDeclaration(createdDeclaration);
}
PsiElement newExpression = createExpression(project, operation.getName());
if (operation.isReplaceAll()) {
List<PsiElement> newOccurrences = new ArrayList<>();
for (PsiElement occurrence : operation.getOccurrences()) {
final PsiElement replaced = replaceExpression(occurrence, newExpression, operation);
if (replaced != null) {
newOccurrences.add(replaced);
}
}
operation.setOccurrences(newOccurrences);
} else {
final PsiElement replaced = replaceExpression(expression, newExpression, operation);
operation.setOccurrences(Collections.singletonList(replaced));
}
postRefactoring(operation.getElement());
}
}.execute().getResultObject();
}
use of com.intellij.openapi.command.WriteCommandAction in project android by JetBrains.
the class NlComponent method ensureLiveId.
/**
* Ensure that there's a id, if not execute a write command to add
* the id to the component.
* @return
*/
public String ensureLiveId() {
String id = getId();
if (id != null) {
return id;
}
AttributesTransaction attributes = startAttributeTransaction();
id = assignId();
attributes.apply();
WriteCommandAction action = new WriteCommandAction(getModel().getProject(), "Added id", getModel().getFile()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
attributes.commit();
}
};
action.execute();
return id;
}
use of com.intellij.openapi.command.WriteCommandAction in project android by JetBrains.
the class AndroidInferNullityAnnotationAction method applyRunnable.
// Intellij code from InferNullityAnnotationsAction.
private static Runnable applyRunnable(Project project, Computable<UsageInfo[]> computable) {
return () -> {
LocalHistoryAction action = LocalHistory.getInstance().startAction(INFER_NULLITY_ANNOTATIONS);
try {
new WriteCommandAction(project, INFER_NULLITY_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) {
ContainerUtil.addIfNotNull(elements, element.getContainingFile());
}
}
if (!FileModificationService.getInstance().preparePsiElementsForWrite(elements))
return;
SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, INFER_NULLITY_ANNOTATIONS, false);
progressTask.setMinIterationTime(200);
progressTask.setTask(new AnnotateTask(project, progressTask, infos));
ProgressManager.getInstance().run(progressTask);
} else {
NullityInferrer.nothingFoundMessage(project);
}
}
}.execute();
} finally {
action.finish();
}
};
}
Aggregations