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.spi.project.support;
043:
044: import java.beans.PropertyChangeListener;
045: import java.io.File;
046: import javax.swing.Icon;
047: import javax.swing.event.ChangeListener;
048: import org.netbeans.api.project.FileOwnerQuery;
049: import org.netbeans.api.project.Project;
050: import org.netbeans.api.project.ProjectManager;
051: import org.netbeans.api.project.ProjectUtils;
052: import org.netbeans.api.project.SourceGroup;
053: import org.netbeans.api.project.Sources;
054: import org.netbeans.api.queries.SharabilityQuery;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057:
058: /**
059: * Factories for standard {@link Sources} implementations.
060: * @author Jesse Glick
061: */
062: public class GenericSources {
063:
064: private GenericSources() {
065: }
066:
067: /**
068: * Lists only one source folder group, of {@link Sources#TYPE_GENERIC},
069: * containing the project directory, as by {@link #group}.
070: * If you think you need this, look at {@link ProjectUtils#getSources} first.
071: * @param p a project
072: * @return a simple sources implementation
073: */
074: public static Sources genericOnly(Project p) {
075: return new GenericOnlySources(p);
076: }
077:
078: private static final class GenericOnlySources implements Sources {
079:
080: private final Project p;
081:
082: GenericOnlySources(Project p) {
083: this .p = p;
084: }
085:
086: public SourceGroup[] getSourceGroups(String type) {
087: if (type.equals(Sources.TYPE_GENERIC)) {
088: return new SourceGroup[] { group(
089: p,
090: p.getProjectDirectory(),
091: "generic", // NOI18N
092: ProjectUtils.getInformation(p).getDisplayName(),
093: null, null), };
094: } else {
095: return new SourceGroup[0];
096: }
097: }
098:
099: public void addChangeListener(ChangeListener listener) {
100: }
101:
102: public void removeChangeListener(ChangeListener listener) {
103: }
104:
105: }
106:
107: /**
108: * Default kind of source folder group.
109: * Contains everything inside the supplied root folder which belongs to the
110: * supplied project and is considered sharable by VCS.
111: * @param p a project
112: * @param rootFolder the root folder to use for sources
113: * @param name a code name for the source group
114: * @param displayName a display name for the source group
115: * @param icon a regular icon to use for the source group, or null
116: * @param openedIcon an opened variant icon to use, or null
117: * @return a new group object
118: */
119: public static SourceGroup group(Project p, FileObject rootFolder,
120: String name, String displayName, Icon icon, Icon openedIcon) {
121: if (name == null) {
122: throw new NullPointerException(
123: "Cannot specify a null name for a source group"); // NOI18N
124: }
125: return new Group(p, rootFolder, name, displayName, icon,
126: openedIcon);
127: }
128:
129: private static final class Group implements SourceGroup {
130:
131: private final Project p;
132: private final FileObject rootFolder;
133: private final String name;
134: private final String displayName;
135: private final Icon icon;
136: private final Icon openedIcon;
137:
138: Group(Project p, FileObject rootFolder, String name,
139: String displayName, Icon icon, Icon openedIcon) {
140: this .p = p;
141: this .rootFolder = rootFolder;
142: this .name = name;
143: this .displayName = displayName;
144: this .icon = icon;
145: this .openedIcon = openedIcon;
146: }
147:
148: public FileObject getRootFolder() {
149: return rootFolder;
150: }
151:
152: public String getName() {
153: return name;
154: }
155:
156: public String getDisplayName() {
157: return displayName;
158: }
159:
160: public Icon getIcon(boolean opened) {
161: return opened ? icon : openedIcon;
162: }
163:
164: public boolean contains(FileObject file)
165: throws IllegalArgumentException {
166: if (file != rootFolder
167: && !FileUtil.isParentOf(rootFolder, file)) {
168: throw new IllegalArgumentException();
169: }
170: if (p != null) {
171: if (file.isFolder() && file != p.getProjectDirectory()
172: && ProjectManager.getDefault().isProject(file)) {
173: // #67450: avoid actually loading the nested project.
174: return false;
175: }
176: if (FileOwnerQuery.getOwner(file) != p) {
177: return false;
178: }
179: }
180: File f = FileUtil.toFile(file);
181: if (f != null) {
182: // MIXED, UNKNOWN, and SHARABLE -> include it
183: return SharabilityQuery.getSharability(f) != SharabilityQuery.NOT_SHARABLE;
184: } else {
185: // Not on disk, include it.
186: return true;
187: }
188: }
189:
190: public void addPropertyChangeListener(PropertyChangeListener l) {
191: // XXX should react to ProjectInformation changes
192: }
193:
194: public void removePropertyChangeListener(
195: PropertyChangeListener l) {
196: // XXX
197: }
198:
199: public String toString() {
200: return "GenericSources.Group[name=" + name + ",rootFolder="
201: + rootFolder + "]"; // NOI18N
202: }
203:
204: }
205:
206: }
|