Search in sources :

Example 1 with FileSaverDescriptor

use of com.intellij.openapi.fileChooser.FileSaverDescriptor in project intellij-community by JetBrains.

the class ApplyPatchSaveToFileExecutor method apply.

@Override
public void apply(@NotNull List<FilePatch> remaining, @NotNull MultiMap<VirtualFile, TextFilePatchInProgress> patchGroupsToApply, @Nullable LocalChangeList localList, @Nullable String fileName, @Nullable ThrowableComputable<Map<String, Map<String, CharSequence>>, PatchSyntaxException> additionalInfo) {
    FileSaverDialog dialog = FileChooserFactory.getInstance().createSaveFileDialog(new FileSaverDescriptor("Save Patch to", ""), myProject);
    VirtualFileWrapper targetFile = dialog.save(myProject.getBaseDir(), "TheirsChanges.patch");
    if (targetFile != null) {
        savePatch(patchGroupsToApply, targetFile);
    }
}
Also used : FileSaverDescriptor(com.intellij.openapi.fileChooser.FileSaverDescriptor) FileSaverDialog(com.intellij.openapi.fileChooser.FileSaverDialog) VirtualFileWrapper(com.intellij.openapi.vfs.VirtualFileWrapper)

Example 2 with FileSaverDescriptor

use of com.intellij.openapi.fileChooser.FileSaverDescriptor in project intellij-community by JetBrains.

the class CaptureConfigurable method createComponent.

