对于Perspective的设置,网络上有很多教程。具体参考百度,谷歌。
对于新建项目,要增加系统自带的透视图Menu。代码如下:
对于新建项目,要增加系统自带的透视图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();
整个工程下载点我