RCP程序中读取其它plugin中的文件

By | 2016年1月12日

RCP程序中读取其它plugin中的文件:


  /**
   * Get the resource located in a plugin.
   * 
   * @param pluginId
   *          the plugin id or plugin name. (de.vogella.example)
   * @param relativePath
   *          file relative path to the plugin.
   * @return the absolute path of the resource or null if the resource is not
   *         found.
   */
  public String getPluginResource(String pluginId, String relativePath) {
    Bundle bundle = Platform.getBundle(pluginId);
    if (bundle == null) {
      return null;
    }
    try {
      return FileLocator.resolve(bundle.getEntry(relativePath)).getPath();
    } catch (IOException e) {
      return null;
    }
  }

  /**
   * Get plugin image.
   * @param pluginId the plugin name.
   * @param relative path to the plugin.
   * 
   * @return a Image that the path indicates.
   */
  public Image getPluginImage(String pluginId, String relativePath) {
    URL url;
    try {
      url = new URL("platform:/plugin/" + pluginId + "/" + relativePath);
      return new Image(Display.getDefault(), url.openConnection().getInputStream());
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }