Example usage for com.intellij.openapi.fileChooser FileSaverDescriptor FileSaverDescriptor

List of usage examples for com.intellij.openapi.fileChooser FileSaverDescriptor FileSaverDescriptor

Introduction

In this page you can find the example usage for com.intellij.openapi.fileChooser FileSaverDescriptor FileSaverDescriptor.

Prototype

public FileSaverDescriptor(@Nls(capitalization = Nls.Capitalization.Title) @NotNull String title,
        @Nls(capitalization = Nls.Capitalization.Sentence) @NotNull String description, String... extensions) 

Source Link

Document

Constructs save dialog properties

Usage

From source file:com.android.tools.idea.actions.ConvertToNinePatchAction.java

License:Apache License

@Override
public void actionPerformed(AnActionEvent e) {
    final VirtualFile pngFile = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
    if (!isPngFile(pngFile)) {
        return;/*from  w w w .  j av a2s .  c  o  m*/
    }

    FileSaverDescriptor descriptor = new FileSaverDescriptor(
            AndroidBundle.message("android.9patch.creator.save.title"), "", SdkConstants.EXT_PNG);
    FileSaverDialog saveFileDialog = FileChooserFactory.getInstance().createSaveFileDialog(descriptor,
            (Project) null);
    VirtualFileWrapper fileWrapper = saveFileDialog.save(pngFile.getParent(),
            pngFile.getNameWithoutExtension().concat(SdkConstants.DOT_9PNG));
    if (fileWrapper == null) {
        return;
    }

    final File patchFile = fileWrapper.getFile();
    new Task.Modal(null, "Creating 9-Patch File", false) {
        private IOException myException;

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            indicator.setIndeterminate(true);
            try {
                BufferedImage pngImage = ImageIO.read(VfsUtilCore.virtualToIoFile(pngFile));
                BufferedImage patchImage = ImageUtils.addMargin(pngImage, 1);
                ImageIO.write(patchImage, SdkConstants.EXT_PNG, patchFile);
                LocalFileSystem.getInstance().refreshAndFindFileByIoFile(patchFile);
            } catch (IOException e) {
                myException = e;
            }
        }

        @Override
        public void onSuccess() {
            if (myException != null) {
                Messages.showErrorDialog(
                        AndroidBundle.message("android.9patch.creator.error", myException.getMessage()),
                        AndroidBundle.message("android.9patch.creator.error.title"));
            }
        }
    }.queue();
}

From source file:com.android.tools.idea.avdmanager.ExportDeviceAction.java

License:Apache License

@Override
public void actionPerformed(ActionEvent e) {
    FileSaverDescriptor descriptor = new FileSaverDescriptor("Export Location",
            "Select a location for the exported device", "xml");
    String homePath = System.getProperty("user.home");
    File parentPath = homePath == null ? new File("/") : new File(homePath);
    VirtualFile parent = LocalFileSystem.getInstance().findFileByIoFile(parentPath);
    VirtualFileWrapper fileWrapper = FileChooserFactory.getInstance()
            .createSaveFileDialog(descriptor, (Project) null).save(parent, "device.xml");
    Device device = myProvider.getDevice();
    if (device != null && fileWrapper != null) {
        DeviceManagerConnection.writeDevicesToFile(ImmutableList.of(device), fileWrapper.getFile());
    }//ww w . j ava2s . c  o  m
}

From source file:com.android.tools.idea.ddms.screenshot.ScreenshotViewer.java

License:Apache License

@Override
protected void doOKAction() {
    FileSaverDescriptor descriptor = new FileSaverDescriptor(
            AndroidBundle.message("android.ddms.screenshot.save.title"), "", SdkConstants.EXT_PNG);
    FileSaverDialog saveFileDialog = FileChooserFactory.getInstance().createSaveFileDialog(descriptor,
            myProject);/*  w ww .ja  va  2 s.  c  o m*/
    VirtualFile baseDir = ourLastSavedFolder != null ? ourLastSavedFolder : myProject.getBaseDir();
    VirtualFileWrapper fileWrapper = saveFileDialog.save(baseDir, getDefaultFileName());
    if (fileWrapper == null) {
        return;
    }

    myScreenshotFile = fileWrapper.getFile();
    try {
        ImageIO.write(myDisplayedImageRef.get(), SdkConstants.EXT_PNG, myScreenshotFile);
    } catch (IOException e) {
        Messages.showErrorDialog(myProject, AndroidBundle.message("android.ddms.screenshot.save.error", e),
                AndroidBundle.message("android.ddms.actions.screenshot"));
        return;
    }

    VirtualFile virtualFile = fileWrapper.getVirtualFile();
    if (virtualFile != null) {
        //noinspection AssignmentToStaticFieldFromInstanceMethod
        ourLastSavedFolder = virtualFile.getParent();
    }

    super.doOKAction();
}

