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.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.io.File;
047: import java.io.IOException;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.ArrayList;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.modules.cnd.api.project.NativeFileItem;
053: import org.netbeans.modules.cnd.api.project.NativeFileItem.Language;
054: import org.netbeans.modules.cnd.api.project.NativeProject;
055: import org.netbeans.modules.cnd.loaders.CCDataLoader;
056: import org.netbeans.modules.cnd.loaders.CCDataObject;
057: import org.netbeans.modules.cnd.loaders.CDataObject;
058: import org.netbeans.modules.cnd.loaders.FortranDataObject;
059: import org.netbeans.modules.cnd.loaders.HDataObject;
060: import org.netbeans.modules.cnd.makeproject.api.remote.FilePathAdaptor;
061: import org.netbeans.modules.cnd.api.utils.IpeUtils;
062: import org.netbeans.modules.cnd.loaders.CDataLoader;
063: import org.netbeans.modules.cnd.loaders.FortranDataLoader;
064: import org.netbeans.modules.cnd.loaders.HDataLoader;
065: import org.netbeans.modules.cnd.makeproject.api.compilers.BasicCompiler;
066: import org.netbeans.modules.cnd.api.compilers.CompilerSetManager;
067: import org.netbeans.modules.cnd.api.compilers.CompilerSet;
068: import org.netbeans.modules.cnd.api.compilers.Tool;
069: import org.netbeans.modules.cnd.settings.CppSettings;
070: import org.openide.ErrorManager;
071: import org.openide.filesystems.FileObject;
072: import org.openide.filesystems.FileUtil;
073: import org.openide.loaders.DataObject;
074: import org.openide.loaders.DataObjectNotFoundException;
075:
076: public class Item implements NativeFileItem, PropertyChangeListener {
077: private final String path;
078: private final String sortName;
079: private Folder folder;
080: private File file = null;
081: private String id = null;
082:
083: public Item(String path) {
084: this .path = path;
085: this .sortName = IpeUtils.getBaseName(path).toLowerCase();
086: // int i = sortName.lastIndexOf("."); // NOI18N
087: // if (i > 0) {
088: // this.sortName = sortName.substring(0, i);
089: // } else {
090: // this.sortName = sortName;
091: // }
092: folder = null;
093: }
094:
095: /**
096: * Rename item.
097: * @param newname new name without suffic or path
098: */
099: public void rename(String newname) {
100: if (newname == null || newname.length() == 0
101: || getFolder() == null)
102: return;
103:
104: // Rename name in path
105: int indexName = path.lastIndexOf('/');
106: if (indexName < 0)
107: indexName = 0;
108: else
109: indexName++;
110: int indexDot = path.lastIndexOf('.');
111: if (indexDot < indexName)
112: indexDot = -1;
113:
114: String oldname;
115: if (indexDot >= 0)
116: oldname = path.substring(indexName, indexDot);
117: else
118: oldname = path.substring(indexName);
119: if (oldname.equals(newname))
120: return;
121:
122: String newPath = ""; // NOI18N
123: if (indexName > 0)
124: newPath = path.substring(0, indexName);
125: newPath += newname;
126: if (indexDot >= 0)
127: newPath += path.substring(indexDot);
128: // Remove old item and insert new with new name
129: moveTo(newPath);
130: }
131:
132: public void moveTo(String newPath) {
133: Folder f = getFolder();
134: // FIXUP: update all configurations with settings from old item....
135: String oldPath = getAbsPath();
136: f.removeItem(this );
137: Item item = new Item(newPath);
138: f.addItem(item);
139: f.renameItemAction(oldPath, item);
140: }
141:
142: public String getPath() {
143: return path;
144: }
145:
146: public String getSortName() {
147: return sortName;
148: }
149:
150: public String getPath(boolean norm) {
151: String pat = "./"; // UNIX path // NOI18N
152: if (norm && getPath().startsWith(pat))
153: return getPath().substring(2);
154: else
155: return getPath();
156: }
157:
158: public String getAbsPath() {
159: String retPath = null;
160: if (IpeUtils.isPathAbsolute(getPath())) {// UNIX path
161: retPath = getPath();
162: retPath = FilePathAdaptor.mapToLocal(retPath);
163: } else if (getFolder() != null) {
164: retPath = getFolder().getConfigurationDescriptor()
165: .getBaseDir()
166: + '/' + getPath(); // UNIX path
167: }
168: return retPath;
169: }
170:
171: public void setFolder(Folder folder) {
172: this .folder = folder;
173: if (folder != null)
174: addPropertyChangeListener();
175: }
176:
177: private DataObject myDataObject = null;
178:
179: public void addPropertyChangeListener() {
180: myDataObject = getDataObject();
181: if (myDataObject != null) {
182: myDataObject.addPropertyChangeListener(this );
183: }
184: }
185:
186: public void removePropertyChangeListener() {
187: //DataObject dataObject = getDataObject();
188: if (myDataObject != null) {
189: myDataObject.removePropertyChangeListener(this );
190: myDataObject = null;
191: }
192: }
193:
194: public DataObject getLastDataObject() {
195: return myDataObject;
196: }
197:
198: public void propertyChange(PropertyChangeEvent evt) {
199: if (evt.getPropertyName().equals("name")) { // NOI18N
200: // File has been renamed
201: rename((String) evt.getNewValue());
202: } else if (evt.getPropertyName().equals("valid")) { // NOI18N
203: // File has been deleted
204: // Do nothing (IZ 87557, 94935)
205: if (!((Boolean) evt.getNewValue()).booleanValue()) {
206: // getFolder().removeItemAction(this);
207: getFolder().refresh(this );
208: }
209: } else if (evt.getPropertyName().equals("primaryFile")) { // NOI18N
210: // File has been moved
211: FileObject fo = (FileObject) evt.getNewValue();
212: String newPath = FileUtil.toFile(fo).getPath();
213: if (!IpeUtils.isPathAbsolute(getPath())) {
214: newPath = IpeUtils.toRelativePath(getFolder()
215: .getConfigurationDescriptor().getBaseDir(),
216: newPath);
217: }
218: newPath = FilePathAdaptor.normalize(newPath);
219: moveTo(newPath);
220: }
221: }
222:
223: public Folder getFolder() {
224: return folder;
225: }
226:
227: public File getFile() {
228: String aPath = getAbsPath();
229: if (aPath != null) {
230: return FileUtil.normalizeFile(new File(aPath));
231: }
232: return null;
233: }
234:
235: public File getCanonicalFile() {
236: if (file == null) {
237: try {
238: file = new File(getAbsPath()).getCanonicalFile();
239: } catch (IOException ioe) {
240: file = new File(getAbsPath());
241: }
242: }
243: return file;
244: }
245:
246: public String getId() {
247: if (id == null) {
248: id = "i-" + getPath(); // NOI18N
249: }
250: return id;
251: }
252:
253: public ItemConfiguration getItemConfiguration(
254: Configuration configuration) {
255: if (configuration != null) {
256: return (ItemConfiguration) configuration
257: .getAuxObject(getId());
258: }
259: return null;
260: }
261:
262: public ItemConfiguration[] getItemConfigurations() {
263: ItemConfiguration[] itemConfigurations;
264: MakeConfigurationDescriptor makeConfigurationDescriptor = getMakeConfigurationDescriptor();
265: if (makeConfigurationDescriptor == null)
266: return new ItemConfiguration[0];
267: Configuration[] configurations = makeConfigurationDescriptor
268: .getConfs().getConfs();
269: itemConfigurations = new ItemConfiguration[configurations.length];
270: for (int i = 0; i < configurations.length; i++) {
271: itemConfigurations[i] = getItemConfiguration(configurations[i]);
272: }
273: return itemConfigurations;
274: }
275:
276: public FileObject getFileObject() {
277: File file = getCanonicalFile();
278: FileObject fo = null;
279: try {
280: fo = FileUtil.toFileObject(file.getCanonicalFile());
281: } catch (IOException e) {
282: }
283: return fo;
284: }
285:
286: public DataObject getDataObject() {
287: DataObject dataObject = null;
288: FileObject fo = getFileObject();
289: if (fo != null) {
290: try {
291: dataObject = DataObject.find(fo);
292: } catch (DataObjectNotFoundException e) {
293: // should not happen
294: ErrorManager.getDefault().notify(e);
295: }
296: }
297: return dataObject;
298: }
299:
300: public int getDefaultTool() {
301: DataObject dataObject = getDataObject();
302: int tool;
303:
304: if (dataObject == null) {
305: if (CCDataLoader.getInstance().getExtensions()
306: .isRegistered(path)) {
307: tool = Tool.CCCompiler;
308: } else if (CDataLoader.getInstance().getExtensions()
309: .isRegistered(path)) {
310: tool = Tool.CCompiler;
311: } else if (FortranDataLoader.getInstance().getExtensions()
312: .isRegistered(path)) {
313: tool = Tool.FortranCompiler;
314: } else {
315: tool = Tool.CustomTool;
316: }
317: } else if (dataObject instanceof CDataObject)
318: tool = Tool.CCompiler;
319: else if (dataObject instanceof HDataObject)
320: tool = Tool.CustomTool;
321: else if (dataObject instanceof CCDataObject)
322: tool = Tool.CCCompiler;
323: else if (CppSettings.getDefault().isFortranEnabled()
324: && dataObject instanceof FortranDataObject)
325: tool = Tool.FortranCompiler;
326: else
327: tool = Tool.CustomTool;
328: return tool;
329: }
330:
331: private MakeConfigurationDescriptor getMakeConfigurationDescriptor() {
332: if (getFolder() == null)
333: return null;
334: return (MakeConfigurationDescriptor) getFolder()
335: .getConfigurationDescriptor();
336: }
337:
338: private MakeConfiguration getMakeConfiguration() {
339: MakeConfigurationDescriptor makeConfigurationDescriptor = getMakeConfigurationDescriptor();
340: if (makeConfigurationDescriptor == null)
341: return null;
342: return (MakeConfiguration) makeConfigurationDescriptor
343: .getConfs().getActive();
344: }
345:
346: public NativeProject getNativeProject() {
347: Folder folder = getFolder();
348: if (folder != null) {
349: Project project = folder.getProject();
350: return (NativeProject) project.getLookup().lookup(
351: NativeProject.class);
352: }
353: return null;
354: }
355:
356: public List getSystemIncludePaths() {
357: List vec = new ArrayList();
358: MakeConfiguration makeConfiguration = getMakeConfiguration();
359: ItemConfiguration itemConfiguration = getItemConfiguration(makeConfiguration);//ItemConfiguration)makeConfiguration.getAuxObject(ItemConfiguration.getId(getPath()));
360: if (itemConfiguration == null
361: || !itemConfiguration.isCompilerToolConfiguration()) // FIXUP: sometimes itemConfiguration is null (should not happen)
362: return vec;
363: CompilerSet compilerSet = CompilerSetManager.getDefault()
364: .getCompilerSet(
365: makeConfiguration.getCompilerSet().getValue());
366: BasicCompiler compiler = (BasicCompiler) compilerSet
367: .getTool(itemConfiguration.getTool());
368: BasicCompilerConfiguration compilerConfiguration = itemConfiguration
369: .getCompilerConfiguration();
370: if (compilerConfiguration instanceof CCCCompilerConfiguration) {
371: // Get include paths from compiler
372: if (compiler != null) {
373: vec.addAll(compiler.getSystemIncludeDirectories());
374: }
375: }
376: return vec;
377: }
378:
379: public List getUserIncludePaths() {
380: List vec = new ArrayList();
381: MakeConfiguration makeConfiguration = getMakeConfiguration();
382: ItemConfiguration itemConfiguration = getItemConfiguration(makeConfiguration);//ItemConfiguration)makeConfiguration.getAuxObject(ItemConfiguration.getId(getPath()));
383: if (itemConfiguration == null
384: || !itemConfiguration.isCompilerToolConfiguration()) // FIXUP: sometimes itemConfiguration is null (should not happen)
385: return vec;
386: CompilerSet compilerSet = CompilerSetManager.getDefault()
387: .getCompilerSet(
388: makeConfiguration.getCompilerSet().getValue());
389: BasicCompiler compiler = (BasicCompiler) compilerSet
390: .getTool(itemConfiguration.getTool());
391: BasicCompilerConfiguration compilerConfiguration = itemConfiguration
392: .getCompilerConfiguration();
393: if (compilerConfiguration instanceof CCCCompilerConfiguration) {
394: // Get include paths from project/file
395: List vec2 = new ArrayList();
396: CCCCompilerConfiguration cccCompilerConfiguration = (CCCCompilerConfiguration) compilerConfiguration;
397: CCCCompilerConfiguration master = (CCCCompilerConfiguration) cccCompilerConfiguration
398: .getMaster();
399: while (master != null
400: && cccCompilerConfiguration.getInheritIncludes()
401: .getValue()) {
402: vec2.addAll(master.getIncludeDirectories().getValue());
403: if (master.getInheritIncludes().getValue())
404: master = (CCCCompilerConfiguration) master
405: .getMaster();
406: else
407: master = null;
408: }
409: vec2.addAll(cccCompilerConfiguration
410: .getIncludeDirectories().getValue());
411: // Convert all paths to absolute paths
412: Iterator iter = vec2.iterator();
413: while (iter.hasNext()) {
414: vec.add(IpeUtils.toAbsolutePath(getFolder()
415: .getConfigurationDescriptor().getBaseDir(),
416: (String) iter.next()));
417: }
418: }
419: return vec;
420: }
421:
422: public List getSystemMacroDefinitions() {
423: List vec = new ArrayList();
424: MakeConfiguration makeConfiguration = getMakeConfiguration();
425: ItemConfiguration itemConfiguration = getItemConfiguration(makeConfiguration); //ItemConfiguration)makeConfiguration.getAuxObject(ItemConfiguration.getId(getPath()));
426: if (itemConfiguration == null
427: || !itemConfiguration.isCompilerToolConfiguration()) // FIXUP: itemConfiguration should never be null
428: return vec;
429: CompilerSet compilerSet = CompilerSetManager.getDefault()
430: .getCompilerSet(
431: makeConfiguration.getCompilerSet().getValue());
432: BasicCompiler compiler = (BasicCompiler) compilerSet
433: .getTool(itemConfiguration.getTool());
434: BasicCompilerConfiguration compilerConfiguration = itemConfiguration
435: .getCompilerConfiguration();
436: if (compilerConfiguration instanceof CCCCompilerConfiguration) {
437: if (compiler != null) {
438: // Get macro definitions from compiler
439: vec.addAll(compiler.getSystemPreprocessorSymbols());
440: }
441: }
442: return vec;
443: }
444:
445: public List getUserMacroDefinitions() {
446: List vec = new ArrayList();
447: MakeConfiguration makeConfiguration = getMakeConfiguration();
448: ItemConfiguration itemConfiguration = getItemConfiguration(makeConfiguration); //ItemConfiguration)makeConfiguration.getAuxObject(ItemConfiguration.getId(getPath()));
449: if (itemConfiguration == null
450: || !itemConfiguration.isCompilerToolConfiguration()) // FIXUP: itemConfiguration should never be null
451: return vec;
452: CompilerSet compilerSet = CompilerSetManager.getDefault()
453: .getCompilerSet(
454: makeConfiguration.getCompilerSet().getValue());
455: BasicCompiler compiler = (BasicCompiler) compilerSet
456: .getTool(itemConfiguration.getTool());
457: BasicCompilerConfiguration compilerConfiguration = itemConfiguration
458: .getCompilerConfiguration();
459: if (compilerConfiguration instanceof CCCCompilerConfiguration) {
460: CCCCompilerConfiguration cccCompilerConfiguration = (CCCCompilerConfiguration) compilerConfiguration;
461: CCCCompilerConfiguration master = (CCCCompilerConfiguration) cccCompilerConfiguration
462: .getMaster();
463: while (master != null
464: && cccCompilerConfiguration
465: .getInheritPreprocessor().getValue()) {
466: vec.addAll(master.getPreprocessorConfiguration()
467: .getValue());
468: if (master.getInheritIncludes().getValue())
469: master = (CCCCompilerConfiguration) master
470: .getMaster();
471: else
472: master = null;
473: }
474: vec.addAll(cccCompilerConfiguration
475: .getPreprocessorConfiguration().getValue());
476: }
477: return vec;
478: }
479:
480: public boolean hasHeaderOrSourceExtension(boolean cFiles,
481: boolean ccFiles) {
482: // Method return true for source files also.
483: String itemPath = getPath();
484: return HDataLoader.getInstance().getExtensions().isRegistered(
485: itemPath)
486: || ccFiles
487: && CCDataLoader.getInstance().getExtensions()
488: .isRegistered(itemPath)
489: || cFiles
490: && CDataLoader.getInstance().getExtensions()
491: .isRegistered(itemPath);
492: }
493:
494: /**
495: * NativeFileItem interface
496: **/
497: public Language getLanguage() {
498: int tool;
499: Language language;
500: ItemConfiguration itemConfiguration = null;
501: MakeConfiguration makeConfiguration = getMakeConfiguration();
502: if (makeConfiguration != null)
503: itemConfiguration = getItemConfiguration(makeConfiguration); //ItemConfiguration)makeConfiguration.getAuxObject(ItemConfiguration.getId(getPath()));
504:
505: if (itemConfiguration != null)
506: tool = itemConfiguration.getTool();
507: else
508: tool = getDefaultTool();
509:
510: if (tool == Tool.CCompiler)
511: language = NativeFileItem.Language.C;
512: else if (tool == Tool.CCCompiler)
513: language = NativeFileItem.Language.CPP;
514: else if (tool == Tool.FortranCompiler)
515: language = NativeFileItem.Language.FORTRAN;
516: else if (hasHeaderOrSourceExtension(true, true))
517: language = NativeFileItem.Language.C_HEADER;
518: else
519: language = NativeFileItem.Language.OTHER;
520:
521: return language;
522: }
523:
524: /**
525: * NativeFileItem interface
526: **/
527: public LanguageFlavor getLanguageFlavor() {
528: return NativeFileItem.LanguageFlavor.GENERIC;
529: }
530:
531: /**
532: * NativeFileItem interface
533: **/
534: public boolean isExcluded() {
535: ItemConfiguration itemConfiguration = getItemConfiguration(getMakeConfiguration());
536: if (itemConfiguration != null) {
537: return itemConfiguration.getExcluded().getValue();
538: }
539: return true;
540: }
541: }
|