Perspective切换

本文介绍如何在Eclipse中通过自定义Action实现透视图的切换,并提供了具体的代码示例,包括如何关闭当前透视图及打开新的透视图。
对于Perspective的设置,网络上有很多教程。具体参考百度,谷歌。
对于新建项目,要增加系统自带的透视图Menu。代码如下:

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
	/** 自定义切换视图 */
	private SwitchPerspectiveAction changeProspect;
	/** 系统自带的切换视图菜单 */
	private IContributionItem perspectivesMenu;
	/** 系统自带打开透视图Action */
	private IWorkbenchAction perspectiveAction;

	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}

	@Override
	protected void makeActions(IWorkbenchWindow window) {
		/** 实例 化并注册 */
		changeProspect = new SwitchPerspectiveAction(window,
				"testRcp.myperspective");
		register(changeProspect);
		/** PERSPECTIVES_SHORTLIST无须注册 */
		perspectivesMenu = ContributionItemFactory.PERSPECTIVES_SHORTLIST
				.create(window);

		perspectiveAction = ActionFactory.OPEN_PERSPECTIVE_DIALOG
				.create(window);
		register(perspectiveAction);
	}

	@Override
	protected void fillMenuBar(IMenuManager menuBar) {
		MenuManager userChange = new MenuManager("自定义切换视图(&N)");
		userChange.add(changeProspect);
		menuBar.add(userChange);

		MenuManager syslayoutChange = new MenuManager("系统切换视图(&T)");
		syslayoutChange.add(perspectivesMenu);
		syslayoutChange.add(perspectiveAction);
		menuBar.add(syslayoutChange);

	}

}

切换视图,利用自定义的Action进行透视图切换时,要注意,切换时,要将 当前的透视图关闭。否则无法切换。
得到图片:
setImageDescriptor(Activator.getDefault().getImageDescriptor("icons/new_wiz.gif"));
得到透视图:
  org.eclipse.ui.IPerspectiveDescriptor[] des=PlatformUI.getWorkbench().getPerspectiveRegistry().getPerspectives();
  PlatformUI.getWorkbench().getPerspectiveRegistry().revertPerspective(des[0]);
/** 获得当前激活的透视图的id */
String m_id=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.getPerspective().getId();

package com.action;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.actions.ActionFactory;

/**
 * @author: Administrator
 * @Description: 描述该类的作用
 */
public class SwitchPerspectiveAction extends Action {
	private final IWorkbenchWindow window;
	private  String id;
	private IPerspectiveDescriptor desc;

	public SwitchPerspectiveAction(IWorkbenchWindow window, String id) {

		this.window = window;
		this.id = id;
		desc = PlatformUI.getWorkbench().getPerspectiveRegistry()
				.findPerspectiveWithId(id);
		if (desc != null) {
			setText(desc.getLabel());
			setImageDescriptor(desc.getImageDescriptor());
		}
		setId("com.action.switchPerspectiveAction");
	}

	public void run() {
		/** 获得当前激活的透视图的id */
		String m_id=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
				.getPerspective().getId();
		/** 关闭当前透视图 */
		IWorkbench w = PlatformUI.getWorkbench();
		ActionFactory.IWorkbenchAction closePerspectiveAction = ActionFactory.CLOSE_PERSPECTIVE
				.create(w.getActiveWorkbenchWindow());
		closePerspectiveAction.run();

		
		if(m_id.equals("testRcp.perspective"))
		{
			id="testRcp.myperspective";
		}
		
		try {
			/** 打开新的透视图 */
			PlatformUI.getWorkbench().showPerspective(id, window);
		} catch (WorkbenchException e) {
			MessageDialog.openError(window.getShell(), "Error",
					"Error opening perspective:" + e.getMessage());
		}
	}
}

/** 关闭当前透视图 */
		IWorkbench w = PlatformUI.getWorkbench();
		ActionFactory.IWorkbenchAction closePerspectiveAction = ActionFactory.CLOSE_PERSPECTIVE
				.create(w.getActiveWorkbenchWindow());
		closePerspectiveAction.run();

整个工程下载点我
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值