KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > project > support > GenericSources


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.spi.project.support;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.io.File JavaDoc;
24 import javax.swing.Icon JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
26 import org.netbeans.api.project.FileOwnerQuery;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.api.project.ProjectManager;
29 import org.netbeans.api.project.ProjectUtils;
30 import org.netbeans.api.project.SourceGroup;
31 import org.netbeans.api.project.Sources;
32 import org.netbeans.api.queries.SharabilityQuery;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35
36 /**
37  * Factories for standard {@link Sources} implementations.
38  * @author Jesse Glick
39  */

40 public class GenericSources {
41     
42     private GenericSources() {}
43     
44     /**
45      * Lists only one source folder group, of {@link Sources#TYPE_GENERIC},
46      * containing the project directory, as by {@link #group}.
47      * If you think you need this, look at {@link ProjectUtils#getSources} first.
48      * @param p a project
49      * @return a simple sources implementation
50      */

51     public static Sources genericOnly(Project p) {
52         return new GenericOnlySources(p);
53     }
54     
55     private static final class GenericOnlySources implements Sources {
56         
57         private final Project p;
58         
59         GenericOnlySources(Project p) {
60             this.p = p;
61         }
62         
63         public SourceGroup[] getSourceGroups(String JavaDoc type) {
64             if (type.equals(Sources.TYPE_GENERIC)) {
65                 return new SourceGroup[] {
66                     group(p, p.getProjectDirectory(), "generic", // NOI18N
67
ProjectUtils.getInformation(p).getDisplayName(),
68                           null, null),
69                 };
70             } else {
71                 return new SourceGroup[0];
72             }
73         }
74         
75         public void addChangeListener(ChangeListener JavaDoc listener) {}
76         
77         public void removeChangeListener(ChangeListener JavaDoc listener) {}
78         
79     }
80     
81     /**
82      * Default kind of source folder group.
83      * Contains everything inside the supplied root folder which belongs to the
84      * supplied project and is considered sharable by VCS.
85      * @param p a project
86      * @param rootFolder the root folder to use for sources
87      * @param name a code name for the source group
88      * @param displayName a display name for the source group
89      * @param icon a regular icon to use for the source group, or null
90      * @param openedIcon an opened variant icon to use, or null
91      * @return a new group object
92      */

93     public static SourceGroup group(Project p, FileObject rootFolder, String JavaDoc name, String JavaDoc displayName, Icon JavaDoc icon, Icon JavaDoc openedIcon) {
94         if (name == null) {
95             throw new NullPointerException JavaDoc("Cannot specify a null name for a source group"); // NOI18N
96
}
97         return new Group(p, rootFolder, name, displayName, icon, openedIcon);
98     }
99     
100     private static final class Group implements SourceGroup {
101         
102         private final Project p;
103         private final FileObject rootFolder;
104         private final String JavaDoc name;
105         private final String JavaDoc displayName;
106         private final Icon JavaDoc icon;
107         private final Icon JavaDoc openedIcon;
108         
109         Group(Project p, FileObject rootFolder, String JavaDoc name, String JavaDoc displayName, Icon JavaDoc icon, Icon JavaDoc openedIcon) {
110             this.p = p;
111             this.rootFolder = rootFolder;
112             this.name = name;
113             this.displayName = displayName;
114             this.icon = icon;
115             this.openedIcon = openedIcon;
116         }
117         
118         public FileObject getRootFolder() {
119             return rootFolder;
120         }
121         
122         public String JavaDoc getName() {
123             return name;
124         }
125         
126         public String JavaDoc getDisplayName() {
127             return displayName;
128         }
129         
130         public Icon JavaDoc getIcon(boolean opened) {
131             return opened ? icon : openedIcon;
132         }
133         
134         public boolean contains(FileObject file) throws IllegalArgumentException JavaDoc {
135             if (file != rootFolder && !FileUtil.isParentOf(rootFolder, file)) {
136                 throw new IllegalArgumentException JavaDoc();
137             }
138             if (p != null) {
139                 if (file.isFolder() && file != p.getProjectDirectory() && ProjectManager.getDefault().isProject(file)) {
140                     // #67450: avoid actually loading the nested project.
141
return false;
142                 }
143                 if (FileOwnerQuery.getOwner(file) != p) {
144                     return false;
145                 }
146             }
147             File JavaDoc f = FileUtil.toFile(file);
148             if (f != null) {
149                 // MIXED, UNKNOWN, and SHARABLE -> include it
150
return SharabilityQuery.getSharability(f) != SharabilityQuery.NOT_SHARABLE;
151             } else {
152                 // Not on disk, include it.
153
return true;
154             }
155         }
156         
157         public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
158             // XXX should react to ProjectInformation changes
159
}
160         
161         public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
162             // XXX
163
}
164         
165         public String JavaDoc toString() {
166             return "GenericSources.Group[name=" + name + ",rootFolder=" + rootFolder + "]"; // NOI18N
167
}
168         
169     }
170     
171 }
172
Popular Tags