Search in sources :

Example 51 with WriteAction

use of com.intellij.openapi.application.WriteAction in project intellij-community by JetBrains.

the class CompilerTester method setFileText.

public void setFileText(final PsiFile file, final String text) throws IOException {
    new WriteAction() {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            final VirtualFile virtualFile = file.getVirtualFile();
            VfsUtil.saveText(ObjectUtils.assertNotNull(virtualFile), text);
        }
    }.execute().throwException();
    touch(file.getVirtualFile());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteAction(com.intellij.openapi.application.WriteAction) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result)

Example 52 with WriteAction

use of com.intellij.openapi.application.WriteAction in project intellij-community by JetBrains.

the class CompilerTester method touch.

public void touch(final VirtualFile file) throws IOException {
    new WriteAction() {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            file.setBinaryContent(file.contentsToByteArray(), -1, file.getTimeStamp() + 1);
            File ioFile = VfsUtilCore.virtualToIoFile(file);
            assert ioFile.setLastModified(ioFile.lastModified() - 100000);
            file.refresh(false, false);
        }
    }.execute().throwException();
}
Also used : WriteAction(com.intellij.openapi.application.WriteAction) NotNull(org.jetbrains.annotations.NotNull) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) Result(com.intellij.openapi.application.Result)

Example 53 with WriteAction

use of com.intellij.openapi.application.WriteAction in project intellij-community by JetBrains.

the class ChangeLibraryLevelActionBase method copyOrMoveFiles.

private boolean copyOrMoveFiles(final Set<File> filesToProcess, @NotNull final String targetDirPath, final Map<String, String> copiedFiles) {
    final Ref<Boolean> finished = Ref.create(false);
    new Task.Modal(myProject, (myCopy ? "Copying" : "Moving") + " Library Files", true) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            final File targetDir = new File(FileUtil.toSystemDependentName(targetDirPath));
            for (final File from : filesToProcess) {
                indicator.checkCanceled();
                final File to = FileUtil.findSequentNonexistentFile(targetDir, FileUtil.getNameWithoutExtension(from), FileUtilRt.getExtension(from.getName()));
                try {
                    if (from.isDirectory()) {
                        if (myCopy) {
                            FileUtil.copyDir(from, to);
                        } else {
                            FileUtil.moveDirWithContent(from, to);
                        }
                    } else {
                        if (myCopy) {
                            FileUtil.copy(from, to);
                        } else {
                            FileUtil.rename(from, to);
                        }
                    }
                } catch (IOException e) {
                    final String actionName = getActionName();
                    final String message = "Cannot " + actionName.toLowerCase() + " file " + from.getAbsolutePath() + ": " + e.getMessage();
                    Messages.showErrorDialog(ChangeLibraryLevelActionBase.this.myProject, message, "Cannot " + actionName);
                    LOG.info(e);
                    return;
                }
                copiedFiles.put(FileUtil.toSystemIndependentName(from.getAbsolutePath()), FileUtil.toSystemIndependentName(to.getAbsolutePath()));
            }
            finished.set(true);
        }
    }.queue();
    if (!finished.get()) {
        return false;
    }
    new WriteAction() {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            for (Map.Entry<String, String> entry : copiedFiles.entrySet()) {
                String fromPath = entry.getKey();
                String toPath = entry.getValue();
                LocalFileSystem.getInstance().refreshAndFindFileByPath(toPath);
                if (!myCopy) {
                    final VirtualFile parent = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(fromPath).getParentFile());
                    if (parent != null) {
                        parent.refresh(false, false);
                    }
                }
            }
        }
    }.execute();
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) WriteAction(com.intellij.openapi.application.WriteAction) IOException(java.io.IOException) Result(com.intellij.openapi.application.Result) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 54 with WriteAction

use of com.intellij.openapi.application.WriteAction in project intellij-community by JetBrains.

the class AddSupportForSingleFrameworkDialog method addSupport.

private boolean addSupport() {
    final LibraryCompositionSettings librarySettings = myComponent.getLibraryCompositionSettings();
    if (librarySettings != null) {
        final ModifiableRootModel modifiableModel = myModifiableModelsProvider.getModuleModifiableModel(myModule);
        if (!askAndRemoveDuplicatedLibraryEntry(modifiableModel, librarySettings.getLibraryDescription())) {
            if (myConfigurable.isOnlyLibraryAdded()) {
                myModifiableModelsProvider.disposeModuleModifiableModel(modifiableModel);
                return false;
            }
            return false;
        }
        new WriteAction() {

            protected void run(@NotNull final Result result) {
                myModifiableModelsProvider.commitModuleModifiableModel(modifiableModel);
            }
        }.execute();
        final boolean downloaded = librarySettings.downloadFiles(getRootPane());
        if (!downloaded) {
            int answer = Messages.showYesNoDialog(getRootPane(), ProjectBundle.message("warning.message.some.required.libraries.wasn.t.downloaded"), CommonBundle.getWarningTitle(), Messages.getWarningIcon());
            if (answer != Messages.YES) {
                return false;
            }
        }
    }
    new WriteAction() {

        protected void run(@NotNull final Result result) {
            final ModifiableRootModel rootModel = myModifiableModelsProvider.getModuleModifiableModel(myModule);
            if (librarySettings != null) {
                librarySettings.addLibraries(rootModel, new ArrayList<>(), myModel.getLibrariesContainer());
            }
            myConfigurable.addSupport(myModule, rootModel, myModifiableModelsProvider);
            myModifiableModelsProvider.commitModuleModifiableModel(rootModel);
        }
    }.execute();
    return true;
}
Also used : LibraryCompositionSettings(com.intellij.facet.impl.ui.libraries.LibraryCompositionSettings) WriteAction(com.intellij.openapi.application.WriteAction) ArrayList(java.util.ArrayList) Result(com.intellij.openapi.application.Result)

Example 55 with WriteAction

use of com.intellij.openapi.application.WriteAction in project intellij-community by JetBrains.

the class ActionRunner method runInsideWriteAction.

public static void runInsideWriteAction(@NotNull final InterruptibleRunnable runnable) throws Exception {
    RunResult result = new WriteAction() {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            runnable.run();
        }
    }.execute();
    if (result.getThrowable() instanceof Exception)
        throw (Exception) result.getThrowable();
    result.throwException();
}
Also used : WriteAction(com.intellij.openapi.application.WriteAction) RunResult(com.intellij.openapi.application.RunResult) Result(com.intellij.openapi.application.Result) RunResult(com.intellij.openapi.application.RunResult)

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