Eclipse RCP应用程序 - 以编程方式创建窗口

时间:2022-06-16 10:50:41

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?

在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 programatically define the "inputObject" that identifies the data shown in the window?

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

Thanks,

4 个解决方案

#1


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);

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:

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

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

MenuManager菜单=新的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();

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.

请注意,'input'是一个Object。它可以是你喜欢的任何课程的对象。当您从Page :: getInput()获取它时,将其强制转换回相应的类。您通常不应该创建一个新类作为输入。您几乎总能使用现有的类。这通常是对象模型的*对象。除了存储它之外,Eclipse框架对此输入不执行任何操作,并在调用Page :: getInput()时将其传回。

#2


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

That site has lots of good articles on Eclipse development:

该站点有很多关于Eclipse开发的好文章:

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所建议的,阅读整篇文章是个好主意。

#3


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.

您是否正在创建一个想要多次打开的插件,在这种情况下,您可能需要一个编辑器并且需要确保您不使用Singleton模式 - 这是您可以在清单文件中指定的。

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.java2s.com/Code/Java/SWT-JFace-Eclipse/CatalogSWT-JFace-Eclipse.htm

#4


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中。

#1


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);

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:

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

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

MenuManager菜单=新的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();

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.

请注意,'input'是一个Object。它可以是你喜欢的任何课程的对象。当您从Page :: getInput()获取它时,将其强制转换回相应的类。您通常不应该创建一个新类作为输入。您几乎总能使用现有的类。这通常是对象模型的*对象。除了存储它之外,Eclipse框架对此输入不执行任何操作,并在调用Page :: getInput()时将其传回。

#2


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

That site has lots of good articles on Eclipse development:

该站点有很多关于Eclipse开发的好文章:

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所建议的,阅读整篇文章是个好主意。

#3


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.

您是否正在创建一个想要多次打开的插件,在这种情况下,您可能需要一个编辑器并且需要确保您不使用Singleton模式 - 这是您可以在清单文件中指定的。

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.java2s.com/Code/Java/SWT-JFace-Eclipse/CatalogSWT-JFace-Eclipse.htm

#4


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中。