import old configs: allow product to provide custom path selector for searching for config folders; consider 'options' folder presence on Windows and Linux

This commit is contained in:
Kirill.Safonov
2011-09-30 20:33:17 +04:00
parent 832b8215c5
commit 21f6a9cdc5
3 changed files with 120 additions and 95 deletions
@@ -20,6 +20,7 @@ import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.PlatformUtils;
import com.intellij.util.SystemProperties;
import com.intellij.util.ThreeState;
import com.intellij.util.ui.UIUtil;
import com.intellij.ui.AppUIUtil;
import org.jetbrains.annotations.NonNls;
@@ -39,22 +40,21 @@ public class ConfigImportHelper {
@NonNls private static final String BUILD_NUMBER_FILE = "build.txt";
@NonNls private static final String PLUGINS_PATH = "plugins";
@NonNls private static final String BIN_FOLDER = "bin";
@NonNls private static final String OPTIONS_XML = "options/options.xml";
@NonNls private static final String OPTIONS_XML = SystemInfo.isMac ? "options/options.xml" : "config/options/options.xml";
private ConfigImportHelper() {}
public static void importConfigsTo(String newConfigPath) {
File oldConfigDir = findOldConfigDir(newConfigPath);
ConfigImportSettings settings = getConfigImportSettings();
File oldConfigDir = findOldConfigDir(newConfigPath, settings.getCustomPathsSelector());
do {
ImportOldConfigsPanel dlg = getCustomDialog(oldConfigDir);
if (dlg == null) {
if (UIUtil.hasJdk6Dialogs()) {
dlg = new ImportOldConfigsPanel(oldConfigDir);
}
else {
dlg = new ImportOldConfigsPanel(oldConfigDir, JOptionPane.getRootFrame());
}
ImportOldConfigsPanel dlg;
if (UIUtil.hasJdk6Dialogs()) {
dlg = new ImportOldConfigsPanel(oldConfigDir, settings);
}
else {
dlg = new ImportOldConfigsPanel(oldConfigDir, JOptionPane.getRootFrame(), settings);
}
UIUtil.setToolkitModal(dlg);
@@ -63,10 +63,10 @@ public class ConfigImportHelper {
if (dlg.isImportEnabled()) {
File instHome = dlg.getSelectedFile();
oldConfigDir = getOldConfigDir(instHome);
if (!validateOldConfigDir(instHome, oldConfigDir)) continue;
if (!validateOldConfigDir(instHome, oldConfigDir, settings)) continue;
doImport(newConfigPath, oldConfigDir);
dlg.importFinished(newConfigPath);
settings.importFinished(newConfigPath);
}
break;
@@ -74,16 +74,15 @@ public class ConfigImportHelper {
while (true);
}
@Nullable
private static ImportOldConfigsPanel getCustomDialog(File oldConfigDir) {
private static ConfigImportSettings getConfigImportSettings() {
try {
Class customDialogClass =
Class.forName("com.intellij.openapi.application." + PlatformUtils.getPlatformPrefix() + "ImportOldConfigsPanel");
if (customDialogClass != null) {
if (ImportOldConfigsPanel.class.isAssignableFrom(customDialogClass)) {
Constructor constructor = customDialogClass.getDeclaredConstructor(File.class);
Class customProviderClass =
Class.forName("com.intellij.openapi.application." + PlatformUtils.getPlatformPrefix() + "ConfigImportSettings");
if (customProviderClass != null) {
if (ConfigImportSettings.class.isAssignableFrom(customProviderClass)) {
Constructor constructor = customProviderClass.getDeclaredConstructor();
if (constructor != null) {
return (ImportOldConfigsPanel)constructor.newInstance(oldConfigDir);
return (ConfigImportSettings)constructor.newInstance();
}
}
}
@@ -98,23 +97,29 @@ public class ConfigImportHelper {
}
catch (IllegalAccessException ignored) {
}
return null;
return new ConfigImportSettings();
}
private static File findOldConfigDir(String newConfigPath) {
private static File findOldConfigDir(String newConfigPath, @Nullable String customPathSelector) {
final File configDir = new File(newConfigPath);
final File selectorDir = SystemInfo.isMac ? configDir : configDir.getParentFile();
final File parent = selectorDir.getParentFile();
if (parent == null || !parent.exists()) return null;
File maxFile = null;
long lastModified = 0;
final String selector = PathManager.getPathsSelector() != null ? PathManager.getPathsSelector() : selectorDir.getName();
final String selector;
if (customPathSelector != null) {
selector = customPathSelector;
}
else {
selector = PathManager.getPathsSelector() != null ? PathManager.getPathsSelector() : selectorDir.getName();
}
final String prefix = selector.replaceAll("\\d", "");
for (File file : parent.listFiles(new FilenameFilter() {
@Override
public boolean accept(File file, String name) {
return name.length() == selector.length() &&
name.startsWith(selector.replaceAll("\\d",""));
return name.length() == selector.length() && name.startsWith(prefix);
}
})) {
final File options = new File(file, OPTIONS_XML);
@@ -139,15 +144,14 @@ public class ConfigImportHelper {
}
}
public static boolean validateOldConfigDir(final File instHome, final File oldConfigDir) {
public static boolean validateOldConfigDir(final File instHome, final File oldConfigDir, ConfigImportSettings settings) {
if (oldConfigDir == null) {
final String message = !instHome.equals(oldConfigDir) ?
ApplicationBundle.message("error.invalid.installation.home", instHome.getAbsolutePath(),
ApplicationNamesInfo.getInstance().getFullProductName()) :
settings.getProductName(ThreeState.YES)) :
ApplicationBundle.message("error.invalid.config.folder", instHome.getAbsolutePath(),
ApplicationNamesInfo.getInstance().getFullProductName()) ;
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
message);
settings.getProductName(ThreeState.YES)) ;
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), message);
return false;
}
@@ -0,0 +1,68 @@
package com.intellij.openapi.application;
import com.intellij.util.SystemProperties;
import com.intellij.util.ThreeState;
import org.jetbrains.annotations.Nullable;
import java.io.File;
/**
* User: ksafonov
*/
public class ConfigImportSettings {
protected String getProductName(ThreeState full) {
ApplicationNamesInfo namesInfo = ApplicationNamesInfo.getInstance();
if (full == ThreeState.YES) {
return namesInfo.getFullProductName();
}
else if (full == ThreeState.NO) {
return namesInfo.getProductName();
}
else {
return namesInfo.getProductName().equals("IDEA") ? namesInfo.getFullProductName() : namesInfo.getProductName();
}
}
protected String getInaccessibleHomeErrorText(String instHome) {
return ApplicationBundle.message("error.no.read.permissions", instHome);
}
protected String getInvalidHomeErrorText(String productWithVendor, String instHome) {
return ApplicationBundle.message("error.does.not.appear.to.be.installation.home", instHome,
productWithVendor);
}
protected String getCurrentHomeErrorText(String productWithVendor) {
return ApplicationBundle.message("error.selected.current.installation.home",
productWithVendor, productWithVendor);
}
protected String getEmptyHomeErrorText(String productWithVendor) {
return ApplicationBundle.message("error.please.select.previous.installation.home", productWithVendor);
}
protected String getHomeLabel(String productName) {
return ApplicationBundle.message("editbox.installation.home", productName);
}
protected String getAutoImportLabel(File guessedOldConfig) {
return ApplicationBundle.message("radio.import.auto", guessedOldConfig.getAbsolutePath().replace(SystemProperties.getUserHome(), "~"));
}
protected String getDoNotImportLabel(String productName) {
return ApplicationBundle.message("radio.do.not.import", productName);
}
protected String getTitleLabel(String productName) {
return ApplicationBundle.message("label.you.can.import", productName);
}
@Nullable
public String getCustomPathsSelector() {
return null;
}
public void importFinished(String newConfigPath) {
}
}
@@ -19,7 +19,6 @@ import com.intellij.openapi.MnemonicHelper;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.SystemProperties;
import com.intellij.util.ThreeState;
import org.jetbrains.annotations.Nullable;
@@ -45,17 +44,20 @@ public class ImportOldConfigsPanel extends JDialog {
private JLabel myHomeLabel;
private JRadioButton myRbImportAuto;
private final File myGuessedOldConfig;
private final ConfigImportSettings mySettings;
public ImportOldConfigsPanel(final File guessedOldConfig, final Frame owner) {
public ImportOldConfigsPanel(final File guessedOldConfig, final Frame owner, ConfigImportSettings settings) {
super(owner, true);
myGuessedOldConfig = guessedOldConfig;
mySettings = settings;
init();
}
public ImportOldConfigsPanel(final File guessedOldConfig) {
public ImportOldConfigsPanel(final File guessedOldConfig, ConfigImportSettings settings) {
super((Dialog) null, true);
myGuessedOldConfig = guessedOldConfig;
mySettings = settings;
init();
}
@@ -68,16 +70,16 @@ public class ImportOldConfigsPanel extends JDialog {
group.add(myRbImportAuto);
myRbDoNotImport.setSelected(true);
final String productName = getProductName(ThreeState.UNSURE);
mySuggestLabel.setText(getTitleLabel(productName));
myRbDoNotImport.setText(getDoNotImportLabel(productName));
final String productName = mySettings.getProductName(ThreeState.UNSURE);
mySuggestLabel.setText(mySettings.getTitleLabel(productName));
myRbDoNotImport.setText(mySettings.getDoNotImportLabel(productName));
if(myGuessedOldConfig != null) {
myRbImportAuto.setText(getAutoImportLabel(myGuessedOldConfig));
myRbImportAuto.setText(mySettings.getAutoImportLabel(myGuessedOldConfig));
myRbImportAuto.setSelected(true);
} else {
myRbImportAuto.setVisible(false);
}
myHomeLabel.setText(getHomeLabel(productName));
myHomeLabel.setText(mySettings.getHomeLabel(productName));
myRbImport.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
@@ -138,22 +140,6 @@ public class ImportOldConfigsPanel extends JDialog {
setLocation((parentSize.width - ownSize.width) / 2, (parentSize.height - ownSize.height) / 2);
}
protected String getProductName(ThreeState full) {
ApplicationNamesInfo namesInfo = ApplicationNamesInfo.getInstance();
if (full == ThreeState.YES) {
return namesInfo.getFullProductName();
}
else if (full == ThreeState.NO) {
return namesInfo.getProductName();
}
else {
return namesInfo.getProductName().equals("IDEA") ? namesInfo.getFullProductName() : namesInfo.getProductName();
}
}
public void importFinished(String newConfigPath) {
}
@Nullable
private static String findPreviousInstallationWindows(String productName) {
String programFiles = System.getenv("ProgramFiles");
@@ -194,32 +180,32 @@ public class ImportOldConfigsPanel extends JDialog {
private void close() {
if (isImportEnabled()) {
final String productWithVendor = getProductName(ThreeState.YES);
final String productWithVendor = mySettings.getProductName(ThreeState.YES);
String instHome = myPrevInstallation.getText();
if ("".equals(instHome)) {
JOptionPane.showMessageDialog(this,
getEmptyHomeErrorText(productWithVendor),
mySettings.getEmptyHomeErrorText(productWithVendor),
ApplicationBundle.message("title.installation.home.required"), JOptionPane.ERROR_MESSAGE);
return;
}
if (PathManager.getHomePath().equals(instHome)) {
JOptionPane.showMessageDialog(this,
getCurrentHomeErrorText(productWithVendor),
mySettings.getCurrentHomeErrorText(productWithVendor),
ApplicationBundle.message("title.installation.home.required"), JOptionPane.ERROR_MESSAGE);
return;
}
if (myRbImport.isSelected() && !ConfigImportHelper.isInstallationHomeOrConfig(instHome, getProductName(ThreeState.NO))) {
if (myRbImport.isSelected() && !ConfigImportHelper.isInstallationHomeOrConfig(instHome, mySettings.getProductName(ThreeState.NO))) {
JOptionPane.showMessageDialog(this,
getInvalidHomeErrorText(productWithVendor, instHome),
mySettings.getInvalidHomeErrorText(productWithVendor, instHome),
ApplicationBundle.message("title.installation.home.required"), JOptionPane.ERROR_MESSAGE);
return;
}
if (!new File(instHome).canRead()) {
JOptionPane.showMessageDialog(this,
getInaccessibleHomeErrorText(instHome),
mySettings.getInaccessibleHomeErrorText(instHome),
ApplicationBundle.message("title.installation.home.required"), JOptionPane.ERROR_MESSAGE);
return;
}
@@ -241,42 +227,9 @@ public class ImportOldConfigsPanel extends JDialog {
}
public static void main(String[] args) {
ImportOldConfigsPanel dlg = new ImportOldConfigsPanel(null);
ImportOldConfigsPanel dlg = new ImportOldConfigsPanel(null, new ConfigImportSettings());
dlg.setVisible(true);
}
protected String getInaccessibleHomeErrorText(String instHome) {
return ApplicationBundle.message("error.no.read.permissions", instHome);
}
protected String getInvalidHomeErrorText(String productWithVendor, String instHome) {
return ApplicationBundle.message("error.does.not.appear.to.be.installation.home", instHome,
productWithVendor);
}
protected String getCurrentHomeErrorText(String productWithVendor) {
return ApplicationBundle.message("error.selected.current.installation.home",
productWithVendor, productWithVendor);
}
protected String getEmptyHomeErrorText(String productWithVendor) {
return ApplicationBundle.message("error.please.select.previous.installation.home", productWithVendor);
}
protected String getHomeLabel(String productName) {
return ApplicationBundle.message("editbox.installation.home", productName);
}
protected String getAutoImportLabel(File guessedOldConfig) {
return ApplicationBundle.message("radio.import.auto", guessedOldConfig.getAbsolutePath().replace(SystemProperties.getUserHome(), "~"));
}
protected String getDoNotImportLabel(String productName) {
return ApplicationBundle.message("radio.do.not.import", productName);
}
protected String getTitleLabel(String productName) {
return ApplicationBundle.message("label.you.can.import", productName);
}
}