@Nullable
@Override
public JComponent createComponent() {
    myTableModel = new MyTableModel();
    JBTable table = new JBTable(myTableModel);
    table.setColumnSelectionAllowed(false);
    TableColumnModel columnModel = table.getColumnModel();
    TableUtil.setupCheckboxColumn(columnModel.getColumn(MyTableModel.ENABLED_COLUMN));
    ToolbarDecorator decorator = ToolbarDecorator.createDecorator(table);
    decorator.setAddAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            myTableModel.addRow();
        }
    });
    decorator.setRemoveAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            TableUtil.removeSelectedItems(table);
        }
    });
    decorator.setMoveUpAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            TableUtil.moveSelectedItemsUp(table);
        }
    });
    decorator.setMoveDownAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            TableUtil.moveSelectedItemsDown(table);
        }
    });
    decorator.addExtraAction(new DumbAwareActionButton("Duplicate", "Duplicate", PlatformIcons.COPY_ICON) {

        @Override
        public boolean isEnabled() {
            return table.getSelectedRowCount() == 1;
        }

        @Override
        public void actionPerformed(@NotNull AnActionEvent e) {
            selectedCapturePoints(table).forEach(c -> {
                try {
                    int idx = myTableModel.add(c.clone());
                    table.getSelectionModel().setSelectionInterval(idx, idx);
                } catch (CloneNotSupportedException ex) {
                    LOG.error(ex);
                }
            });
        }
    });
    decorator.addExtraAction(new DumbAwareActionButton("Enable Selected", "Enable Selected", PlatformIcons.SELECT_ALL_ICON) {

        @Override
        public boolean isEnabled() {
            return table.getSelectedRowCount() > 0;
        }

        @Override
        public void actionPerformed(@NotNull AnActionEvent e) {
            selectedCapturePoints(table).forEach(c -> c.myEnabled = true);
            table.repaint();
        }
    });
    decorator.addExtraAction(new DumbAwareActionButton("Disable Selected", "Disable Selected", PlatformIcons.UNSELECT_ALL_ICON) {

        @Override
        public boolean isEnabled() {
            return table.getSelectedRowCount() > 0;
        }

        @Override
        public void actionPerformed(@NotNull AnActionEvent e) {
            selectedCapturePoints(table).forEach(c -> c.myEnabled = false);
            table.repaint();
        }
    });
    new DumbAwareAction("Toggle") {

        @Override
        public void update(@NotNull AnActionEvent e) {
            e.getPresentation().setEnabled(table.getSelectedRowCount() == 1);
        }

        @Override
        public void actionPerformed(@NotNull final AnActionEvent e) {
            selectedCapturePoints(table).forEach(c -> c.myEnabled = !c.myEnabled);
            table.repaint();
        }
    }.registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0)), table);
    decorator.addExtraAction(new DumbAwareActionButton("Import", "Import", AllIcons.Actions.Install) {

        @Override
        public void actionPerformed(@NotNull final AnActionEvent e) {
            FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, true, false, true, true) {

                @Override
                public boolean isFileVisible(VirtualFile file, boolean showHiddenFiles) {
                    return super.isFileVisible(file, showHiddenFiles) && (file.isDirectory() || "xml".equals(file.getExtension()) || file.getFileType() == FileTypes.ARCHIVE);
                }

                @Override
                public boolean isFileSelectable(VirtualFile file) {
                    return file.getFileType() == StdFileTypes.XML;
                }
            };
            descriptor.setDescription("Please select a file to import.");
            descriptor.setTitle("Import Capture Points");
            VirtualFile[] files = FileChooser.chooseFiles(descriptor, e.getProject(), null);
            if (ArrayUtil.isEmpty(files))
                return;
            table.getSelectionModel().clearSelection();
            for (VirtualFile file : files) {
                try {
                    Document document = JDOMUtil.loadDocument(file.getInputStream());
                    List<Element> children = document.getRootElement().getChildren();
                    children.forEach(element -> {
                        int idx = myTableModel.addIfNeeded(XmlSerializer.deserialize(element, CapturePoint.class));
                        table.getSelectionModel().addSelectionInterval(idx, idx);
                    });
                } catch (Exception ex) {
                    final String msg = ex.getLocalizedMessage();
                    Messages.showErrorDialog(e.getProject(), msg != null && msg.length() > 0 ? msg : ex.toString(), "Export Failed");
                }
            }
        }
    });
    decorator.addExtraAction(new DumbAwareActionButton("Export", "Export", AllIcons.Actions.Export) {

        @Override
        public void actionPerformed(@NotNull final AnActionEvent e) {
            VirtualFileWrapper wrapper = FileChooserFactory.getInstance().createSaveFileDialog(new FileSaverDescriptor("Export Selected Capture Points to File...", "", "xml"), e.getProject()).save(null, null);
            if (wrapper == null)
                return;
            Element rootElement = new Element("capture-points");
            selectedCapturePoints(table).forEach(c -> {
                try {
                    CapturePoint clone = c.clone();
                    clone.myEnabled = false;
                    rootElement.addContent(XmlSerializer.serialize(clone));
                } catch (CloneNotSupportedException ex) {
                    LOG.error(ex);
                }
            });
            try {
                JDOMUtil.write(rootElement, wrapper.getFile());
            } catch (Exception ex) {
                final String msg = ex.getLocalizedMessage();
                Messages.showErrorDialog(e.getProject(), msg != null && msg.length() > 0 ? msg : ex.toString(), "Export Failed");
            }
        }

        @Override
        public boolean isEnabled() {
            return table.getSelectedRowCount() > 0;
        }
    });
    BorderLayoutPanel panel = JBUI.Panels.simplePanel();
    panel.addToCenter(decorator.createPanel());
    myCaptureVariables = new JCheckBox(DebuggerBundle.message("label.capture.configurable.capture.variables"));
    panel.addToBottom(myCaptureVariables);
    return panel;
}
Also used : JVMNameUtil(com.intellij.debugger.engine.JVMNameUtil) JavaDebuggerSupport(com.intellij.debugger.ui.JavaDebuggerSupport) AllIcons(com.intellij.icons.AllIcons) VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileSaverDescriptor(com.intellij.openapi.fileChooser.FileSaverDescriptor) Document(org.jdom.Document) Nls(org.jetbrains.annotations.Nls) BorderLayoutPanel(com.intellij.util.ui.components.BorderLayoutPanel) JBUI(com.intellij.util.ui.JBUI) VirtualFileWrapper(com.intellij.openapi.vfs.VirtualFileWrapper) FileTypes(com.intellij.openapi.fileTypes.FileTypes) FileChooserDescriptor(com.intellij.openapi.fileChooser.FileChooserDescriptor) Messages(com.intellij.openapi.ui.Messages) Logger(com.intellij.openapi.diagnostic.Logger) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) FileChooserFactory(com.intellij.openapi.fileChooser.FileChooserFactory) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) KeyEvent(java.awt.event.KeyEvent) com.intellij.ui(com.intellij.ui) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Debugger(org.jetbrains.annotations.Debugger) AnnotatedElementsSearch(com.intellij.psi.search.searches.AnnotatedElementsSearch) StreamEx(one.util.streamex.StreamEx) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) Registry(com.intellij.openapi.util.registry.Registry) IntStreamEx(one.util.streamex.IntStreamEx) com.intellij.psi(com.intellij.psi) ConfigurationException(com.intellij.openapi.options.ConfigurationException) NotNull(org.jetbrains.annotations.NotNull) XmlSerializer(com.intellij.util.xmlb.XmlSerializer) ArrayUtil(com.intellij.util.ArrayUtil) CustomShortcutSet(com.intellij.openapi.actionSystem.CustomShortcutSet) TableColumnModel(javax.swing.table.TableColumnModel) ArrayList(java.util.ArrayList) JDOMUtil(com.intellij.openapi.util.JDOMUtil) AbstractTableModel(javax.swing.table.AbstractTableModel) Project(com.intellij.openapi.project.Project) DebuggerBundle(com.intellij.debugger.DebuggerBundle) PlatformIcons(com.intellij.util.PlatformIcons) DecompiledLocalVariable(com.intellij.debugger.jdi.DecompiledLocalVariable) StdFileTypes(com.intellij.openapi.fileTypes.StdFileTypes) StringUtil(com.intellij.openapi.util.text.StringUtil) ItemRemovable(com.intellij.util.ui.ItemRemovable) JBTable(com.intellij.ui.table.JBTable) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) Element(org.jdom.Element) FileChooser(com.intellij.openapi.fileChooser.FileChooser) SearchableConfigurable(com.intellij.openapi.options.SearchableConfigurable) javax.swing(javax.swing) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Element(org.jdom.Element) TableColumnModel(javax.swing.table.TableColumnModel) JBTable(com.intellij.ui.table.JBTable) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) Document(org.jdom.Document) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) CustomShortcutSet(com.intellij.openapi.actionSystem.CustomShortcutSet) List(java.util.List) ArrayList(java.util.ArrayList) FileChooserDescriptor(com.intellij.openapi.fileChooser.FileChooserDescriptor) BorderLayoutPanel(com.intellij.util.ui.components.BorderLayoutPanel) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) ConfigurationException(com.intellij.openapi.options.ConfigurationException) FileSaverDescriptor(com.intellij.openapi.fileChooser.FileSaverDescriptor) VirtualFileWrapper(com.intellij.openapi.vfs.VirtualFileWrapper) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with FileSaverDescriptor

