问题描述
在RCP应用程序中,我如何可以编程定义和打开一个新窗口?
我想打开几个窗口 - 每个窗口显示不同的数据。如何为每个窗口设置不同的输入?
In an RCP application, how I can programtically 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 programatically 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菜单,并且想要创建一个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菜单= new MenuManager(
& Window,
IWorkbenchActionConstants.M_WINDOW);
MenuManager menu = new MenuManager( "&Window", IWorkbenchActionConstants.M_WINDOW);
这将为您提供一个菜单项,将以与Window-> New Window菜单项在Eclipse IDE中。
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.
如果您支持使用不同输入的多个窗口,则应在每个窗口中设置一个标题,区分每个窗口: / p>
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提供的某些视图使用页面输入。例如,通用浏览器视图将使用页面输入作为导航树的根元素。
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应用程序 - 以编程方式创建窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!