偷香 小说下载:从插件/RCP中取得文件路径的方法 中国Eclipse社区

来源:百度文库 编辑:中财网 时间:2024/05/11 02:29:56
从插件/RCP中取得文件路径的方法

作者:hopeshared
引用地址:http://www.blogjava.net/hopeshared/archive/2005/12/20/24798.html

最近社区里问这个问题的人特别多,所以在这里将自己用到的几个方法写出来。假如以后还有其他的方法,会进行更新。

从插件中获得绝对路径:
    AaaaPlugin.getDefault().getStateLocation().makeAbsolute().toFile().getAbsolutePath());


通过文件得到Project:

IProject project = ((IFile)o).getProject();


通过文件得到全路径:

String path = ((IFile)o).getLocation().makeAbsolute().toFile().getAbsolutePath();



得到整个Workspace的根:

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();


从根来查找资源:

IResource resource = root.findMember(new Path(containerName));

从Bundle来查找资源:

Bundle bundle = Platform.getBundle(pluginId);
URL fullPathString = BundleUtility.find(bundle, filePath);


得到Appliaction workspace:

Platform.asLocalURL(PRODUCT_BUNDLE.getEntry("")).getPath()).getAbsolutePath();

得到runtimeworkspace:
Platform.getInstanceLocation().getURL().getPath();

从编辑器来获得编辑文件:

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()

///////////////////////////////////////////////////////////////////////////////////////////////////////////
从插件中获得绝对路径:
  AaaaPlugin.getDefault().getStateLocation().makeAbsolute().toFile().getAbsolutePath());

getStateLocation() 报错NullPointerException!
///////////////////////////////////////////////////////////////////////////////////////////////////////////
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());
这个方法不错,我花了一个下午看源代码找到的。
如果你的RCP工程没有发布,得到的是eclipse的路径。如果你的RCP工程已经发布(假如在D:\RCP\下面),得到的路径就是D:/RCP/。
大家可以试试看 。
///////////////////////////////////////////////////////////////////////////////////////////////////////////

请问IEditorPart editor =((DefaultEditDomain)(parent.getViewer().getEditDomain())).getEditorPart();这一句里面的parent是什么?我直接用下面的代码取代parent.getViewer().getEditDomain()这一句可不可以?

IWorkbenchPage workbenchPage = VecasCompile.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editor = workbenchPage.getActiveEditor();