use of com.intellij.openapi.fileChooser.FileSaverDescriptor in project intellij-community by JetBrains.

the class InjectionsSettingsUI method createActions.

private void createActions(ToolbarDecorator decorator) {
    final Consumer<BaseInjection> consumer = injection -> addInjection(injection);
    final Factory<BaseInjection> producer = (NullableFactory<BaseInjection>) () -> {
        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, (o1, o2) -> Comparing.compare(o1.getTemplatePresentation().getText(), o2.getTemplatePresentation().getText()));
    decorator.disableUpDownActions();
    decorator.setAddActionUpdater(new AnActionButtonUpdater() {

        @Override
        public boolean isEnabled(AnActionEvent e) {
            return !myAddActions.isEmpty();
        }
    });
    decorator.setAddAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            performAdd(button);
        }
    });
    decorator.setRemoveActionUpdater(new AnActionButtonUpdater() {

        @Override
        public boolean isEnabled(AnActionEvent e) {
            boolean enabled = false;
            for (InjInfo info : getSelectedInjections()) {
                if (!info.bundled) {
                    enabled = true;
                    break;
                }
            }
            return enabled;
        }
    });
    decorator.setRemoveAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            performRemove();
        }
    });
    decorator.setEditActionUpdater(new AnActionButtonUpdater() {

        @Override
        public boolean isEnabled(AnActionEvent e) {
            AnAction edit = getEditAction();
            if (edit != null)
                edit.update(e);
            return edit != null && edit.getTemplatePresentation().isEnabled();
        }
    });
    decorator.setEditAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            performEditAction();
        }
    });
    decorator.addExtraAction(new DumbAwareActionButton("Duplicate", "Duplicate", PlatformIcons.COPY_ICON) {

        @Override
        public boolean isEnabled() {
            return getEditAction() != null;
        }

        @Override
        public void actionPerformed(@NotNull AnActionEvent e) {
            final InjInfo injection = getSelectedInjection();
            if (injection != null) {
                addInjection(injection.injection.copy());
            //performEditAction(e);
            }
        }
    });
    decorator.addExtraAction(new DumbAwareActionButton("Enable Selected Injections", "Enable Selected Injections", PlatformIcons.SELECT_ALL_ICON) {

        @Override
        public void actionPerformed(@NotNull final AnActionEvent e) {
            performSelectedInjectionsEnabled(true);
        }
    });
    decorator.addExtraAction(new DumbAwareActionButton("Disable Selected Injections", "Disable Selected Injections", PlatformIcons.UNSELECT_ALL_ICON) {

        @Override
        public void actionPerformed(@NotNull final AnActionEvent e) {
            performSelectedInjectionsEnabled(false);
        }
    });
    new DumbAwareAction("Toggle") {

        @Override
        public void update(@NotNull AnActionEvent e) {
            SpeedSearchSupply supply = SpeedSearchSupply.getSupply(myInjectionsTable);
            e.getPresentation().setEnabled(supply == null || !supply.isPopupActive());
        }

        @Override
        public void actionPerformed(@NotNull final AnActionEvent e) {
            performToggleAction();
        }
    }.registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0)), myInjectionsTable);
    if (myInfos.length > 1) {
        AnActionButton shareAction = new DumbAwareActionButton("Move to IDE Scope", null, PlatformIcons.IMPORT_ICON) {

            {
                addCustomUpdater(new AnActionButtonUpdater() {

                    @Override
                    public boolean isEnabled(AnActionEvent e) {
                        CfgInfo cfg = getTargetCfgInfo(getSelectedInjections());
                        e.getPresentation().setText(cfg == getDefaultCfgInfo() ? "Move to IDE Scope" : "Move to Project Scope");
                        return cfg != null;
                    }
                });
            }

            @Override
            public void actionPerformed(@NotNull 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);
            }

            @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 null;
                for (CfgInfo info : myInfos) {
                    if (info != cfg)
                        return info;
                }
                throw new AssertionError();
            }
        };
        shareAction.setShortcut(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, InputEvent.SHIFT_DOWN_MASK)));
        decorator.addExtraAction(shareAction);
    }
    decorator.addExtraAction(new DumbAwareActionButton("Import", "Import", AllIcons.Actions.Install) {

        @Override
        public void actionPerformed(@NotNull final AnActionEvent e) {
            doImportAction(e.getDataContext());
            updateCountLabel();
        }
    });
    decorator.addExtraAction(new DumbAwareActionButton("Export", "Export", AllIcons.Actions.Export) {

        @Override
        public void actionPerformed(@NotNull 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);
            try {
                JdomKt.write(configuration.getState(), wrapper.getFile().toPath());
            } catch (IOException ex) {
                final String msg = ex.getLocalizedMessage();
                Messages.showErrorDialog(myProject, msg != null && msg.length() > 0 ? msg : ex.toString(), "Export Failed");
            }
        }

        @Override
        public boolean isEnabled() {
            return !getSelectedInjections().isEmpty();
        }
    });
}
Also used : Language(com.intellij.lang.Language) AllIcons(com.intellij.icons.AllIcons) VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileSaverDescriptor(com.intellij.openapi.fileChooser.FileSaverDescriptor) ColumnInfo(com.intellij.util.ui.ColumnInfo) THashSet(gnu.trove.THashSet) TableCellRenderer(javax.swing.table.TableCellRenderer) TObjectHashingStrategy(gnu.trove.TObjectHashingStrategy) Nls(org.jetbrains.annotations.Nls) VirtualFileWrapper(com.intellij.openapi.vfs.VirtualFileWrapper) FileTypes(com.intellij.openapi.fileTypes.FileTypes) FileChooserDescriptor(com.intellij.openapi.fileChooser.FileChooserDescriptor) Messages(com.intellij.openapi.ui.Messages) NullableFactory(com.intellij.openapi.util.NullableFactory) TableView(com.intellij.ui.table.TableView) FileChooserFactory(com.intellij.openapi.fileChooser.FileChooserFactory) Configurable(com.intellij.openapi.options.Configurable) ReferenceInjector(com.intellij.psi.injection.ReferenceInjector) KeyEvent(java.awt.event.KeyEvent) com.intellij.ui(com.intellij.ui) SpeedSearchSupply(com.intellij.ui.speedSearch.SpeedSearchSupply) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) SplitterProportionsData(com.intellij.openapi.ui.SplitterProportionsData) InjectionPlace(org.intellij.plugins.intelliLang.inject.config.InjectionPlace) com.intellij.util(com.intellij.util) NotNull(org.jetbrains.annotations.NotNull) Factory(com.intellij.openapi.util.Factory) InputEvent(java.awt.event.InputEvent) java.util(java.util) InjectedLanguage(org.intellij.plugins.intelliLang.inject.InjectedLanguage) ContainerUtil(com.intellij.util.containers.ContainerUtil) Comparing(com.intellij.openapi.util.Comparing) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection) Project(com.intellij.openapi.project.Project) ListTableModel(com.intellij.util.ui.ListTableModel) DataManager(com.intellij.ide.DataManager) AbstractLanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.AbstractLanguageInjectionSupport) InjectorUtils(org.intellij.plugins.intelliLang.inject.InjectorUtils) SplitterProportionsDataImpl(com.intellij.ide.ui.SplitterProportionsDataImpl) StdFileTypes(com.intellij.openapi.fileTypes.StdFileTypes) StringUtil(com.intellij.openapi.util.text.StringUtil) Convertor(com.intellij.util.containers.Convertor) IOException(java.io.IOException) FileType(com.intellij.openapi.fileTypes.FileType) MouseEvent(java.awt.event.MouseEvent) java.awt(java.awt) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) LanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.LanguageInjectionSupport) FileChooser(com.intellij.openapi.fileChooser.FileChooser) SearchableConfigurable(com.intellij.openapi.options.SearchableConfigurable) javax.swing(javax.swing) NullableFactory(com.intellij.openapi.util.NullableFactory) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) NotNull(org.jetbrains.annotations.NotNull) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection) SpeedSearchSupply(com.intellij.ui.speedSearch.SpeedSearchSupply) List(java.util.List) AbstractLanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.AbstractLanguageInjectionSupport) LanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.LanguageInjectionSupport) IOException(java.io.IOException) FileSaverDescriptor(com.intellij.openapi.fileChooser.FileSaverDescriptor) VirtualFileWrapper(com.intellij.openapi.vfs.VirtualFileWrapper)

