本文介绍如何给Eclipse自带的tomcat插件添加一些功能。
添加扩展点 org.eclipse.ui.popupMenus [codesyntax lang="xml"]
<extension point="org.eclipse.ui.popupMenus">
<objectContribution adaptable="true"
id="org.eclipse.jst.server.tomcat.ui.serveractions" objectClass="org.eclipse.wst.server.core.IServer">
<visibility>
<objectState name="serverType" value="org.eclipse.jst.server.tomcat.*" />
</visibility>
<action class="surenpi.com.dev.debugger.tomcat.OpenDirAction"
enablesFor="1" id="surenpi.com.dev.debugger.tomcat.openserverdiraction"
label="Open Deploy Directory">
</action>
</objectContribution>
<objectContribution adaptable="true"
id="org.eclipse.jst.server.tomcat.ui.serveractions" objectClass="org.eclipse.wst.server.ui.internal.view.servers.ModuleServer">
<visibility>
<and>
<objectState name="serverType" value="org.eclipse.jst.server.tomcat.*" />
<objectState name="moduleType" value="jst.web" />
</and>
</visibility>
<action class="surenpi.com.dev.debugger.tomcat.OpenDirAction"
enablesFor="1" id="surenpi.com.dev.debugger.tomcat.openmodulediraction"
label="Open Deploy Directory">
</action>
</objectContribution>
</extension>
[/codesyntax] 我们实现的功能是通过右键菜单打开tomcat下部署的项目: [codesyntax lang="java"]
/**
* http://surenpi.com
*/
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jst.server.tomcat.core.internal.TomcatServerBehaviour;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.wst.server.core.IModule;
import org.eclipse.wst.server.core.IServer;
import org.eclipse.wst.server.ui.IServerModule;
/**
* @author surenpi.com
* @since jdk1.6
* 2015年8月19日
*/
public class OpenDirAction implements IObjectActionDelegate {
private IServer selectedServer;
private IModule selectedModule;
@Override
public void run(IAction action) {
if(selectedServer == null) {
return;
}
IPath targetPath = null;
try {
Class<?> clazz = Class.forName("org.eclipse.jst.server.tomcat.core.internal.TomcatServerBehaviour");
TomcatServerBehaviour behaviour = (TomcatServerBehaviour) selectedServer.loadAdapter(clazz, null);
if(selectedModule != null) {
targetPath = behaviour.getModuleDeployDirectory(selectedModule);
} else {
targetPath = behaviour.getRuntimeBaseDirectory();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if(targetPath != null) {
File file = targetPath.toFile();
if(Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().open(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public void selectionChanged(IAction action, ISelection selection) {
if ((!selection.isEmpty()) && ((selection instanceof IStructuredSelection))) {
Object obj = ((IStructuredSelection) selection).getFirstElement();
if ((obj instanceof IServer)) {
this.selectedServer = ((IServer) obj);
} else if ((obj instanceof IServerModule)) {
IServerModule sm = (IServerModule) obj;
IModule[] module = sm.getModule();
this.selectedModule = module[(module.length - 1)];
if (this.selectedModule != null)
this.selectedServer = sm.getServer();
}
}
}
@Override
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
}
}
[/codesyntax]
添加扩展点 org.eclipse.ui.viewActions [codesyntax lang="xml"]
<extension
point="org.eclipse.ui.viewActions">
<viewContribution id="surenpi.com.dev.explore.tomcat.view.openserverdir"
targetID="org.eclipse.wst.server.ui.ServersView">
<action id="surenpi.com.dev.explore.tomcat.view.openserverdir"
label="Open Server Directory"
menubarPath="additions"
toolbarPath="additions"
icon="icons/explore.png"
tooltip="Open Server Directory"
class="surenpi.com.dev.explore.tomcat.OpenTomcatDir"
enablesFor="1">
<selection class="org.eclipse.wst.server.core.IServer" />
</action>
</viewContribution>
</extension>
[/codesyntax] 实现类与上面的OpenDirAction相同,只是要实现接口
org.
eclipse.
ui.IViewActionDelegate
如果你想知道怎么才能把菜单或者工具栏配置到你希望出现的地方,可以看这篇文章。