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
          主站蜘蛛池模板: 定襄县| 衡阳市| 新源县| 潼南县| 龙海市| 莲花县| 东乡族自治县| 九龙城区| 广州市| 阿拉善右旗| 兰州市| 锦州市| 鹤峰县| 龙州县| 靖江市| 綦江县| 宣汉县| 福泉市| 陆川县| 仁布县| 丁青县| 静宁县| 天门市| 金乡县| 玉山县| 法库县| 关岭| 大埔区| 天门市| 临洮县| 台山市| 观塘区| 济宁市| 鸡泽县| 兴国县| 仪陇县| 武清区| 汕尾市| 大足县| 南木林县| 化德县|