From source file:com.microsoft.intellij.ui.NewCertificateDialog.java

License:Open Source License

private ActionListener getFileSaverListener(final TextFieldWithBrowseButton field,
        final TextFieldWithBrowseButton fieldToUpdate, final String suffixToReplace, final String suffix) {
    return new ActionListener() {
        @Override//from   ww w  .j a  va 2s. c om
        public void actionPerformed(ActionEvent e) {
            final FileSaverDialog dialog = FileChooserFactory.getInstance().createSaveFileDialog(
                    new FileSaverDescriptor(message("newCertDlgBrwFldr"), "", suffixToReplace), field);
            final VirtualFile baseDir = myProject.getBaseDir();
            final VirtualFileWrapper save = dialog.save(baseDir, "");
            if (save != null) {
                field.setText(FileUtil.toSystemDependentName(save.getFile().getAbsolutePath()));
                if (fieldToUpdate.getText().isEmpty()) {
                    fieldToUpdate.setText(Utils.replaceLastSubString(field.getText(), suffixToReplace, suffix));
                }
            }
        }
    };
}

From source file:org.cdv.intellij.ui.CDVIDEAFileSaveChooser.java

License:Open Source License

public File choose(String message, String suffix) {
    final FileSaverDialog dialog = FileChooserFactory.getInstance()
            .createSaveFileDialog(new FileSaverDescriptor(message, "", suffix), project);
    final VirtualFile baseDir = project.getBaseDir();
    final VirtualFileWrapper save = dialog.save(baseDir, "");
    if (save != null) {
        return save.getFile();
        //            field.setText(FileUtil.toSystemDependentName(save.getFile().getAbsolutePath()));
        //            if (fieldToUpdate.getText().isEmpty()) {
        //                fieldToUpdate.setText(Utils.replaceLastSubString(field.getText(), suffixToReplace, suffix));
        //            }
    }//  w  w w.  ja v a 2s .  c o  m
    return 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 .  j  a  va  2s .  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();

}

From source file:org.intellij.plugins.intelliLang.InjectionsSettingsUI.java

License:Apache License

