Search in sources :

Example 66 with WriteAction

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteAction(com.intellij.openapi.application.WriteAction) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) Nullable(org.jetbrains.annotations.Nullable)

Example 67 with WriteAction

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 });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteAction(com.intellij.openapi.application.WriteAction) PsiClass(com.intellij.psi.PsiClass) Module(com.intellij.openapi.module.Module) Result(com.intellij.openapi.application.Result)

Example 68 with WriteAction

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());
}
Also used : WriteAction(com.intellij.openapi.application.WriteAction) File(java.io.File) Result(com.intellij.openapi.application.Result) Test(org.junit.Test)

Example 69 with WriteAction

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);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) WriteAction(com.intellij.openapi.application.WriteAction) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) RunResult(com.intellij.openapi.application.RunResult) Project(com.intellij.openapi.project.Project) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) RunResult(com.intellij.openapi.application.RunResult)

Example 70 with WriteAction

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();
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) WriteAction(com.intellij.openapi.application.WriteAction) Document(com.intellij.openapi.editor.Document) CompleteMacro(com.intellij.codeInsight.template.macro.CompleteMacro) Result(com.intellij.openapi.application.Result) RunResult(com.intellij.openapi.application.RunResult) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) TemplateManager(com.intellij.codeInsight.template.TemplateManager) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

Result (com.intellij.openapi.application.Result)85 WriteAction (com.intellij.openapi.application.WriteAction)85 NotNull (org.jetbrains.annotations.NotNull)38 VirtualFile (com.intellij.openapi.vfs.VirtualFile)37 File (java.io.File)20 IOException (java.io.IOException)16 Module (com.intellij.openapi.module.Module)10 PsiFile (com.intellij.psi.PsiFile)7 Sdk (com.intellij.openapi.projectRoots.Sdk)5 RunResult (com.intellij.openapi.application.RunResult)4 Document (com.intellij.openapi.editor.Document)4 Project (com.intellij.openapi.project.Project)4 JavaSdk (com.intellij.openapi.projectRoots.JavaSdk)4 Library (com.intellij.openapi.roots.libraries.Library)4 NewVirtualFile (com.intellij.openapi.vfs.newvfs.NewVirtualFile)4 ModifiableFacetModel (com.intellij.facet.ModifiableFacetModel)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 FacetManager (com.intellij.facet.FacetManager)2 ProjectFacetManager (com.intellij.facet.ProjectFacetManager)2