Eclipse 插件开发 常用知识梳理
==================
1.得到Eclipse安装目录(eclipse.exe所在的目录)绝对路径:
Platform.getInstallLocation().getURL().getPath().substring(1);
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; }
org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot().getLocation().toString();
IProject[] pros=ResourcesPlugin.getWorkspace().getRoot().getProjects();
(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 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();
IWorkbenchPage page =PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();(1) IEditorPart part=IDE.openEditor(page, input, editorId);//通过page,input,和editorID 打开并获得EditorPart(2)part = IDE.openEditor(page, input);//打开默认编辑器
IPath path = new Path(filePath);IFileStore fileStore = EFS.getLocalFileSystem().getStore(path);part = IDE.openEditorOnFileStore(page, fileStore);
(1)page.findView(viewId)//通过ID找视图(2)page.showView(viewId)//视图显示(3)IViewReference viewRef = page.findViewReference(viewId); page.hideView(viewRef);//视图隐藏
IPerspectiveRegistry reg = PlatformUI.getWorkbench().getPerspectiveRegistry(); IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); page.setPerspective(reg.findPerspectiveWithId(perspectiveID));//perspectiveID为所要转换透视图的ID
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);