private DefaultActionGroup createActions() {
    final Consumer<BaseInjection> consumer = new Consumer<BaseInjection>() {
        @Override/*w w  w .j  a  va  2s  .  co  m*/
        public void consume(final BaseInjection injection) {
            addInjection(injection);
        }
    };
    final Factory<BaseInjection> producer = new NullableFactory<BaseInjection>() {
        @Override
        public BaseInjection create() {
            final InjInfo info = getSelectedInjection();
            return info == null ? null : info.injection;
        }
    };
    for (LanguageInjectionSupport support : InjectorUtils.getActiveInjectionSupports()) {
        ContainerUtil.addAll(myAddActions, support.createAddActions(myProject, consumer));
        final AnAction action = support.createEditAction(myProject, producer);
        myEditActions.put(support.getId(),
                action == null ? AbstractLanguageInjectionSupport.createDefaultEditAction(myProject, producer)
                        : action);
        mySupports.put(support.getId(), support);
    }
    Collections.sort(myAddActions, new Comparator<AnAction>() {
        @Override
        public int compare(final AnAction o1, final AnAction o2) {
            return Comparing.compare(o1.getTemplatePresentation().getText(),
                    o2.getTemplatePresentation().getText());
        }
    });

    final DefaultActionGroup group = new DefaultActionGroup();
    final AnAction addAction = new AnAction("Add", "Add", IconUtil.getAddIcon()) {
        @Override
        public void update(final AnActionEvent e) {
            e.getPresentation().setEnabled(!myAddActions.isEmpty());
        }

        @Override
        public void actionPerformed(final AnActionEvent e) {
            performAdd(e);
        }
    };
    final AnAction removeAction = new AnAction("Remove", "Remove", PlatformIcons.DELETE_ICON) {
        @Override
        public void update(final AnActionEvent e) {
            boolean enabled = false;
            for (InjInfo info : getSelectedInjections()) {
                if (!info.bundled) {
                    enabled = true;
                    break;
                }
            }
            e.getPresentation().setEnabled(enabled);
        }

        @Override
        public void actionPerformed(final AnActionEvent e) {
            performRemove();
        }
    };

    final AnAction editAction = new AnAction("Edit", "Edit", PlatformIcons.PROPERTIES_ICON) {
        @Override
        public void update(final AnActionEvent e) {
            final AnAction action = getEditAction();
            e.getPresentation().setEnabled(action != null);
            if (action != null) {
                action.update(e);
            }
        }

        @Override
        public void actionPerformed(final AnActionEvent e) {
            performEditAction(e);
        }
    };
    final AnAction copyAction = new AnAction("Duplicate", "Duplicate", PlatformIcons.COPY_ICON) {
        @Override
        public void update(final AnActionEvent e) {
            final AnAction action = getEditAction();
            e.getPresentation().setEnabled(action != null);
            if (action != null) {
                action.update(e);
            }
        }

        @Override
        public void actionPerformed(final AnActionEvent e) {
            final InjInfo injection = getSelectedInjection();
            if (injection != null) {
                addInjection(injection.injection.copy());
                //performEditAction(e);
            }
        }
    };
    group.add(addAction);
    group.add(removeAction);
    group.add(copyAction);
    group.add(editAction);

    addAction.registerCustomShortcutSet(CommonShortcuts.INSERT, myInjectionsTable);
    removeAction.registerCustomShortcutSet(CommonShortcuts.DELETE, myInjectionsTable);
    editAction.registerCustomShortcutSet(CommonShortcuts.ENTER, myInjectionsTable);

    group.addSeparator();
    group.add(new AnAction("Enable Selected Injections", "Enable Selected Injections",
            PlatformIcons.SELECT_ALL_ICON) {
        @Override
        public void actionPerformed(final AnActionEvent e) {
            performSelectedInjectionsEnabled(true);
        }
    });
    group.add(new AnAction("Disable Selected Injections", "Disable Selected Injections",
            PlatformIcons.UNSELECT_ALL_ICON) {
        @Override
        public void actionPerformed(final AnActionEvent e) {
            performSelectedInjectionsEnabled(false);
        }
    });

    new AnAction("Toggle") {
        @Override
        public void actionPerformed(final AnActionEvent e) {
            performToggleAction();
        }
    }.registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0)),
            myInjectionsTable);

    if (myInfos.length > 1) {
        group.addSeparator();
        final AnAction shareAction = new AnAction("Make Global", null, PlatformIcons.IMPORT_ICON) {
            @Override
            public void actionPerformed(final AnActionEvent e) {
                final List<InjInfo> injections = getSelectedInjections();
                final CfgInfo cfg = getTargetCfgInfo(injections);
                if (cfg == null) {
                    return;
                }
                for (InjInfo info : injections) {
                    if (info.cfgInfo == cfg) {
                        continue;
                    }
                    if (info.bundled) {
                        continue;
                    }
                    info.cfgInfo.injectionInfos.remove(info);
                    cfg.addInjection(info.injection);
                }
                final int[] selectedRows = myInjectionsTable.getSelectedRows();
                myInjectionsTable.getListTableModel().setItems(getInjInfoList(myInfos));
                TableUtil.selectRows(myInjectionsTable, selectedRows);
            }

            @Override
            public void update(final AnActionEvent e) {
                final CfgInfo cfg = getTargetCfgInfo(getSelectedInjections());
                e.getPresentation().setEnabled(cfg != null);
                e.getPresentation().setText(cfg == getDefaultCfgInfo() ? "Make Global" : "Move to Project");
                super.update(e);
            }

            @Nullable
            private CfgInfo getTargetCfgInfo(final List<InjInfo> injections) {
                CfgInfo cfg = null;
                for (InjInfo info : injections) {
                    if (info.bundled) {
                        continue;
                    }
                    if (cfg == null) {
                        cfg = info.cfgInfo;
                    } else if (cfg != info.cfgInfo) {
                        return info.cfgInfo;
                    }
                }
                if (cfg == null) {
                    return cfg;
                }
                for (CfgInfo info : myInfos) {
                    if (info != cfg) {
                        return info;
                    }
                }
                throw new AssertionError();
            }
        };
        shareAction.registerCustomShortcutSet(
                new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, InputEvent.SHIFT_DOWN_MASK)),
                myInjectionsTable);
        group.add(shareAction);
    }
    group.addSeparator();
    group.add(new AnAction("Import", "Import", AllIcons.Actions.Install) {
        @Override
        public void actionPerformed(final AnActionEvent e) {
            doImportAction(e.getDataContext());
            updateCountLabel();
        }
    });
    group.add(new AnAction("Export", "Export", AllIcons.Actions.Export) {
        @Override
        public void actionPerformed(final AnActionEvent e) {
            final List<BaseInjection> injections = getInjectionList(getSelectedInjections());
            final VirtualFileWrapper wrapper = FileChooserFactory.getInstance()
                    .createSaveFileDialog(
                            new FileSaverDescriptor("Export Selected " + "Injections to File...", "", "xml"),
                            myProject)
                    .save(null, null);
            if (wrapper == null) {
                return;
            }
            final Configuration configuration = new Configuration();
            configuration.setInjections(injections);
            final Document document = new Document(configuration.getState());
            try {
                JDOMUtil.writeDocument(document, wrapper.getFile(), "\n");
            } catch (IOException ex) {
                final String msg = ex.getLocalizedMessage();
                Messages.showErrorDialog(myProject, msg != null && msg.length() > 0 ? msg : ex.toString(),
                        "Export Failed");
            }
        }

        @Override
        public void update(final AnActionEvent e) {
            e.getPresentation().setEnabled(!getSelectedInjections().isEmpty());
        }
    });

    return group;
}