Example 4 with FileSaverDescriptor

use of com.intellij.openapi.fileChooser.FileSaverDescriptor in project intellij-community by JetBrains.

the class FileChooserUtil method getFileToSelect.

@Nullable
public static VirtualFile getFileToSelect(@NotNull FileChooserDescriptor descriptor, @Nullable Project project, @Nullable VirtualFile toSelect, @Nullable VirtualFile lastPath) {
    boolean chooseDir = descriptor instanceof FileSaverDescriptor;
    VirtualFile result;
    if (toSelect == null && lastPath == null) {
        result = project == null ? null : project.getBaseDir();
    } else if (toSelect != null && lastPath != null) {
        if (Boolean.TRUE.equals(descriptor.getUserData(PathChooserDialog.PREFER_LAST_OVER_EXPLICIT))) {
            result = lastPath;
        } else {
            result = toSelect;
        }
    } else if (toSelect == null) {
        result = lastPath;
    } else {
        result = toSelect;
    }
    if (result != null) {
        if (chooseDir && !result.isDirectory()) {
            result = result.getParent();
        }
    } else if (SystemInfo.isUnix) {
        result = VfsUtil.getUserHomeDir();
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileSaverDescriptor(com.intellij.openapi.fileChooser.FileSaverDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with FileSaverDescriptor

use of com.intellij.openapi.fileChooser.FileSaverDescriptor in project android by JetBrains.

the class ConvertToNinePatchAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final VirtualFile pngFile = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
    if (!isPngFile(pngFile)) {
        return;
    }
    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();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileSaverDescriptor(com.intellij.openapi.fileChooser.FileSaverDescriptor) Task(com.intellij.openapi.progress.Task) FileSaverDialog(com.intellij.openapi.fileChooser.FileSaverDialog) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) IOException(java.io.IOException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) VirtualFileWrapper(com.intellij.openapi.vfs.VirtualFileWrapper)

Aggregations

FileSaverDescriptor (com.intellij.openapi.fileChooser.FileSaverDescriptor)13 VirtualFileWrapper (com.intellij.openapi.vfs.VirtualFileWrapper)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 FileSaverDialog (com.intellij.openapi.fileChooser.FileSaverDialog)8 Project (com.intellij.openapi.project.Project)4 File (java.io.File)4 IOException (java.io.IOException)3 Nullable (org.jetbrains.annotations.Nullable)3 AllIcons (com.intellij.icons.AllIcons)2 FileChooser (com.intellij.openapi.fileChooser.FileChooser)2 FileChooserDescriptor (com.intellij.openapi.fileChooser.FileChooserDescriptor)2 FileChooserFactory (com.intellij.openapi.fileChooser.FileChooserFactory)2 FileTypes (com.intellij.openapi.fileTypes.FileTypes)2 StdFileTypes (com.intellij.openapi.fileTypes.StdFileTypes)2 SearchableConfigurable (com.intellij.openapi.options.SearchableConfigurable)2 DumbAwareAction (com.intellij.openapi.project.DumbAwareAction)2 Messages (com.intellij.openapi.ui.Messages)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2 com.intellij.ui (com.intellij.ui)2 KeyEvent (java.awt.event.KeyEvent)2