Reputation: 4383
We would like to reset a perspective programmatically in an Eclipse RCP 4 application. I've been looking at examples for how this can be done but it seems that there isn't a tidy method of doing so.
Here are a few examples we've looked at:
1 https://www.eclipse.org/forums/index.php/t/210165/
Copying all perspectives into Snippets
. This means maintaining the duplicates in the Perspective Stack
and Snippets
which is not suitable for a large RCP application.
2 How to reset perspective programmaticly in Eclipse RCP E4 not E3
Calling resetPerspectiveModel
. This doesn't appear to do anything. Another user has commented mentioning this too.
3 How do I reset perspective for Eclipse e4 RCP application?
The first answer mentions using RCP 3 code.
The second answer removes the current perspective, adds it, then calls to switch to the current perspective. This does nothing.
We've been trying variations of each of these methods and we can't get it to reset.
What is the correct way of resetting perspectives in a pure e4 application?
Upvotes: 1
Views: 519
Reputation: 691
For my app, which is using -clearPersistedState on every start, I'm basically using 1 but with automatical cloning of the Perspectives on startup to Snippets.
I'm doing it in the lifeCycleURI class defined in plugin.xml like this
<extension
id="product"
point="org.eclipse.core.runtime.products">
<product
name="My App"
application="org.eclipse.e4.ui.workbench.swt.E4Application">
...
<property
name="lifeCycleURI"
value="bundleclass://my.main.plugin/some.package.StartupLifeCycleHandler">
</property>
java method annotated with @ProcessRemovals:
@ProcessRemovals
public void saveOriginalPerspectiveLayouts(IEclipseContext context) {
MApplication app = context.get(MApplication.class);
EModelService modelService = context.get(EModelService.class);
MPerspectiveStack perspStack = (MPerspectiveStack) modelService.find(
"my.perspstack.id", app);
for (MPerspective persp : perspStack.getChildren()) {
modelService.cloneElement(persp, app);
}
}
My Reset Handler looks like this. I left out shared area, tags and what else there may be since I don't need it atm, could probably be gleaned from org.eclipse.ui.internal.WorkbenchPage.resetPerspective() if needed.
@Inject
private EModelService modelService;
@Inject
private EPartService partService;
@Inject
private MApplication app;
@Execute
public void execute() {
MWindow window = (MWindow) modelService.find("my.main.window.id", app);
MPerspective persp = modelService.getActivePerspective(window);
// the perspective's single PartSashContainer
persp.getChildren().get(0).setToBeRendered(false);
persp.getChildren().clear();
// close possibly existing part windows
for (MWindow pWindow : persp.getWindows())
pWindow.setVisible(false);
persp.getWindows().clear();
// remove minimized-stack controls
modelService.resetPerspectiveModel(persp, window);
// reinstate original perspective part layout
MPerspective clonedPersp = (MPerspective) modelService.cloneSnippet(app,
persp.getElementId(), window);
// the cloned PartSashContainer with the initial part layout
persp.getChildren().addAll(clonedPersp.getChildren());
// give focus to a Part
partService.requestActivation();
}
Upvotes: 0
Reputation: 137
Setting a preference, restart the application, detect the preference on startup and add the clearPersistedState system property is the only way I found to do this in a tricky way. It works, but it would be desirable to reset without restarting. The bad thing is that it will reset the entire model.
Use this code in your "reset perspective" handler:
@Execute
public void restart(IWorkbench application, @Preference IEclipsePreferences preferences) {
preferences.putBoolean("PLEASE_RESET_ON_STARTUP", true);
preferences.flush();
application.restart();
}
and something like this in your life cycle manager class:
@PostContextCreate
public void doTheTrick(@Preference IEclipsePreferences preferences) {
if (preferences.get("PLEASE_RESET_ON_STARTUP", "false").equals("true") {
System.setProperty(IWorkbench.CLEAR_PERSISTED_STATE, "true");
preferences.remove("PLEASE_RESET_ON_STARTUP");
preferences.flush();
}
}
Maybe you need to add some exception handling also.
Upvotes: 0