KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > suite > SuiteSubprojectProviderImpl


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.apisupport.project.suite;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.event.ChangeEvent JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.api.project.ProjectManager;
33 import org.netbeans.modules.apisupport.project.NbModuleProject;
34 import org.netbeans.modules.apisupport.project.Util;
35 import org.netbeans.spi.project.SubprojectProvider;
36 import org.netbeans.spi.project.support.ant.AntProjectHelper;
37 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
38 import org.netbeans.spi.project.support.ant.PropertyUtils;
39 import org.openide.ErrorManager;
40 import org.openide.filesystems.FileObject;
41
42 /**
43  * Lists modules in a suite.
44  * @author Jesse Glick
45  */

46 final class SuiteSubprojectProviderImpl implements SubprojectProvider {
47     
48     private Set JavaDoc<NbModuleProject> projects;
49     private final AntProjectHelper helper;
50     private final PropertyEvaluator eval;
51     
52     private final Set JavaDoc<ChangeListener JavaDoc> listeners = new HashSet JavaDoc();
53     private boolean reloadNeeded;
54     
55     public SuiteSubprojectProviderImpl(AntProjectHelper helper, PropertyEvaluator eval) {
56         this.helper = helper;
57         this.eval = eval;
58         eval.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
59             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
60                 if ("modules".equals(evt.getPropertyName())) { // NOI18N
61
SuiteSubprojectProviderImpl.this.reloadNeeded = true;
62                     SuiteSubprojectProviderImpl.this.fireChange();
63                 }
64             }
65         });
66     }
67     
68     public Set JavaDoc<NbModuleProject> getSubprojects() {
69         if (projects == null || reloadNeeded) {
70             projects = loadProjects();
71             reloadNeeded = false;
72         }
73         return projects;
74     }
75     
76     public final void addChangeListener(ChangeListener JavaDoc l) {
77         synchronized (listeners) {
78             listeners.add(l);
79         }
80     }
81     
82     public final void removeChangeListener(ChangeListener JavaDoc l) {
83         synchronized (listeners) {
84             listeners.remove(l);
85         }
86     }
87     
88     private void fireChange() {
89         Iterator JavaDoc it;
90         ChangeEvent JavaDoc e = new ChangeEvent JavaDoc(this);
91         synchronized (listeners) {
92             if (listeners.isEmpty()) {
93                 return;
94             }
95             it = new HashSet JavaDoc(listeners).iterator();
96         }
97         while (it.hasNext()) {
98             ChangeListener JavaDoc l = (ChangeListener JavaDoc) it.next();
99             l.stateChanged(e);
100         }
101     }
102     
103     private Set JavaDoc<NbModuleProject> loadProjects() {
104         Set JavaDoc<NbModuleProject> newProjects = new HashSet JavaDoc();
105         String JavaDoc modules = eval.getProperty("modules"); // NOI18N
106
if (modules != null) {
107             String JavaDoc[] pieces = PropertyUtils.tokenizePath(modules);
108             for (int i = 0; i < pieces.length; i++) {
109                 FileObject dir = helper.resolveFileObject(pieces[i]);
110                 if (dir != null) {
111                     try {
112                         Project subp = ProjectManager.getDefault().findProject(dir);
113                         if (subp != null && subp instanceof NbModuleProject) {
114                             newProjects.add((NbModuleProject) subp);
115                         }
116                     } catch (IOException JavaDoc e) {
117                         Util.err.notify(ErrorManager.INFORMATIONAL, e);
118                     }
119                 }
120             }
121         }
122         return Collections.unmodifiableSet(newProjects);
123     }
124     
125 }
126
Popular Tags