Eclipse RCP Updating Toolbar and Menu Entries - eclipse

I am using menu entries and toolbar buttons to link them to actions (AbstractHandler).
For example, I can connect to or disconnect from hardware. If I connect, there are menu and toolbar entries dependent on the connection state. They update after I click in the view or do another action within the view. For UX improvements it would be nice if they update after the action was executed.
Is there a way to update the view programmatically or do I have to implement this differently?
The connect handler is implemented very simple:
public class ConnectHandler extends AbstractHandler {
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
// Connect to hardware.
return null;
}
}
The menu and toolbar entries are defined in the plugin.xml.
I tried some stuff with the ICommandService and refreshElements, but was unsuccessful.

Related

I want to trigger a View in Eclipse RCP through the handler also want to pass source object which have invoked the command

I have added a command (push button) in my plugin project.
Now in the handler I want whenever that command is initiated a new view will open.
But the thing is I also want to pass some input to view from my handler.
Please suggest with an example.
Assuming this is a 3.x compatibility mode plugin you show the view using:
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IViewPart viewPart = page.showView("view id");
where "view id" is the id of the view you declared in the org.eclipse.ui.views extension point.
The view part returned is an instance of your view class so you can call a method on the view to pass in your data:
MyView myView = (myView)viewPart;
myView.setParameters(your parameters);
where MyView is your view class and setParameters is a method that you write.

IInputSelectionProvider not considered by listeners of RCP SelectionService

I have a RCP application with different views. The views should interact with each other through the Eclipse SelectionService.
In view 1 I have added a SelectionListener with
getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(this.listener);
In view 2 I have added a SelectionProvider with
getSite().setSelectionProvider(this);
To get this working, I implemented the methods from the IInputSelectionProvider in view 2. When I run my program, view 1s selection listener is not invoked. After debugging,
I found out that view 1 is not added in the list of listeners of view 2. In view 2 I have a method
private ListenerList listenersList = new ListenerList();
#Override
public void addSelectionChangedListener(ISelectionChangedListener iselectionchangedlistener) {
// TODO Auto-generated method stub
listenersList.add(iselectionchangedlistener);
}
which adds listeners to the IInputSelectionProvider. My question is: Who should call this method. My understanding is that Eclipse SelectionService should does this with
getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(this.listener);
But it doesn't work. Do I have to fill the listenerList by myself? If yes, why do I have to use the SelectionService at all?
Or do I have to iterate through the list of listeners by calling any other method and not using the list at all? Because if I inspect the ISelectionService object
ISelectionService service = getSite().getWorkbenchWindow().getSelectionService();
I see all the listeners.
But they are not part of the listenerList above.
The addSelectionChangedListener is called every time a view gets activated and removed when a view is not longer active. This means: If View A is active and 'setSeletion' is called, all views which are listening are notified. If these views by themselves call 'setSelection' nothing happens. No notification is started.

What is lifetime of custom view extends ViewPart in Eclipse platform?

What is lifetime of a custom view which extends ViewPart in Eclipse platform?
Is it created when View appears and destroyed when it disappears?
If so then why creation occurs in method createPartControl() but not in constructor?
The view part is constructed the first time it needs to be shown. createPartControl is called during this construction but the
public void init(IViewSite site);
and
public void init(IViewSite site, IMemento memento);
methods are called before createPartControl to allow some things to be initialized. The base ViewPart class normally deals with this but the methods can be overridden if required.
Updated:
Although the API to 'close' a view is actually called hideView it does appear to call the dispose method of the view and reconstructs it on the next use. If the view is open on multiple perspectives it is not disposed until the last reference is closed.

Activate part warning

My application is a perspective with two views.
When starting it, the application shows viewA with a table, the viewB is hidden.
When I select an item from the table, opens the viewB, send the item you selected to viewB, and hides the viewA.
I can perform these actions but on the console I have the following warning:
"Prevented recursive attempt to activate part "viewB" while still in the middle of activating part "viewA".
Some help to solve this warning?.
Are you doing the work to display "viewB" inside the button click event method? If so, you probably need to queue that work up for the UI thread to do later on by wrapping it in:
Display.getDefault().asyncExec(new Runnable() {
#Override
public void run() {
// Your UI update code.
}
};

MVP, best practice for passing row events back to the presenter

I have a CellTable within a GWT MVP view and want to inform the presenter when certain actions are taken on a row. For example a popup menu is presented for a row, and an action (Delete/Edit/etc.) selected. There's obviously a SelectionModel that is available via HasData, but how would I use this to pass back the action 'action'.
Is there a standard interface (like HasData) that I could use to pass back to the Presenter?
Usually in GWT MVP View exposes an object that can register event handlers: usually thay come in form of HasXyxHandlers, like HasClickHandlers or HasChangeHandlers.
In case of CellTable it's named differently: SelectionModel. Just implement in View a method that returns it:
SelectionModel<YourClass> getSelectionModel();
then Presenter calls this method and registers itself:
final SelectionModel<YourClass> selectionModel = view.getSelectionModel();
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
#Override
public void onSelectionChange(SelectionChangeEvent event) {
YourClass selectedObject = selectionModel.getSelectedObject();
// do something with selectedObject
}
});
Thinking about this more, I could be over engineering it. If I have one popup menu that I reuse in the view, and have the presenter listen to click events on the menu (rather than the CellList), then I can query the CellList selection model through within the handler for the button(s).