hengheng123456789

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  297 Posts :: 68 Stories :: 144 Comments :: 0 Trackbacks

今天在开发Eclipse插件时用到了Eclipse 文件资源,不知道在调试插件过程中如何调用自定义的图片,于是在网上查了许久,有如下说明:

对路径的测试代码:
    System.out.println( Thread.currentThread().getContextClassLoader().getResource(""));
    System.out.println(PathTest.
class.getClassLoader().getResource(""));
    System.out.println(ClassLoader.getSystemResource(
""));
    System.out.println(PathTest.
class.getResource(""));
    System.out.println(PathTest.
class.getResource("/"));
    System.out.println(
new File("").getAbsolutePath());
    System.out.println(System.getProperty(
"user.dir"));
结果如下:
   file:/E:/wxxr_projects2/wxxr-applications/wxxr-boss-connector/target/test-classes/
   file:
/E:/wxxr_projects2/wxxr-applications/wxxr-boss-connector/target/test-classes/
   file:
/E:/wxxr_projects2/wxxr-applications/wxxr-boss-connector/target/test-classes/
   file:
/E:/wxxr_projects2/wxxr-applications/wxxr-boss-connector/target/test-classes/com/wxxr/boss/config/
   file:
/E:/wxxr_projects2/wxxr-applications/wxxr-boss-connector/target/test-classes/
   E:\wxxr_projects2\wxxr
-applications\wxxr-boss-connector
   E:\wxxr_projects2\wxxr
-applications\wxxr-boss-connector


一般情况是使用ClassLoader.getSystemResource("");
但是在调试插件时其返回结果为NULL,郁闷不已,于是又看到如下文章:

从插件中获得绝对路径:
       

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

最后暂时使用如下方法获得成功:
URL url = Thread.currentThread().getContextClassLoader().getResource(path + name);
ImageIcon icon = new ImageIcon( url );
OK暂时解决,但发布后不知如何实现,再研究吧:)
posted on 2006-09-28 15:55 哼哼 阅读(566) 评论(0)  编辑  收藏 所属分类: Eclipse

只有注册用户登录后才能发表评论。


网站导航: