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.modelimpl.trace;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.ArrayList;
047: import java.util.Collection;
048: import java.util.Collections;
049: import java.util.List;
050: import org.netbeans.modules.cnd.api.project.NativeFileItem;
051: import org.netbeans.modules.cnd.api.project.NativeFileItemSet;
052: import org.netbeans.modules.cnd.api.project.NativeProject;
053: import org.netbeans.modules.cnd.api.project.NativeProjectItemsListener;
054: import org.netbeans.modules.cnd.loaders.CCDataLoader;
055: import org.netbeans.modules.cnd.loaders.CCDataObject;
056: import org.netbeans.modules.cnd.loaders.CDataLoader;
057: import org.netbeans.modules.cnd.loaders.CDataObject;
058: import org.netbeans.modules.cnd.loaders.CndDataObject;
059: import org.netbeans.modules.cnd.loaders.HDataLoader;
060: import org.netbeans.modules.cnd.loaders.HDataObject;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileUtil;
063: import org.openide.loaders.DataObject;
064: import org.openide.loaders.DataObjectNotFoundException;
065:
066: /**
067: *
068: * @author vv159170
069: */
070: public final class NativeProjectProvider {
071:
072: /** Creates a new instance of NativeProjectProvider */
073: private NativeProjectProvider() {
074: }
075:
076: public static NativeProject createProject(String projectRoot,
077: List<File> files, List<String> sysIncludes,
078: List<String> usrIncludes, List<String> sysMacros,
079: List<String> usrMacros, boolean pathsRelCurFile) {
080:
081: NativeProjectImpl project = new NativeProjectImpl(projectRoot,
082: sysIncludes, usrIncludes, sysMacros, usrMacros,
083: pathsRelCurFile);
084:
085: project.addFiles(files);
086:
087: return project;
088: }
089:
090: public static void fireAllFilesChanged(NativeProject nativeProject) {
091: if (nativeProject instanceof NativeProjectImpl) {
092: ((NativeProjectImpl) nativeProject).fireAllFilesChanged();
093: }
094: }
095:
096: private static final class NativeProjectImpl implements
097: NativeProject {
098:
099: private final List<String> sysIncludes;
100: private final List<String> usrIncludes;
101: private final List<String> sysMacros;
102: private final List<String> usrMacros;
103:
104: private final List<NativeFileItem> files = new ArrayList<NativeFileItem>();
105:
106: private final String projectRoot;
107: private boolean pathsRelCurFile;
108:
109: private List<NativeProjectItemsListener> listeners = new ArrayList<NativeProjectItemsListener>();
110: private Object listenersLock = new Object();
111:
112: public NativeProjectImpl(String projectRoot,
113: List<String> sysIncludes, List<String> usrIncludes,
114: List<String> sysMacros, List<String> usrMacros) {
115:
116: this (projectRoot, sysIncludes, usrIncludes, sysMacros,
117: usrMacros, false);
118: }
119:
120: public NativeProjectImpl(String projectRoot,
121: List<String> sysIncludes, List<String> usrIncludes,
122: List<String> sysMacros, List<String> usrMacros,
123: boolean pathsRelCurFile) {
124:
125: this .projectRoot = projectRoot;
126: this .pathsRelCurFile = pathsRelCurFile;
127:
128: this .sysIncludes = createIncludes(sysIncludes);
129: this .usrIncludes = createIncludes(usrIncludes);
130: this .sysMacros = new ArrayList<String>(sysMacros);
131: this .usrMacros = new ArrayList<String>(usrMacros);
132: }
133:
134: private List<String> createIncludes(List<String> src) {
135: if (pathsRelCurFile) {
136: return new ArrayList<String>(src);
137: } else {
138: List<String> result = new ArrayList<String>(src.size());
139: for (String path : src) {
140: File file = new File(path);
141: result.add(file.getAbsolutePath());
142: }
143: return result;
144: }
145: }
146:
147: private void addFiles(List<File> files) {
148: for (File file : files) {
149: addFile(file);
150: }
151: }
152:
153: public Object getProject() {
154: return null;
155: }
156:
157: public List<String> getSourceRoots() {
158: return Collections.<String> emptyList();
159: }
160:
161: public String getProjectRoot() {
162: return this .projectRoot;
163: }
164:
165: public String getProjectDisplayName() {
166: return getProjectRoot();
167: }
168:
169: public List<NativeFileItem> getAllFiles() {
170: return Collections.unmodifiableList(files);
171: }
172:
173: public void addProjectItemsListener(
174: NativeProjectItemsListener listener) {
175: synchronized (listenersLock) {
176: listeners.add(listener);
177: }
178: }
179:
180: public void removeProjectItemsListener(
181: NativeProjectItemsListener listener) {
182: synchronized (listenersLock) {
183: listeners.remove(listener);
184: }
185: }
186:
187: private void fireAllFilesChanged() {
188: List<NativeProjectItemsListener> listenersCopy;
189: synchronized (listenersLock) {
190: listenersCopy = new ArrayList<NativeProjectItemsListener>(
191: listeners);
192: }
193: List<NativeFileItem> items = Collections
194: .unmodifiableList(files);
195: for (NativeProjectItemsListener listener : listenersCopy) {
196: listener.filesPropertiesChanged(items);
197: }
198: }
199:
200: public NativeFileItem findFileItem(File file) {
201: String path = file.getAbsolutePath();
202: for (NativeFileItem item : files) {
203: if (item.getFile().getAbsolutePath().equalsIgnoreCase(
204: path)) {
205: return item;
206: }
207: }
208: return null;
209: }
210:
211: public List<String> getSystemIncludePaths() {
212: return this .sysIncludes;
213: }
214:
215: public List<String> getUserIncludePaths() {
216: return this .usrIncludes;
217: }
218:
219: public List<String> getSystemMacroDefinitions() {
220: return this .sysMacros;
221: }
222:
223: public List<String> getUserMacroDefinitions() {
224: return this .usrMacros;
225: }
226:
227: private void addFile(File file) {
228: DataObject dobj = getDataObject(file);
229: NativeFileItem.Language lang = getLanguage(file, dobj);
230: NativeFileItem item = new NativeFileItemImpl(file, this ,
231: lang);
232: //TODO: put item in loockup of DataObject
233: // registerItemInDataObject(dobj, item);
234: this .files.add(item);
235: }
236:
237: NativeFileItem.Language getLanguage(File file, DataObject dobj) {
238: if (dobj == null) {
239: String path = file.getAbsolutePath();
240: if (CCDataLoader.getInstance()
241: .getDefaultExtensionList().isRegistered(path)) {
242: return NativeFileItem.Language.CPP;
243: } else if (CDataLoader.getInstance()
244: .getDefaultExtensionList().isRegistered(path)) {
245: return NativeFileItem.Language.C;
246: } else if (HDataLoader.getInstance()
247: .getDefaultExtensionList().isRegistered(path)) {
248: return NativeFileItem.Language.C_HEADER;
249: } else {
250: return NativeFileItem.Language.OTHER;
251: }
252: } else if (dobj instanceof CCDataObject) {
253: return NativeFileItem.Language.CPP;
254: } else if (dobj instanceof HDataObject) {
255: return NativeFileItem.Language.C_HEADER;
256: } else if (dobj instanceof CDataObject) {
257: return NativeFileItem.Language.C;
258: } else {
259: return NativeFileItem.Language.OTHER;
260: }
261: }
262:
263: // /*package*/ void addHeaders(List<String> files) {
264: // addFiles(files, this.sources, NativeFileItem.Language.C_HEADER);
265: // }
266: //
267: // /*package*/ void addSources(List<String> files) {
268: // addFiles(files, this.sources, NativeFileItem.Language.CPP);
269: // }
270: //
271: // private void addFiles(List<String> files, List<NativeFileItem> dest, NativeFileItem.Language lang) {
272: // for (String path : files) {
273: // NativeFileItem item = new NativeFileItemImpl(path, this, lang);
274: // dest.add(item);
275: // }
276: // }
277:
278: public List<NativeProject> getDependences() {
279: return Collections.<NativeProject> emptyList();
280: }
281: }
282:
283: private static DataObject getDataObject(File file) {
284:
285: DataObject dobj = null;
286: try {
287: FileObject fo = FileUtil.toFileObject(file
288: .getCanonicalFile());
289: if (fo != null) {
290: try {
291: dobj = DataObject.find(fo);
292: } catch (DataObjectNotFoundException ex) {
293: // skip;
294: }
295: }
296: } catch (IOException ioe) {
297: // skip;
298: }
299:
300: return dobj;
301: }
302:
303: /*package*/static void registerItemInDataObject(DataObject obj,
304: NativeFileItem item) {
305: if (obj != null) {
306: NativeFileItemSet set = obj.getLookup().lookup(
307: NativeFileItemSet.class);
308: if (set == null) {
309: set = new NativeFileItemSetImpl();
310: if (obj instanceof CndDataObject) {
311: ((CndDataObject) obj).addCookie(set);
312: }
313: }
314: set.add(item);
315: }
316: }
317:
318: private static class NativeFileItemSetImpl implements
319: NativeFileItemSet {
320: private List<NativeFileItem> items = new ArrayList<NativeFileItem>(
321: 1);
322:
323: public synchronized Collection<NativeFileItem> getItems() {
324: return new ArrayList<NativeFileItem>(items);
325: }
326:
327: public synchronized void add(NativeFileItem item) {
328: if (!items.contains(item)) {
329: items.add(item);
330: }
331: }
332:
333: public synchronized void remove(NativeFileItem item) {
334: items.remove(item);
335: }
336:
337: public boolean isEmpty() {
338: return items.isEmpty();
339: }
340: }
341:
342: private static final class NativeFileItemImpl implements
343: NativeFileItem {
344:
345: private final File file;
346: private final NativeProjectImpl project;
347: private final NativeFileItem.Language lang;
348:
349: public NativeFileItemImpl(File file, NativeProjectImpl project,
350: NativeFileItem.Language language) {
351:
352: this .project = project;
353: this .file = file;
354: this .lang = language;
355: }
356:
357: public NativeProject getNativeProject() {
358: return project;
359: }
360:
361: public File getFile() {
362: return file;
363: }
364:
365: public List<String> getSystemIncludePaths() {
366: List<String> result = project.getSystemIncludePaths();
367: return project.pathsRelCurFile ? toAbsolute(result)
368: : result;
369: }
370:
371: public List<String> getUserIncludePaths() {
372: List<String> result = project.getUserIncludePaths();
373: return project.pathsRelCurFile ? toAbsolute(result)
374: : result;
375: }
376:
377: private List<String> toAbsolute(List<String> orig) {
378: File base = file.getParentFile();
379: List<String> result = new ArrayList<String>(orig.size());
380: for (String path : orig) {
381: File pathFile = new File(path);
382: if (pathFile.isAbsolute()) {
383: result.add(path);
384: } else {
385: pathFile = new File(base, path);
386: result.add(pathFile.getAbsolutePath());
387: }
388: }
389: return result;
390: }
391:
392: public List<String> getSystemMacroDefinitions() {
393: return project.getSystemMacroDefinitions();
394: }
395:
396: public List<String> getUserMacroDefinitions() {
397: return project.getUserMacroDefinitions();
398: }
399:
400: public NativeFileItem.Language getLanguage() {
401: return lang;
402: }
403:
404: public NativeFileItem.LanguageFlavor getLanguageFlavor() {
405: return NativeFileItem.LanguageFlavor.GENERIC;
406: }
407:
408: public boolean isExcluded() {
409: return false;
410: }
411: }
412: }
|