1
/*******************************************************************************
2
* Copyright (c) 2008 Wind River Systems, Inc. and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Markus Schorn - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.internal.core.resources;
14
import java.util.Arrays;
15
import java.util.Collections;
16
import java.util.Comparator;
18
import org.eclipse.core.resources.IFile;
19
import org.eclipse.core.resources.IProject;
20
import org.eclipse.core.runtime.IPath;
23
* Allows for looking up resources by location or name.
25
public class ResourceLookup {
26
private static ResourceLookupTree lookupTree= new ResourceLookupTree();
28
public static void startup() {
32
public static void shutdown() {
33
lookupTree.shutdown();
37
* Searches for files with the given location suffix.
39
* At this point the method works for sources and headers (no other content types), only.
40
* This is done to use less memory and can be changed if necessary.
41
* For linked resource files, the name of the link target is relevant.
43
* @param locationSuffix the suffix to match, always used as relative path.
44
* @param projects the projects to search
45
* @param ignoreCase whether or not to ignore case when comparing the suffix.
47
public static IFile[] findFilesByName(IPath locationSuffix, IProject[] projects, boolean ignoreCase) {
48
return lookupTree.findFilesByName(locationSuffix, projects, ignoreCase);
52
* Uses a lookup-tree that finds resources for locations using the canonical representation
55
public static IFile[] findFilesForLocationURI(URI location) {
56
return lookupTree.findFilesForLocationURI(location);
60
* Uses a lookup-tree that finds resources for locations using the canonical representation
61
* of the path. The method does not work for files where the name (last segment) of the
62
* resources differs from the name of the location.
64
public static IFile[] findFilesForLocation(IPath location) {
65
return lookupTree.findFilesForLocation(location);
69
* Uses {@link #findFilesForLocationURI(URI)} and selects the most relevant file
70
* from the result. Files form the first project, from cdt-projects and those on source
71
* roots are preferred, see {@link FileRelevance}.
72
* @param location an URI for the location of the files to search for.
73
* @param preferredProject a project to be preferred over others, or <code>null</code>.
74
* @return a file for the location in one of the given projects, or <code>null</code>.
75
* NB the returned IFile may not exist
77
public static IFile selectFileForLocationURI(URI location, IProject preferredProject) {
78
return selectFile(findFilesForLocationURI(location), preferredProject);
82
* Uses {@link #findFilesForLocation(IPath)} and selects the most relevant file
83
* from the result. Files form the preferred project, from cdt-projects and those on source
84
* roots are preferred, see {@link FileRelevance}.
85
* @param location a path for the location of the files to search for.
86
* @param preferredProject a project to be preferred over others, or <code>null</code>.
87
* @return a file for the location or <code>null</code>.
88
* NB the returned IFile may not exist
90
public static IFile selectFileForLocation(IPath location, IProject preferredProject) {
91
return selectFile(findFilesForLocation(location), preferredProject);
94
private static IFile selectFile(IFile[] files, IProject preferredProject) {
95
if (files.length == 0)
98
if (files.length == 1)
102
int bestRelevance= -1;
104
for (int i = 0; i < files.length; i++) {
105
IFile file = files[i];
106
int relevance= FileRelevance.getRelevance(file, preferredProject);
107
if (best == null || relevance > bestRelevance ||
108
(relevance == bestRelevance &&
109
best.getFullPath().toString().compareTo(file.getFullPath().toString()) > 0)) {
110
bestRelevance= relevance;
118
* Sorts files by relevance for CDT, by the criteria listed below. The most relevant files
120
* <br> Accessible files
121
* <br> Files of preferred project
122
* <br> Files of CDT projects
123
* <br> Files on a source root of a CDT project
125
public static void sortFilesByRelevance(IFile[] filesToSort, final IProject preferredProject) {
126
Collections.sort(Arrays.asList(filesToSort), new Comparator<IFile>() {
127
public int compare(IFile f1, IFile f2) {
128
int r1= FileRelevance.getRelevance(f1, preferredProject);
129
int r2= FileRelevance.getRelevance(f2, preferredProject);
136
return f1.getFullPath().toString().compareTo(f2.getFullPath().toString());
144
public static void dump() {
150
public static void unrefNodeMap() {
151
lookupTree.unrefNodeMap();
156
public static void simulateNodeMapCollection() {
157
lookupTree.simulateNodeMapCollection();