Search in sources :

Example 76 with WriteAction

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

the class CodeInsightTestCase method configureByText.

protected PsiFile configureByText(@NotNull final FileType fileType, @NotNull final String text, @Nullable String _extension) {
    try {
        final String extension = _extension == null ? fileType.getDefaultExtension() : _extension;
        File dir = createTempDirectory();
        final File tempFile = FileUtil.createTempFile(dir, "tempFile", "." + extension, true);
        final FileTypeManager fileTypeManager = FileTypeManager.getInstance();
        if (fileTypeManager.getFileTypeByExtension(extension) != fileType) {
            new WriteCommandAction(getProject()) {

                @Override
                protected void run(@NotNull Result result) throws Exception {
                    fileTypeManager.associateExtension(fileType, extension);
                }
            }.execute();
        }
        final VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(tempFile);
        assert vFile != null;
        new WriteAction() {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                vFile.setCharset(CharsetToolkit.UTF8_CHARSET);
                VfsUtil.saveText(vFile, text);
            }
        }.execute();
        final VirtualFile vdir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(dir);
        PsiTestUtil.addSourceRoot(myModule, vdir);
        configureByExistingFile(vFile);
        assertEquals(fileType, myFile.getVirtualFile().getFileType());
        return myFile;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) WriteAction(com.intellij.openapi.application.WriteAction) FileTypeManager(com.intellij.openapi.fileTypes.FileTypeManager) IOException(java.io.IOException) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) IOException(java.io.IOException) Result(com.intellij.openapi.application.Result)

Example 77 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 78 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 79 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 80 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)

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