Suzan Cioc
Suzan Cioc

Reputation: 30107

How to add and position new ViewPart programmatically from another view or handler?

I know I can create new views by plugin.xml. Also I can create new views by IPerpspectiveFactory.createInitialLayout(IPageLayout) with code like

layout.addView(ChatView.ID, IPageLayout.LEFT, 1.0f, editorArea);

Is it possible to do the same from another view by command?

Also my view is not singleton, so I wish to create view copies.

Unfortunately, with code like

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

I have no ability to define view position within a layout. I woould like to pass all new views to layout folder.

UPDATE

I have modified the perspective code the following way:

public void createInitialLayout(IPageLayout layout) {

        String editorArea = layout.getEditorArea();

        layout.addView(IConsoleConstants.ID_CONSOLE_VIEW, IPageLayout.BOTTOM, 0.75f, editorArea);

        layout.addView(CoreSettingsView.ID, IPageLayout.LEFT, 0.25f, editorArea);

        //layout.addView(ChatView.ID, IPageLayout.LEFT, 1.0f, editorArea);

        IFolderLayout folder = layout.createFolder("chatfolder", IPageLayout.LEFT, 1.0f, editorArea);
        folder.addPlaceholder(ChatView.ID);
    }

Commented line previously was causing that chat view appeared in the "main" region of perspective.

Now I changed it according to advices to have placeholder in the same place.

The code of handler's execute is follows:

@Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        try {
            ChatView chatView = (ChatView) HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().showView(
                    ChatView.ID, 
                    Integer.toString(counter++), 
                    IWorkbenchPage.VIEW_ACTIVATE);

        } catch (PartInitException e) {
            e.printStackTrace();
        }
        return event;
    }

this causes new view appear, but they all appear in the same place as console view located. I.e. not that place it was appearing when positioned directly.

Why?

UPDATE 2

This is is picture about how it happens with placeholder. Red arrows show where new views actually appear if green button pressed. Blue arrow shows where views should appear and where it (one instance) actually appears if explicitly positioned in perspective:

enter image description here

UPDATE 3

The following worked:

    IFolderLayout folder = layout.createFolder("chatfolder", IPageLayout.LEFT, 1.0f, editorArea);
    folder.addPlaceholder(ChatView.ID + ":*");

Also I used 1.0f despite 0.95f is considered to be a maximum. If I use 0.95f then not full area is occupied.

Upvotes: 0

Views: 2253

Answers (2)

greg-449
greg-449

Reputation: 111142

You can open several views with the same id by using a unique secondary id for each view and using

IWorkbenchPage.showView(viewId, secondardId, IWorbenchPage.VEW_ACTIVATE);

All the views will be placed in the folder defined for the view id.

You would do something like this in the perspective factory to set this up:

IFolderLayout folder = layout.createFolder(folderId, IPageLayout.BOTTOM, 0.75f, layout.getEditorArea());

folder.addPlaceholder(viewId);

Upvotes: 1

Georgian
Georgian

Reputation: 8960

You add placeholders when you create your initial layout. After that, from other views, you just show the view you want to show. It will turn up in the placeholder for it.

See 9.2 Perspective

Upvotes: 4

Related Questions