Eclipse RCP应用程序-以编程方式创建一个窗口 [英] Eclipse RCP application - Create a window programmatically

查看:91
本文介绍了Eclipse RCP应用程序-以编程方式创建一个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在RCP应用程序中,我如何以编程方式定义和打开新窗口?
我想打开几个窗口-每个窗口显示不同的数据。如何为每个窗口设置不同的输入?

In an RCP application, how I can programmatically define and open a new window? I want to open several window - each window show different data. How can I set different input for each window?

我想模拟Eclipse IDE的相同功能(Window-> New Window),但是我希望每个新打开的窗口窗口具有不同的输入。我正在尝试使用:
IWorkbenchPage newPage = window.openPage(inputObject);
如何以编程方式定义用于标识窗口中显示的数据的 inputObject?

I want to simulate the same functionality of Eclipse IDE (Window --> New Window), but I want each new opened window to have different input. I'm trying to use : IWorkbenchPage newPage = window.openPage(inputObject); How can I programmatically define the "inputObject" that identifies the data shown in the window?

推荐答案

工作台窗口Eclipse术语中的窗口是一个窗口,通常包含菜单,工具栏,编辑器区域和视图。 Eclipse RCP应用程序通常包含一个窗口,但是某些应用程序允许创建多个窗口。例如,在Eclipse IDE中,可以通过从窗口菜单中选择新窗口来打开另一个窗口。可以在每个窗口中独立设置透视图。

A workbench window in Eclipse terminology is a window that contains, typically, a menu, a toolbar, an editor area, and views. Eclipse RCP applications generally contain a single window but some applications allow multiple windows to be created. For example, in the Eclipse IDE one may open another window by selecting 'New Window' from the window menu. Perspectives can be set independently into each window.

尽管多个窗口可能会造成混淆,但它们也非常有用。例如,如果用户可能正在使用两个不同的数据源,但是针对每个数据源打开了多个编辑器和视图,则打开两个窗口将很有用。通过打开RCP应用程序的两个实例可以实现相同的效果。但是,这将需要加载代码和其他资源的多个副本,将需要为每个数据源完全初始化应用程序,并且这将使窗口之间的交叉通信更加困难。

Although multiple windows can be confusing, they can also be very useful. For example, if a user may be working on two different datasources but have multiple editors and views open against each datasource then it would be useful to have two windows open. The same effect could be achieved by opening two instances of the RCP application. However that would require multiple copies of code and other resources to be loaded, it would require a full initialization of the application for each datasource, and it would make cross-communications between the windows more difficult.

要允许RCP应用程序的用户打开另一个窗口,您有两种选择。

To allow users of your RCP application to open another window, you have two choices.

您可以在RCP应用程序中添加新窗口菜单项。这可以通过将工作台提供的操作添加到RCP应用程序中来完成。修改您的ActionBarAdvisor类:

You can include the 'New Window' menu item in your RCP application. This can be done by adding to your RCP application the action supplied by the workbench. Modify your ActionBarAdvisor class:

添加到字段声明中:

private IWorkbenchAction newWindowAction;

添加到执行操作的代码中(通常称为makeActions的方法):

add to the code where you make the actions (typically a method called makeActions):

newWindowAction = ActionFactory.OPEN_NEW_WINDOW.create(window);
register(newWindowAction);

添加到创建菜单的代码中:

add to the code where you create the menus:

menu.add(newWindowAction);

menu.add(newWindowAction);

其中菜单通常是窗口菜单。如果您的应用程序中还没有Window菜单,并且想创建一个Window菜单,那么以下行将起作用:

where menu is typically the Window menu. If you don't have a Window menu already in your application and would like to create one, the following line will work:

MenuManager menu = new MenuManager(
& Window,
IWorkbenchActionConstants.M_WINDOW);

MenuManager menu = new MenuManager( "&Window", IWorkbenchActionConstants.M_WINDOW);

这将为您提供一个菜单项,该菜单项的创建方式与Eclipse IDE中的Window-> New Window菜单项。

