What are wrong with this code on Activating/Deactivating IHandlerActivation of Command Eclipse Plugin Development - eclipse

So, I have 2 commands, which are identified by PLAY_COMMAND_ID and STOP_COMMAND_ID. Each of command have each handler, respectively playHandler and stopHandler (these are extending AbstractHandler class).
These commands are contributed to my view's toolbar in Button style. Basically what I want is initially the PLAY_COMMAND is active but the STOP_COMMAND not. When the PLAY_COMMAND is clicked, then it will activate the STOP_COMMAND then deactivate itself(PLAY_COMMAND). And vice versa when the STOP_COMMAND clicked.
So what I do is like this. At first it works (I clicked play-button, then stop-button is activated and play-button disabled. I clicked stop-button, then play-button is active and stop-button is disabled. But when I clicked the play-button again, the play-button is still active when the stop-button is active too). So what's wrong with my code here:
private AbstractHandler playHandler, stopHandler, pauseHandler, stepHandler;
private IHandlerActivation playActivation, stopActivation, pauseActivation, stepActivation;
private void createHandlers(){
final IHandlerService handlerService = (IHandlerService)getSite().getService(IHandlerService.class);
playHandler = new AbstractHandler() {
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
handlerService.deactivateHandler(playActivation);
if(stopActivation == null){
stopActivation = handlerService.activateHandler(STOP_COMMAND_ID, stopHandler);
} else {
handlerService.activateHandler(stopActivation);
}
return null;
}
};
stopHandler = new AbstractHandler() {
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
handlerService.deactivateHandler(stopActivation);
handlerService.activateHandler(playActivation);
return null;
}
};
playActivation = handlerService.activateHandler(PLAY_COMMAND_ID, playHandler);
}
}
The createHandlers() method is called at the end of createPartControl(Composite parent) method in my View.

Okay, so here what I found. The IHandlerActivation, that is returned when calling activateHandler(IHandlerActivation) method, when it is deactivated, can't be used again in activating the same handler. So the solution is try calling handlerService.activateHandler(commandID, playHandler) instead of calling handlerService.activateHandler(playActivation).

Related

Evaluation of #CanExecute for RCP e4 application

