3

My application has 5 plugins. Each plugin has a perspective of it's own and hence each perspective extension definition is under individual plugin's plugin.xml.

Now, I want to control the order in which these perspectives appear in my application. How to do it?

There is one main plugin that holds "ApplicationWorkBenchAdvisor.java". This has initialize() method in which I am iterating through the perspective registry using

PlatformUI.getWorkbench().getPerspectiveRegistry().getPerspectives();

and then appending perspective ids in a comma separated fashion to a String variable (pbar) which is used later like this.

PlatformUI.getPreferenceStore().setDefault(IWorkbenchPreferenceConstants.PERSPECTIVE_BAR_EXTRAS, pbar);
PlatformUI.getPreferenceStore().setValue(IWorkbenchPreferenceConstants.PERSPECTIVE_BAR_EXTRAS, pbar);

When iterating thourgh the perspective registry, I can compare perspective ids and sort it(when adding to 'pbar' by comparing ids) the way I want it to appear but, I don't want to do this ordering here as it appears like a dirty way.

Is there any other place where we can fix the order in which perspectives appear? (Each perspective resides in different plugin).

ADDED

1) Could we also control the ordering in the perspective switcher?

2) Is there a way to control entry into perspective registry to in inflict the desired order. If not could we write back into perspective registry?

Real Red.
  • 4,991
  • 8
  • 32
  • 44
  • Added some comments, as requested. – VonC Apr 02 '09 at 12:40
  • Does your last comment means you are able to set the right order of perspective with the setDefault() call *after* having specified that order in the ini file ? – VonC Apr 07 '09 at 06:55
  • Fix the comment in my answer, but I suppose it does not solve *exactly* the "perspective order" issue, am I right ? – VonC Apr 07 '09 at 10:57
  • Thank you for your last comment. I have updated my entry with this clarification. That looks to me as close as a solution to your problem as we will ever get. – VonC Apr 08 '09 at 04:03
  • Thank you very much for prolonged assistance. Also thanks for updating your answer with information from comments. That makes answer more readable to others as well. Yeah, I guess now it's a complete answer Monsieur. – Real Red. Apr 08 '09 at 05:25
  • To finalize this question, you could post an answer of your own with a extract of the source code you finally ended up with to further illustrate what you were able to achieve. I will vote it up ;) – VonC Apr 08 '09 at 05:58

2 Answers2

8

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

alt text => to: alt text

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


Extreme solution: re-implement a perspective switcher.

http://www.richclient2.de/wp-content/uploads/2006/08/persp_header.png

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Brilliant, the line in ini file does the trick. But, I have a default view in which I am displaying links to perspectives. I want these links to be in the same order as well.(I am iterating thr' perspective registry here so its not in desired order). (cont)... – Real Red. Apr 02 '09 at 10:21
  • Also that perspective switcher doesn't display perspectives in order. Is there a way to access the the variable defined in ini file? Using that variable I can at least fix ordering in the view if not pers. switcher. Or better...is there a way to control/alter entry into pers. registry? Many Thanks. – Real Red. Apr 02 '09 at 10:26
  • I confirm that, when I iterate though perspective registry(as you have shown), perspectives are not in the same order as given in ini file. But, ini file entry does make perspective appear in order on UI. Is there a way to read this ini file entry in my program? Much thanks. – Real Red. Apr 03 '09 at 02:59
  • "Is there a way to read this ini file entry in my program?": not that I know of. I will search some more. – VonC Apr 03 '09 at 20:42
  • Hi VonC, I just figured out that we can retreive the ini file value with another tweak using API "PlatformUI.getPreferenceStore().setDefault(IWorkbenchPreferenceConstants.PERSPECTIVE_BAR_EXTRAS)". Afterall, ini entry goes into pref. store :-). Thanks for all your help. I can up you only once :-( – Real Red. Apr 07 '09 at 06:38
  • Thank you for this feedback. I have included your comment in my answer – VonC Apr 07 '09 at 06:52
  • Oh Sorry....my bad. I meant to write .getDefault() ended up writing setDefault(). Could you please edit ou answer accordingly so that it doesn't mislead others?(I can't edit your answer - no rights yet) Sorry for the trouble. – Real Red. Apr 07 '09 at 09:02
  • I meant, The ini file entry that we add, internally gets recorded into preference store. Therefore, we can retreive it using - PlatformUI.getPreferenceStore().getDefault(IWorkbenchPreferenceConstants.PERSPECTIVE_BAR_EXTRAS); – Real Red. Apr 07 '09 at 09:16
  • Done (fix the comment in my answer). But I suppose that does not solve the "perspective order" problem... – VonC Apr 07 '09 at 10:54
  • No, perspective registry remains unaltered. But, what we achieve here is "readily access to ordered list of perspectives". 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. – Real Red. Apr 08 '09 at 03:09
  • Excellent. I have added that bit of knowledge into the answer. – VonC Apr 08 '09 at 04:00
  • To clarify more, By iterating through the above said variable(which is a comma separated list of pers.ids) we can fetch the perspectives using PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(pid[i]); where pid[i] is ith comma separated pers.id. Now answer is more complete. – Real Red. Apr 08 '09 at 05:18
5

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.

Real Red.
  • 4,991
  • 8
  • 32
  • 44