Search code examples
javavieweclipse-pluginswteclipse-rcp

Eclipse RCP - How to create and open View programmatically?


I want to open a View which includes table when operation is executed.

I can open view by viewId by that code:

    display.asyncExec(new Runnable(){

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

    }});

This view's id defined on plugin.xml but I have to pass some parameters to the table on this view. I can create my custom view programatically but this time i can't open it becuase I don't have its id. Here is my view class:

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

    }
}

So how can I access this programmatically created view and open it?


Solution

  • In Eclipse 3.x you can open a View like this:

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

    Or if you are implementing a command handler, you can call:

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

    To set some content you can simply add a method like void setInput(MyContent input) to your ViewPart and pass the needed arguments to this method, after opening it.