A RCP E4 application includes a TreeViewer to manage the visivility/selection of a collection of “Packages”. The part is named as Package Navigator.
When one Package is ready to be send, the TreeViewer icon shows
and the button to start the shipment
should be enabled.
When the package is not ready, the icon is
and the button (handler) to ship should be disabled.
The code to implement such
behavior is:
private TreeViewer viewer;
#PostConstruct
public void createComposite(Composite parent, IEnviosService theModel) {
...
Tree t = (Tree) viewer.getControl();
t.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
boolean check = false;
System.out.print("Selection listener ....");
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
if (selection.getFirstElement() instanceof Package) {
check = ((Package)selection.getFirstElement()).isReadyToShip();
System.out.print("IT'S A PACKAGE....");
// evaluate all #CanExecute methods
broker.post(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC, check);
}
System.out.print("\n");
}
});
}
The handler to execute the shipment is
public class ShipmentHandler {
#Execute
public void execute(Shell shell) {
//TODO
}
#Inject
#Optional
#CanExecute
public boolean canExecute(#UIEventTopic(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC) boolean checkPackageReady) {
System.out.println("Inside canExecute method... " + checkPackageReady);
if (checkPackageReady)
return true;
return false;
}
}
But the button never is disabled, even whe the #canExecute method returns false, for example, after click on Packages 88 , 89 and 110 and 112 shows the following console output whith the button always enabled:
Selection listener PACKAGE: 88...true
Inside canExecute method... true
Selection listener PACKAGE: 89...false
Inside canExecute method... false
Selection listener PACKAGE: 110...false
Inside canExecute method... false
Selection listener PACKAGE: 112...true
Inside canExecute method... true
I don't think you can mix #CanExecute and #UIEventTopic like that. In any case UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC is a rather special topic which isn't intended to be handled like this.
The argument to the broker.post(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC is supposed to be an element id, UIEvents.ALL_ELEMENT_ID or a org.eclipse.e4.ui.workbench.Selector, nothing else. The argument selects which handlers are updated.
So you can't pass the 'checkPackageReady' value directly to the handler, you will have to use some other mechanism - such as a model object that the view and the handler can both inject.
You could also use the ESelectionService to set the current selection for the part, you can then access this information in the handler:
View part:
#Inject
ESelectionService selectionService;
...
public void widgetSelected(SelectionEvent e)
{
selectionService.setSelection(viewer.getStructuredSelection());
broker.post(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC, ... selector);
}
Can execute:
#CanExecute
public boolean canExecute(#Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, #Named(IServiceConstants.ACTIVE_PART) MPart part)
{
// TODO check part is your part
// TODO check the selection
}

Close Eclipse ViewPart tab when createPartControl function fails when launched from "Quick Access"?

The default behaviour when creating a new Eclipse ViewPart is to show the new tab regardless of what happens in the createPartControl function. For example, if didn't create anything, no widgets, nothing, a blank tab will be shown. I don't like this behaviour. I want to close that tab if initialization in createPartControl fails.
Now, I have a mouse-button-context-menu handler that can do this, e.g.
public class MyPartMB3Handler extends AbstractHandler {
#Override
public Object execute(final ExecutionEvent event)
throws ExecutionException {
// Create a view and show it.
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
IWorkbenchPage page = window.getActivePage();
try {
MyPart viewPart = (MyPart)page.showView(MyPart.ID);
if(!viewPart.isCreated()) {
page.hideView(viewPart);
}
}
catch(PartInitException e) {
e.printStackTrace();
}
return null;
}
}
The isCreated function is a little hack that lets me know if my ViewPart initialization fails, e.g.
public class MyPart extends ViewPart {
public static final String ID = "com.myplugin.MyPart";
private Composite _parent = null;
#Override
public void createPartControl(Composite parent) {
if(!MyPlugin.createPartControl(parent) { // Some common part creation code I use.
//PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(this);
return;
}
_parent = parent;
}
#Override
public void setFocus() {
}
public boolean isCreated() {
return _parent != null;
}
}
The problem arises when I launch this ViewPart from the Eclipse "Quick Access" field. I don't own the handler now. From an exception I forced, the handler might be org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.createPartControl or org.eclipse.ui.internal.e4.compatibility.CompatibilityView.createPartControl or org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.create.
I tried hiding the view inside the createPartControl function (see the commented line above), but Eclipse did not like that and spewed a pile of exceptions.
I thought maybe I could throw a PartInitException in createPartControl, but Eclipse tells me I'm not allowed to do that.
So, how do I get my menu handler behaviour when launching from "Quick Access"?
An underlying question might be, is there a better/proper way to achieve this behaviour?
You can close the view by running the hideView asynchronously after the createPartControl has finished - like this:
#Override
public void createPartControl(Composite parent) {
parent.getDisplay().asyncExec(new Runnable() {
#Override
public void run()
{
getSite().getPage().hideView(MyPart.this);
}
});

Catch closing event of a part (Eclipse e4 RCP)

I'm currently working on a eclipse e4 RCP application and I have a part that serves as a job manager where the user can see all active jobs and their progresses, like one in eclipse. The problem is now that the user can open the progress part by double clicking in the toolbar and he should also be able to close the progress part whenever he wants, but instead of disposing the part I want to just make it invisible.
I thought at first this shouldn't be a problem because I can set the part to be not visible, but the problem is how to catch the closing event and process it by my way. Is there any event, interfaces or listeners I can implement to catch the closing event and prevent the part from getting disposed?
You can implement a CustomSaveHandler and replace the Default Eclipse Save Handler with a Processor. In that SaveHandler you can control if the Part shoud get closed or not. So you could do not close it and make it invisible.
ExampleCode:
public class ReplaceSaveHandlerProcessor {
#Named("your.id.to.window")
#Inject
MWindow window;
#Inject
IEventBroker eventBroker;
#Execute
void installIntoContext() {
eventBroker.subscribe(UIEvents.Context.TOPIC_CONTEXT, new EventHandler() {
#Override
public void handleEvent(final Event event) {
if (UIEvents.isSET(event)) {
if (window.equals(event.getProperty("ChangedElement")) && (window.getContext() != null)) {
window.getContext().runAndTrack(new RunAndTrack() {
private final ISaveHandler saveHandler = new CustomSaveHandler();
#Override
public boolean changed(final IEclipseContext context) {
Object getSaveHandlerValue = context.get(ISaveHandler.class);
if (!saveHandler.equals(getSaveHandlerValue)) { // prevents endless loop
ContextInjectionFactory.inject(saveHandler, window.getContext());
context.set(ISaveHandler.class, saveHandler);
}
return true; // ture keeps tracking and the saveHandler as the only opportunity
}
});
}
}
}
});
}
}
You have to define a Extention for ExtentionPoint org.eclipse.e4.workbench.model
With Your ReplaceSaveHandlerProcessor. (You have to declare the window id as "element" in this extention. (Added Screenshot: )
The CustomSaveHandler have to implement the ISaveHandler interface. In its Methods ypu can say if the Part should realy be closed.
public class CustomSaveHandler implements ISaveHandler {
#Override
public boolean save(MPart dirtyPart, boolean confirm) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean saveParts(Collection<MPart> dirtyParts, boolean confirm) {
// TODO Auto-generated method stub
return false;
}
#Override
public Save promptToSave(MPart dirtyPart) {
// TODO Auto-generated method stub
return null;
}
#Override
public Save[] promptToSave(Collection<MPart> dirtyParts) {
// TODO Auto-generated method stub
return null;
}
}

GWT - Reference to a global variable set by callback method

Please help me, as I will go mad with this soon:
When I run the code, on first occasion loadNewPoint() is executed and displays some data from global variable - allPointsAndPlaces
However when I click a button (from a child class), the same method loadNewPoint() gives me null pointer for allPointsAndPlaces.
I have changed the code structure a lot from an original trying to solve this issue, and moved this method (loadNewPoint()) to a parent class to see, if it would solve the issue.
Parent class:
public class CabbieApp implements EntryPoint {
private GetLocationsServiceAsync getAllLocationsService = GWT.create(GetLocationsService.class);
CabbiePoint[] allPointsAndPlaces;
PointsQuiz quiz;
/**
* Entry point method.
*/
public void onModuleLoad() {
//Get all the required data from DB
getAllPointsAndLocations();
}
private void loadAppPages(){
// Associate the Main panel with the HTML host page.
RootPanel rootPanel = RootPanel.get("pointsList");
quiz = new PointsQuiz();
rootPanel.setStyleName("GWTapp");
rootPanel.add(quiz.getMainPanel());
loadNewPoint();
}
private void getAllPointsAndLocations() {
// Initialize the service proxy.
if (getAllLocationsService == null) {
getAllLocationsService = GWT.create(GetLocationsService.class);
}
// Set up the callback object.
AsyncCallback<CabbiePoint[]> callback = new AsyncCallback<CabbiePoint[]>() {
public void onFailure(Throwable caught) {
System.out.println(caught.getMessage());
}
public void onSuccess(CabbiePoint[] result) {
//allPointsAndPlaces = result;
System.out.println(result.length);
allPointsAndPlaces = result;
loadAppPages();
}
};
// Make the call to the service.
getAllLocationsService.getLocations(callback);
}
void loadNewPoint(){
int r = Random.nextInt(allPointsAndPlaces.length);
quiz.CurrentPlace = allPointsAndPlaces[r].getPlaceName();
quiz.CurrentLocation = allPointsAndPlaces[r].getPlaceLocation();
quiz.point.setText(quiz.CurrentPlace);
quiz.location.setText(quiz.CurrentLocation);
quiz.location.setStyleName("invisibleText");
}
}
Child class:
public class PointsQuiz extends CabbieApp{
VerticalPanel mainPanel = new VerticalPanel();
HorizontalPanel navigation = new HorizontalPanel();
TextBox point = new TextBox();
TextBox location = new TextBox();
Button showLocation = new Button("Show Location");
Button nextPoint = new Button("Next Point");
String CurrentPlace, CurrentLocation;
public PointsQuiz() {
// Assemble Add Stock panel.
navigation.add(showLocation);
navigation.add(nextPoint);
navigation.setCellHorizontalAlignment(nextPoint, HasHorizontalAlignment.ALIGN_RIGHT);
navigation.addStyleName("addPanel");
mainPanel.setSpacing(5);
mainPanel.setStyleName("body");
mainPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
// Assemble Main panel.
mainPanel.add(point);
point.setWidth("200px");
mainPanel.add(location);
location.setWidth("200px");
mainPanel.add(navigation);
navigation.setWidth("200px");
// Move cursor focus to the input box.
showLocation.setFocus(true);
// Listen for mouse events on the show location button.
showLocation.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
showCurrentLocation();}
});
// Listen for mouse events on the next point button.
nextPoint.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
loadNewPoint();
}
});
}
private void showCurrentLocation(){
location.setStyleName("visibleText");
}
public VerticalPanel getMainPanel() {
return mainPanel;
}
}
I managed to find a solution to this problem with Bhumika's help.
To make this work I had to change CabbiePoint[] allPointsAndPlaces to static.
This would solve the reference problem one way - from child to parent.
Also I managed to find out trough debugging, that this reference
quiz = new PointsQuiz();
is also null on a second run of loadNewPoint(). So this child reference (PointsQuiz quiz;) and any other references to children were set also to static.
You are getting null pointer error because of allPointsAndPlaces is null. As per your coding The value of allPointsAndPlaces is assigned after completion of RPC call in getAllPointsAndLocations() method. so the allPointsAndPlaces has some assigned values.
Here you try to directly access loadNewPoint() method in child class. At a time, allPointsAndPlaces is not assigned.

