Eclipse插件开发tomcat扩展

  • 介绍
本文介绍如何给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
  • 参考
如果你想知道怎么才能把菜单或者工具栏配置到你希望出现的地方,可以看这篇文章。

转载于:https://my.oschina.net/surenpi/blog/604795

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值