blob: 9c8ad357f766338053914297cc36d294cb64a364 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.core.filebuffers;
import java.io.File;
import org.eclipse.core.internal.filebuffers.FileBuffersPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
/**
* Facade for the file buffers plug-in. Provides access to the text file buffer
* manager and helper methods for location handling. This facade is available
* independent from the activation status of the file buffers plug-in.
*
* @since 3.0
*/
public final class FileBuffers {
/**
* Cannot be instantiated.
*/
private FileBuffers() {
}
/**
* Returns the text file buffer manager. May return <code>null</code> if
* the file buffers plug-in may no be activated. This is, for example, the
* case when the method is called on plug-in shutdown.
*
* @return the text file buffer manager or <code>null</code>
*/
public static ITextFileBufferManager getTextFileBufferManager() {
FileBuffersPlugin plugin= FileBuffersPlugin.getDefault();
return plugin != null ? plugin.getFileBufferManager() : null;
}
/**
* Returns the workspace file at the given location or <code>null</code> if
* the location is not a valid location in the workspace.
*
* @param location the location
* @return the workspace file at the location or <code>null</code>
*/
public static IFile getWorkspaceFileAtLocation(IPath location) {
IPath normalized= normalizeLocation(location);
if (normalized.segmentCount() >= 2) {
// @see IContainer#getFile for the required number of segments
IWorkspaceRoot workspaceRoot= ResourcesPlugin.getWorkspace().getRoot();
IFile file= workspaceRoot.getFile(normalized);
if (file != null && file.exists())
return file;
}
return null;
}
/**
* Returns a copy of the given location in a normalized form.
*
* @param location the location to be normalized
* @return normalized copy of location
*/
public static IPath normalizeLocation(IPath location) {
IWorkspaceRoot workspaceRoot= ResourcesPlugin.getWorkspace().getRoot();
IProject[] projects= workspaceRoot.getProjects();
for (int i= 0, length= projects.length; i < length; i++) {
IPath path= projects[i].getLocation();
if (path != null && path.isPrefixOf(location)) {
IPath filePath= location.removeFirstSegments(path.segmentCount());
filePath= projects[i].getFullPath().append(filePath);
return filePath.makeAbsolute();
}
}
return location.makeAbsolute();
}
/**
* Returns the file in the local file system for the given location.
* <p>
* The location is either a full path of a workspace resource or an
* absolute path in the local file system.
* </p>
*
* @param location the location
* @return the {@link File} in the local file system for the given location
*/
public static File getSystemFileAtLocation(IPath location) {
if (location == null)
return null;
IFile file= getWorkspaceFileAtLocation(location);
if (file != null) {
IPath path= file.getLocation();
return path.toFile();
}
return location.toFile();
}
}
|