从插件/RCP中取得文件路径
1)从插件中获得绝对路径:
AaaaPlugin.getDefault().getStateLocation().makeAbsolute().toFile().getAbsolutePath());
通过文件得到Project:
IProject project = ((IFile)o).getProject();
2)通过文件得到全路径:
①String path = ((IFile)o).getLocation().makeAbsolute().toFile().getAbsolutePath();
②IFolder srcFolder =
ResourcesPlugin.getPlugin().getWorkspace().getRoot().getProject(
"yourprojectname").getFolder("src");
IFile ifile = srcFolder.getFile("hibernate.cfg.xml");
String path= ifile.getLocation().makeAbsolute().toFile().getAbsolutePath();
其中path 得出的是真正的绝对路径.
3)得到整个Workspace的根:
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
4)从根来查找资源:
IResource resource = root.findMember(new Path(containerName));
5)从Bundle来查找资源:
Bundle bundle = Platform.getBundle(pluginId);
URL fullPathString = BundleUtility.find(bundle, filePath);
6)得到Appliaction workspace:
Platform.asLocalURL(
PRODUCT_BUNDLE.getEntry("")).getPath()).getAbsolutePath();
7)得到runtimeworkspace:
Platform.getInstanceLocation().getURL().getPath();
8)从编辑器来获得编辑文件:
IEditorPart editor = ((DefaultEditDomain)(parent.getViewer().getEditDomain())).getEditorPart();
IEditorInput input = editor.getEditorInput();
if(input instanceof IFileEditorInput){
IFile file = ((IFileEditorInput)input).getFile();
}
得到项目的绝对路径:
FileLocator.toFileURL(
Platform.getBundle(Application.PLUGIN_ID).getEntry("")).getPath()
10)如果你的RCP工程没有发布,得到的是eclipse的路径。如果你的RCP工程已经发布(假如在D:\RCP\下面),得到的路径就是D:/RCP/。
Location installLoc = LocationManager.getInstallLocation();
String path = null;
String installPath = null;
if (installLoc != null)
{
URL installURL = installLoc.getURL();
// assume install URL is file: based
path = installURL.getPath();
}
installPath = path.substring(1, path.length());
11)显示相对路径的image的方法。如下:
①ImageDescriptor imageDes =
AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, "/images/schemabase.jpg");
Image image = imageDes.createImage();
②Put a class in image directory.
For instance:
../../resource/icon1.bmp
../../resource/Resource.class
InputStream istream = Resources.class.getResourceAsStream("icon1.bmp");
Image icon_image = new Image(Display.getCurrent(), istream);
12)获取plugin根目录路径的方法
public static String getRoot(String pluginID){
String path=null;
try {
path = FileLocator.toFileURL(
Platform.getBundle(pluginID).getEntry("")).getPath();
path = path.substring(path.indexOf("/") + 1, path.length());
} catch (Exception e) {
e.printStackTrace();
}
return path;
}
13)eclipse 3.1.1以后用:
URL bundleRootURL = YourPlugin.getDefault().getBundle()
.getEntry("/");
try {
URL pluginURL = Platform.resolve(bundleRootURL);
return pluginURL.getPath();
} catch (IOException e) {
}
其中URL pluginURL = Platform.resolve(bundleRootURL);
eclipse 3.2M6建议使用FileLocator#resolve(URL)替代 Platform.resolve