最近重新看了一下插件开发的相关知识,补充一下以备以后需要。主要研究的是《Eclipse插件开发》那本书的第6章和第7章,自己也模仿了写了一个例子,但是不知道那里出了错,Eclipse Application就是打不开,也不报出错信息,很郁闷。趁着自己还有一点激情把一些成果先发出来,免得以后全忘了。
第7章例子的plug.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <?eclipse version="3.2"?> <plugin> <extension point="org.eclipse.ui.views"> <category name="QualityEclipse" id="com.qualityeclipse.favorites"> </category> <view name="Favorites" icon="icons/sample.gif" category="com.qualityeclipse.favorites" class="com.qualityeclipse.favorites.views.FavoritesView" id="com.qualityeclipse.favorites.views.FavoritesView"> </view> </extension> <extension point="org.eclipse.ui.commands"> <category description="Commands related to the Favorites View" id="com.qualityeclipse.favorites.commands.category" name="Favorites"> </category> <command categoryId="com.qualityeclipse.favorites.commands.category" description="Open the Favorites view if it is not already visible" id="com.qualityeclipse.favorites.commands.openView" name="Open Favorites View"> </command> <command categoryId="com.qualityeclipse.favorites.commands.category" description="Add selected items to the Favorites view" id="com.qualityeclipse.favorites.commands.add" name="Add"> </command> <command categoryId="com.qualityeclipse.favorites.commands.category" description="Copy selected items in the Favorites view" id="com.qualityeclipse.favorites.commands.copy" name="Copy"> </command> <command categoryId="com.qualityeclipse.favorites.commands.category" description="Cut selected items from the Favorites view" id="com.qualityeclipse.favorites.commands.cut" name="Cut"> </command> <command categoryId="com.qualityeclipse.favorites.commands.category" description="Paste clipboard contents into the Favorites view" id="com.qualityeclipse.favorites.commands.paste" name="Paste"> </command> <command categoryId="com.qualityeclipse.favorites.commands.category" description="Rename the selected entry in the Favorites view" id="com.qualityeclipse.favorites.commands.rename" name="Rename..."> </command> </extension> <extension point="org.eclipse.ui.menus"> <menuContribution locationURI="menu:org.eclipse.ui.main.menu?after=additions"> <menu id="com.qualityeclipse.favorites.menus.favoritesMenu" label="Favorites" mnemonic="v"> <command commandId="com.qualityeclipse.favorites.commands.openView" icon="icons/sample.gif" id="com.qualityeclipse.favorites.menus.openFavoritesView" mnemonic="O"> <visibleWhen checkEnabled="false"> <with variable="activeContexts"> <iterate ifEmpty="false" operator="or"> <equals value="com.qualityeclipse.favorites.workbenchActionSet"> </equals> </iterate> </with> </visibleWhen> </command> </menu> </menuContribution> <menuContribution locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions"> <toolbar id="com.qualityeclipse.favorites.toolbars.main"> <command commandId="com.qualityeclipse.favorites.commands.openView" icon="icons/sample.gif" id="com.qualityeclipse.favorites.toolbars.openFavoritesView" tooltip="Open the Favorites view"> <visibleWhen checkEnabled="false"> <with variable="activeContexts"> <iterate ifEmpty="false" operator="or"> <equals value="com.qualityeclipse.favorites.workbenchActionSet"> </equals> </iterate> </with> </visibleWhen> </command> </toolbar> </menuContribution> <menuContribution locationURI="popup:org.eclipse.ui.popup.any?after=additions"> <menu label="Favorites"> <command commandId="com.qualityeclipse.favorites.commands.add" icon="icons/sample.gif" style="push"> <visibleWhen checkEnabled="false"> <with variable="selection"> <iterate ifEmpty="false" operator="or"> <and> <or> <instanceof value="org.eclipse.core.resources.IResource"> </instanceof> <instanceof value="org.eclipse.jdt.core.IJavaElement"> </instanceof> </or> <test property="com.qualityeclipse.favorites.notFavorite"> </test> </and> </iterate> </with> </visibleWhen> </command> </menu> </menuContribution> <menuContribution locationURI="menu:org.eclipse.ui.views.ProblemView?after=additions"> <command commandId="com.qualityeclipse.favorites.commands.openView" icon="icons/sample.gif" tooltip="Open the Favorites view"> </command> </menuContribution> <menuContribution locationURI="toolbar:org.eclipse.ui.views.ProblemView?after=additions"> <command commandId="com.qualityeclipse.favorites.commands.openView" icon="icons/sample.gif" tooltip="Open the Favorites view"> </command> </menuContribution> <menuContribution locationURI="popup:org.eclipse.ui.views.ProblemView?after=additions"> <command commandId="com.qualityeclipse.favorites.commands.openView" icon="icons/sample.gif" tooltip="Open the Favorites view"> </command> </menuContribution> <menuContribution locationURI="menu:window?after=additions"> <command commandId="com.qualityeclipse.favorites.commands.openView" icon="icons/sample.gif" style="push"> <visibleWhen checkEnabled="false"> <with variable="activeEditorId"> <equals value="org.eclipse.ui.DefaultTextEditor"> </equals> </with> </visibleWhen> </command> </menuContribution> <menuContribution locationURI="popup:com.qualityeclipse.favorites.views.FavoritesView?before=edit"> <command commandId="com.qualityeclipse.favorites.commands.cut" icon="icons/cut_edit.gif" tooltip="Cut the currently selected items from the Favorites view"> </command> <command commandId="com.qualityeclipse.favorites.commands.copy" icon="icons/copy_edit.gif" tooltip="Copy the currently selected items in the Favorites view"> </command> <command commandId="com.qualityeclipse.favorites.commands.paste" icon="icons/paste_edit.gif" tooltip="Paste clipboard contents into the Favorites view"> </command> <command commandId="com.qualityeclipse.favorites.commands.rename" tooltip="Rename the currently selected Favorites item"> </command> </menuContribution> <menuContribution locationURI="toolbar:com.qualityeclipse.favorites.views.FavoritesView?before=edit"> <command commandId="com.qualityeclipse.favorites.commands.cut" icon="icons/cut_edit.gif" tooltip="Cut the currently selected favorites items"> </command> <command commandId="com.qualityeclipse.favorites.commands.copy" icon="icons/copy_edit.gif" tooltip="Copy the currently selected favorite items"> </command> <command commandId="com.qualityeclipse.favorites.commands.paste" helpContextId="Paste the contents of the clipboard into the Favorites view" icon="icons/paste_edit.gif"> </command> </menuContribution> </extension> <extension point="org.eclipse.ui.handlers"> <handler class="com.qualityeclipse.favorites.handlers.OpenFavoritesViewHandler" commandId="com.qualityeclipse.favorites.commands.openView"> </handler> <handler class="com.qualityeclipse.favorites.handlers.AddToFavoritesHandler" commandId="com.qualityeclipse.favorites.commands.add"> </handler> <handler class="com.qualityeclipse.favorites.handlers.CopyFavoritesHandler" commandId="org.eclipse.ui.edit.copy"> <activeWhen> <with variable="activePartId"> <equals value="com.qualityeclipse.favorites.views.FavoritesView"> </equals> </with> </activeWhen> <enabledWhen> <with variable="selection"> <count value="+"> </count> </with> </enabledWhen> </handler> <handler class="com.qualityeclipse.favorites.handlers.CopyFavoritesHandler" commandId="com.qualityeclipse.favorites.commands.copy"> <enabledWhen> <with variable="selection"> <count value="+"> </count> </with> </enabledWhen> </handler> <handler class="com.qualityeclipse.favorites.handlers.CutFavoritesHandler" commandId="org.eclipse.ui.edit.cut"> <activeWhen> <with variable="activePartId"> <equals value="com.qualityeclipse.favorites.views.FavoritesView"> </equals> </with> </activeWhen> <enabledWhen> <with variable="selection"> <count value="+"> </count> </with> </enabledWhen> </handler> <handler class="com.qualityeclipse.favorites.handlers.CutFavoritesHandler" commandId="com.qualityeclipse.favorites.commands.cut"> <enabledWhen> <with variable="selection"> <count value="+"> </count> </with> </enabledWhen> </handler> <handler class="com.qualityeclipse.favorites.handlers.PasteFavoritesHandler" commandId="org.eclipse.ui.edit.paste"> <activeWhen> <with variable="activePartId"> <equals value="com.qualityeclipse.favorites.views.FavoritesView"> </equals> </with> </activeWhen> </handler> <handler class="com.qualityeclipse.favorites.handlers.PasteFavoritesHandler" commandId="com.qualityeclipse.favorites.commands.paste"> </handler> <handler class="com.qualityeclipse.favorites.handlers.RenameFavoritesHandler" commandId="com.qualityeclipse.favorites.commands.rename"> <enabledWhen> <with variable="selection"> <count value="+"> </count> </with> </enabledWhen> </handler> </extension> <extension point="org.eclipse.ui.actionSets"> <actionSet id="com.qualityeclipse.favorites.workbenchActionSet" label="Favorites ActionSet" visible="true"> </actionSet> </extension> <extension point="org.eclipse.core.expressions.propertyTesters"> <propertyTester class="com.qualityeclipse.favorites.propertyTester.FavoritesTester" id="com.qualityeclipse.favorites.propertyTester" namespace="com.qualityeclipse.favorites" properties="isFavorite, notFavorite" type="java.lang.Object"> </propertyTester> </extension> <extension point="org.eclipse.ui.bindings"> <key commandId="com.qualityeclipse.favorites.commands.add" contextId="org.eclipse.ui.textEditorScope" schemeId="org.eclipse.ui.defaultAcceleratorConfiguration" sequence="Ctrl+Shift+A"> </key> </extension> </plugin>
这个插件目的是创建一个收藏夹,可以把感兴趣的文件加进去,显示并修改。也是一个例子而已,实用价值并不是很高。
这个插件主要有以下几个扩展点:
org.eclipse.ui.view(line5)
org.eclipse.ui.commands(line20)
org.eclipse.ui.menus(line63)
org.eclipse.ui.handlers(line233)
org.eclipse.actionSets(line334)
org.eclipse.core.expressions.propertyTesters(line342)
org.eclipse.ui.bindings(line352)
org.eclipse.ui.view主要定义了一个视图,这个视图就是收藏夹的主要部分,其他的所有标签都是围绕它所展开的。这里有两个子标签,categroy和view。category实际上就是我们打开Open Views后看到的一个个小文件夹,所有的视图都属于一个这种category。category有两个属性:一个是name,就是显示的名称;另外一个是ID,在后面定义的view属性中可以用到。view标签则是描述了一个完整的视图组件,这里有几个重要的属性:name就是实际上显示出来的名称,category就是上述定义的类别;calss这个属性实际上就是构建视图的代码所在位置,我见到的都是从ViewPart派生出来的子类,一般该类含有一个TableViewer类型或TreeViewer类型的成员变量;id属性唯一标明该视图。
org.eclipse.ui.commands定义了对这个视图的一系列的操作。可以看出这里面有一个category和六个command。category不在累述,和上面的作用其实是一样的,不过只是换了一个地方而已。六个command可以按照字面的意思来理解,分别是:打开Favorites视图、将选中的条目加入到收藏夹中、复制选中的收藏夹条目、剪切选中的收藏夹条目、粘贴收藏夹条目、重命名选中的条目。这六个command与界面无关,例如打开收藏夹这个command我们既可以在Windows->Open Views中打开收藏夹,也可以自定义一个toolbar按钮,或者自定义一个菜单项,但是它们都可以链接到这个command上。
每个command有如下的几个重要属性:description表示command的描述,具体在界面上怎么体现出来不是很清楚、categoryId就不说了,和上面的一样、id也是、name就是显示出来的样子。
org.eclipse.ui.menus这个扩展点主要是描述插件的可视界面。这里总共有9个类型为menuContribution子标签。下面以第一个标签为例,menuContribution中的属性locationURI标明了该项菜单的位置,这里的值为menu:org.eclipse.ui.main.menu?after=addition,这个值可以分为三个部分,第一部分指明了这个菜单的类型,可以是“menu”、“popup”和“toolbar”,其中的含义也可以从字面上理解出来。第二部分是菜单放在哪个插件中,可以是自定义的插件ID,也可以eclipse自带的插件ID,这里是eclipse的主菜单中(可以参见《Eclipse插件开发》6.4.8节,这节中给出了大多数eclipse自带的上下文菜单的ID)。第三部分是菜单的位置,格式为<placement>=<id>,<placement>可以是“after”或者“before”,<id>则是已经定义好的id,这里是after=addition,可以试试把它改成after=window和before=window看看有什么不同;Label标签表示显示出来的内;mnemonic表示快捷键字母。menu子标签则真实的代表了一个菜单了,commandId链接到已经定义好的command中。Menu的子标签是一个表达式,表示满足条件后就显示出来,具体怎么表示还是不太清楚。
org.eclipse.ui.handlers作用是链接command和class文件,上面command标签并没有指定动作的具体代码,这里的handlers就是command与代码之间的中间层,所以这里的commandId属性和class属性也就不难理解了。
org.eclipse.ui.bindings在这里是绑定加速键的。这里只有一个加速键在key子标签中被绑定了。commandId表示了绑定的command;contextId表示了这个加速键在什么范围内是有效的;schemeId表示了加速键的类型;一般都是org.eclipse.ui.defaultAcceleratorConfiguration;sequence表示了加速键的具体按键。