From source file:org.jetbrains.android.util.SaveFileListener.java

License:Apache License

@Override
public void actionPerformed(ActionEvent e) {
    String path = myTextField.getText().trim();
    if (path.length() == 0) {
        String defaultLocation = getDefaultLocation();
        path = defaultLocation != null && defaultLocation.length() > 0 ? defaultLocation
                : SystemProperties.getUserHome();
    }//from   w w  w  . j  av a2  s  .c o m
    File file = new File(path);
    if (!file.exists()) {
        path = SystemProperties.getUserHome();
    }
    FileSaverDescriptor descriptor = new FileSaverDescriptor(myDialogTitle, "Save as *." + myExtension,
            myExtension);
    FileSaverDialog saveFileDialog = FileChooserFactory.getInstance().createSaveFileDialog(descriptor,
            myContentPanel);

    VirtualFile vf = LocalFileSystem.getInstance().findFileByIoFile(file.exists() ? file : new File(path));
    if (vf == null) {
        vf = VfsUtil.getUserHomeDir();
    }

    VirtualFileWrapper result = saveFileDialog.save(vf, null);

    if (result == null || result.getFile() == null) {
        return;
    }

    myTextField.setText(result.getFile().getPath());
}

From source file:org.twodividedbyzero.idea.findbugs.gui.preferences.ConfigurationPanel.java

License:Open Source License

private void exportPreferences() {
    final PersistencePreferencesBean prefs = _plugin.getState();
    final VirtualFileWrapper wrapper = FileChooserFactory.getInstance()
            .createSaveFileDialog(new FileSaverDescriptor("Export FindBugs Preferences to File...", "", "xml"),
                    this)
            .save(null, null);/*from   w  w w . j  a  va 2 s  .  co m*/
    if (wrapper == null)
        return;
    final Element el = XmlSerializer.serialize(prefs);
    el.setName(SonarImporterUtil.PERSISTENCE_ROOT_NAME); // rename "PersistencePreferencesBean"
    final Document document = new Document(el);
    try {
        JDOMUtil.writeDocument(document, wrapper.getFile(), "\n");
    } catch (final IOException ex) {
        LOGGER.error(ex);
        final String msg = ex.getLocalizedMessage();
        Messages.showErrorDialog(this, msg != null && !msg.isEmpty() ? msg : ex.toString(), "Export Failed");
    }
}

From source file:org.twodividedbyzero.idea.findbugs.gui.settings.FilterTab.java

License:Open Source License

void addRFilerFilter() {
    final VirtualFileWrapper wrapper = FileChooserFactory.getInstance()
            .createSaveFileDialog(new FileSaverDescriptor(
                    StringUtil.capitalizeWords(ResourcesLoader.getString("filter.rFile.save.title"), true),
                    ResourcesLoader.getString("filter.rFile.save.text"), XmlFileType.DEFAULT_EXTENSION), this)
            .save(null, "findbugs-android-exclude");
    if (wrapper == null) {
        return;// w w  w.  j a v  a2  s  .  c  o  m
    }

    final File file = wrapper.getFile();
    try {
        FileUtil.writeToFile(file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<FindBugsFilter>\n"
                + "    <!-- http://stackoverflow.com/questions/7568579/eclipsefindbugs-exclude-filter-files-doesnt-work -->\n"
                + "    <Match>\n" + "        <Class name=\"~.*\\.R\\$.*\"/>\n" + "    </Match>\n"
                + "    <Match>\n" + "    <Class name=\"~.*\\.Manifest\\$.*\"/>\n" + "    </Match>\n"
                + "</FindBugsFilter>");

        exclude.addFile(file);
    } catch (final Exception e) {
        throw ErrorUtil.toUnchecked(e);
    }
}