[Eclipse插件开发指南]2.3 审查已生成的代码

通过新建插件项目导航提供的一个简明的简单插件去审查这些代码。

● 插件清单

● 插件类

● 常用的视图

2.3.1. 插件清单

插件清单编辑器显示了两个插件清单文件:META-INF/MANIFEST.MFplugin.xml,它们是用于定义在系统中怎样去关联所有的其他东西。在你创建插件项目后这个编辑器将在第一个页面自动打开(图2-9).如果插件清单编辑器关掉了,双击文件META-INF/MANIFEST.MF或者plugin.xml能够重新打开它。接下来将对这个清单编辑器进行一个预览,在第三章我们将发现更多插件清单的细节。

虽然这个编辑器可以很方便的去修改插件的描述,它仍然可以使我们窥看编辑器不同部分是联系到哪些底层代码。点击MANIFEST.MF这个标签去显示META-INF/MANIFEST.MF的内容,这个文件对这个插件的运行方便进行了定义(图2-7).前两行将它定义成一个OSGi清单文件(看3.3部分,插件清单)。随后的那些规定了插件的名字、版本号、标识符、类路径以及该插件所依赖的其它插件。所有的这些都可以在插件清单编辑器中进行编辑。

图2-7. 插件清单编辑器MANIFEST.MF页面

image

点击plugin.xml标签显示文件plugin.xml的内容,它用于定义差价的扩张方面的内容(图2-8)。第一行表明了他是一个XLM文件,剩下的规定了插件的扩展部分。

图2-8. 插件清单编辑器plugin.xml页面

image

在清单插件的预览(Overview)页面显示了插件清单的一些大概的东西(图2-9)。在这个页面部分描述了大体的信息,比如插件的标识符(ID)、版本、名字、类以及提供者,这些相当于META-INF/MANIFEST.MF文件中的大部分信息。

Bundle-Name: Favorites Plug-in Bundle-SymbolicName: com.qualityeclipse.favorites; singleton:=true Bundle-Version: 1.0.0 Bundle-Activator: com.qualityeclipse.favorites.FavoritesPlugin Bundle-Vendor: Quality Eclipse

图2-9.插件清单编辑器预览页面

image

你可以在预览(Overview)页面编辑这些信息,或者是切换到MANIFEST.MF页面去直接修改。

附语:

改变除了plugin.xml和MANIFEST.MF的页面也许会导致插件清单去重定源的格式。如果你对某个清单文件的格式有特殊的要求,然后可以只用plugin.xml和MANIFEST.MF页面去执行编辑或者用其它的编辑器。

警告:META-INF/MANIFEST.MF的格式规则包括一些完整的非感性的规则,它们涉及到了一句的长度以及换行。谨慎小心的去编辑plugin.xml和META-INF/MANIFEST.MF。

系统插件依赖显示在插件清单编辑器Dependencies页面(图2-10)。

图2-10.插件清单编辑器Dependencies页面

image

它相当于META-INF/MANIFEST.MF文件的Require-Bundle

Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime

对于Favorites Plug-in来说,这个部分指出了它依赖org.eclipse.core.runtimeorg.eclipse.ui这两个插件。这个依赖声明不同于Favorites Plug-in项目的JAVA构建路径(以及我们所知的编译时期的classpath),原因是JAVA构建路径是编译时期要做的事,而插件的依赖关系同时也是在插件执行的时候生效的。因为项目被创建成了插件项目,而它具有org.eclipse.pde.PluginNature这样的性质(第14.3,性质,更多的项目特性),任何依赖关系的改变将会自动的反映到Java构建路径上去。如果你的两部分的插件没有同步,你可以有一个编译可构建了的插件,但是可能不会执行。

附语:编辑这些依赖列表而不是Java构建路径(Java build path),这样就可以做到两边总是自动同步。

作为选择,依赖可以被表示成Imported Packages在清单编辑器Dependencies页面(图2-10),这个可以相当于META-INF/ MANIFEST.MF文件中的Import-Package部分:

Import-Package: org.eclipse.ui.views, org.eclipse.core.runtime.model

运行期(Runtime)页面相当于META-INF/MANIFEST.MF文件中的Bundle-ClassPath,它是用于定义了插件用到了哪些库以及插件在执行的时候用到它们,这些包的前缀被用在每个库(用于优化插件的加载时间)。对于Favorites plug-in,所有的代码已经包含在了一个单独的JAR文件中,名字叫favorites.jar,它包含的所有类用"com.qualityeclipse.favorites"前缀:

Bundle-ClassPath: favorites.jar

图2-11.插件清单编辑器运行期(Runtime)页面

image

Favorites plug-in没有为插件导入任何的包去用或者是为了扩展。

扩展(Extensions)页(图2-12)显示了怎样扩展在系统中已经存在的插件,他们相当于plugin.xml文件的<extension point="org.eclipse.ui.views">

1
 

图2-12.插件清单编辑器扩展(Extensions)页面

image

Favorites plug-in声明了扩展自org.eclipse.ui插件在org.eclipse.ui.views扩展点中提供了一个附加的视图种类,名称叫做Quality Eclipse,还有一个新的视图叫Favorites。在扩展(Extensions)页的树的左边选中一个选项,它的属性会被显示在右边。在扩展(Extensions)页选择Favorites (视图)显示了名字、标识、类以及更多的信息。它们相当于XML属性<view>定义的内容。

