# HG changeset patch # User Milos Kleint # Date 1234276814 -3600 # Node ID d2c4d648ef7049f29cae3c9ea945226f5123cadb # Parent 82fe84a414e39c1643daffe8428fc4358d07b46e #102711 add api that allows to create SourceGroups of given type if not pre-existing. Includes implementation for maven project's main and test java source roots. diff -r 82fe84a414e3 -r d2c4d648ef70 java.project/apichanges.xml --- a/java.project/apichanges.xml Mon Feb 09 14:07:50 2009 +0100 +++ b/java.project/apichanges.xml Tue Feb 10 15:40:14 2009 +0100 @@ -106,6 +106,23 @@ + + + + Constants for SourceGroupModifier + + + + + +

+ Added a few constants to JavaProjectConstants for use by SourceGroupModifier with relation + to java projects. +

+
+ + +
diff -r 82fe84a414e3 -r d2c4d648ef70 java.project/manifest.mf --- a/java.project/manifest.mf Mon Feb 09 14:07:50 2009 +0100 +++ b/java.project/manifest.mf Tue Feb 10 15:40:14 2009 +0100 @@ -3,7 +3,7 @@ OpenIDE-Module-Layer: org/netbeans/modules/java/project/layer.xml OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/java/project/Bundle.properties OpenIDE-Module-Needs: javax.script.ScriptEngine.freemarker -OpenIDE-Module-Specification-Version: 1.23 +OpenIDE-Module-Specification-Version: 1.24 OpenIDE-Module-Recommends: org.netbeans.spi.java.project.runner.JavaRunnerImplementation AutoUpdate-Show-In-Client: false diff -r 82fe84a414e3 -r d2c4d648ef70 java.project/src/org/netbeans/api/java/project/JavaProjectConstants.java --- a/java.project/src/org/netbeans/api/java/project/JavaProjectConstants.java Mon Feb 09 14:07:50 2009 +0100 +++ b/java.project/src/org/netbeans/api/java/project/JavaProjectConstants.java Tue Feb 10 15:40:14 2009 +0100 @@ -62,6 +62,23 @@ */ public static final String SOURCES_TYPE_RESOURCES = "resources"; // NOI18N + + /** + * Hint for SourceGroupModifier to create a SourceGroup + * for main project codebase. + * @see org.netbeans.api.project.SourceGroupModifier + * @since org.netbeans.modules.java.project/1 1.24 + */ + public static final String SOURCES_HINT_MAIN = "main"; //NOI18N + + /** + * Hint for SourceGroupModifier to create a SourceGroup + * for project's tests. + * @see org.netbeans.api.project.SourceGroupModifier + * @since org.netbeans.modules.java.project/1 1.24 + */ + public static final String SOURCES_HINT_TEST = "test"; //NOI18N + /** * Standard artifact type representing a JAR file, presumably * used as a Java library of some kind. diff -r 82fe84a414e3 -r d2c4d648ef70 maven/nbproject/project.xml --- a/maven/nbproject/project.xml Mon Feb 09 14:07:50 2009 +0100 +++ b/maven/nbproject/project.xml Tue Feb 10 15:40:14 2009 +0100 @@ -123,7 +123,7 @@ 1 - 1.18 + 1.24 @@ -194,7 +194,7 @@ 1 - 1.22 + 1.24 diff -r 82fe84a414e3 -r d2c4d648ef70 maven/src/org/netbeans/modules/maven/MavenSourcesImpl.java --- a/maven/src/org/netbeans/modules/maven/MavenSourcesImpl.java Mon Feb 09 14:07:50 2009 +0100 +++ b/maven/src/org/netbeans/modules/maven/MavenSourcesImpl.java Tue Feb 10 15:40:14 2009 +0100 @@ -70,6 +70,7 @@ import org.netbeans.api.project.Sources; import org.netbeans.api.queries.SharabilityQuery; import org.netbeans.modules.maven.spi.nodes.NodeUtils; +import org.netbeans.spi.project.SourceGroupModifierImplementation; import org.netbeans.spi.project.support.GenericSources; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; @@ -85,7 +86,7 @@ * IMHO at least.. * @author Milos Kleint */ -public class MavenSourcesImpl implements Sources { +public class MavenSourcesImpl implements Sources, SourceGroupModifierImplementation { public static final String TYPE_OTHER = "Resources"; //NOI18N public static final String TYPE_TEST_OTHER = "TestResources"; //NOI18N public static final String TYPE_GEN_SOURCES = "GeneratedSources"; //NOI18N @@ -378,6 +379,36 @@ } return changed; } + + public SourceGroup createSourceGroup(String type, String hint) { + assert type != null; + MavenProject mp = project.getOriginalMavenProject(); + File folder = null; + if (JavaProjectConstants.SOURCES_TYPE_JAVA.equals(type)) { + if (JavaProjectConstants.SOURCES_HINT_MAIN.equals(hint)) { + folder = FileUtilities.convertStringToFile(mp.getBuild().getSourceDirectory()); + } + if (JavaProjectConstants.SOURCES_HINT_TEST.equals(hint)) { + folder = FileUtilities.convertStringToFile(mp.getBuild().getTestSourceDirectory()); + } + } + if (folder != null) { + folder.mkdirs(); + FileUtil.refreshFor(folder); + checkChanges(false); + FileObject fo = FileUtil.toFileObject(folder); + assert fo != null; + SourceGroup[] grps = getSourceGroups(type); + for (SourceGroup sg : grps) { + if (fo.equals(sg.getRootFolder())) { + return sg; + } + } + //shall we somehow report it? + } + + return null; + } public static final class OtherGroup implements SourceGroup { diff -r 82fe84a414e3 -r d2c4d648ef70 projectapi/apichanges.xml --- a/projectapi/apichanges.xml Mon Feb 09 14:07:50 2009 +0100 +++ b/projectapi/apichanges.xml Tue Feb 10 15:40:14 2009 +0100 @@ -104,6 +104,25 @@ + + + introduction of SourceGroupModifier, a way to create SourceGroup if missing + + + + + +

