Example usage for com.intellij.openapi.vfs VirtualFileWrapper getVirtualFile

List of usage examples for com.intellij.openapi.vfs VirtualFileWrapper getVirtualFile

Introduction

In this page you can find the example usage for com.intellij.openapi.vfs VirtualFileWrapper getVirtualFile.

Prototype

@Nullable
public VirtualFile getVirtualFile(boolean createIfNotExist) 

Source Link

Document

Looks for VirtualFile and tries to create new VirtualFile if createIfNotExist is true and file is not exist.

Usage

From source file:com.hp.alm.ali.idea.action.attachment.AttachmentDownloadAction.java

License:Apache License

@Override
protected void actionPerformed(AnActionEvent event, Project project, Entity entity) {
    String name = entity.getPropertyValue("name");
    FileSaverDescriptor desc = new FileSaverDescriptor("Download Attachment",
            "Download attachment to the local filesystem.");
    final VirtualFileWrapper file = FileChooserFactory.getInstance().createSaveFileDialog(desc, project)
            .save(lastDir, name.replaceFirst("\\.agmlink$", ""));
    if (file != null) {
        VirtualFile vf = file.getVirtualFile(true);
        if (vf == null) {
            Messages.showErrorDialog("Invalid file specified", "Error");
            return;
        }/*  w w w .j  a  v  a2s.c  o  m*/
        lastDir = vf.getParent();
        if (!name.endsWith(".agmlink") || file.getFile().getName().endsWith(".agmlink")) {
            // either regular file or we explicitly ask for .agmlink file
            ProgressManager.getInstance()
                    .run(new AttachmentDownloadTask(project, file.getFile(), name,
                            Integer.valueOf(entity.getPropertyValue("file-size")),
                            new EntityRef(entity.getPropertyValue("parent-type"),
                                    Integer.valueOf(entity.getPropertyValue("parent-id"))),
                            null));
        } else {
            // download referenced content instead
            ProgressManager.getInstance()
                    .run(new AttachmentAgmLinkDownloadTask(project, file.getFile(), name,
                            Integer.valueOf(entity.getPropertyValue("file-size")),
                            new EntityRef(entity.getPropertyValue("parent-type"),
                                    Integer.valueOf(entity.getPropertyValue("parent-id"))),
                            null));
        }
    }
}

From source file:org.intellij.grammar.actions.BnfGenerateLexerAction.java

License:Apache License

@Override
public void actionPerformed(AnActionEvent e) {
    final PsiFile file = LangDataKeys.PSI_FILE.getData(e.getDataContext());
    if (!(file instanceof BnfFile))
        return;/*from  w  w  w  .jav  a2  s.  c  o  m*/

    final Project project = file.getProject();

    final BnfFile bnfFile = (BnfFile) file;
    final String flexFileName = getFlexFileName(bnfFile);

    Collection<VirtualFile> files = FilenameIndex.getVirtualFilesByName(project, flexFileName,
            ProjectScope.getAllScope(project));
    VirtualFile firstItem = ContainerUtil.getFirstItem(files);

    FileSaverDescriptor descriptor = new FileSaverDescriptor("Save JFlex Lexer", "", "flex");
    VirtualFile baseDir = firstItem != null ? firstItem.getParent() : bnfFile.getVirtualFile().getParent();
    VirtualFileWrapper fileWrapper = FileChooserFactory.getInstance().createSaveFileDialog(descriptor, project)
            .save(baseDir, firstItem != null ? firstItem.getName() : flexFileName);
    if (fileWrapper == null)
        return;
    final VirtualFile virtualFile = fileWrapper.getVirtualFile(true);
    if (virtualFile == null)
        return;

    new WriteCommandAction.Simple(project) {
        @Override
        protected void run() throws Throwable {
            try {
                PsiDirectory psiDirectory = PsiManager.getInstance(project)
                        .findDirectory(virtualFile.getParent());
                assert psiDirectory != null;
                PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory);
                String packageName = aPackage == null ? null : aPackage.getQualifiedName();

                String text = generateLexerText(bnfFile, packageName);

                VfsUtil.saveText(virtualFile, text);

                Notifications.Bus.notify(
                        new Notification(BnfConstants.GENERATION_GROUP, virtualFile.getName() + " generated",
                                "to " + virtualFile.getParent().getPath(), NotificationType.INFORMATION),
                        project);

                associateFileTypeAndNavigate(project, virtualFile);
            } catch (final IncorrectOperationException e) {
                ApplicationManager.getApplication().invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        Messages.showErrorDialog(project,
                                "Unable to create file " + flexFileName + "\n" + e.getLocalizedMessage(),
                                "Create JFlex Lexer");
                    }
                });
            }
        }
    }.execute();

}