Eclipse 4.2(juno) did not call saveState method of ViewPart when workspace is closing - eclipse

I have a eclipse-plugin that have two perspectives. There is a view which extends ViewPart in one of the two perspectives. In this view, I overrided saveState method of ViewPart to save my data.
First, I open the prespective that has this view. Then i add some data in the view which should be save in saveState.
Next, I navigate to the other perspective that does not have this view.
Finally, I close the eclipse's workspace.
In eclipse 4.2(juno), saveState method of the view do not have been called. My data lost.
In eclipse 3.6(Helios), saveState method of the view have been called. My date has been persisted.
Does anyone know the reason? How can I insure that the saveState will be called when closing the workspace on all version of eclipse?

Eclipse e4 has no ApplicationWorkbenchAdvisor class and the application model has no property to set this,totally different with Eclipse 3.x.
You can get more from the wiki and vogella tutorial blog.

With joy, this problem persists into 4.4.2.
At its base, the issue is the compatibility layer WorkbenchPart.getViewReferences() only searches the currently active perspective. This behavior is different than the 3.x. The relevant code from the 4.4.2 Eclipse WorkbenchPart is here (notice the call to getCurrentPerspective()).
public IViewReference[] getViewReferences() {
MPerspective perspective = getCurrentPerspective();
if (perspective != null) {
List<MPlaceholder> placeholders = modelService.findElements(window, null,
MPlaceholder.class, null, EModelService.PRESENTATION);
List<IViewReference> visibleReferences = new ArrayList<IViewReference>();
for (ViewReference reference : viewReferences) {
for (MPlaceholder placeholder : placeholders) {
if (reference.getModel() == placeholder.getRef()
&& placeholder.isToBeRendered()) {
// only rendered placeholders are valid view references
visibleReferences.add(reference);
}
}
}
return visibleReferences.toArray(new IViewReference[visibleReferences.size()]);
}
return new IViewReference[0];
}
Therefore, if one has a view open and then changes to a perspective where that view is not shown, the saveState() method will not be called.
We have added an OSGI event listener for the UIEvents.UILifeCycle.appShutdownStarted and made a call to the saveState(). However, it is necessary to obtain the IMemento manually, since it is not present. Example code is in the org.eclipse.ui.internal.ViewReference (http://grepcode.com/file/repository.grepcode.com/java/eclipse.org/4.2.2/org.eclipse.ui/workbench/3.104.0/org/eclipse/ui/internal/ViewReference.java#ViewReference).
One could also add a part close listener with the IPartListener class to potentially persist settings if the user closes the view rather than application shutting down.
We have not found an OSGI event for the part being closed, but there may be one.
This discussion (Eclipse call ViewPart saveState on View close) suggested using IDialogSettings rather the IMemento. This discussion also proposed perhaps adding something into the dispose() method, but it is unclear how many resources are necessarily still available at the point of the dispose() being called.

Related

How to activate my eclipse plugin when a specific editors opens

My plugin needs to listen to changes (selection and content) of xtext based editors provided by another third-party plugin.
Edit1
The issue is not how to listen to the specific events in general.
Instead the issue is how to trigger the listener registration, since there's no code of my plugin executed (lazy loading) unless it is used by the user through a command for instance.
Edit2
Using org.eclipse.ui.IStartup extension point the issue is that in IStartup.earlyStartup() PlatformUI.getWorkbench().getActiveWorkbenchWindow(); is returning null. It looks like this is too early in the startup phase to register the listeners.
You can use an org.eclipse.ui.IPartListener2 listener to be told about all parts opening, closing, being activated ....
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IPartService partService = window.getPartService();
partService.addPartListener(your listener);
The
public void partOpened(IWorkbenchPartReference partRef)
method of the listener will be called when a part (editor or view) is opened. The partRef.getId() method will give you the id of the part.
Use the org.eclipse.ui.startup extension point to declare that your plugin needs to be started early. This lets you declare a class implementing org.eclipse.ui.IStartup that is called during Eclipse startup.
Note that the startup runs quite early so not everything is set up. Use Display.asyncExec to schedule code to run later:
Display.getDefault().asyncExec(runnable);

Refresh Eclipse 4 RCP view on wizard perform finish

Rookie question that I'm not having much luck with. In my e4 RCP application, I have a couple of instances where I create an object in a wizard that should then appear in one of my views.
The desired behavior is similar to how the eclipse Package Explorer View updates after a new project is created.
I was thinking I could just grab the view from the partService and run my own update method:
MPart ingredientsView = partService.showPart("com.personal.recipes.part.ingredientsview", PartState.ACTIVATE);
IngredientsView iv = (IngredientsView) ingredientsView.getObject();
iv.updateView();
While this works in other places, when called from a wizard 'partService' is null and the app NPE's out.
So what is the proscribed method of forcing e4 views to update after modifying their contents?
EDIT:
I tried to use the ContextInjectionFactory like #greg-449 showed in his answer, but I'm uncertain where to place it in my code, or how to define the context. I'm launching the wizard from a toolbar button, and placed the following code in my handler:
#Execute
public void execute(Shell shell) {
IEclipseContext context = EclipseContextFactory.create();
IWizard ingredientWizard = ContextInjectionFactory.make(IngredientWizard.class, context);
WizardDialog wizardDialog = new WizardDialog(shell, ingredientWizard);
wizardDialog.open();
}
However, when I tried to get the part service with #Inject EPartService partService; I got an InjectionException saying no error was found.
Once injection is available, using the EventBroker looks like the way to go.
enter code hereThe best way to update a view is to use a model for the content of the view. Your wizard seems to allow editing or creating ingredients. When you perform the finish of your wizard you are probably modifying some ingredient data. The ingredient model should be informed of these changes. If the view uses a content provider that observes this model is will update automatically when the model sees the update (this is the observer pattern).
How this works depends on the nature of your data. You could use the PropertyChange-Support in Java.
To do so let the content provider implement the org.eclipse.jface.util.IPropertyChangeListener interface and fire property change events when the data is changed.
UPDATE
My ContentProvider implements the property change interface. Whenever a property change event is received the viewer is refreshed(asynchronously). All my persistence operations are handled by data managers similar to Fowler's the table data gateway pattern but sometimes for more than one table. The data manager fires the property change event. This way the UI (wizard) does not need to know about persistence
Injection is only done on objects that the application model knows about. So it is not done on Wizards or Dialogs unless you do it 'manually' using ContextInjectionFactory when you create the dialog:
IWizard wizard = ContextInjectionFactory.make(YourWizardClass.class, eclipseContext);
WizardDialog dialog = new WizardDialog(shell, wizard);
This will do injection on your wizard class giving you access to the EPartService.
You could also use the 'event broker' (IEventBroker) to broadcast an event to anything that is interested rather than finding your specific view.

Eclipse: Converting actions in commands for a specific view

My original RCP was started in 3.x and currently I am running it on Juno with the compatibility layer. I was looking into doing a soft migration so I have started to slowly change my practices. One of the things I am doing is to change my actions into commands.
I have a view (which is like a directory explorer) currently that adds actions to the toolbar and popup menu of the view. These actions call specific methods in the view, for example to go up one directory.
It was easy to do this by action because I just create my action in the View class itself and programmatically add them to the toolbar
IToolBarManager mgr = getViewSite().getActionBars().getToolBarManager();
mgr.add(upDirectory);
mgr.add(refresh);
mgr.add(changeRoot);
and the creation of the actions are called from the createPartControl()
upDirectory = new Action("Go up one directory") {
public void run() {
goUpOneDirectory();
}
};
where goUpOneDirectory() is a method in the view
If I want to convert this to a command, I want to be able to access this method of the view in my handler. So I tried the following,
private void createHandlers()
{
IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
IHandler upDirHandler = new AbstractHandler() {
public Object execute(ExecutionEvent event)
throws ExecutionException {
goUpOneDirectory();
return null;
}
};
handlerService.activateHandler("updir.id", upDirHandler);
}
And createHandlers is called in the createPartControl, and the command is added via the plugin.xml to the toolbar of the view. The problem is that the moment my view is out of focus it disables the buttons in the toolbar for these commands.
I want them to remain enabled at all times. How can I do that?
I know that the isEnabled() returns true all the time so I am not sure why it happens. The activateHanlder is called once in createPartControl so I feel that it should remain active all the time.
Edit: Ok I just saw this ,
IHandlerService from the workbench part site is the part handler
service. Any handlers activated through the part handlers service will
only be active when that part is active. Any listeners added to the
part handler service will be removed when the part is disposed, and
any active handlers will be deactivated (but not disposed).
So how can I get this,
IHandlerService from the workbench is the global handler service. it
provides no special activation scoping or lifecycle.
Sorry, I should have waited a bit longer before asking, I figured it out!
I changed the,
IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
to
IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
and it worked.
I will leave the question in case it helps other people.

Refreshing the workbench

HI,
I am facing some problem.. I want to hide the menu when eclipse workbench starts.
But the problem is menu is not hiding when the eclipse workbench starts. It is hiding only
when some refresh is happened. for example: when I change the default perspective to some other perspective, I am getting the desired out put. That means menu is hiding.
But when the eclipse workbench is loaded it is not hiding the menu. Below is my code.
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
public void run() {
try {
IWorkbenchWindow window = Workbench.getInstance().getActiveWorkbenchWindow()
if(window instanceof WorkbenchWindow) {
MenuManager menuManager = ((WorkbenchWindow)window).getMenuManager();
IContributionItem[] items = menuManager.getItems();
for(IContributionItem item:items){
System.out.println("item.getId()::: "+item.getId());
menuManager.remove("org.eclipse.ui.run");
menuManager.remove("help");
menuManager.remove("project");
}
}
}`
}
};
Given that you are looking to hide some features, I don't think that this is the best approach. (Not I am using the term feature here in the colloquial way, not as an Eclipse feature.
I would recommend one of two avenues:
Perspectives: See the extension point org.eclipse.ui.perspectives. This allows you to create a new perspective like the debug perspective or the Java perspective. Using a perspective, you can select exactly what menu items and views are shown and which ones are hidden.
Capabilities (aka activites): See the extension point org.eclipse.ui.activities. This allows you to have some fairly fine-grained control over what features are available in the workspace. See more info here: http://wiki.eclipse.org/Galileo_Capabilities
Put Your code in org.eclipse.ui.startup extention point. Make a Startup class after implementing the interface IStartup. For Details follow this link:-
Eclipse plugin : disable/enable dynamically an action from main menubar

Programmatically showing a View from an Eclipse Plug-in

I have a plug-in to an Eclipse RCP application that has a view. After an event occurs in the RCP application, the plug-in is instantiated, its methods are called to populate the plug-in's model, but I cannot find how to make the view appear without going to the "Show View..." menu.
I would think that there would be something in the workbench singleton that could handle this, but I have not found out how anywhere.
You are probably looking for this:
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("viewId");
If called from handler of a command
HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().showView(viewId);
would be better, as I know.
I found the need to bring the view to the front after it had been opened and pushed to the background. The activate method does the trick.
PlatformUI.getWorkbench()
.getActiveWorkbenchWindow()
.getActivePage()
.activate(workbenchPartToActivate);
NOTE: The workbenchPartToActivate is an instance of IWorkbenchPart.
In e4, the EPartService is responsible for opening Parts. This can also be used to open e3 ViewParts. Instantiate the following class through your IEclipseContext, call the openPart-Method, and you should see the Eclipse internal browser view.
public class Opener {
#Inject
EPartService partService;
public void openPart() {
MPart part = partService.createPart("org.eclipse.ui.browser.view");
part.setLabel("Browser");
partService.showPart(part, PartState.ACTIVATE);
}
}
Here you can find an example of how this works together with your Application.e4xmi.