+ SourceGroupModifier provides ways of create specific folders (SourceGroup root folders) + in case they don't exist, eg. cannot be retrieved from Sources. + The project supporting automated creation of SourceGroup root folders needs to + provide SourceGroupModifierImplementation in the project's lookup. +

+
+ + + +
diff -r 82fe84a414e3 -r d2c4d648ef70 projectapi/manifest.mf --- a/projectapi/manifest.mf Mon Feb 09 14:07:50 2009 +0100 +++ b/projectapi/manifest.mf Tue Feb 10 15:40:14 2009 +0100 @@ -1,7 +1,7 @@ Manifest-Version: 1.0 OpenIDE-Module: org.netbeans.modules.projectapi/1 OpenIDE-Module-Install: org/netbeans/modules/projectapi/Installer.class -OpenIDE-Module-Specification-Version: 1.23 +OpenIDE-Module-Specification-Version: 1.24 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/projectapi/Bundle.properties OpenIDE-Module-Layer: org/netbeans/modules/projectapi/layer.xml diff -r 82fe84a414e3 -r d2c4d648ef70 projectapi/src/org/netbeans/api/project/SourceGroupModifier.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projectapi/src/org/netbeans/api/project/SourceGroupModifier.java Tue Feb 10 15:40:14 2009 +0100 @@ -0,0 +1,131 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2009 Sun Microsystems, Inc. + */ + +package org.netbeans.api.project; + +import org.netbeans.spi.project.SourceGroupModifierImplementation; + +/** + * SourceGroupModifier provides ways of create specific folders ({@link org.netbeans.api.project.SourceGroup} root folders) + * in case they don't exist, eg. cannot be retrieved from {@link org.netbeans.api.project.Sources} + * The project type supporting automated creation of {@link org.netbeans.api.project.SourceGroup} root folders needs to + * provide {@link org.netbeans.spi.project.SourceGroupModifierImplementation} in the project's lookup. + * + * @since org.netbeans.modules.projectapi 1.24 + * @author mkleint + */ +public final class SourceGroupModifier { + + private SourceGroupModifier() { + } + + /** + * Creates a {@link org.netbeans.api.project.SourceGroup} in the given {@link org.netbeans.api.project.Project} of the given type and hint. + * Typically a type is a constant for java/groovy/ruby source roots and hint is a constant for main sources or test sources. + * Please consult specific APIs fro the supported types/hints. Eg. JavaProjectConstants for java related project sources. + * + * @param project + * @param type constant for type of sources + * @param hint + * @return the created SourceGroup or null + */ + public static final SourceGroup createSourceGroup(Project project, String type, String hint) { + assert project != null; + SourceGroupModifierImplementation impl = project.getLookup().lookup(SourceGroupModifierImplementation.class); + if (impl == null) { + return null; + } + return impl.createSourceGroup(type, hint); + } + + /** + * Creates a {@link org.netbeans.api.project.SourceGroupModifier.Future} object + * that is capable of lazily creating {@link org.netbeans.api.project.SourceGroup} in the given {@link org.netbeans.api.project.Project} of the given type and hint. + * Typically a type is a constant for java/groovy/ruby source roots and hint is a constant for main sources or test sources. + * Please consult specific APIs fro the supported types/hints. Eg. JavaProjectConstants for java related project sources. + * @param project + * @param type + * @param hint + * @return Future instance that is capable of creating a SourceGroup or null + */ + public static final SourceGroupModifier.Future createSourceGroupFuture(Project project, String type, String hint) { + assert project != null; + SourceGroupModifierImplementation impl = project.getLookup().lookup(SourceGroupModifierImplementation.class); + if (impl == null) { + return null; + } + return new Future(impl, type, hint); + } + + /** + * A wrapper class that is capable of lazily creating a {@link org.netbeans.api.project.SourceGroup} instance. + */ + public static final class Future { + + SourceGroupModifierImplementation impl; + private String hint; + private String type; + + private Future(SourceGroupModifierImplementation im, String type, String hint) { + this.impl = im; + this.hint = hint; + this.type = type; + } + + /** + * Create the instance of {@link org.netbeans.api.project.SourceGroup} wrapped by + * this object. + * @param type + * @param hint + * @return + */ + public final SourceGroup createSourceGroup() { + return impl.createSourceGroup(type, hint); + } + + + public String getType() { + return type; + } + + public String getHint() { + return hint; + } + } +} diff -r 82fe84a414e3 -r d2c4d648ef70 projectapi/src/org/netbeans/spi/project/SourceGroupModifierImplementation.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projectapi/src/org/netbeans/spi/project/SourceGroupModifierImplementation.java Tue Feb 10 15:40:14 2009 +0100 @@ -0,0 +1,67 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2009 Sun Microsystems, Inc. + */ + +package org.netbeans.spi.project; + +import org.netbeans.api.project.SourceGroup; + +/** + * The SPI side of {@link org.netbeans.api.project.SourceGroupModifier}. + * Expected to be present in project lookup of project types supporting automated creation + * of {@link org.netbeans.api.project.SourceGroup} root folders. + * @since org.netbeans.modules.projectapi 1.24 + * @author mkleint + */ +public interface SourceGroupModifierImplementation { + /** + * Creates a {@link org.netbeans.api.project.SourceGroup} of the given type and hint. + * Typically a type is a constant for java/groovy/ruby source roots and hint is a constant for main sources or test sources. + * Please consult specific APIs fro the supported types/hints. Eg. JavaProjectConstants for java related project sources. + * If the SourceGroup's type/hint is not supported, the implementation shall silently return null and not throw any exceptions. + * If the SourceGroup of given type/hint already exists it shall be returned as well. + * + * @param project + * @param type constant for type of sources + * @param hint + * @return the created or existing SourceGroup or null + */ + + public SourceGroup createSourceGroup(String type, String hint); + +}