Eclipse RCP: How to order perspective buttons belonging to different plugins?

余生长醉 提交于 2019-11-30 10:24:05
VonC

If your application is encapsulated as an eclipse product, you may tweak the plugin.properties/plugin_customization.ini file.
(file referenced by the 'preferenceCustomization' property in your product extension point.)
This file is a java.io.Properties format file. Typically this file is used to set the values for preferences that are published as part of a plug-in's public API.
(Example of such a file for org.eclipse.platform)

So if the string representing the order of perspective can be referenced as a property, you can define the default order in there.
Since the source code of IWorkbenchPreferenceConstants mentions:

 /**
  * Lists the extra perspectives to show in the perspective bar.
  * The value is a comma-separated list of perspective ids.
  * The default is the empty string.
  *
  * @since 3.2
  */
 public static final String JavaDoc PERSPECTIVE_BAR_EXTRAS = "PERSPECTIVE_BAR_EXTRAS"; //$NON-NLS-1$

Maybe a line in the plugin_customization.ini file:

org.eclipse.ui/PERSPECTIVE_BAR_EXTRAS=perspectiveId1,perspectiveId2,perspectiveId3

would allow you to specify that order without having to hard-code it.

Additional notes:

IPerspectiveRegistry (or PerspectiveRegistry) is not made to write anything (especially for perspective defined in an extension)

Ordering may be found in the state of the workbench (stored in the workspace and then restored when its launched again, .metadata/.plugins/org.eclipse.ui.workbench/workbench.xml)

Do you confirm that:

IPerspectiveRegistry registry = PlatformUI.getWorkbench().getPerspectiveRegistry();
IPerspectiveDescriptor[] perspectives = registry.getPerspectives();

is not in the right order when the plugin_customization.ini does define that order correctly ?

Liverpool 5 - 0 Aston Villa does confirm that (in the comments), but also indicates the (ordered) ini file entries internally get recorded into preference store, which means they can be retrieved through the preference store API:

PatformUI.getPreferenceStore().getDefault( 
    IWorkbenchPreferenceConstants.PERSPECTIVE_BAR_EXTRAS)

Liverpool 5 - 0 Aston Villa then add:

perspective registry (the initial "PlatformUI.getWorkbench().getPerspectiveRegistry().getPerspectives();" bit) remains unaltered (and unordered).
But, you still can "readily access to ordered list of perspectives" through preference store.
So, for other tasks, instead of iterating though perspective registry (which is still unordered), we can use the ordered variable that stores list of ordered perpective ids.

.
.
.
.


Note: another possibility is to Replace the Perspective-Switcher in RCP apps

=> to:

You can more easily define the order in a menu or in buttons there.


Extreme solution: re-implement a perspective switcher.

To sum up all the observations and findings,

1) It is not possible to alter entries in the perspective registry. It is read-only.

2) To make perspective appear in the order that we want on perspective bar, we can achieve it by adding an entry in plugin_customization.ini (or preferences.ini) as shown below.

org.eclipse.ui/PERSPECTIVE_BAR_EXTRAS=perspectiveId1,perspectiveId2,perspectiveId3

3) If we want to fetch this ordered list, we can't fetch it directly. But as this ini file entry internally gets recorded in PreferenceStore we can fetch the same value from PreferenceStore using the following API as shown below.

PlatformUI.getPreferenceStore().getDefault( 
    IWorkbenchPreferenceConstants.PERSPECTIVE_BAR_EXTRAS);

Why would someone need to access the entry defined in ini file at all?

Well, in my case I had a view in which i had to display links to every perspective. As my perspective bar was sorted in desired order, I also wanted to maintain the same order in my view while displaying links to perspectives.

4) There is no known way to inflict the same sort order in the display of default perspective switcher. While a new custom perspective switcher can be written to achieve the desired effect.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!