use of com.intellij.openapi.application.WriteAction in project intellij-community by JetBrains.
the class ManifestFileUtil method createManifestFile.
@Nullable
public static VirtualFile createManifestFile(@NotNull final VirtualFile directory, @NotNull final Project project) {
ApplicationManager.getApplication().assertIsDispatchThread();
final Ref<IOException> exc = Ref.create(null);
final VirtualFile file = new WriteAction<VirtualFile>() {
protected void run(@NotNull final Result<VirtualFile> result) {
VirtualFile dir = directory;
try {
if (!dir.getName().equals(MANIFEST_DIR_NAME)) {
dir = VfsUtil.createDirectoryIfMissing(dir, MANIFEST_DIR_NAME);
}
final VirtualFile file = dir.createChildData(this, MANIFEST_FILE_NAME);
final OutputStream output = file.getOutputStream(this);
try {
final Manifest manifest = new Manifest();
ManifestBuilder.setVersionAttribute(manifest.getMainAttributes());
manifest.write(output);
} finally {
output.close();
}
result.setResult(file);
} catch (IOException e) {
exc.set(e);
}
}
}.execute().getResultObject();
final IOException exception = exc.get();
if (exception != null) {
LOG.info(exception);
Messages.showErrorDialog(project, exception.getMessage(), CommonBundle.getErrorTitle());
return null;
}
return file;
}
use of com.intellij.openapi.application.WriteAction in project intellij-community by JetBrains.
the class PlainTextUsagesTest method testXmlOutOfScope.
public void testXmlOutOfScope() throws Exception {
final VirtualFile resourcesDir = ModuleRootManager.getInstance(myModule).getSourceRoots()[0].findChild("resources");
assertNotNull(resourcesDir);
new WriteAction() {
@Override
protected void run(@NotNull final Result result) {
final Module module = createModule("res");
PsiTestUtil.addContentRoot(module, resourcesDir);
final VirtualFile child = resourcesDir.findChild("Test.xml");
assert child != null;
assertSame(module, ModuleUtil.findModuleForFile(child, getProject()));
}
}.execute();
PsiClass aClass = myJavaFacade.findClass("com.Foo", GlobalSearchScope.allScope(myProject));
assertNotNull(aClass);
doTest("com.Foo", aClass, new String[] { "Test.xml" }, new int[] { 28 }, new int[] { 35 });
}
use of com.intellij.openapi.application.WriteAction in project intellij-community by JetBrains.
the class SymlinkHandlingTest method testLinkDeleteIsSafe.
@Test
public void testLinkDeleteIsSafe() throws Exception {
File targetFile = myTempDir.newFile("target");
File linkFile = createSymLink(targetFile.getPath(), myTempDir.getRoot() + "/link");
VirtualFile linkVFile = refreshAndFind(linkFile);
assertTrue("link=" + linkFile + ", vLink=" + linkVFile, linkVFile != null && !linkVFile.isDirectory() && linkVFile.is(VFileProperty.SYMLINK));
new WriteAction() {
@Override
protected void run(@NotNull Result result) throws Throwable {
linkVFile.delete(SymlinkHandlingTest.this);
}
}.execute();
assertFalse(linkVFile.toString(), linkVFile.isValid());
assertFalse(linkFile.exists());
assertTrue(targetFile.exists());
File targetDir = myTempDir.newFolder("targetDir");
File childFile = new File(targetDir, "child.txt");
assertTrue(childFile.getPath(), childFile.exists() || childFile.createNewFile());
File linkDir = createSymLink(targetDir.getPath(), myTempDir.getRoot() + "/linkDir");
VirtualFile linkVDir = refreshAndFind(linkDir);
assertTrue("link=" + linkDir + ", vLink=" + linkVDir, linkVDir != null && linkVDir.isDirectory() && linkVDir.is(VFileProperty.SYMLINK) && linkVDir.getChildren().length == 1);
new WriteAction() {
@Override
protected void run(@NotNull Result result) throws Throwable {
linkVDir.delete(SymlinkHandlingTest.this);
}
}.execute();
assertFalse(linkVDir.toString(), linkVDir.isValid());
assertFalse(linkDir.exists());
assertTrue(targetDir.exists());
assertTrue(childFile.exists());
}
use of com.intellij.openapi.application.WriteAction in project intellij-community by JetBrains.
the class VirtualFileDeleteProvider method deleteElement.
public void deleteElement(@NotNull DataContext dataContext) {
final VirtualFile[] files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
if (files == null || files.length == 0)
return;
Project project = CommonDataKeys.PROJECT.getData(dataContext);
String message = createConfirmationMessage(files);
int returnValue = Messages.showOkCancelDialog(message, UIBundle.message("delete.dialog.title"), ApplicationBundle.message("button.delete"), CommonBundle.getCancelButtonText(), Messages.getQuestionIcon());
if (returnValue != Messages.OK)
return;
Arrays.sort(files, FileComparator.getInstance());
List<String> problems = ContainerUtil.newLinkedList();
CommandProcessor.getInstance().executeCommand(project, () -> {
new Task.Modal(project, "Deleting Files...", true) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
indicator.setIndeterminate(false);
int i = 0;
for (VirtualFile file : files) {
indicator.checkCanceled();
indicator.setText2(file.getPresentableUrl());
indicator.setFraction((double) i / files.length);
i++;
RunResult result = new WriteAction() {
@Override
protected void run(@NotNull Result result) throws Throwable {
file.delete(this);
}
}.executeSilently();
if (result.hasException()) {
LOG.info("Error when deleting " + file, result.getThrowable());
problems.add(file.getName());
}
}
}
@Override
public void onSuccess() {
reportProblems();
}
@Override
public void onCancel() {
reportProblems();
}
private void reportProblems() {
if (!problems.isEmpty()) {
reportDeletionProblem(problems);
}
}
}.queue();
}, "Deleting files", null);
}
use of com.intellij.openapi.application.WriteAction in project intellij-community by JetBrains.
the class AddWithParamFix method invoke.
public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) throws IncorrectOperationException {
final RunResult<SmartPsiElementPointer<XmlTag>> result = new WriteAction<SmartPsiElementPointer<XmlTag>>() {
protected void run(@NotNull Result<SmartPsiElementPointer<XmlTag>> result) throws Throwable {
final XmlTag withParamTag = RefactoringUtil.addWithParam(myTag);
withParamTag.setAttribute("name", myName != null ? myName : "dummy");
withParamTag.setAttribute("select", "dummy");
result.setResult(SmartPointerManager.getInstance(project).createSmartPsiElementPointer(withParamTag));
}
}.execute();
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
final Document doc = psiDocumentManager.getDocument(file);
assert doc != null;
psiDocumentManager.doPostponedOperationsAndUnblockDocument(doc);
final XmlTag withParamTag = result.getResultObject().getElement();
assert withParamTag != null;
final TemplateBuilderImpl builder = new TemplateBuilderImpl(withParamTag);
final XmlAttribute selectAttr = withParamTag.getAttribute("select", null);
assert selectAttr != null;
PsiElement dummy = XsltSupport.getAttValueToken(selectAttr);
builder.replaceElement(dummy, new MacroCallNode(new CompleteMacro()));
if (myName == null) {
final XmlAttribute nameAttr = withParamTag.getAttribute("name", null);
assert nameAttr != null;
dummy = XsltSupport.getAttValueToken(nameAttr);
builder.replaceElement(dummy, new MacroCallNode(new CompleteMacro()));
}
moveTo(editor, withParamTag);
new WriteAction() {
@SuppressWarnings({ "RawUseOfParameterizedType" })
protected void run(@NotNull Result result) throws Throwable {
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
final TemplateManager mgr = TemplateManager.getInstance(myTag.getProject());
mgr.startTemplate(editor, builder.buildInlineTemplate());
}
}.execute();
}
Aggregations