KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > Subprojects


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.modules.ant.freeform;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.api.project.ProjectManager;
32 import org.netbeans.modules.ant.freeform.spi.support.Util;
33 import org.netbeans.spi.project.SubprojectProvider;
34 import org.openide.ErrorManager;
35 import org.openide.filesystems.FileObject;
36 import org.w3c.dom.Element JavaDoc;
37
38 /**
39  * Manages list of subprojects.
40  * @author Jesse Glick
41  * @see "#46713"
42  */

43 final class Subprojects implements SubprojectProvider {
44     
45     private final FreeformProject project;
46     
47     public Subprojects(FreeformProject project) {
48         this.project = project;
49     }
50
51     public Set JavaDoc<? extends Project> getSubprojects() {
52         return new LazySubprojectsSet();
53     }
54     
55     /**Analyzes subprojects element.
56      * Works in two modes:
57      * 1. Lazy mode, detects only if the set of subprojects would be empty.
58      * Enabled if the provided subprojects parameter is null.
59      * The subprojects set is empty if createSubprojects(null) == null.
60      * Used by the LazySubprojectsSet, see #58639. Works as fast as possible.
61      * 2. Full mode, creates the set of projects.
62      * Enabled if the subprojects parameter is not-null.
63      * The provided instance of Set is filled by the projects and returned.
64      *
65      * This method never allocates a new set.
66      */

67     private Set JavaDoc<Project> createSubprojects(Set JavaDoc<Project> subprojects) {
68         Element JavaDoc config = project.getPrimaryConfigurationData();
69         Element JavaDoc subprjsEl = Util.findElement(config, "subprojects", FreeformProjectType.NS_GENERAL); // NOI18N
70
if (subprjsEl != null) {
71             for (Element JavaDoc prjEl : Util.findSubElements(subprjsEl)) {
72                 assert prjEl.getLocalName().equals("project") : "Bad element " + prjEl + " in <subprojects> for " + project;
73                 String JavaDoc rawtext = Util.findText(prjEl);
74                 assert rawtext != null : "Need text content for <project> in " + project;
75                 String JavaDoc evaltext = project.evaluator().evaluate(rawtext);
76                 if (evaltext == null) {
77                     continue;
78                 }
79                 FileObject subprjDir = project.helper().resolveFileObject(evaltext);
80                 if (subprjDir == null) {
81                     continue;
82                 }
83                 try {
84                     Project p = ProjectManager.getDefault().findProject(subprjDir);
85                     if (p != null) {
86                         if (subprojects == null)
87                             return Collections.emptySet();
88                         else
89                             subprojects.add(p);
90                     }
91                 } catch (IOException JavaDoc e) {
92                     org.netbeans.modules.ant.freeform.Util.err.notify(ErrorManager.INFORMATIONAL, e);
93                 }
94             }
95         }
96
97         return subprojects;
98     }
99
100     public void addChangeListener(ChangeListener JavaDoc listener) {
101         // XXX
102
}
103
104     public void removeChangeListener(ChangeListener JavaDoc listener) {
105         // XXX
106
}
107     
108     /**Fix for #58639: the subprojects should be loaded lazily, so invoking the popup menu
109      * with "Open Required Projects" is fast.
110      */

111     private final class LazySubprojectsSet implements Set JavaDoc<Project> {
112         
113         private Set JavaDoc<Project> delegateTo = null;
114         
115         private synchronized Set JavaDoc<Project> getDelegateTo() {
116             if (delegateTo == null) {
117                 delegateTo = createSubprojects(new HashSet JavaDoc<Project>());
118             }
119             
120             return delegateTo;
121         }
122         
123         public boolean contains(Object JavaDoc o) {
124             return getDelegateTo().contains(o);
125         }
126         
127         public boolean add(Project p) {
128             throw new UnsupportedOperationException JavaDoc();
129         }
130         
131         public boolean remove(Object JavaDoc o) {
132             throw new UnsupportedOperationException JavaDoc();
133         }
134         
135         public boolean removeAll(Collection JavaDoc c) {
136             throw new UnsupportedOperationException JavaDoc();
137         }
138         
139         public boolean retainAll(Collection JavaDoc c) {
140             throw new UnsupportedOperationException JavaDoc();
141         }
142         
143         public boolean addAll(Collection JavaDoc<? extends Project> c) {
144             throw new UnsupportedOperationException JavaDoc();
145         }
146         
147         public boolean containsAll(Collection JavaDoc c) {
148             return getDelegateTo().containsAll(c);
149         }
150         
151         public <T> T[] toArray(T[] a) {
152             return getDelegateTo().toArray(a);
153         }
154         
155         public void clear() {
156             throw new UnsupportedOperationException JavaDoc();
157         }
158         
159         public int size() {
160             return getDelegateTo().size();
161         }
162         
163         public synchronized boolean isEmpty() {
164             if (delegateTo == null) {
165                 return createSubprojects(null) == null;
166             } else {
167                 return delegateTo.isEmpty();
168             }
169         }
170         
171         public Iterator JavaDoc<Project> iterator() {
172             return getDelegateTo().iterator();
173         }
174         
175         public Object JavaDoc[] toArray() {
176             return getDelegateTo().toArray();
177         }
178
179         public int hashCode() {
180             return getDelegateTo().hashCode();
181         }
182
183         public boolean equals(Object JavaDoc obj) {
184             return getDelegateTo().equals(obj);
185         }
186
187         public String JavaDoc toString() {
188             return getDelegateTo().toString();
189         }
190         
191     }
192     
193 }
194
Popular Tags