Example usage for com.intellij.openapi.vfs ReadonlyStatusHandler ensureFilesWritable

List of usage examples for com.intellij.openapi.vfs ReadonlyStatusHandler ensureFilesWritable

Introduction

In this page you can find the example usage for com.intellij.openapi.vfs ReadonlyStatusHandler ensureFilesWritable.

Prototype

public static boolean ensureFilesWritable(@NotNull Project project, VirtualFile @NotNull... files) 

Source Link

Usage

From source file:com.android.tools.idea.gradle.structure.editors.AndroidProjectConfigurable.java

License:Apache License

@Override
public void apply() throws ConfigurationException {
    if (myGradleBuildFile == null) {
        return;/*  www. j a  v  a  2s .  co  m*/
    }
    VirtualFile file = myGradleBuildFile.getFile();
    if (!ReadonlyStatusHandler.ensureFilesWritable(myProject, file)) {
        throw new ConfigurationException(String.format("Build file %1$s is not writable", file.getPath()));
    }

    CommandProcessor.getInstance().runUndoTransparentAction(() -> {
        try {
            ActionRunner.runInsideWriteAction(() -> {
                for (BuildFileKey key : PROJECT_PROPERTIES) {
                    if (key == BuildFileKey.GRADLE_WRAPPER_VERSION || !myModifiedKeys.contains(key)) {
                        continue;
                    }
                    Object value = myProjectProperties.get(key);
                    if (value != null) {
                        myGradleBuildFile.setValue(key, value);
                    } else {
                        myGradleBuildFile.removeValue(null, key);
                    }
                }
                Object wrapperVersion = myProjectProperties.get(BuildFileKey.GRADLE_WRAPPER_VERSION);
                GradleWrapper gradleWrapper = GradleWrapper.find(myProject);
                if (wrapperVersion != null && gradleWrapper != null) {
                    boolean updated = gradleWrapper
                            .updateDistributionUrlAndDisplayFailure(wrapperVersion.toString());
                    if (updated) {
                        VirtualFile virtualFile = gradleWrapper.getPropertiesFile();
                        if (virtualFile != null) {
                            virtualFile.refresh(false, false);
                        }
                    }
                }
                myModifiedKeys.clear();
            });
        } catch (Exception e) {
            LOG.error("Error while applying changes", e);
        }
    });
}

From source file:com.android.tools.idea.structure.AndroidModuleConfigurable.java

License:Apache License

@Override
public void apply() throws ConfigurationException {
    VirtualFile file = GradleUtil.getGradleBuildFile(myModule);
    if (!ReadonlyStatusHandler.ensureFilesWritable(myModule.getProject(), file)) {
        throw new ConfigurationException(String.format("Build file %1$s is not writable", file.getPath()));
    }/*w  w w  . j  av a 2s. c  o  m*/
    myModuleEditor.apply();
}

From source file:com.android.tools.idea.structure.AndroidProjectConfigurable.java

License:Apache License

@Override
public void apply() throws ConfigurationException {
    if (myGradleBuildFile == null) {
        return;/*  w  w  w  .  ja va  2 s.  co  m*/
    }
    VirtualFile file = myGradleBuildFile.getFile();
    if (!ReadonlyStatusHandler.ensureFilesWritable(myProject, file)) {
        throw new ConfigurationException(String.format("Build file %1$s is not writable", file.getPath()));
    }

    CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
        @Override
        public void run() {
            try {
                ActionRunner.runInsideWriteAction(new ActionRunner.InterruptibleRunnable() {
                    @Override
                    public void run() throws Exception {
                        for (BuildFileKey key : PROJECT_PROPERTIES) {
                            if (key == BuildFileKey.GRADLE_WRAPPER_VERSION || !myModifiedKeys.contains(key)) {
                                continue;
                            }
                            Object value = myProjectProperties.get(key);
                            if (value != null) {
                                myGradleBuildFile.setValue(key, value);
                            } else {
                                myGradleBuildFile.removeValue(null, key);
                            }
                        }
                        Object wrapperVersion = myProjectProperties.get(BuildFileKey.GRADLE_WRAPPER_VERSION);
                        File wrapperPropertiesFile = GradleUtil.findWrapperPropertiesFile(myProject);
                        if (wrapperVersion != null && wrapperPropertiesFile != null) {
                            boolean updated = GradleUtil.updateGradleDistributionUrl(wrapperVersion.toString(),
                                    wrapperPropertiesFile);
                            if (updated) {
                                VirtualFile virtualFile = VfsUtil.findFileByIoFile(wrapperPropertiesFile, true);
                                if (virtualFile != null) {
                                    virtualFile.refresh(false, false);
                                }
                            }
                        }
                        myModifiedKeys.clear();
                    }
                });
            } catch (Exception e) {
                LOG.error("Error while applying changes", e);
            }
        }
    });
}

