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 哼哼 閱讀(594) 評論(0)  編輯  收藏 所屬分類: Eclipse
          主站蜘蛛池模板: 临颍县| 黔西| 天峨县| 平和县| 保定市| 沧源| 永顺县| 娄烦县| 呼伦贝尔市| 黎城县| 张家港市| 浠水县| 天全县| 郸城县| 竹山县| 无棣县| 邵武市| 津市市| 宁波市| 文化| 苗栗县| 香格里拉县| 黄石市| 富锦市| 井陉县| 乃东县| 余干县| 贺兰县| 南江县| 台东县| 宜宾市| 克什克腾旗| 永清县| 剑阁县| 涿州市| 交口县| 古田县| 平乡县| 鹤壁市| 巫山县| 巴楚县|