maven: properly setup content root for a new maven module (IDEA-66021)

This commit is contained in:
nik
2018-01-18 17:44:37 +03:00
parent 35b77e8d95
commit 18422f8056
5 changed files with 55 additions and 22 deletions
@@ -41,7 +41,7 @@ import java.io.File;
/**
* @author nik
*/
public class ModuleNameLocationComponent {
public class ModuleNameLocationComponent implements ModuleNameLocationSettings {
private final WizardContext myWizardContext;
private JTextField myModuleName;
private TextFieldWithBrowseButton myModuleContentRoot;
@@ -58,6 +58,7 @@ public class ModuleNameLocationComponent {
private boolean myImlLocationDocListenerEnabled = true;
private boolean myUpdatePathsWhenNameIsChanged;
private boolean myUpdateNameWhenPathIsChanged;
public ModuleNameLocationComponent(@NotNull WizardContext wizardContext) {
myWizardContext = wizardContext;
@@ -113,7 +114,7 @@ public class ModuleNameLocationComponent {
namePathComponent.getPathComponent().getDocument().addDocumentListener(new DocumentAdapter() {
protected void textChanged(final DocumentEvent e) {
if (!myContentRootChangedByUser) {
setModuleContentRoot(namePathComponent.getPath());
setModuleContentRoot(namePathComponent.getPath(), true);
}
}
});
@@ -132,10 +133,7 @@ public class ModuleNameLocationComponent {
path += "/" + moduleName;
}
if (!myContentRootChangedByUser) {
final boolean f = myModuleNameChangedByUser;
myModuleNameChangedByUser = true;
setModuleContentRoot(path);
myModuleNameChangedByUser = f;
}
if (!myImlLocationChangedByUser) {
setImlFileLocation(path);
@@ -150,20 +148,14 @@ public class ModuleNameLocationComponent {
if (!myImlLocationChangedByUser) {
setImlFileLocation(getModuleContentRoot());
}
if (!myModuleNameChangedByUser) {
if (!myModuleNameChangedByUser && myUpdateNameWhenPathIsChanged) {
final String path = FileUtil.toSystemIndependentName(getModuleContentRoot());
final int idx = path.lastIndexOf("/");
boolean f = myContentRootChangedByUser;
myContentRootChangedByUser = true;
boolean i = myImlLocationChangedByUser;
myImlLocationChangedByUser = true;
boolean oldValue = myUpdatePathsWhenNameIsChanged;
myUpdatePathsWhenNameIsChanged = false;
setModuleName(idx >= 0 ? path.substring(idx + 1) : "");
myContentRootChangedByUser = f;
myImlLocationChangedByUser = i;
myUpdatePathsWhenNameIsChanged = oldValue;
}
}
});
@@ -264,7 +256,8 @@ public class ModuleNameLocationComponent {
return true;
}
private String getModuleContentRoot() {
@NotNull
public String getModuleContentRoot() {
return myModuleContentRoot.getText();
}
@@ -289,13 +282,19 @@ public class ModuleNameLocationComponent {
myImlLocationDocListenerEnabled = true;
}
private void setModuleContentRoot(final String path) {
public void setModuleContentRoot(@NotNull final String path) {
setModuleContentRoot(path, false);
}
private void setModuleContentRoot(@NotNull String path, boolean updateName) {
myUpdateNameWhenPathIsChanged = updateName;
myContentRootDocListenerEnabled = false;
myModuleContentRoot.setText(FileUtil.toSystemDependentName(path));
myContentRootDocListenerEnabled = true;
myUpdateNameWhenPathIsChanged = true;
}
public void setModuleName(String moduleName) {
public void setModuleName(@NotNull String moduleName) {
myModuleNameDocListenerEnabled = false;
myModuleName.setText(moduleName);
myModuleNameDocListenerEnabled = true;
@@ -305,7 +304,8 @@ public class ModuleNameLocationComponent {
return myModuleName;
}
private String getModuleName() {
@NotNull
public String getModuleName() {
return myModuleName.getText().trim();
}
}
@@ -221,6 +221,12 @@ public class ProjectSettingsStep extends ModuleWizardStep implements SettingsSte
return getNameComponent();
}
@Nullable
@Override
public ModuleNameLocationSettings getModuleNameLocationSettings() {
return myModuleNameLocationComponent;
}
@TestOnly
@Nullable
public ModuleWizardStep getSettingsStep() {
@@ -0,0 +1,16 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.util.projectWizard;
import org.jetbrains.annotations.NotNull;
public interface ModuleNameLocationSettings {
@NotNull
String getModuleName();
void setModuleName(@NotNull String moduleName);
@NotNull
String getModuleContentRoot();
void setModuleContentRoot(@NotNull String path);
}
@@ -34,6 +34,14 @@ public interface SettingsStep {
void addExpertPanel(@NotNull JComponent panel);
void addExpertField(@NotNull String label, @NotNull JComponent field);
/**
* @deprecated use {@link #getModuleNameLocationSettings()} instead
*/
@Nullable
JTextField getModuleNameField();
@Nullable
default ModuleNameLocationSettings getModuleNameLocationSettings() {
return null;
}
}
@@ -228,9 +228,12 @@ public class MavenModuleBuilder extends ModuleBuilder implements SourcePathsBuil
@Nullable
@Override
public ModuleWizardStep modifySettingsStep(@NotNull SettingsStep settingsStep) {
final JTextField moduleNameField = settingsStep.getModuleNameField();
if (moduleNameField != null && myProjectId != null && myProjectId.getArtifactId() != null) {
moduleNameField.setText(StringUtil.sanitizeJavaIdentifier(myProjectId.getArtifactId()));
final ModuleNameLocationSettings nameLocationSettings = settingsStep.getModuleNameLocationSettings();
if (nameLocationSettings != null && myProjectId != null && myProjectId.getArtifactId() != null) {
nameLocationSettings.setModuleName(StringUtil.sanitizeJavaIdentifier(myProjectId.getArtifactId()));
if (myAggregatorProject != null) {
nameLocationSettings.setModuleContentRoot(myAggregatorProject.getDirectory() + "/" + myProjectId.getArtifactId());
}
}
return super.modifySettingsStep(settingsStep);
}