eclipse_RCP添加视图

想要在RCP里添加视图. 那就必须先建立视图:

package com.view;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.part.ViewPart;

public class TreeView extends ViewPart {

	public static final String ID = "view.TreeView"; //$NON-NLS-1$

	public TreeView() {
	}

	/**
	 * Create contents of the view part.
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new FillLayout(SWT.HORIZONTAL));
		{
			Tree tree = new Tree(container, SWT.BORDER);
			TreeItem item = new TreeItem(tree, SWT.NONE);
			item.setText("测试");
		}
		
		
		createActions();
		initializeToolBar();
		initializeMenu();
	}

	/**
	 * Create the actions.
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();
	}

	/**
	 * Initialize the menu.
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars().getMenuManager();
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

}
以上是建立视图的代码, 是用软件生成的.

之后在plugin.xml里面添加:

   
      
      
   

最后在Perspective.java文件里添加:

import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
import com.userdatamanager.view.TreeView;

public class Perspective implements IPerspectiveFactory {

	public void createInitialLayout(IPageLayout layout) {
		String editorArea = layout.getEditorArea();
		//添加树视图
		layout.addView(TreeView.ID, IPageLayout.LEFT, 0.2f, editorArea);
	}
}

添加成功的多个视图:

eclipse_RCP添加视图_第1张图片



如果在其他地方获取View并打开View:

//获取视图.此处传入的是视图ID.并不是包路径
IViewPart part = (IViewPart)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("com.test.views.TestView");
MyView view = (MyView)part;
view.printHello();

//打开视图.
try {
	PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("com.test.views.TestView");
} catch (PartInitException e) {
	e.printStackTrace();
}



你可能感兴趣的:(EclipsePlug-in)