有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Eclipse RCP如何以编程方式创建和打开视图?

我想在执行操作时打开一个包含表的视图

我可以通过以下代码打开view by viewId视图:

    display.asyncExec(new Runnable(){

        public void run() {
        ApplicationGIS.getView(true, viewId);

    }});

此视图的id在插件上定义。但是我必须在这个视图上向表传递一些参数。我可以通过编程创建自定义视图,但这次我无法打开它,因为我没有它的id。下面是我的视图类:

public class MyCustomView extends ViewPart {

    private Text text;
    private Table table;
    private TableViewer tableViewer;


    @Override
    public void createPartControl(Composite parent) {
        // TODO Auto-generated method stub
        parent.setLayout(new GridLayout(4, false));

        Composite composite = new Composite(parent, SWT.NONE);
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1));
        composite.setLayout(new GridLayout(2, false));

        text = new Text(composite, SWT.BORDER);
        text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        Composite composite_1 = new Composite(composite, SWT.NONE);
        composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
        GridLayout gl_composite_1 = new GridLayout(1, false);
        gl_composite_1.horizontalSpacing = 0;
        gl_composite_1.marginHeight = 0;
        gl_composite_1.marginWidth = 0;
        gl_composite_1.verticalSpacing = 0;
        composite_1.setLayout(gl_composite_1);

        tableViewer = new TableViewer(composite_1, SWT.BORDER | SWT.FULL_SELECTION);

        table = tableViewer.getTable();
        table.setHeaderVisible(true);
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    }

    @Override
    public void setFocus() {
        // TODO Auto-generated method stub

    }
}

那么,我如何访问这个以编程方式创建的视图并打开它呢


共 (2) 个答案

  1. # 1 楼答案

    在Eclipse 3中。x您可以打开如下视图:

    MyView view = (MyView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(viewer_ID);
    

    或者,如果要实现命令处理程序,可以调用:

    HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().showView(viewId);
    

    要设置一些内容,只需在ViewPart中添加一个类似void setInput(MyContent input)的方法,并在打开该方法后将所需的参数传递给该方法