From 2e6db86bf0e1a5229d22115e898aa5e80baf74ef Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Fri, 8 Nov 2013 15:37:23 +0100 Subject: [PATCH] IDEA-100279 ChooseComponentsToExportDialog --- .../ChooseComponentsToExportDialog.java | 50 +++++++++++-------- .../ide/actions/ImportSettingsAction.java | 25 ++++++---- 2 files changed, 45 insertions(+), 30 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/ide/actions/ChooseComponentsToExportDialog.java b/platform/platform-impl/src/com/intellij/ide/actions/ChooseComponentsToExportDialog.java index 0122e88489ef..7bbc3e0c6cba 100644 --- a/platform/platform-impl/src/com/intellij/ide/actions/ChooseComponentsToExportDialog.java +++ b/platform/platform-impl/src/com/intellij/ide/actions/ChooseComponentsToExportDialog.java @@ -26,11 +26,13 @@ import com.intellij.openapi.fileChooser.FileChooserDescriptor; import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory; import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.VerticalFlowLayout; +import com.intellij.openapi.util.AsyncResult; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.ui.FieldPanel; +import com.intellij.util.Consumer; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -86,11 +88,13 @@ public class ChooseComponentsToExportDialog extends DialogWrapper { final ActionListener browseAction = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - String oldPath = myPathPanel.getText(); - String path = chooseSettingsFile(oldPath, getWindow(), IdeBundle.message("title.export.file.location"), - IdeBundle.message("prompt.choose.export.settings.file.path")); - if (path == null) return; - myPathPanel.setText(FileUtil.toSystemDependentName(path)); + chooseSettingsFile(myPathPanel.getText(), getWindow(), IdeBundle.message("title.export.file.location"), IdeBundle.message("prompt.choose.export.settings.file.path")) + .doWhenDone(new Consumer() { + @Override + public void consume(String path) { + myPathPanel.setText(FileUtil.toSystemDependentName(path)); + } + }); } }; @@ -167,8 +171,8 @@ public class ChooseComponentsToExportDialog extends DialogWrapper { return file != null; } - @Nullable - public static String chooseSettingsFile(String oldPath, Component parent, final String title, final String description) { + @NotNull + public static AsyncResult chooseSettingsFile(String oldPath, Component parent, final String title, final String description) { FileChooserDescriptor chooserDescriptor = FileChooserDescriptorFactory.createSingleLocalFileDescriptor(); chooserDescriptor.setDescription(description); chooserDescriptor.setHideIgnored(false); @@ -185,19 +189,25 @@ public class ChooseComponentsToExportDialog extends DialogWrapper { else { initialDir = null; } - final VirtualFile file = FileChooser.chooseFile(chooserDescriptor, parent, null, initialDir); - if (file == null) { - return null; - } - String path; - if (file.isDirectory()) { - String defaultName = new File(DEFAULT_PATH).getName(); - path = file.getPath() + "/" + defaultName; - } - else { - path = file.getPath(); - } - return path; + final AsyncResult result = new AsyncResult(); + FileChooser.chooseFiles(chooserDescriptor, null, parent, initialDir, new FileChooser.FileChooserConsumer() { + @Override + public void consume(List files) { + VirtualFile file = files.get(0); + if (file.isDirectory()) { + result.setDone(file.getPath() + '/' + new File(DEFAULT_PATH).getName()); + } + else { + result.setDone(file.getPath()); + } + } + + @Override + public void cancelled() { + result.setRejected(); + } + }); + return result; } @Override diff --git a/platform/platform-impl/src/com/intellij/ide/actions/ImportSettingsAction.java b/platform/platform-impl/src/com/intellij/ide/actions/ImportSettingsAction.java index 539cbd7fd324..efe1ac6b405a 100644 --- a/platform/platform-impl/src/com/intellij/ide/actions/ImportSettingsAction.java +++ b/platform/platform-impl/src/com/intellij/ide/actions/ImportSettingsAction.java @@ -35,6 +35,7 @@ import com.intellij.openapi.project.DumbAware; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.updateSettings.impl.UpdateSettings; import com.intellij.openapi.util.io.FileUtil; +import com.intellij.util.Consumer; import com.intellij.util.io.ZipUtil; import java.awt.*; @@ -47,14 +48,19 @@ import java.util.zip.ZipException; import java.util.zip.ZipFile; public class ImportSettingsAction extends AnAction implements DumbAware { + @Override public void actionPerformed(AnActionEvent e) { final DataContext dataContext = e.getDataContext(); final Component component = PlatformDataKeys.CONTEXT_COMPONENT.getData(dataContext); - final String path = ChooseComponentsToExportDialog.chooseSettingsFile(PathManager.getConfigPath(), component, - IdeBundle.message("title.import.file.location"), - IdeBundle.message("prompt.choose.import.file.path")); - if (path == null) return; + ChooseComponentsToExportDialog.chooseSettingsFile(PathManager.getConfigPath(), component, IdeBundle.message("title.import.file.location"), IdeBundle.message("prompt.choose.import.file.path")).doWhenDone(new Consumer() { + @Override + public void consume(String path) { + doImport(path); + } + }); + } + private static void doImport(String path) { final File saveFile = new File(path); try { if (!saveFile.exists()) { @@ -62,9 +68,8 @@ public class ImportSettingsAction extends AnAction implements DumbAware { IdeBundle.message("title.file.not.found")); return; } - final ZipFile zipFile = new ZipFile(saveFile); - final ZipEntry magicEntry = zipFile.getEntry(ImportSettingsFilenameFilter.SETTINGS_JAR_MARKER); + final ZipEntry magicEntry = new ZipFile(saveFile).getEntry(ImportSettingsFilenameFilter.SETTINGS_JAR_MARKER); if (magicEntry == null) { Messages.showErrorDialog( IdeBundle.message("error.file.contains.no.settings.to.import", presentableFileName(saveFile), promptLocationMessage()), @@ -76,8 +81,8 @@ public class ImportSettingsAction extends AnAction implements DumbAware { final Map> filesToComponents = ExportSettingsAction.getRegisteredComponentsAndFiles(registeredComponents); List components = getComponentsStored(saveFile, registeredComponents); final ChooseComponentsToExportDialog dialog = new ChooseComponentsToExportDialog(components, filesToComponents, false, - IdeBundle.message("title.select.components.to.import"), - IdeBundle.message("prompt.check.components.to.import")); + IdeBundle.message("title.select.components.to.import"), + IdeBundle.message("prompt.check.components.to.import")); dialog.show(); if (!dialog.isOK()) return; final Set chosenComponents = dialog.getExportableComponents(); @@ -115,13 +120,13 @@ public class ImportSettingsAction extends AnAction implements DumbAware { ApplicationNamesInfo.getInstance().getFullProductName()), IdeBundle.message("title.restart.needed"), Messages.getQuestionIcon()); if (ret == 0) { - ((ApplicationEx) ApplicationManager.getApplication()).restart(true); + ((ApplicationEx)ApplicationManager.getApplication()).restart(true); } } catch (ZipException e1) { Messages.showErrorDialog( IdeBundle.message("error.reading.settings.file", presentableFileName(saveFile), e1.getMessage(), promptLocationMessage()), - IdeBundle.message("title.invalid.file")); + IdeBundle.message("title.invalid.file")); } catch (IOException e1) { Messages.showErrorDialog(IdeBundle.message("error.reading.settings.file.2", presentableFileName(saveFile), e1.getMessage()),