mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-85332 check if package name is already used in android new module wizard
This commit is contained in:
+44
-14
@@ -18,11 +18,15 @@ package org.jetbrains.android.newProject;
|
||||
import com.intellij.lexer.JavaLexer;
|
||||
import com.intellij.lexer.Lexer;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.options.ConfigurationException;
|
||||
import com.intellij.openapi.roots.ui.configuration.ModulesProvider;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.JavaTokenType;
|
||||
import com.intellij.ui.DocumentAdapter;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import org.jetbrains.android.dom.manifest.Manifest;
|
||||
import org.jetbrains.android.facet.AndroidFacet;
|
||||
import org.jetbrains.android.util.AndroidBundle;
|
||||
import org.jetbrains.android.util.AndroidCommonUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -45,7 +49,12 @@ public class AndroidAppPropertiesEditor {
|
||||
private JLabel myErrorLabel;
|
||||
private JPanel myContentPanel;
|
||||
|
||||
public AndroidAppPropertiesEditor(String moduleName) {
|
||||
private final ModulesProvider myModulesProvider;
|
||||
private boolean myApp;
|
||||
|
||||
public AndroidAppPropertiesEditor(String moduleName, ModulesProvider modulesProvider) {
|
||||
myModulesProvider = modulesProvider;
|
||||
|
||||
if (moduleName != null) {
|
||||
myApplicationNameField.setText(moduleName);
|
||||
myPackageNameField.setText(getDefaultPackageNameByModuleName(moduleName));
|
||||
@@ -58,7 +67,7 @@ public class AndroidAppPropertiesEditor {
|
||||
myPackageNameField.getDocument().addDocumentListener(new DocumentAdapter() {
|
||||
@Override
|
||||
protected void textChanged(DocumentEvent e) {
|
||||
String message = validatePackageName();
|
||||
String message = validatePackageName(!myApp);
|
||||
myErrorLabel.setText(message);
|
||||
}
|
||||
});
|
||||
@@ -71,6 +80,20 @@ public class AndroidAppPropertiesEditor {
|
||||
});
|
||||
}
|
||||
|
||||
public void update(boolean app) {
|
||||
myApplicationNameField.setEnabled(app);
|
||||
myHelloAndroidCheckBox.setEnabled(app);
|
||||
if (app) {
|
||||
updateActivityPanel();
|
||||
}
|
||||
else {
|
||||
UIUtil.setEnabled(myActivtiyPanel, app, true);
|
||||
}
|
||||
myApp = app;
|
||||
final String message = validatePackageName(!app);
|
||||
myErrorLabel.setText(message);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String getDefaultPackageNameByModuleName(@NotNull String moduleName) {
|
||||
return "com.example." + toIdentifier(moduleName);
|
||||
@@ -101,7 +124,7 @@ public class AndroidAppPropertiesEditor {
|
||||
UIUtil.setEnabled(myActivtiyPanel, myHelloAndroidCheckBox.isSelected(), true);
|
||||
}
|
||||
|
||||
private String validatePackageName() {
|
||||
private String validatePackageName(boolean library) {
|
||||
String candidate = myPackageNameField.getText().trim();
|
||||
if (candidate.length() == 0) {
|
||||
return AndroidBundle.message("specify.package.name.error");
|
||||
@@ -112,6 +135,21 @@ public class AndroidAppPropertiesEditor {
|
||||
if (!AndroidCommonUtils.contains2Identifiers(candidate)) {
|
||||
return AndroidBundle.message("package.name.must.contain.2.ids.error");
|
||||
}
|
||||
|
||||
if (!library) {
|
||||
for (Module module : myModulesProvider.getModules()) {
|
||||
final AndroidFacet facet = AndroidFacet.getInstance(module);
|
||||
if (facet != null && !facet.getConfiguration().LIBRARY_PROJECT) {
|
||||
final Manifest manifest = facet.getManifest();
|
||||
if (manifest != null) {
|
||||
final String packageName = manifest.getPackage().getValue();
|
||||
if (candidate.equals(packageName)) {
|
||||
return "Package name '" + packageName + "' is already used by module '" + module.getName() + "'";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -147,12 +185,12 @@ public class AndroidAppPropertiesEditor {
|
||||
return myContentPanel;
|
||||
}
|
||||
|
||||
public void validate(boolean testProject) throws ConfigurationException {
|
||||
String message = validatePackageName();
|
||||
public void validate(boolean library) throws ConfigurationException {
|
||||
String message = validatePackageName(library);
|
||||
if (message.length() > 0) {
|
||||
throw new ConfigurationException(message);
|
||||
}
|
||||
if (!testProject) {
|
||||
if (!library) {
|
||||
message = validateActivityName();
|
||||
if (message.length() > 0) {
|
||||
throw new ConfigurationException(message);
|
||||
@@ -172,10 +210,6 @@ public class AndroidAppPropertiesEditor {
|
||||
return myApplicationNameField.getText().trim();
|
||||
}
|
||||
|
||||
public JCheckBox getHelloAndroidCheckBox() {
|
||||
return myHelloAndroidCheckBox;
|
||||
}
|
||||
|
||||
public JTextField getApplicationNameField() {
|
||||
return myApplicationNameField;
|
||||
}
|
||||
@@ -183,8 +217,4 @@ public class AndroidAppPropertiesEditor {
|
||||
public JTextField getPackageNameField() {
|
||||
return myPackageNameField;
|
||||
}
|
||||
|
||||
public JPanel getActivtiyPanel() {
|
||||
return myActivtiyPanel;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -672,7 +672,7 @@ public class AndroidModuleBuilder extends JavaModuleBuilder {
|
||||
});
|
||||
}
|
||||
|
||||
steps.add(new AndroidModuleWizardStep(this, wizardContext));
|
||||
steps.add(new AndroidModuleWizardStep(this, wizardContext, modulesProvider));
|
||||
return steps.toArray(new ModuleWizardStep[steps.size()]);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.intellij.openapi.options.ConfigurationException;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.projectRoots.ProjectJdkTable;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.openapi.roots.ui.configuration.ModulesProvider;
|
||||
import com.intellij.openapi.ui.LabeledComponent;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
@@ -90,11 +91,11 @@ public class AndroidModuleWizardStep extends ModuleWizardStep {
|
||||
@NonNls private static final String TARGET_SELECTION_MODE_FOR_NEW_MODULE_PROPERTY = "ANDROID_TARGET_SELECTION_MODE_FOR_NEW_MODULE";
|
||||
@NonNls private static final String TARGET_AVD_FOR_NEW_MODULE_PROPERTY = "ANDROID_TARGET_AVD_FOR_NEW_MODULE";
|
||||
|
||||
public AndroidModuleWizardStep(@NotNull AndroidModuleBuilder moduleBuilder, WizardContext context) {
|
||||
public AndroidModuleWizardStep(@NotNull AndroidModuleBuilder moduleBuilder, WizardContext context, ModulesProvider modulesProvider) {
|
||||
super();
|
||||
myApplicationProjectButton.setSelected(true);
|
||||
|
||||
myAppPropertiesEditor = new AndroidAppPropertiesEditor(moduleBuilder.getName());
|
||||
myAppPropertiesEditor = new AndroidAppPropertiesEditor(moduleBuilder.getName(), modulesProvider);
|
||||
Project project = context.getProject();
|
||||
myTestPropertiesEditor = project != null ? new AndroidTestPropertiesEditor(project) : null;
|
||||
myPropertiesPanel.setLayout(new OverlayLayout(myPropertiesPanel));
|
||||
@@ -201,6 +202,7 @@ public class AndroidModuleWizardStep extends ModuleWizardStep {
|
||||
myDoNotCreateConfigurationRadioButton.setSelected(true);
|
||||
}
|
||||
}
|
||||
updatePropertiesEditor();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -223,15 +225,7 @@ public class AndroidModuleWizardStep extends ModuleWizardStep {
|
||||
if (myTestPropertiesEditor != null) {
|
||||
myTestPropertiesEditor.getContentPanel().setVisible(false);
|
||||
}
|
||||
boolean app = myApplicationProjectButton.isSelected();
|
||||
myAppPropertiesEditor.getApplicationNameField().setEnabled(app);
|
||||
myAppPropertiesEditor.getHelloAndroidCheckBox().setEnabled(app);
|
||||
if (app) {
|
||||
myAppPropertiesEditor.updateActivityPanel();
|
||||
}
|
||||
else {
|
||||
UIUtil.setEnabled(myAppPropertiesEditor.getActivtiyPanel(), app, true);
|
||||
}
|
||||
myAppPropertiesEditor.update(myApplicationProjectButton.isSelected());
|
||||
}
|
||||
else {
|
||||
myAppPropertiesEditor.getContentPanel().setVisible(false);
|
||||
@@ -285,7 +279,7 @@ public class AndroidModuleWizardStep extends ModuleWizardStep {
|
||||
}
|
||||
|
||||
if (myApplicationProjectButton.isSelected() || myLibProjectButton.isSelected()) {
|
||||
myAppPropertiesEditor.validate(myTestProjectButton.isSelected());
|
||||
myAppPropertiesEditor.validate(myLibProjectButton.isSelected());
|
||||
}
|
||||
else {
|
||||
assert myTestPropertiesEditor != null;
|
||||
|
||||
Reference in New Issue
Block a user