MyException - 我的异常网
当前位置:我的异常网» Eclipse » Eclipse 插件开发 惯用知识梳理

Eclipse 插件开发 惯用知识梳理

www.MyException.Cn  网友分享于:2013-02-20  浏览:12次
Eclipse 插件开发 常用知识梳理
==================
1.得到Eclipse安装目录(eclipse.exe所在的目录)绝对路径:
Platform.getInstallLocation().getURL().getPath().substring(1);

2.得到项目中资源路径(常用的比如图片等)
e.g.:getPluginResourcePath(“org.xxxx.xxx.xx.pluginId”,"icons/xxx.jpg");//就可以得到图片的绝对路径了

public static String getPluginResourcePath(String pluginId, String relativePathName) {
  try {
   Bundle bundle = Platform.getBundle(pluginId);
   URL url = bundle.getResource(relativePathName);
   if (url != null) {
    return FileLocator.toFileURL(url).getPath().substring(1);
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  return null;
 }

3.得到Eclipse的工作空间
(1)得到工作空间路径
org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot().getLocation().toString();

(2) 得到工作空间中的项目
IProject[] pros=ResourcesPlugin.getWorkspace().getRoot().getProjects();

4.缓存资源相关,包括字体、颜色、图像等
org.eclipse.jface.resource.JFaceResources
(1)FontRegistry fontRegistry=JFaceResources.getFontRegistry();//注册字体,用于缓存
 fontRegistry.put(key, new FontData[] { new FontData(fontName, height, style) });//填充数据
(2)ColorRegistry colorRegistry = JFaceResources.getColorRegistry();//注册颜色
    colorRegistry .put(key, new RGB(red, green, blue));
(3)ImageRegistry imageRegistry = JFaceResources.getImageRegistry();
  //Image image = new Image(Display.getDefault(), fullPathString);
  // ImageDescriptor descriptor = ImageDescriptor.createFromImage(image);

    ImageDescriptor descriptor = AbstractUIPlugin.imageDescriptorFromPlugin(pluginId, imageFilePath);
    imageRegistry .put(key, descriptor);
-----

Image的渐变效果:color1/color2是提供的原始颜色
Image image = new Image(null, width, height);
   GC gc = new GC(image);
   gc.setForeground(color1);
   gc.setBackground(color2);
   gc.fillGradientRectangle(0, 0, width, height, true);//设置有颜色梯度的效果
   gc.dispose();

---------------------------------
5.Eclipse编辑器Editor相关:
IWorkbenchPage page =PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

(1) IEditorPart part=IDE.openEditor(page, input, editorId);//通过page,input,和editorID 打开并获得EditorPart

(2)part = IDE.openEditor(page, input);//打开默认编辑器

(3)通过文件打开对应的编辑器
IPath path = new Path(filePath);
IFileStore fileStore = EFS.getLocalFileSystem().getStore(path);
part = IDE.openEditorOnFileStore(page, fileStore);

6.Eclipse视图相关的

(1)page.findView(viewId)//通过ID找视图

(2)page.showView(viewId)//视图显示

(3)IViewReference viewRef = page.findViewReference(viewId);

    page.hideView(viewRef);//视图隐藏

7.Eclipse中透视图转换
  IPerspectiveRegistry reg = PlatformUI.getWorkbench().getPerspectiveRegistry();
  IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
  page.setPerspective(reg.findPerspectiveWithId(perspectiveID));//perspectiveID为所要转换透视图的ID


8. 通过反射给菜单区域添加背景色
public void setMenuBG(Color bgColor) {
  Menu menu = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getMenuBar();

 //反射的好处,只需要知道方法名及参数类型就可以调用

  invoke("setBackground", menu, new Class[] { Color.class }, new Color[] { bgColor });

 }

 Object invoke(String methodName, Object object, Class<?>[] argsTypes, Object[] args) {
  Object result = null;
  try {
   Method m = object.getClass().getDeclaredMethod(methodName, argsTypes);
   m.setAccessible(true);
   result = m.invoke(object, args);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return result;
 }
9.设置标题工具栏可见(可以动态设定)

WorkbenchWindow activeWorkbenchWindow = (WorkbenchWindow) PlatformUI.getWorkbench().getActiveWorkbenchWindow();
 activeWorkbenchWindow.setCoolBarVisible(true); 


10.

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有