This will give you a menu item that will create a new window in the same way as the Window->New Window menu item in the Eclipse IDE.

但是,这不能控制输入。第二个窗口可能具有不同的视图集,并且打开的编辑器也可能具有不同的透视图集,但仍将具有相同的输入。例如,在Eclipse IDE中,您可以打开第二个窗口,但是如果切换工作区,则该窗口将适用于所有窗口。

However this gives no control over the input. The second window may have a different set of views and editors open, and may have a different perspective set, but it will still have the same 'input'. For example, in the Eclipse IDE you can open a second window but if you switch workspaces then that will apply to all windows.

创建新窗口的第二种方法是通过创建页面以编程方式执行此操作。这使您可以为窗口设置输入。因此,在一个窗口中打开一个视图可能会导致显示的数据与在另一个窗口中打开同一视图时的显示数据不同。

A second way to create a new window is to do so programatically by creating pages. This allows you to set an 'input' to a window. So opening a view in one window may result in different data being shown than if you opened the same view in another window.

从技术上讲,一个窗口没有输入。页面已输入。一个窗口最多可以包含一页。从某些方法名称看来,一个窗口可以具有多个页面(例如,getActivePage表示存在不活动的页面)。这些方法名称是从Eclipse 2.0开始支持多个页面的日子。

Technically, a window does not have input. Pages have input. A window can contain at most one page. In may seem from some of the method names that a window can have multiple pages (e.g. getActivePage implies there are inactive pages). Those method names are holdovers from Eclipse 2.0 days when multiple pages were supported.

以编程方式打开新页面:

To open a new page programatically:

        IWorkbenchPage newPage = window.openPage(myInput);

如果该窗口尚未包含页面,则此方法将在给定窗口中创建一个新页面,

This method will create a new page in the given window if the window does not already contain a page, otherwise a new window will be created to contain the page.

如果您支持多个具有不同输入的窗口,则应在每个窗口中设置一个区分每个窗口的标题:

If you support multiple windows with different input then you should set a title in each window that distinguishes each window:

        newPage.getWorkbenchWindow().getShell().setText("My App - " + myInput.getName());

在某些情况下,您可能希望将输入更改为窗口。您无法将输入更改为页面,因此必须通过关闭现有页面并创建新页面来完成。以下代码将关闭现有页面:

There are situations in which you may want to change the input to a window. You cannot change the input to a page, so you must do this by closing the existing page and creating a new page. The following code will close the existing page:

        IWorkbenchPage activePage = window.getActivePage();
        activePage.close();

请注意,Eclipse提供的某些视图使用页面输入。例如,Common Navigator视图将使用页面输入作为导航树的根元素。

Note that some views provided by Eclipse use the page input. For example, the Common Navigator view will use the page input as the root element of the navigation tree.

要从您自己的视图访问页面输入,您可以调用 site.getPage()。getInput()。如果没有站点上下文开始,则调用以下命令将为您提供输入:

To access the page input from your own view, you would call site.getPage().getInput(). If you have no site context to start from, calling the following will get you the input:

PlatformUI.getWorkbench()。getActiveWorkbenchWindow()。getActivePage()。getInput( );

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getInput();

请注意,输入是一个对象。它可以是您喜欢的任何类的对象。当您从 Page :: getInput()取回它时,将其转换回相应的类。通常,您不应创建新类作为输入。您几乎总是可以使用现有的类。通常,这是对象模型的顶级对象。 Eclipse框架对此输入不执行任何操作,只不过将其存储并在调用 Page :: getInput()时传递回。

Note that the 'input' is an Object. It can be an object of any class you like. When you get it back from Page::getInput(), cast it back to the appropriate class. You should not usually create a new class to be the input. You can almost always use an existing class. This is generally the top level object of your object model. The Eclipse framework does nothing with this input except to store it and to pass it back when Page::getInput() is called.

这篇关于Eclipse RCP应用程序-以编程方式创建一个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