赞
踩
eclipse插件开发
在为IDE Eclipse环境开发插件时,您有几个设计注意事项。 这些注意事项可确保您:
本教程讨论了如何利用这些设计注意事项来处理和显示与存储在工作空间中的资源相关联的数据。 我们将研究Eclipse如何提供标记接口来存储和处理有关资源的信息。
我们提供了针对资源处理数据标记的最佳实践。 首先,我们展示如何标记数据,然后建立知识以在用户界面上表示标记,然后随着资源的变化更新标记。 在这种情况下,资源是实现IResource接口的Eclipse对象,例如项目,文件,文件夹和Java™对象(包括程序包,类和源代码)。
本教程适用于可以编写基本插件但希望在处理Eclipse资源时学习最佳实践的开发人员。
Eclipse.org记录了各个扩展点和接口。 本文为选择结合使用它们的最佳实践提供了帮助。 阅读更多内容以了解如何利用现有的Eclipse函数来提供新功能。
标记用于将信息链接到资源,而无需更改资源。 标记信息的常见示例是断点,书签和编译错误。 以编译错误为例,每次执行编译作业时,该作业都会浏览源并通过创建新标记突出显示错误。
在本教程的第一部分,我们将创建自己的标记类型。 这是通过使用标记扩展点并只编写一行代码来完成的。
您可以通过扩展IMarker
扩展点org.eclipse.core.resources.markers来创建自己的标记。 首先添加清单1中的代码。 到您的plugin.xml文件。 这表示您将创建一个名为My Marker的标记,其标识为com.ibm.mymarkers.mymarker。 它的超类型是org.eclipse.core.resources.marker。 这是最基本的标记类型。 您可以扩展其他超类型,这些超类型提供的功能稍微更具体。 可用的其他超类型是:
注意:您可以根据需要使用尽可能多的这些超类型。
- <extension point="org.eclipse.core.resources.markers" id="com.ibm.mymarkers.mymarker"
- name="My Marker">
- <super type="org.eclipse.core.resources.marker"/>
- <persistent value="false"/><attribute name="description"/>
- </extension>
注意清单1中的两个元素persistent
和attribute
。 这些是赋予mymarker
标记的属性。 持久状态指示是否应将标记存储在工作空间中。 还为mymarker
提供了新的属性description
。
您可以通过在所需资源上调用createMarker
方法来创建标记。 您可能还希望设置其属性。
清单2中所示的方法也可以用于创建标记。 它唯一需要的输入是链接到标记的IResource
。 创建后,即可设置其属性。
- public static IMarker createMarker(IResource res)
- throws CoreException {
- IMarker marker = null;
- //note: you use the id that is defined in your plugin.xml
- marker = res.createMarker("com.ibm.mymarkers.mymarker");
- marker.setAttribute("description," "this is one of my markers");
- //note: you can also use attributes from your supertype
- marker.setAttribute(IMarker.MESSAGE, "My Marker");
- return marker;
- }
要查找与IResource
关联的标记,您可以查询资源以获取特定ID的所有标记,并指定是否搜索相关资源。 要搜索标记,请在资源上调用findMarkers
方法。 指定参数,例如标记类型和搜索深度。
清单3中的代码通过使用IResource.DEPTH_ZERO
参数找到与该资源直接链接的标记。
- public static final String MARKER = "com.ibm.mymarkers.mymarker";
-
- public static List<IMarker> findMarkers(IResource resource) {
- try {
- return Arrays.asList(resource.findMarkers(MARKER, true,
- IResource.DEPTH_ZERO));
- } catch (CoreException e) {
- return new ArrayList<IMarker>();
- }
- }
将资源深度更改为IResource.DEPTH_INFINITE
将返回与此资源或任何子资源相关的所有标记。 例如,您可以传入一个包并获取与该包中的资源链接的所有标记。
注释用于标记编辑器中的文本区域。 它们在Eclipse中得到广泛使用,用于显示诸如错误,警告,构建问题,任务或断点之类的信息。 注释在编辑器的标尺和文本上可见。 图1显示了显示语法错误的注释。
注释的常见实现在解析文件并挑选出诸如错误和“ todo”标签之类的内容时会生成标记。 通常在构建时完成。 其他链接到诸如断点之类的持久标记。 本示例使用一个持久标记。
可以在“ 常规” >“ 编辑器” >“ 文本编辑器” >“ 注释”下的首选项面板中自定义用户查看注释的方式。 这意味着无需其他工作即可允许用户自定义注释。
在本教程的第1部分中,标记仅扩展了默认标记类型。 在第2部分中,我们将扩展文本标记类型,以便我们可以标记文本位置。 此类型定义两个关键属性: charStart
和charEnd
。 我们还希望标记在会话之间保持不变,因此我们将持久值更改为true
。 为此,我们需要更新清单4中定义的标记。
- <extension point="org.eclipse.core.resources.markers" id="com.ibm.mymarkers.mymarker"
- name="My Marker">
- <super type="org.eclipse.core.resources.textmarker"/>
- <super type="org.eclipse.core.resources.marker"/>
- <persistent value="true"/>
- </extension>
要创建自己的注释,请使用扩展点org.eclipse.ui.editors.markerAnnotationSpecification
。 (请参见清单5。 )这定义了注释的属性及其默认显示选项。
- <extension point="org.eclipse.ui.editors.markerAnnotationSpecification"
- id="myannotationspecification" name="MyAnnotation">
- <specification annotationType="com.ibm.example.myannotation"
- label="MyAnnotation"
- icon="icons/sample.gif"
- overviewRulerPreferenceKey="clruler"
- overviewRulerPreferenceValue="true"
- colorPreferenceKey="clcolor"
- colorPreferenceValue="255,255,0"
- textPreferenceKey="cltext"
- textPreferenceValue="true"
- verticalRulerPreferenceKey="clvertical"
- verticalRulerPreferenceValue="true"
- textStylePreferenceKey="clstyle"
- textStylePreferenceValue="BOX">
- </specification>
- </extension>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
我们将用于链接到规范的XML部分是id
和annotationType
属性。 Eclipse.org文档,这些文档使用了自定义的其他属性(参见相关主题 )。
下一步是使用扩展点org.eclipse.ui.editors.annotationTypes
将现有标记链接到新注释规范。 我们使用来自规范的注释类型和来自标记定义的id链接规范。
- <extension point="org.eclipse.ui.editors.annotationTypes">
- <type markerSeverity="0"
- super="org.eclipse.ui.workbench.texteditor.info"
- name="com.ibm.example.myannotation"markerType="com.ibm.mymarkers.mymarker"/>
- </extension>
请参阅相关主题上找到从Eclipse.org此扩展点的更多信息。
清单7中的代码用于创建注释并将其添加到编辑器。
- public static void addAnnotation(IMarker marker, ITextSelection selection,
- ITextEditor editor) {
- //The DocumentProvider enables to get the document currently loaded in the editor
- IDocumentProvider idp = editor.getDocumentProvider();
-
- //This is the document we want to connect to. This is taken from
- //the current editor input.
- IDocument document = idp.getDocument(editor.getEditorInput());
-
- //The IannotationModel enables to add/remove/change annotation to a Document
- //loaded in an Editor
- IAnnotationModel iamf = idp.getAnnotationModel(editor.getEditorInput());
-
- //Note: The annotation type id specify that you want to create one of your
- //annotations
- SimpleMarkerAnnotation ma = new SimpleMarkerAnnotation(
- “com.ibm.example.myannotation”,marker);
-
- //Finally add the new annotation to the model
- iamf.connect(document);
- iamf.addAnnotation(ma,newPosition(selection.getOffset(),selection.getLength()));
- iamf.disconnect(document);
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
注释模型负责在编辑文档时移动注释。 但是,当注释移动时,它不会更新标记的属性。 在这种情况下,我们要更新标记的charStart
和charEnd
属性。 最后一个扩展点是标记更新器。 它定义了一个类,该类用于在注释移动时更新标记。
- <extension point="org.eclipse.ui.editors.markerUpdaters">
- <updater
- id="com.ibm.example.MarkerUpdater"
- class="com.ibm.example.mymarker.MarkerUpdater"
- markerType="com.ibm.mymarkers.mymarker">
- </updater>
- </extension>:
我们使用IMarkerUpdater接口来提供我们要在注释移动时执行的代码。 清单9中所示的类是我们的标记更新器。 我们感兴趣的代码是updateMarker
方法。 在这里,我们使用它来更新标记的charStart
和charEnd
属性。
- public class MarkerUpdater implements IMarkerUpdater {
- /*
- *Returns the attributes for which this updater is responsible.
- *If the result is null, the updater assumes responsibility for any attributes.
- */
- @Override
- public String[] getAttribute() {
- return null;
- }
-
- @Override
- public String getMarkerType() {
- //returns the marker type that we are interested in updating
- return "com.ibm.mymarkers.mymarker";
- }
-
- @Override
- public boolean updateMarker(IMarker marker, IDocument doc, Position position) {
- try {
- int start = position.getOffset();
- int end = position.getOffset() + position.getLength();
- marker.setAttribute(IMarker.CHAR_START, start);
- marker.setAttribute(IMarker.CHAR_END, end);
- return true;
- } catch (CoreException e) {
- return false;
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
在Eclipse中,装饰器用于向工作台中的对象添加视觉信息。 通常,它们显示对象类型和当前与该对象关联的所有键属性。 图3显示了如何在包浏览器中向用户显示装饰器。 装饰器显示哪些项目是Java源文件或软件包,并显示包含警告或错误的源和软件包的标记图标。 在这里,装饰器还用于添加团队详细信息,例如文件与存储库是同步还是不同步。
添加装饰器的第一步是扩展org.eclipse.ui.decorators
扩展点。 它启用了新装饰器的定义,并选择将装饰的对象类型。
这里的重要字段是:
ILightweightLabelDecorator
的类的完全限定名称(因为lightweight设置为true)。 - <extension point="org.eclipse.ui.decorators">
- <decorator id="com.ibm.example.filedecorator"
- label="MyMarker Decorator"
- state="true"
- class= "com.ibm.example.mymarker.FileDecorator"
- adaptable="true"
- lightweight="true">
- <enablement>
- <objectClass name="org.eclipse.core.resources.IResource"/>
- </enablement>
- </decorator>
- </extension>
请参阅参考资料,以获取有关来自Eclipse.org的扩展点的更多文档。
注意:轻量级与非轻量级:根据API,非轻量级装饰器可能会在Eclipse的未来版本中弃用。
我们需要实现FileDecorator
类来确定装饰器的行为。 此类必须实现ILightweightLabelDecorator
。 扩展LabelProvider
是一个好主意,因为这将使我们仅覆盖感兴趣的方法,即decorate()
。
清单11显示了decorate()
基本实现。
decorate()
基本实现 - public void decorate(Object resource, IDecoration decoration) {
- decoration.addOverlay(ImageDescriptor.createFromFile(FileDecorator.class,
- "/icons/sample.gif"), IDecoration.TOP_RIGHT);
- decoration.addPrefix("My Prefix ");
- decoration.addSuffix(" My Suffix");
- }
IDecoration
对象还允许自定义字体和文本/背景色。
decorate()
的第一个参数可用于过滤我们要装饰的资源。 如果只想装饰包含特定标记的资源,则使用清单12中的示例代码。
- public void decorate(Object resource, IDecoration decoration) {
- if(resource instanceof IResource){
- List<IMarker> markers = MyMarkerFactory.findMarkers((IResource) resource);
- if (markers.size() > 0) {
- decoration.addSuffix(" Marker !!");
- }
- }
- }
您已按照本教程进行操作,下一步是什么?
高级改进可以包括:
在本教程中,我们使用Eclipse轻松创建和定制标记并执行高级资源标记。 鼓励开发人员使用此简单但功能强大的工具将其插件完美地集成到Eclipse IDE中。 但是请记住,如果实施范围太广,此功能可能会给用户带来麻烦。 此外,开发人员有责任通过考虑《 Eclipse用户界面指南》来维护Eclipse的外观,请参阅参考资料 。
翻译自: https://www.ibm.com/developerworks/opensource/tutorials/os-eclipse-plugin-guide/index.html
eclipse插件开发
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。