IDEA-100279 ChooseComponentsToExportDialog

This commit is contained in:
Vladimir Krivosheev
2013-11-08 16:43:34 +01:00
parent 72ee12cf41
commit 2e6db86bf0
2 changed files with 45 additions and 30 deletions
@@ -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<String>() {
@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<String> 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<String> result = new AsyncResult<String>();
FileChooser.chooseFiles(chooserDescriptor, null, parent, initialDir, new FileChooser.FileChooserConsumer() {
@Override
public void consume(List<VirtualFile> 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
@@ -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<String>() {
@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<File, Set<ExportableComponent>> filesToComponents = ExportSettingsAction.getRegisteredComponentsAndFiles(registeredComponents);
List<ExportableComponent> 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<ExportableComponent> 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()),