简体   繁体   English

如何以编程方式从另一个视图或处理程序添加和放置新的ViewPart?

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

I know I can create new views by plugin.xml . 我知道我可以通过plugin.xml创建新视图。 Also I can create new views by IPerpspectiveFactory.createInitialLayout(IPageLayout) with code like 我也可以通过IPerpspectiveFactory.createInitialLayout(IPageLayout)使用如下代码创建新视图

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 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. 这会导致出现新视图,但是它们都与控制台视图位于同一位置。 Ie not that place it was appearing when positioned directly. 即不是直接放置时出现的那个地方。

Why? 为什么?

UPDATE 2 更新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: 蓝色箭头显示了在透视图中明确放置的视图应该出现的位置以及视图(一个实例)的实际出现位置:

在此处输入图片说明

UPDATE 3 更新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. 我也使用1.0f,尽管0.95f被认为是最大值。 If I use 0.95f then not full area is occupied. 如果我使用0.95f,则不会占满整个区域。

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 见9.2透视图

You can open several views with the same id by using a unique secondary id for each view and using 您可以通过为每个视图使用唯一的secondary id并使用来打开多个具有相同ID的视图

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

All the views will be placed in the folder defined for the view id. 所有视图都将放置在为视图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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM