001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project.queries;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.net.URI;
047: import java.net.URISyntaxException;
048: import java.net.URL;
049: import java.util.Arrays;
050: import org.netbeans.api.java.classpath.ClassPath;
051: import org.netbeans.api.java.project.JavaProjectConstants;
052: import org.netbeans.api.project.ProjectManager;
053: import org.netbeans.api.project.ProjectUtils;
054: import org.netbeans.api.project.SourceGroup;
055: import org.netbeans.api.project.ant.AntArtifact;
056: import org.netbeans.api.project.libraries.Library;
057: import org.netbeans.modules.apisupport.project.NbModuleProject;
058: import org.netbeans.modules.apisupport.project.Util;
059: import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
060: import org.netbeans.spi.java.project.classpath.ProjectClassPathModifierImplementation;
061: import org.openide.util.Exceptions;
062: import org.openide.util.NbBundle;
063:
064: /**
065: * Makes sure you can safely use natural layout in forms you develop for your module.
066: * @author Jesse Glick
067: * @see "#62942"
068: */
069: public final class ModuleProjectClassPathExtender extends
070: ProjectClassPathModifierImplementation {
071:
072: private static final String LIBRARY_NAME = "swing-layout"; // NOI18N
073: private static final String MODULE_NAME = "org.jdesktop.layout"; // NOI18N
074:
075: private final NbModuleProject project;
076:
077: /**
078: * Create new extender.
079: * @param project a module project
080: */
081: public ModuleProjectClassPathExtender(NbModuleProject project) {
082: this .project = project;
083: }
084:
085: protected SourceGroup[] getExtensibleSourceGroups() {
086: for (SourceGroup g : ProjectUtils
087: .getSources(project)
088: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA)) {
089: if (g.getRootFolder() == project.getSourceDirectory()) {
090: return new SourceGroup[] { g };
091: }
092: }
093: return new SourceGroup[0];
094: }
095:
096: protected String[] getExtensibleClassPathTypes(
097: SourceGroup sourceGroup) {
098: return new String[] { ClassPath.COMPILE };
099: }
100:
101: protected boolean addLibraries(Library[] libraries,
102: SourceGroup sourceGroup, String type) throws IOException,
103: UnsupportedOperationException {
104: boolean cpChanged = false;
105: if (libraries.length == 0) {
106: return false;
107: }
108: for (Library library : libraries) {
109: if (!library.getName().equals(LIBRARY_NAME)) {
110: IOException e = new IOException("unknown lib "
111: + library.getName()); // NOI18N
112: Exceptions.attachLocalizedMessage(e, NbBundle
113: .getMessage(
114: ModuleProjectClassPathExtender.class,
115: "ERR_unsupported_library", library
116: .getDisplayName()));
117: throw e;
118: }
119: }
120: for (Library library : libraries) {
121: ModuleEntry entry = project.getModuleList().getEntry(
122: MODULE_NAME);
123: if (entry != null) {
124: cpChanged = Util.addDependency(project, MODULE_NAME);
125: } else {
126: IOException e = new IOException("no module "
127: + MODULE_NAME); // NOI18N
128: Exceptions.attachLocalizedMessage(e, NbBundle
129: .getMessage(
130: ModuleProjectClassPathExtender.class,
131: "ERR_could_not_find_module",
132: MODULE_NAME));
133: throw e;
134: }
135: }
136: if (cpChanged) {
137: ProjectManager.getDefault().saveProject(project);
138: }
139: return cpChanged;
140: }
141:
142: protected boolean removeLibraries(Library[] libraries,
143: SourceGroup sourceGroup, String type) throws IOException,
144: UnsupportedOperationException {
145: throw new UnsupportedOperationException(); // XXX could be supported
146: }
147:
148: protected boolean addRoots(URL[] classPathRoots,
149: SourceGroup sourceGroup, String type) throws IOException,
150: UnsupportedOperationException {
151: UnsupportedOperationException e = new UnsupportedOperationException(
152: "not implemented: " + Arrays.asList(classPathRoots)); // NOI18N
153: // XXX handle >1 args
154: String displayName = classPathRoots.length > 0 ? classPathRoots[0]
155: .toString()
156: : "<nothing>";
157: if (classPathRoots.length > 0
158: && "file".equals(classPathRoots[0].getProtocol())) { // NOI18N
159: try {
160: displayName = new File(classPathRoots[0].toURI())
161: .getAbsolutePath();
162: } catch (URISyntaxException x) {
163: assert false : x;
164: }
165: }
166: Exceptions.attachLocalizedMessage(e, NbBundle.getMessage(
167: ModuleProjectClassPathExtender.class, "ERR_jar",
168: displayName));
169: throw e;
170: }
171:
172: protected boolean removeRoots(URL[] classPathRoots,
173: SourceGroup sourceGroup, String type) throws IOException,
174: UnsupportedOperationException {
175: throw new UnsupportedOperationException();
176: }
177:
178: protected boolean addAntArtifacts(AntArtifact[] artifacts,
179: URI[] artifactElements, SourceGroup sourceGroup, String type)
180: throws IOException, UnsupportedOperationException {
181: // XXX ideally would check to see if it was owned by a NBM project in this universe...
182: UnsupportedOperationException e = new UnsupportedOperationException(
183: "not implemented: " + Arrays.asList(artifactElements)); // NOI18N
184: // XXX handle >1 args
185: String displayName = artifactElements.length > 0 ? artifactElements[0]
186: .toString()
187: : "<nothing>";
188: if (artifactElements.length > 0
189: && "file".equals(artifactElements[0].getScheme())) { // NOI18N
190: displayName = new File(artifactElements[0])
191: .getAbsolutePath();
192: }
193: Exceptions.attachLocalizedMessage(e, NbBundle.getMessage(
194: ModuleProjectClassPathExtender.class, "ERR_jar",
195: displayName));
196: throw e;
197: }
198:
199: protected boolean removeAntArtifacts(AntArtifact[] artifacts,
200: URI[] artifactElements, SourceGroup sourceGroup, String type)
201: throws IOException, UnsupportedOperationException {
202: throw new UnsupportedOperationException();
203: }
204:
205: }
|