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