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.
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2008 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  * If you wish your version of this file to be governed by only the CDDL
    25  * or only the GPL Version 2, indicate your decision by adding
    26  * "[Contributor] elects to include this software in this distribution
    27  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    28  * single choice of license, a recipient has the option to distribute
    29  * your version of this file under either the CDDL, the GPL Version 2 or
    30  * to extend the choice of license to its licensees as provided above.
    31  * However, if you add GPL Version 2 code and therefore, elected the GPL
    32  * Version 2 license, then the option applies only if the new code is
    33  * made subject to such option by the copyright holder.
    34  *
    35  * Contributor(s):
    36  *
    37  * Portions Copyrighted 2008 Sun Microsystems, Inc.
    38  */
    39 package org.netbeans.modules.ada.project;
    40 
    41 import java.beans.PropertyChangeEvent;
    42 import java.beans.PropertyChangeListener;
    43 import javax.swing.event.ChangeEvent;
    44 import javax.swing.event.ChangeListener;
    45 import org.netbeans.api.project.FileOwnerQuery;
    46 import org.netbeans.api.project.Project;
    47 import org.netbeans.api.project.ProjectManager;
    48 import org.netbeans.api.project.SourceGroup;
    49 import org.netbeans.api.project.Sources;
    50 import org.netbeans.modules.ada.project.ui.properties.AdaProjectProperties;
    51 import org.netbeans.spi.project.support.ant.AntProjectHelper;
    52 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
    53 import org.netbeans.spi.project.support.ant.SourcesHelper;
    54 import org.openide.util.ChangeSupport;
    55 import org.openide.util.Mutex;
    56 
    57 /**
    58  *
    59  * @author Andrea Lucarelli
    60  */
    61 public class AdaSources implements Sources, ChangeListener, PropertyChangeListener {
    62 
    63     private static final String BUILD_DIR_PROP = "${" + AdaProjectProperties.BUILD_DIR + "}";    //NOI18N
    64     private static final String DIST_DIR_PROP = "${" + AdaProjectProperties.DIST_DIR + "}";    //NOI18N
    65     public static final String SOURCES_TYPE_ADA = "ADASOURCE"; // NOI18N
    66     private final Project project;
    67     private final ChangeSupport changeSupport;
    68     private final AntProjectHelper helper;
    69     private final PropertyEvaluator evaluator;
    70     private final SourceRoots sourceRoots;
    71     private final SourceRoots testRoots;
    72     private Sources delegate;
    73     private SourcesHelper sourcesHelper;
    74     private boolean externalRootsRegistered;
    75 
    76     public AdaSources(Project project, final AntProjectHelper helper, final PropertyEvaluator eval, final SourceRoots sources, final SourceRoots tests) {
    77         assert project != null;
    78         assert helper != null;
    79         assert eval != null;
    80         assert sources != null;
    81         assert tests != null;
    82 
    83         this.project = project;
    84         this.helper = helper;
    85         this.evaluator = eval;
    86         this.sourceRoots = sources;
    87         this.testRoots = tests;
    88         this.changeSupport = new ChangeSupport(this);
    89         this.sourceRoots.addPropertyChangeListener(this);
    90         this.testRoots.addPropertyChangeListener(this);
    91         this.evaluator.addPropertyChangeListener(this);
    92         initSources();
    93     }
    94 
    95     public SourceGroup[] getSourceGroups(final String type) {
    96         return ProjectManager.mutex().readAccess(new Mutex.Action<SourceGroup[]>() {
    97 
    98             public SourceGroup[] run() {
    99                 Sources _delegate;
   100                 synchronized (AdaSources.this) {
   101                     if (delegate == null) {
   102                         delegate = initSources();
   103                         delegate.addChangeListener(AdaSources.this);
   104                     }
   105                     _delegate = delegate;
   106                 }
   107                 SourceGroup[] groups = _delegate.getSourceGroups(type);
   108                 return groups;
   109             }
   110         });
   111     }
   112 
   113     private Sources initSources() {
   114         this.sourcesHelper = new SourcesHelper(project, helper, evaluator);
   115         register(sourceRoots);
   116         register(testRoots);
   117         this.sourcesHelper.addNonSourceRoot(BUILD_DIR_PROP);
   118         this.sourcesHelper.addNonSourceRoot(DIST_DIR_PROP);
   119         externalRootsRegistered = false;
   120         ProjectManager.mutex().postWriteRequest(new Runnable() {
   121 
   122             public void run() {
   123                 if (!externalRootsRegistered) {
   124                     sourcesHelper.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
   125                     externalRootsRegistered = true;
   126                 }
   127             }
   128         });
   129         return this.sourcesHelper.createSources();
   130     }
   131 
   132     private void register(SourceRoots roots) {
   133         String[] propNames = roots.getRootProperties();
   134         String[] rootNames = roots.getRootNames();
   135         for (int i = 0; i < propNames.length; i++) {
   136             String prop = propNames[i];
   137             String displayName = roots.getRootDisplayName(rootNames[i], prop);
   138             String loc = "${" + prop + "}"; // NOI18N
   139 //            sourcesHelper.addPrincipalSourceRoot(loc, displayName, null, null); // NOI18N
   140 //            sourcesHelper.addTypedSourceRoot(loc, SOURCES_TYPE_ADA, displayName, null, null);
   141             sourcesHelper.sourceRoot(loc).displayName(displayName)
   142                     .add() // principal root
   143                     .type(SOURCES_TYPE_ADA).add();    // typed root
   144 
   145         }
   146     }
   147 
   148     public void addChangeListener(ChangeListener changeListener) {
   149         changeSupport.addChangeListener(changeListener);
   150     }
   151 
   152     public void removeChangeListener(ChangeListener changeListener) {
   153         changeSupport.removeChangeListener(changeListener);
   154     }
   155 
   156     private void fireChange() {
   157         synchronized (this) {
   158             if (delegate != null) {
   159                 delegate.removeChangeListener(this);
   160                 delegate = null;
   161             }
   162         }
   163         changeSupport.fireChange();
   164     }
   165 
   166     public void propertyChange(PropertyChangeEvent evt) {
   167         String propName = evt.getPropertyName();
   168         if (SourceRoots.PROP_ROOT_PROPERTIES.equals(propName) ||
   169                 AdaProjectProperties.BUILD_DIR.equals(propName) ||
   170                 AdaProjectProperties.DIST_DIR.equals(propName)) {
   171             this.fireChange();
   172         }
   173     }
   174 
   175     public void stateChanged(ChangeEvent event) {
   176         this.fireChange();
   177     }
   178 }