Execute code when loading an Eclipse perspective - eclipse

I'm creating an Eclipse plug-in which amongst other things creates a new perspective. I want to I execute some code when the perspective loads. Previously I was doing this through createInitialLayout of IPerspectiveFactory but then I realized that this is for defining the page layout only and is usually called when when launching the perspective for the first time only.
How can I specify some code to execute whenever the perspective is displayed? (e.g. when it is loaded as the default perspective by Eclipse)
Thanks and regards,
Krt_Malta

Check the documentation for the IPerspectiveListener interface or the PerspectiveAdapter class, the perspective lifecycle events are explained there.

Related

How do I show all perspectives in my rcp project eclipse perspective bar (coolbar) upon startup?

Upon startup I only have the Open Perspective button and the default perspective that I set in method getInitialWindowPerspectiveId on its right. I want to show all my other perspectives on that coolbar.
Until now i tried:
config.ini file with org.eclipse.ui/PERSPECTIVE_BAR_EXTRAS=id1,id2,id3
plugin_customization.ini with org.eclipse.ui/PERSPECTIVE_BAR_EXTRAS=id1,id2,id3
in class ApplicationWorkbenchAdvisor in the initialize method someone said to do this:
PlatformUI.getPreferenceStore().setDefault(IWorkbenchPreferenceConstants.PERSPECTIVE_BAR_EXTRAS,"id1,id2,id3");
PlatformUI.getPreferenceStore().setValue(IWorkbenchPreferenceConstants.PERSPECTIVE_BAR_EXTRAS,"id1,id2,id3");
Nothing worked.
However i found a workaround:
in class ApplicationWorkbenchWindowAdvisor in the postWindowOpen i manually show all my perspectives. This leaves them opened in the coolbar. However this is not the optimum way and maybe someone knows the proper way to show all my perspective shortcuts on the coolbar.
PlatformUI.getWorkbench().showPerspective("id1", getWindowConfigurer().getWindow());
PlatformUI.getWorkbench().showPerspective("id2", getWindowConfigurer().getWindow());
PlatformUI.getWorkbench().showPerspective("id3", getWindowConfigurer().getWindow());
Thanks
The 'plugin_customization.ini' method should work
The 'plugin_customization.ini' file must be in your RCP plugin
The 'plugin_customization.ini' must be included in the build in the 'build.properties' file.

Programmatically affecting load order of perspectives

So, I'm working on an Eclipse Plugin which includes a custom view based on analysis of source code. The majority of the time, it works great. However, if I quit Eclipse with that view open, when I reopen it, it runs into an error with either IWorkbenchWindow.getActivePage() or IWorkbenchPage.getEditorReferences() returning null. This inconsistency seems to be because the view has the focus when Eclipse quits and is the first thing that Eclipse tries to reconstruct on start up. the focus is on a non-window shell (I don't fully understand this, but that's what this said). Is there a workaround so that I can ensure that Eclipse fully loads its IWorkbenchWindow before my custom plugin regardless of what has the focus when Eclipse closes?
Thanks
You can consider using the site instead: getSite().getPage()...
Tonny Madsen pointed out in the comments that, from within a View, I can access the Active Page from getSite().getPage(), which solved the issues.

Activate the activator class