GWT is making an unexpected event call

My code is below: I am seeing that on running the app the loadWidget method gets invoked even when the adminLink is not clicked. This is not want I want, but I'm not sure what is causing the issue. Please advise
public class LoginModule implements EntryPoint {
LoginPopup loginPopup;
private class LoginPopup extends PopupPanel {
public LoginPopup() {
super(true);
}
public void loadWidget(){
System.out.println("I am called 1");
CommonUi cUi = new CommonUi();
//#342 moved code to common area
FormPanel loginForm = cUi.getLoginFormUi();
setWidget(loginForm);
}
}
#Override
public void onModuleLoad() {
//#251 improved login popup ui.
final Anchor adminLink = new Anchor("User Login");
// final Label adminLink = new Label("User Login");
adminLink.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// Instantiate the popup and show it.
loginPopup = new LoginPopup();
loginPopup.loadWidget();
loginPopup.showRelativeTo(adminLink);
loginPopup.show();
}
});
if(RootPanel.get("admin") !=null)
RootPanel.get("admin").add(adminLink);
}
}
Running Dev Mode, set a breakpoint in that method in your Java IDE, and take a look at the current stack, what code is calling that method. If that is the only code in your app, then this only appears to be invokable from that onClick handlers, so it is a matter of figuring out why that is being invoked.