001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.makeproject.api.configurations;
043:
044: import java.util.Collection;
045: import java.util.HashSet;
046: import java.util.Iterator;
047: import javax.swing.SwingUtilities;
048: import org.netbeans.modules.cnd.makeproject.configurations.ConfigurationXMLReader;
049: import org.openide.filesystems.FileObject;
050: import org.openide.util.Lookup;
051: import org.openide.util.RequestProcessor;
052:
053: public class ConfigurationDescriptorProvider {
054: private FileObject projectDirectory;
055: private ConfigurationDescriptor projectDescriptor = null;
056: boolean hasTried = false;
057: private String relativeOffset = null;
058:
059: public ConfigurationDescriptorProvider(FileObject projectDirectory) {
060: this .projectDirectory = projectDirectory;
061: }
062:
063: public void setRelativeOffset(String relativeOffset) {
064: this .relativeOffset = relativeOffset;
065: }
066:
067: private Object readLock = new Object();
068:
069: public ConfigurationDescriptor getConfigurationDescriptor() {
070: if (projectDescriptor == null) {
071: // attempt to read configuration descriptor
072: if (!hasTried) {
073: // do this only once
074: synchronized (readLock) {
075: // check again that someone already havn't read
076: if (!hasTried) {
077: ConfigurationXMLReader reader = new ConfigurationXMLReader(
078: projectDirectory);
079:
080: if (SwingUtilities.isEventDispatchThread()) {
081: //System.err.println("ConfigurationDescriptorProvider Switching thead...");
082: ProjectReader projectReader = new ProjectReader(
083: reader, relativeOffset);
084: RequestProcessor.Task task = RequestProcessor
085: .getDefault().post(projectReader);
086: task.waitFinished();
087: projectDescriptor = projectReader.projectDescriptor;
088: } else {
089: try {
090: projectDescriptor = reader
091: .read(relativeOffset);
092: } catch (java.io.IOException x) {
093: ; // most likely open failed
094: }
095: }
096:
097: hasTried = true;
098: }
099: }
100: }
101: }
102: return projectDescriptor;
103: }
104:
105: public boolean gotDescriptor() {
106: return projectDescriptor != null;
107: }
108:
109: private class ProjectReader implements Runnable {
110: public ConfigurationDescriptor projectDescriptor = null;
111: private ConfigurationXMLReader reader;
112: private String relativeOffset;
113:
114: public ProjectReader(ConfigurationXMLReader reader,
115: String relativeOffset) {
116: this .reader = reader;
117: this .relativeOffset = relativeOffset;
118: }
119:
120: public void run() {
121: try {
122: projectDescriptor = reader.read(relativeOffset);
123: } catch (java.io.IOException x) {
124: ; // most likely open failed
125: }
126: }
127: }
128:
129: public static ConfigurationAuxObjectProvider[] getAuxObjectProviders() {
130: HashSet auxObjectProviders = new HashSet();
131: Lookup.Template template = new Lookup.Template(
132: ConfigurationAuxObjectProvider.class);
133: Lookup.Result result = Lookup.getDefault().lookup(template);
134: Collection collection = result.allInstances();
135: // System.err.println("-------------------------------collection " + collection);
136: Iterator iterator = collection.iterator();
137: while (iterator.hasNext()) {
138: Object caop = iterator.next();
139: if (caop instanceof ConfigurationAuxObjectProvider) {
140: auxObjectProviders.add(caop);
141: }
142: }
143: // System.err.println("-------------------------------auxObjectProviders " + auxObjectProviders);
144: return (ConfigurationAuxObjectProvider[]) auxObjectProviders
145: .toArray(new ConfigurationAuxObjectProvider[auxObjectProviders
146: .size()]);
147: }
148: }
|