I am using eclipse 3.6. I created one sample plugin application. It is neither a eclipse rcp nor workbench. Now when I run the eclipse I want that plugin also to be loaded. But I dont want to use IStartUp. Because what I have found out is
IStartup will be called after the workbench is loaded. I want to refresh some menu. So Is there any way to activate my plugin while the eclipse loaded?
I tried to use Bundle Activation policy. But that is also not activating my Activator class. I just put one System.out. println("Inside start()"). So that is not called. Now can I make it activate my activator?
EDIT:
what my exact requirement is, I have created one workbench application.It is not eclipse rcp application. Now I want to remove the following menu and menu items from the eclipse before the eclipe is loaded.
1. File Menu
2.) Search Menu
3.)Run Menu
4.)Help->search,Dynamic Help,Key assist,Tips and trick,Report Bug,Cheat Sheet.
These menus are inbuilt menu of eclipse. So that is the reason I have to do in this way.
So I already implemented by using startup extension point. But the early startup is called after the eclipse is started.So I need to do some refreshment on the workbench.Then only the menu item will get removed.So I thought I need startup extension point will not satisfy my requirement as it doesnot refresh the workbench.I need to activate the my plugin and refresh the workbench before it is loaded.
Thanks
Bhanu
You can set the needed start level for your plugin using touch point instruction.
You are just a plugin that expects to run in the Eclipse IDE?
Then the answer is, you cannot do what you want.
If you start before the workbench has finished initializing, most of the services that could be used won't work: The workbench itself, menu service, command service, etc.
For most plugins in eclipse, the plugin.xml should be used to add menus, views, editors, etc to eclipse. When necessary, the framework will instantiate them.
org.eclipse.ui.IStartup is available and as you mentioned it will be called after the workbench has been initialized, but before any windows have been shown. It's not to be used lightly, and not by plugins contributing to the UI as it allows all extension from that plugin to be loaded.
EDIT:
If you are an RCP app, you control the main menu. As an RCP app, you have access to the ActionBarAdvisor, WorkbenchAdvisor, WorkbenchWindowAdvisor, which all have lifecycle methods.
If you are an an eclipse plugin, you can add to the main menu ... you cannot easily remove from the main menu. This is by design. Start levels and org.eclipse.ui.startup are 2 mechanisms that won't do what you want.
You still need to answer these questions:
There might still be a way. The crux of your problem is: "I want to refresh some menu"
What kind of menu (popup, main menu,
compound list of menu items in a
menu, etc)?
Where is the menu contribution coming from?
Which specific menu item is it?
Please edit your question (do not comment) and include the information from the above 3 questions, please.

How can I make my Eclipse RCP app's keyboard shortcuts (accelerators) work immediately after startup?

Our Eclipse RCP application was originally built in the 3.1/3.2 era and was running on 3.2 until we switched to 3.6 recently. Its IApplication runs via PlatformUI.createAndRunWorkbench(...). The WorkbenchAdvisor we pass to that function overridescreateWorkbenchWindowAdvisor(...) to return a WorkbenchWindowAdvisor whose createActionBarAdvisor(...) returns an ActionBarAdvisor.
This ActionBarAdvisor's makeActions(...) creates and register()s a bunch of org.eclipse.jface.action.Actions, many of which do things like setAccelerator(SWT.CTRL | 'O'); in their constructors. The Actions are subsequently installed in the ActionBarAdvisor's fillMenuBar(...) and fillCoolBar(...) methods.
The problem we are having (now that we are on Eclipse RCP 3.6) is that these accelerators don't seem to be active until their menus are shown (even if no action is taken besides closing the menu again).
We see a relevant bug but are having some difficulty understanding how to apply its remedy to our situation. We recognize that instead of Actions we "ought" to be using commands, handlers, and key bindings. But we're hoping we don't have to go down that path just yet.
How can we make our accelerators "live" as soon as the application starts up?
If you don't choose to use o.e.ui.bindings extension point, then there isn't a better way. You should only force update the menuManager yourself as you have done in your answer.
As #Prakash mentioned, if you want to keep down that path in your RCP app you must render all of the main menus to see your accelerators.
There is a partial upgrade path that will get you on the right track to commands without forcing a complete switch over right away. For each action in your menu, define a command with an id and define a binding to the shortcut you want in your plugin.xml. Then, when you create the action in your ActionBarAdvisor, don't set the accelerator. Set the IAction.setActionDefinitionId(*) to the command id, can call register(action);
Then you no longer need to use menuManager.updateAll(true) to eagerly render all of your main menu.
After hunting around and experimenting a bit trying to apply the advice from the bug, we added the following to our WorkbenchWindowAdvisor, which seems to have done the trick:
#Override
public void postWindowCreate() {
getWindowConfigurer().getActionBarConfigurer().getMenuManager().updateAll(true);
}
We have no idea how well this fits with the Workbench's design expectations; there could be a better way.

Execute code when Eclipse workbench loads

I'm writing an Eclipse plug-in and I've bumped into an issue. Amongst others I'm creating a new custom perspective. I need to execute some code when the workbench loads. I'm using a WorkbenchAdvisor and putting the code in the initialize method. However as it is now it's not being called...
Apparently I need to call this PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor(); but I don't know where to put this... I can't put it in the createInitialLayout of the perspective because this is only called when the perspective is created for the first time.
Any ideas please?
Thanks and regards,
Krt_Malta
You can use the startup extension point to run code before your plugin is loaded. You should place the extension in an seperate plugin, as all code inside the plugin with the startup extension is loaded after the workbench is started.
The interface to look for is org.eclipse.ui.IStartup.
http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/extension-points/org_eclipse_ui_startup.html
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/ui/IStartup.html