Skip to content

Commit

Permalink
Added a new project wizard to create a subproject of a multi-project …
Browse files Browse the repository at this point in the history
…build.
  • Loading branch information
kelemen committed Sep 7, 2012
1 parent 4aaed7d commit 969ad35
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ MSG_IllegalIdentifier=Illegal Java identifier.
MSG_ShouldNotUseDefaultPackage=Classes should not be placed into the default package.
MSG_InvalidGroupId=The Maven GroupId is invalid.
MSG_InvalidVersion=The version string is invalid.

MSG_NotRootProject=The folder does not contain a root project created by NetBeans.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.netbeans.gradle.project.validate.GroupValidator;
import org.netbeans.gradle.project.validate.Validator;

@SuppressWarnings("serial")
public final class GradleSingleProjectPropertiesPanel extends javax.swing.JPanel {
Expand Down Expand Up @@ -49,6 +50,12 @@ public void changedUpdate(DocumentEvent e) {
jMainClassEdit.getDocument().addDocumentListener(validationPerformer);
}

public void addProjectLocationValidator(Validator<String> validator) {
validators.addValidator(
validator,
NewProjectUtils.createCollector(jProjectLocationEdit));
}

public void startValidation() {
bckgValidator.setValidators(validators);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.netbeans.gradle.project.newproject;

import java.awt.Component;
import java.io.File;
import java.util.concurrent.atomic.AtomicReference;
import javax.swing.event.ChangeListener;
import org.netbeans.gradle.project.GradleProjectConstants;
import org.netbeans.gradle.project.validate.Problem;
import org.netbeans.gradle.project.validate.Validator;
import org.openide.WizardDescriptor;
import org.openide.util.HelpCtx;

public final class GradleSubProjectConfigPanel implements WizardDescriptor.Panel<WizardDescriptor> {
private final AtomicReference<GradleSingleProjectPropertiesPanel> panel;
private final AtomicReference<GradleSingleProjectConfig> configRef;

public GradleSubProjectConfigPanel(AtomicReference<GradleSingleProjectConfig> configRef) {
if (configRef == null) throw new NullPointerException("configRef");

this.configRef = configRef;
this.panel = new AtomicReference<GradleSingleProjectPropertiesPanel>();
}

private GradleSingleProjectPropertiesPanel getPanel() {
GradleSingleProjectPropertiesPanel result = panel.get();
if (result == null) {
GradleSingleProjectPropertiesPanel newPanel
= new GradleSingleProjectPropertiesPanel();
if (panel.compareAndSet(null, newPanel)) {
newPanel.addProjectLocationValidator(new Validator<String>() {
private Problem checkFile(File projectDir, String fileName) {
if (!new File(projectDir, fileName).isFile()) {
return Problem.severe(NewProjectStrings.getNotRootProject());
}
return null;
}

@Override
public Problem validateInput(String inputType) {
File rootProject = new File(inputType);

Problem problem;

problem = checkFile(rootProject, GradleProjectConstants.BUILD_FILE_NAME);
if (problem != null) {
return problem;
}

problem = checkFile(rootProject, GradleProjectConstants.SETTINGS_FILE_NAME);
if (problem != null) {
return problem;
}

problem = checkFile(rootProject, "parent.gradle");
if (problem != null) {
return problem;
}
return null;
}
});
}
result = panel.get();
}
return result;
}

@Override
public Component getComponent() {
return getPanel();
}

@Override
public HelpCtx getHelp() {
return null;
}

@Override
public void readSettings(WizardDescriptor settings) {
getPanel().startValidation();
}

@Override
public void storeSettings(WizardDescriptor settings) {
configRef.set(getPanel().getConfig());
}

@Override
public boolean isValid() {
return getPanel().containsValidData();
}

@Override
public void addChangeListener(ChangeListener listener) {
getPanel().addChangeListener(listener);
}

@Override
public void removeChangeListener(ChangeListener listener) {
getPanel().removeChangeListener(listener);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package org.netbeans.gradle.project.newproject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import javax.swing.event.ChangeListener;
import org.netbeans.api.annotations.common.StaticResource;
import org.netbeans.api.templates.TemplateRegistration;
import org.netbeans.gradle.project.GradleProjectConstants;
import org.netbeans.gradle.project.NbIcons;
import org.netbeans.gradle.project.StringUtils;
import org.openide.WizardDescriptor;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.NbBundle.Messages;

@TemplateRegistration(
folder="Project/Gradle",
displayName="#template.subGradleProject",
iconBase=NbIcons.PROJECT_ICON_PATH)
@Messages("template.subGradleProject=Gradle Subproject")
public final class GradleSubProjectWizardIterator
implements
WizardDescriptor.BackgroundInstantiatingIterator<WizardDescriptor> {

@StaticResource
private static final String SINGLE_PROJECT_BUILD_GRADLE = "org/netbeans/gradle/project/resources/newproject/subproject.gradle";

private final List<WizardDescriptor.Panel<WizardDescriptor>> descriptors;
private final AtomicReference<GradleSingleProjectConfig> configRef;
private int descriptorIndex;

public GradleSubProjectWizardIterator() {
this.descriptors = new ArrayList<WizardDescriptor.Panel<WizardDescriptor>>(1);
this.descriptorIndex = 0;
this.configRef = new AtomicReference<GradleSingleProjectConfig>(null);
}

private static void createBuildGradle(
File projectDir,
GradleSingleProjectConfig config) throws IOException {
String mainClass = config.getMainClass();

String buildGradleContent = StringUtils.getResourceAsString(
SINGLE_PROJECT_BUILD_GRADLE,
NewProjectUtils.DEFAULT_FILE_ENCODING);

buildGradleContent = buildGradleContent.replace("${MAIN_CLASS}",
mainClass != null ? mainClass : "");

File buildGradle = new File(projectDir, GradleProjectConstants.BUILD_FILE_NAME);
StringUtils.writeStringToFile(buildGradleContent, NewProjectUtils.DEFAULT_FILE_ENCODING, buildGradle);
}

@Override
public Set<FileObject> instantiate() throws IOException {
GradleSingleProjectConfig config = configRef.get();
if (config == null) {
throw new IOException("Missing configuration.");
}

File projectDirAsFile = FileUtil.normalizeFile(config.getProjectFolder());
FileObject projectDir = FileUtil.createFolder(projectDirAsFile);

NewProjectUtils.createDefaultSourceDirs(projectDir);
createBuildGradle(projectDirAsFile, config);

String mainClass = config.getMainClass();
if (mainClass != null) {
NewProjectUtils.createMainClass(projectDirAsFile, mainClass);
}

return Collections.singleton(projectDir);
}

@Override
public void initialize(WizardDescriptor wizard) {
uninitialize(wizard);

descriptorIndex = 0;
descriptors.add(new GradleSubProjectConfigPanel(configRef));
}

@Override
public void uninitialize(WizardDescriptor wizard) {
descriptors.clear();
configRef.set(null);
}

@Override
public WizardDescriptor.Panel<WizardDescriptor> current() {
return descriptors.get(descriptorIndex);
}

@Override
public String name() {
return "GradleSubProjectTemplate";
}

@Override
public boolean hasNext() {
return descriptorIndex < descriptors.size() - 1;
}

@Override
public boolean hasPrevious() {
return descriptorIndex > 0;
}

@Override
public void nextPanel() {
if (!hasNext()) {
throw new NoSuchElementException();
}
descriptorIndex++;
}

@Override
public void previousPanel() {
if (!hasPrevious()) {
throw new NoSuchElementException();
}
descriptorIndex--;
}

@Override
public void addChangeListener(ChangeListener l) {
}

@Override
public void removeChangeListener(ChangeListener l) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public static String getInvalidVersion() {
return NbBundle.getMessage(NewProjectStrings.class, "MSG_InvalidVersion");
}

public static String getNotRootProject() {
return NbBundle.getMessage(NewProjectStrings.class, "MSG_NotRootProject");
}

private NewProjectStrings() {
throw new AssertionError();
}
Expand Down

0 comments on commit 969ad35

Please sign in to comment.