Eclipse RCP 应用程序 - 以编程方式创建窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/864224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eclipse RCP application - Create a window programmatically
提问by user85259
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?
在 RCP 应用程序中,如何以编程方式定义和打开新窗口?我想打开几个窗口 - 每个窗口显示不同的数据。如何为每个窗口设置不同的输入?
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 IDE 的相同功能(窗口 --> 新窗口),但我希望每个新打开的窗口都有不同的输入。我正在尝试使用: IWorkbenchPage newPage = window.openPage(inputObject); 如何以编程方式定义标识窗口中显示的数据的“inputObject”?
回答by user85259
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.
Eclipse 术语中的工作台窗口是一个通常包含菜单、工具栏、编辑器区域和视图的窗口。Eclipse RCP 应用程序通常包含一个窗口,但有些应用程序允许创建多个窗口。例如,在 Eclipse IDE 中,可以通过从窗口菜单中选择“新建窗口”来打开另一个窗口。可以在每个窗口中独立设置透视。
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 应用程序的用户打开另一个窗口,您有两种选择。
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:
您可以在 RCP 应用程序中包含“新窗口”菜单项。这可以通过将工作台提供的操作添加到 RCP 应用程序来完成。修改您的 ActionBarAdvisor 类:
add to the field declarations:
添加到字段声明:
private IWorkbenchAction newWindowAction;
add to the code where you make the actions (typically a method called makeActions):
添加到您执行操作的代码中(通常称为 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);
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:
其中 menu 通常是 Window 菜单。如果您的应用程序中还没有 Window 菜单并想创建一个,则以下行将起作用:
MenuManager menu = new MenuManager( "&Window", IWorkbenchActionConstants.M_WINDOW);
MenuManager menu = new MenuManager( "&Window", IWorkbenchActionConstants.M_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 中的 Window->New Window 菜单项相同的方式创建一个新窗口。
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.
然而,这无法控制输入。第二个窗口可能有一组不同的视图和编辑器打开,可能有不同的透视组,但它仍然具有相同的“输入”。例如,在 Eclipse IDE 中,您可以打开第二个窗口,但如果您切换工作区,则这将适用于所有窗口。
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.
创建新窗口的第二种方法是通过创建页面以编程方式进行。这允许您为窗口设置“输入”。因此,与在另一个窗口中打开相同视图相比,在一个窗口中打开视图可能会导致显示不同的数据。
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.
从技术上讲,窗口没有输入。页面有输入。一个窗口最多可以包含一页。从一些方法名称来看,一个窗口可以有多个页面(例如 getActivePage 暗示有非活动页面)。这些方法名称是 Eclipse 2.0 支持多个页面时保留的名称。
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();
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.
请注意,Eclipse 提供的某些视图使用页面输入。例如,Common Navigator 视图将使用页面输入作为导航树的根元素。
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:
要从您自己的视图访问页面输入,您需要调用site.getPage().getInput()
。如果您没有要开始的站点上下文,则调用以下命令将为您提供输入:
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getInput();
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().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.
请注意,“输入”是一个对象。它可以是您喜欢的任何类的对象。当你从 中取回它时Page::getInput()
,将它投射回适当的类。您通常不应该创建一个新类作为输入。您几乎总是可以使用现有的类。这通常是对象模型的顶级对象。Eclipse 框架除了存储它并在Page::getInput()
被调用时将它传回之外,不会对这个输入做任何事情。
回答by Dr. Faust
You need to understand how to implement views in the Eclipse plug-in model. This can be achieved either by adding extension points and configuring properties or through code. Configuring properties is the preferred approach. Both are explained at:
您需要了解如何在 Eclipse 插件模型中实现视图。这可以通过添加扩展点和配置属性或通过代码来实现。配置属性是首选方法。两者的解释都在:
http://www.vogella.de/articles/RichClientPlatform/article.html#views
http://www.vogella.de/articles/RichClientPlatform/article.html#views
That site has lots of good articles on Eclipse development:
该站点有很多关于 Eclipse 开发的好文章:
http://www.vogella.de/eclipse.html
http://www.vogella.de/eclipse.html
Anyway, as PSU_Kardi suggested, it would be a good idea to read through the entire article.
无论如何,正如 PSU_Kardi 建议的那样,通读整篇文章是个好主意。
回答by Andrew Eidsness
You should make sure that you really want to open a bunch of other windows. Maybe you could achieve the same thing by opening some new views or editors in the existing window? Using multiple views is usually easier for the user to understand since they are no presented with several windows that look nearly the same. It also makes is easier for you to show the relationships among the views.
你应该确保你真的想打开一堆其他的窗口。也许您可以通过在现有窗口中打开一些新视图或编辑器来实现相同的目的?使用多个视图通常更容易让用户理解,因为它们没有呈现几个看起来几乎相同的窗口。它还使您更容易显示视图之间的关系。
That being said, you can call IWorkbench.openWorkbenchWindow
to create an entirely new Window. A good example of this is the code for Window --> New Window, which is in OpenNewWindowMenu.
话虽如此,您可以调用IWorkbench.openWorkbenchWindow
创建一个全新的窗口。一个很好的例子是 Window --> New Window 的代码,它在OpenNewWindowMenu 中。
回答by ist_lion
I think you'll need to better define a 'window' for me or someone else to answer this question.
我认为您需要更好地为我或其他人定义一个“窗口”来回答这个问题。
Are you creating a plug-in that you want to be open multiple times, in which case you might want an editor and need to assure you're not using the Singleton pattern - which is something you can specify in the manifest file.
您是否正在创建一个想要多次打开的插件,在这种情况下,您可能需要一个编辑器并需要确保您没有使用单例模式 - 您可以在清单文件中指定这种模式。
Or are you trying to create a window that you want to display data? Like a View? If you're doing that you'll want to read up on how to create a ViewPart and make sure you extend everything properly.
或者您是否正在尝试创建一个要显示数据的窗口?像一个视图?如果您这样做,您将需要阅读有关如何创建 ViewPart 并确保正确扩展所有内容的信息。
Might I suggest going to my two favorite RCP sites
我可以建议去我最喜欢的两个 RCP 网站吗
http://www.vogella.de/articles/RichClientPlatform/article.html
http://www.vogella.de/articles/RichClientPlatform/article.html
http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/CatalogSWT-JFace-Eclipse.htm
http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/CatalogSWT-JFace-Eclipse.htm