最后,在清单编辑器扩展点页面(图2-13)可以定义一个新的扩展点,这样其他的插件能够在这个插件基础上进行扩展。现在Favorites plug-in没有定义任何的扩展点,因此不能被其他插件扩展。

图2-13.插件清单编辑器扩展点(Extension Point)页面


image

2.3.2. 插件的类

每个插件可以随意的定义一个类,标识插件来自一个可编程的扩展点,如预览(Overview)页(图2-9)显示的那样。在Favorites plug-in中类名为com.qualityeclipse.favorites.FavoritesPlugin.

package com.qualityeclipse.favorites; import org.eclipse.ui.plugin.*; import org.eclipse.jface.resource.ImageDescriptor; import org.osgi.framework.BundleContext; /** * The main plugin class to be used in the desktop. */ public class FavoritesPlugin extends AbstractUIPlugin { // The shared instance. private static FavoritesPlugin plugin; /** * The constructor. */ public FavoritesPlugin() { plugin = this; } /** * This method is called upon plug-in activation. */ public void start(BundleContext context) throws Exception { super.start(context); } /** * This method is called when the plug-in is stopped. */ public void stop(BundleContext context) throws Exception { super.stop(context); plugin = null; } /** * Returns the shared instance. */ public static FavoritesPlugin getDefault() { return plugin; } /** * Returns an image descriptor for the image file * at the given plug-in relative path. * * @param path * the path * @return the image descriptor */ public static ImageDescriptor getImageDescriptor(String path) { return AbstractUIPlugin.imageDescriptorFromPlugin( "com.qualityeclipse.favorites", path); } }

当插件被激活的时候,Eclipse系统会将实例化插件类并在其他类之前进行加载。这个单独的类实例被Eclipse系统自始至终的运用。

典型的插件类声明成一个静态的域去引用这个单例,这样可以很容易的共享个插件。Favorites plug-in定义了一个名为plugin的域在构造器中进行了赋值,可以用getdefault方法去使用它。

附语:Eclipse系统总是实例化一个实例去集合一个插件的插件类。你可以谨慎的在构造器中增加一些代码,但是要确定你的代码不创造出任何实例出来。比如:

public FavoritesPlugin() { if (plugin != null) throw new IllegalStateException( "Plug-in class already exists"); plugin = this; }

2.3.3. Favorites视图

除了插件清单以及插件类,新建插件项目向导自动生成了一个简单视图的代码,叫做Favorites。视图用一个简单的模型创建和显示了一些信息;在接下来的章节,这个视图将会与favorites模型挂钩,用来显示favorites模型包含的一些选项的信息。

package com.qualityeclipse.favorites.views; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.part.*; import org.eclipse.jface.viewers.*; import org.eclipse.swt.graphics.Image; import org.eclipse.jface.action.*; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.ui.*; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.SWT; /** * This sample class demonstrates how to plug-in a new workbench * view. The view shows data obtained from the model. The sample * creates a dummy model on the fly, but a real implementation * would connect to the model available either in this or another * plug-in (e.g., the workspace). The view is connected to the * model using a content provider. * * The view uses a label provider to define how model objects * should be presented in the view. Each view can present the * same model objects using different labels and icons, if * needed. Alternatively, a single label provider can be shared * between views in order to ensure that objects of the same type * are presented in the same way everywhere. * */ public class FavoritesView extends ViewPart { private TableViewer viewer; /* * The content provider class is responsible for providing * objects to the view. It can wrap existing objects in * adapters or simply return objects as-is. These objects may * be sensitive to the current input of the view, or ignore it * and always show the same content (Task List, for * example). */ class ViewContentProvider implements IStructuredContentProvider { public void inputChanged( Viewer v, Object oldInput, Object newInput) { } public void dispose() { } public Object[] getElements(Object parent) { return new String[] { "One", "Two", "Three" }; } } /* * The label provider class is responsible for translating * objects into text and images that are displayed * in the various cells of the table. */ class ViewLabelProvider extends LabelProvider implements ITableLabelProvider { public String getColumnText(Object obj, int index) { return getText(obj); } public Image getColumnImage(Object obj, int index) { return getImage(obj); } public Image getImage(Object obj) { return PlatformUI.getWorkbench().getSharedImages() .getImage(ISharedImages.IMG_OBJ_ELEMENT); } } /** * The constructor. */ public FavoritesView() { } /** * This is a callback that will allow us to create the viewer * and initialize it. */ public void createPartControl(Composite parent) { viewer = new TableViewer( parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); viewer.setContentProvider(new ViewContentProvider()); viewer.setLabelProvider(new ViewLabelProvider()); viewer.setInput(getViewSite()); } private void showMessage(String message) { MessageDialog.openInformation( viewer.getControl().getShell(), "Favorites", message); } /** * Passing the focus request to the viewer's control. */ public void setFocus() { viewer.getControl().setFocus(); } }

本文系eclipselight.org(日食之光)原创文章,转载请注明出处。

固定链接:http://www.eclipselight.org/eclipse-plugin-tutorial/594/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值