KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > EarSources


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.j2ee.earproject;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.event.ChangeEvent JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
28 import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties;
29 import org.openide.util.Mutex;
30 import org.netbeans.api.project.Sources;
31 import org.netbeans.api.project.SourceGroup;
32 import org.netbeans.api.project.ProjectManager;
33 import org.netbeans.api.project.FileOwnerQuery;
34 import org.netbeans.spi.project.support.ant.SourcesHelper;
35 import org.netbeans.spi.project.support.ant.AntProjectHelper;
36 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
37
38 class EarSources implements Sources, PropertyChangeListener JavaDoc, ChangeListener JavaDoc {
39
40     private final AntProjectHelper helper;
41     private final PropertyEvaluator evaluator;
42     private Sources delegate;
43     private final List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
44     private SourcesHelper sourcesHelper;
45
46     EarSources(AntProjectHelper helper, PropertyEvaluator evaluator) {
47         this.helper = helper;
48         this.evaluator = evaluator;
49         initSources(); // have to register external build roots eagerly
50
}
51
52
53     public SourceGroup[] getSourceGroups(final String JavaDoc type) {
54         return (SourceGroup[]) ProjectManager.mutex().readAccess(new Mutex.Action() {
55             public Object JavaDoc run() {
56                 if (delegate == null) {
57                     delegate = initSources();
58                     delegate.addChangeListener(EarSources.this);
59                 }
60                 return delegate.getSourceGroups(type);
61             }
62         });
63     }
64
65     private Sources initSources() {
66         sourcesHelper = new SourcesHelper(helper, evaluator);
67         String JavaDoc configFilesLabel = org.openide.util.NbBundle.getMessage(EarSources.class, "LBL_Node_ConfigBase"); //NOI18N
68
sourcesHelper.addPrincipalSourceRoot("${"+EarProjectProperties.META_INF+"}", configFilesLabel, /*XXX*/null, null);
69         // XXX add build dir too?
70
ProjectManager.mutex().postWriteRequest(new Runnable JavaDoc() {
71             public void run() {
72                 sourcesHelper.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
73             }
74         });
75         return sourcesHelper.createSources();
76     }
77
78     public void addChangeListener(ChangeListener JavaDoc changeListener) {
79         synchronized (listeners) {
80             listeners.add(changeListener);
81         }
82     }
83
84     public void removeChangeListener(ChangeListener JavaDoc changeListener) {
85         synchronized (listeners) {
86             listeners.remove(changeListener);
87         }
88     }
89
90     private void fireChange() {
91         ChangeListener JavaDoc[] _listeners;
92         synchronized (this) {
93             if (delegate != null) {
94                 delegate.removeChangeListener(this);
95                 delegate = null;
96             }
97         }
98         synchronized (listeners) {
99             if (listeners.isEmpty()) {
100                 return;
101             }
102             _listeners = listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
103         }
104         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
105         for (int i = 0; i < _listeners.length; i++) {
106             _listeners[i].stateChanged(ev);
107         }
108     }
109     
110     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
111     }
112
113     public void stateChanged (ChangeEvent JavaDoc event) {
114         this.fireChange();
115     }
116
117 }
118
Popular Tags