~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to results/plugins/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/resources/ResourceLookup.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
7
 
 *
8
 
 * Contributors:
9
 
 *    Markus Schorn - initial API and implementation
10
 
 *******************************************************************************/ 
11
 
package org.eclipse.cdt.internal.core.resources;
12
 
 
13
 
import java.net.URI;
14
 
import java.util.Arrays;
15
 
import java.util.Collections;
16
 
import java.util.Comparator;
17
 
 
18
 
import org.eclipse.core.resources.IFile;
19
 
import org.eclipse.core.resources.IProject;
20
 
import org.eclipse.core.runtime.IPath;
21
 
 
22
 
/**
23
 
 * Allows for looking up resources by location or name.
24
 
 */
25
 
public class ResourceLookup {
26
 
        private static ResourceLookupTree lookupTree= new ResourceLookupTree();
27
 
 
28
 
        public static void startup() {
29
 
                lookupTree.startup();
30
 
        }
31
 
        
32
 
        public static void shutdown() {
33
 
                lookupTree.shutdown();
34
 
        }
35
 
        
36
 
        /**
37
 
         * Searches for files with the given location suffix. 
38
 
         * 
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.
42
 
         * 
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.
46
 
         */
47
 
        public static IFile[] findFilesByName(IPath locationSuffix, IProject[] projects, boolean ignoreCase) {
48
 
                return lookupTree.findFilesByName(locationSuffix, projects, ignoreCase);
49
 
        }
50
 
        
51
 
        /**
52
 
         * Uses a lookup-tree that finds resources for locations using the canonical representation
53
 
         * of the path. 
54
 
         */
55
 
        public static IFile[] findFilesForLocationURI(URI location) {
56
 
                return lookupTree.findFilesForLocationURI(location);
57
 
        }
58
 
 
59
 
        /**
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.
63
 
         */
64
 
        public static IFile[] findFilesForLocation(IPath location) {
65
 
                return lookupTree.findFilesForLocation(location);
66
 
        }
67
 
 
68
 
        /**
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
76
 
         */
77
 
        public static IFile selectFileForLocationURI(URI location, IProject preferredProject) {
78
 
                return selectFile(findFilesForLocationURI(location), preferredProject);
79
 
        }
80
 
 
81
 
        /**
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
89
 
         */
90
 
        public static IFile selectFileForLocation(IPath location, IProject preferredProject) {
91
 
                return selectFile(findFilesForLocation(location), preferredProject);
92
 
        }
93
 
 
94
 
        private static IFile selectFile(IFile[] files, IProject preferredProject) {
95
 
                if (files.length == 0)
96
 
                        return null;
97
 
 
98
 
                if (files.length == 1)
99
 
                        return files[0];
100
 
 
101
 
                IFile best= null;
102
 
                int bestRelevance= -1;
103
 
 
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;
111
 
                                best= file;
112
 
                        }
113
 
                }
114
 
                return best;
115
 
        }
116
 
 
117
 
        /**
118
 
         * Sorts files by relevance for CDT, by the criteria listed below. The most relevant files
119
 
         * is listed first.
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
124
 
         */
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);
130
 
                                
131
 
                                if (r1 > r2)
132
 
                                        return -1;
133
 
                                if (r1 < r2)
134
 
                                        return 1;
135
 
                                
136
 
                                return f1.getFullPath().toString().compareTo(f2.getFullPath().toString());
137
 
                        }
138
 
                });
139
 
        }
140
 
 
141
 
        /** 
142
 
         * For testing, only.
143
 
         */
144
 
        public static void dump() {
145
 
                lookupTree.dump();
146
 
        }
147
 
        /** 
148
 
         * For testing, only.
149
 
         */
150
 
        public static void unrefNodeMap() {
151
 
                lookupTree.unrefNodeMap();
152
 
        }
153
 
        /** 
154
 
         * For testing, only.
155
 
         */
156
 
        public static void simulateNodeMapCollection() {
157
 
                lookupTree.simulateNodeMapCollection();
158
 
        }
159
 
}