在Eclipse中的众多特性中有一条可以自动更新的功能,
我们可能在实际使用中也需要Eclipse那样的更新功能,而Eclipse提供了更新和管理插件的配置对话框,在这里我们可用发扬发扬"拿来主义",直接拿过来用,过程很简单,步骤如下
新建一个插件项目,然后打开 plugin.xml 文件,切换到 Extensions 选项卡,增加"org.eclipse.ui.actionSets" 扩展点,然后新建如下节点
下面是整个plugin.xml 文件内容
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
< extension
id="application"
point ="org.eclipse.core.runtime.applications">
<application>
< run
class="updateandinstall.Application">
</run>
</application>
</extension>
< extension
point="org.eclipse.ui.perspectives">
< perspective
name="Perspective"
class ="updateandinstall.Perspective"
id ="UpdateAndInstall.perspective">
</perspective>
</extension>
< extension
point="org.eclipse.ui.views">
< view
name="View"
class ="updateandinstall.View"
id ="UpdateAndInstall.view">
</view>
</extension>
< extension
point="org.eclipse.ui.actionSets">
< actionSet
id="SMS.actionSet1"
label ="软件更新">
< menu
id="SMS.menu3"
label ="软件更新">
< separator
name="group0">
</separator>
< separator
name="group1">
</separator>
</menu>
< action
class="updateandinstall.actions.InstallWizardAction"
disabledIcon ="icons/update/usearch_obj.gif"
icon ="icons/update/usearch_obj.gif"
id ="SMS.action3"
label ="查找并安装"
style ="push">
</action>
< action
class="updateandinstall.actions.ConfigurationManagerAction"
disabledIcon ="icons/update/configs.gif"
icon ="icons/update/configs.gif"
id ="SMS.action4"
label ="配置管理"
style ="push">
</action>
</actionSet>
</extension>
</plugin>
<?eclipse version="3.2"?>
<plugin>
< extension
id="application"
point ="org.eclipse.core.runtime.applications">
<application>
< run
class="updateandinstall.Application">
</run>
</application>
</extension>
< extension
point="org.eclipse.ui.perspectives">
< perspective
name="Perspective"
class ="updateandinstall.Perspective"
id ="UpdateAndInstall.perspective">
</perspective>
</extension>
< extension
point="org.eclipse.ui.views">
< view
name="View"
class ="updateandinstall.View"
id ="UpdateAndInstall.view">
</view>
</extension>
< extension
point="org.eclipse.ui.actionSets">
< actionSet
id="SMS.actionSet1"
label ="软件更新">
< menu
id="SMS.menu3"
label ="软件更新">
< separator
name="group0">
</separator>
< separator
name="group1">
</separator>
</menu>
< action
class="updateandinstall.actions.InstallWizardAction"
disabledIcon ="icons/update/usearch_obj.gif"
icon ="icons/update/usearch_obj.gif"
id ="SMS.action3"
label ="查找并安装"
style ="push">
</action>
< action
class="updateandinstall.actions.ConfigurationManagerAction"
disabledIcon ="icons/update/configs.gif"
icon ="icons/update/configs.gif"
id ="SMS.action4"
label ="配置管理"
style ="push">
</action>
</actionSet>
</extension>
</plugin>
我们要使这个扩展菜单项生效,必须要新建对应的Action类来负责执行操作,先来看"查找并安装"对应的Action类 InstallWizardAction .java
package
updateandinstall.actions;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.update.ui.UpdateManagerUI;
public class InstallWizardAction extends Action implements IWorkbenchWindowActionDelegate{
private IWorkbenchWindow window;
public InstallWizardAction(){
}
public void dispose() {
// TODO 自动生成方法存根
}
public void init(IWorkbenchWindow window) {
this.window= window;
}
public void run(IAction action) {
openInstaller(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
}
public void selectionChanged(IAction action, ISelection selection) {
// TODO 自动生成方法存根
}
private void openInstaller(final IWorkbenchWindow window){
BusyIndicator.showWhile(window.getShell().getDisplay(),new Runnable(){
public void run(){
UpdateManagerUI.openInstaller(window.getShell());
}
});
}
}
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.update.ui.UpdateManagerUI;
public class InstallWizardAction extends Action implements IWorkbenchWindowActionDelegate{
private IWorkbenchWindow window;
public InstallWizardAction(){
}
public void dispose() {
// TODO 自动生成方法存根
}
public void init(IWorkbenchWindow window) {
this.window= window;
}
public void run(IAction action) {
openInstaller(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
}
public void selectionChanged(IAction action, ISelection selection) {
// TODO 自动生成方法存根
}
private void openInstaller(final IWorkbenchWindow window){
BusyIndicator.showWhile(window.getShell().getDisplay(),new Runnable(){
public void run(){
UpdateManagerUI.openInstaller(window.getShell());
}
});
}
}
我们通过这个类直接调用更新管理器中提供的API就行了
"配置管理" 对应的Action类 ConfigurationManagerAction .java
package
updateandinstall.actions;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.update.ui.UpdateManagerUI;
public class ConfigurationManagerAction implements IWorkbenchWindowActionDelegate{
private IWorkbenchWindow window;
public void dispose() {
// TODO 自动生成方法存根
}
public void init(IWorkbenchWindow window) {
this.window= window;
}
public void run(IAction action) {
BusyIndicator.showWhile(window.getShell().getDisplay(),new Runnable(){
public void run(){
UpdateManagerUI.openConfigurationManager(window.getShell());
}
});
}
public void selectionChanged(IAction action, ISelection selection) {
// TODO 自动生成方法存根
}
}
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.update.ui.UpdateManagerUI;
public class ConfigurationManagerAction implements IWorkbenchWindowActionDelegate{
private IWorkbenchWindow window;
public void dispose() {
// TODO 自动生成方法存根
}
public void init(IWorkbenchWindow window) {
this.window= window;
}
public void run(IAction action) {
BusyIndicator.showWhile(window.getShell().getDisplay(),new Runnable(){
public void run(){
UpdateManagerUI.openConfigurationManager(window.getShell());
}
});
}
public void selectionChanged(IAction action, ISelection selection) {
// TODO 自动生成方法存根
}
}
这样的话我们就可以使用Eclipse中更新管理器中的更新功能了,在程序打包的时候好记得把关于更新的插件给打包进去就行了