~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to java/j2seproject/src/org/netbeans/modules/java/j2seproject/J2SESources.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.java.j2seproject;
 
43
 
 
44
import java.beans.PropertyChangeListener;
 
45
import java.beans.PropertyChangeEvent;
 
46
import javax.swing.event.ChangeEvent;
 
47
import javax.swing.event.ChangeListener;
 
48
import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
 
49
import org.openide.util.Mutex;
 
50
import org.netbeans.api.project.Sources;
 
51
import org.netbeans.api.project.SourceGroup;
 
52
import org.netbeans.api.project.ProjectManager;
 
53
import org.netbeans.api.project.FileOwnerQuery;
 
54
import org.netbeans.api.java.project.JavaProjectConstants;
 
55
import org.netbeans.spi.project.support.ant.SourcesHelper;
 
56
import org.netbeans.spi.project.support.ant.AntProjectHelper;
 
57
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
 
58
import org.openide.util.ChangeSupport;
 
59
 
 
60
/**
 
61
 * Implementation of {@link Sources} interface for J2SEProject.
 
62
 */
 
63
public class J2SESources implements Sources, PropertyChangeListener, ChangeListener  {
 
64
    
 
65
    private static final String BUILD_DIR_PROP = "${" + J2SEProjectProperties.BUILD_DIR + "}";    //NOI18N
 
66
    private static final String DIST_DIR_PROP = "${" + J2SEProjectProperties.DIST_DIR + "}";    //NOI18N
 
67
 
 
68
    private final AntProjectHelper helper;
 
69
    private final PropertyEvaluator evaluator;
 
70
    private final SourceRoots sourceRoots;
 
71
    private final SourceRoots testRoots;
 
72
    private SourcesHelper sourcesHelper;
 
73
    private Sources delegate;
 
74
    /**
 
75
     * Flag to forbid multiple invocation of {@link SourcesHelper#registerExternalRoots} 
 
76
     **/
 
77
    private boolean externalRootsRegistered;    
 
78
    private final ChangeSupport changeSupport = new ChangeSupport(this);
 
79
 
 
80
    J2SESources(AntProjectHelper helper, PropertyEvaluator evaluator,
 
81
                SourceRoots sourceRoots, SourceRoots testRoots) {
 
82
        this.helper = helper;
 
83
        this.evaluator = evaluator;
 
84
        this.sourceRoots = sourceRoots;
 
85
        this.testRoots = testRoots;
 
86
        this.sourceRoots.addPropertyChangeListener(this);
 
87
        this.testRoots.addPropertyChangeListener(this);        
 
88
        this.evaluator.addPropertyChangeListener(this);
 
89
        initSources(); // have to register external build roots eagerly
 
90
    }
 
91
 
 
92
    /**
 
93
     * Returns an array of SourceGroup of given type. It delegates to {@link SourcesHelper}.
 
94
     * This method firstly acquire the {@link ProjectManager#mutex} in read mode then it enters
 
95
     * into the synchronized block to ensure that just one instance of the {@link SourcesHelper}
 
96
     * is created. These instance is cleared also in the synchronized block by the
 
97
     * {@link J2SESources#fireChange} method.
 
98
     */
 
99
    public SourceGroup[] getSourceGroups(final String type) {
 
100
        return ProjectManager.mutex().readAccess(new Mutex.Action<SourceGroup[]>() {
 
101
            public SourceGroup[] run() {
 
102
                Sources _delegate;
 
103
                synchronized (J2SESources.this) {
 
104
                    if (delegate == null) {                    
 
105
                        delegate = initSources();
 
106
                        delegate.addChangeListener(J2SESources.this);
 
107
                    }
 
108
                    _delegate = delegate;
 
109
                }
 
110
                return _delegate.getSourceGroups(type);
 
111
            }
 
112
        });
 
113
    }
 
114
 
 
115
    private Sources initSources() {        
 
116
        this.sourcesHelper = new SourcesHelper(helper, evaluator);   //Safe to pass APH        
 
117
        register(sourceRoots);
 
118
        register(testRoots);
 
119
        this.sourcesHelper.addNonSourceRoot (BUILD_DIR_PROP);
 
120
        this.sourcesHelper.addNonSourceRoot(DIST_DIR_PROP);
 
121
        externalRootsRegistered = false;
 
122
        ProjectManager.mutex().postWriteRequest(new Runnable() {
 
123
            public void run() {                
 
124
                if (!externalRootsRegistered) {
 
125
                    sourcesHelper.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
 
126
                    externalRootsRegistered = true;
 
127
                }
 
128
            }
 
129
        });
 
130
        return this.sourcesHelper.createSources();
 
131
    }
 
132
 
 
133
    private void register(SourceRoots roots) {
 
134
        String[] propNames = roots.getRootProperties();
 
135
        String[] rootNames = roots.getRootNames();
 
136
        for (int i = 0; i < propNames.length; i++) {
 
137
            String prop = propNames[i];
 
138
            String displayName = roots.getRootDisplayName(rootNames[i], prop);
 
139
            String loc = "${" + prop + "}"; // NOI18N
 
140
            String includes = "${" + J2SEProjectProperties.INCLUDES + "}"; // NOI18N
 
141
            String excludes = "${" + J2SEProjectProperties.EXCLUDES + "}"; // NOI18N
 
142
            sourcesHelper.addPrincipalSourceRoot(loc, includes, excludes, displayName, null, null); // NOI18N
 
143
            sourcesHelper.addTypedSourceRoot(loc, includes, excludes, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, null, null); // NOI18N
 
144
        }
 
145
    }
 
146
 
 
147
    public void addChangeListener(ChangeListener changeListener) {
 
148
        changeSupport.addChangeListener(changeListener);
 
149
    }
 
150
 
 
151
    public void removeChangeListener(ChangeListener changeListener) {
 
152
        changeSupport.removeChangeListener(changeListener);
 
153
    }
 
154
 
 
155
    private void fireChange() {
 
156
        synchronized (this) {
 
157
            if (delegate != null) {
 
158
                delegate.removeChangeListener(this);
 
159
                delegate = null;
 
160
            }
 
161
        }
 
162
        changeSupport.fireChange();
 
163
    }
 
164
 
 
165
    public void propertyChange(PropertyChangeEvent evt) {
 
166
        String propName = evt.getPropertyName();
 
167
        if (SourceRoots.PROP_ROOT_PROPERTIES.equals(propName) ||
 
168
            J2SEProjectProperties.BUILD_DIR.equals(propName)  ||
 
169
            J2SEProjectProperties.DIST_DIR.equals(propName)) {
 
170
            this.fireChange();
 
171
        }
 
172
    }
 
173
    
 
174
    public void stateChanged (ChangeEvent event) {
 
175
        this.fireChange();
 
176
    }
 
177
 
 
178
}