From source file:com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel.java

License:Apache License

@Override
protected boolean execute(ThrowableRunnable<Exception> operation, boolean updateProperties) {
    if (!ReadonlyStatusHandler.ensureFilesWritable(getProject(), myFile)) {
        return false;
    }//from   www .j  ava  2 s.c  o m
    try {
        myPsiChangeListener.stop();
        operation.run();
        updateRenderer(updateProperties);
        return true;
    } catch (Throwable e) {
        showError("Execute command", e);
        return false;
    } finally {
        myPsiChangeListener.start();
    }
}

From source file:com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel.java

License:Apache License

@Override
protected void executeWithReparse(ThrowableRunnable<Exception> operation) {
    if (!ReadonlyStatusHandler.ensureFilesWritable(getProject(), myFile)) {
        return;//from   w ww  .ja  v a2s .co m
    }
    try {
        myPsiChangeListener.stop();
        operation.run();
        myPsiChangeListener.start();
        reparseFile();
    } catch (Throwable e) {
        showError("Execute command", e);
        myPsiChangeListener.start();
    }
}

From source file:com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel.java

License:Apache License

@Override
protected void execute(List<EditOperation> operations) {
    if (!ReadonlyStatusHandler.ensureFilesWritable(getProject(), myFile)) {
        return;//from w  w w. j  a v a  2s .c  o m
    }
    try {
        myPsiChangeListener.stop();
        for (EditOperation operation : operations) {
            operation.execute();
        }
        updateRenderer(true);
    } catch (Throwable e) {
        showError("Execute command", e);
    } finally {
        myPsiChangeListener.start();
    }
}

From source file:com.intellij.codeInsight.CodeInsightUtilBase.java

License:Apache License

@Override
public boolean prepareFileForWrite(@Nullable final PsiFile psiFile) {
    if (psiFile == null)
        return false;
    final VirtualFile file = psiFile.getVirtualFile();
    final Project project = psiFile.getProject();

    if (ReadonlyStatusHandler.ensureFilesWritable(project, file)) {
        return true;
    }//www .  ja v  a 2 s .c o m
    ApplicationManager.getApplication().invokeLater(new Runnable() {
        @Override
        public void run() {
            final Editor editor = FileEditorManager.getInstance(project)
                    .openTextEditor(new OpenFileDescriptor(project, file), true);
            if (editor != null && editor.getComponent().isDisplayable()) {
                HintManager.getInstance().showErrorHint(editor,
                        CodeInsightBundle.message("error.hint.file.is.readonly", file.getPresentableUrl()));
            }
        }
    }, project.getDisposed());

    return false;
}

From source file:org.cordovastudio.editors.designer.designSurface.CordovaDesignerEditorPanel.java

License:Apache License

protected boolean execute(ThrowableRunnable<Exception> operation, final boolean updateProperties) {
    if (!ReadonlyStatusHandler.ensureFilesWritable(getProject(), myFile)) {
        return false;
    }/*  ww w.j  ava  2  s  .co  m*/
    try {
        myPsiChangeListener.stop();
        operation.run();
        ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
                boolean active = myPsiChangeListener.isActive();
                if (active) {
                    myPsiChangeListener.stop();
                }
                updateRenderer(updateProperties);
                if (active) {
                    myPsiChangeListener.start();
                }
            }
        });
        return true;
    } catch (Throwable e) {
        showError("Execute command", e);
        return false;
    } finally {
        myPsiChangeListener.start();
    }
}

From source file:org.cordovastudio.editors.designer.designSurface.CordovaDesignerEditorPanel.java

License:Apache License

protected void executeWithReparse(ThrowableRunnable<Exception> operation) {
    if (!ReadonlyStatusHandler.ensureFilesWritable(getProject(), myFile)) {
        return;//from w w w  . j a  v a  2s  . c o m
    }
    try {
        myPsiChangeListener.stop();
        operation.run();
        myPsiChangeListener.start();
        reparseFile();
    } catch (Throwable e) {
        showError("Execute command", e);
        myPsiChangeListener.start();
    }
}

From source file:org.cordovastudio.editors.designer.designSurface.CordovaDesignerEditorPanel.java

License:Apache License

protected void execute(java.util.List<EditOperation> operations) {
    if (!ReadonlyStatusHandler.ensureFilesWritable(getProject(), myFile)) {
        return;/*from www.j  a  v a  2 s.c  om*/
    }
    try {
        myPsiChangeListener.stop();
        for (EditOperation operation : operations) {
            operation.execute();
        }
        updateRenderer(true);
    } catch (Throwable e) {
        showError("Execute command", e);
    } finally {
        myPsiChangeListener.start();
    }
}