RCP创建视图
1、创建plugin.xml项目,这里默认
2、勾选yes
3、选择第二个
4、创建视图类
package myviewrcp2; import org.eclipse.jface.action.Action; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ViewerSorter; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.ISharedImages; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.part.ViewPart; /** * @Title: * @Description:TODO * @Company:HF * @ClassName:SampleView.java * @Author:wushigao * @CreateDate:2021年6月8日上午9:41:06 * @UpdateUser:wushigao * @UpdateDate:2021年6月8日 上午9:41:06 * @Version:1.0 */ public class SampleView extends ViewPart { //该视图的id,在plugin.xml文件中定义,通常使用字符串常量的方式 public static final String ID = "myviewrcp2.SampleView"; private TableViewer viewer;//视图中显示的表格对象 //操作对象 //操作对象 private Action action1; private Action action2; //构造方法 public SampleView() { } //为父类中的抽象方法,创建视图中的各种控件 public void createPartControl(Composite parent) { //创建一个表格对象 viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); viewer.setContentProvider(new ViewContentProvider()); viewer.setLabelProvider(new ViewLabelProvider()); viewer.setSorter(new NameSorter()); viewer.setInput(getViewSite()); makeActions();//创建操作对象 } private void makeActions() { action1 = new Action() { public void run() { showMessage("Action 1 executed"); } }; action1.setText("Action 1"); action1.setToolTipText("Action 1 tooltip"); action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK)); action2 = new Action() { public void run() { showMessage("Action 2 executed"); } }; action2.setText("Action 2"); action2.setToolTipText("Action 2 tooltip"); action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK)); } private void showMessage(String message) { MessageDialog.openInformation( viewer.getControl().getShell(), "样本视图", message); } //父类中的抽象方法,该视图获得焦点时,将焦点设置为表格 public void setFocus() { viewer.getControl().setFocus(); } class ViewContentProvider implements IStructuredContentProvider { public void inputChanged(Viewer v, Object oldInput, Object newInput) { } public void dispose() { } public Object[] getElements(Object parent) { return new String[] { "One1", "Two2", "Three3" }; } } class ViewLabelProvider extends LabelProvider implements ITableLabelProvider { public String getColumnText(Object obj, int index) { return getText(obj); } public Image getColumnImage(Object obj, int index) { return getImage(obj); } public Image getImage(Object obj) { return PlatformUI.getWorkbench(). getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT); } } class NameSorter extends ViewerSorter { } }
5、这里一个很关键的是要把透视图的扩展点也加上,否则没有效果
<?xml version="1.0" encoding="UTF-8"?> <?eclipse version="3.4"?> <plugin> <extension id="application" point="org.eclipse.core.runtime.applications"> <application> <run class="myviewrcp2.Application"> </run> </application> </extension> <extension point="org.eclipse.ui.perspectives"> <perspective name="RCP Perspective" class="myviewrcp2.Perspective" id="MyViewRCP2.perspective"> </perspective> </extension> <extension point="org.eclipse.ui.views"> <view name="Sample View" class="myviewrcp2.SampleView" id="myviewrcp2.SampleView"> </view> </extension> </plugin>
6、在透视图类中加上addView
package myviewrcp2; import org.eclipse.ui.IPageLayout; import org.eclipse.ui.IPerspectiveFactory; public class Perspective implements IPerspectiveFactory { public void createInitialLayout(IPageLayout layout) { String editorArea = layout.getEditorArea(); //添加树视图 layout.addView(SampleView.ID, IPageLayout.LEFT, 0.2f, editorArea); } }