0

I have a command that I have added to the context menu of a view (which has a treeviewer) and the context menu of my custom editor.

In my handler is there any way for me to differentiate which context menu the command has been called from? This is because in the case of the view, I use something like this to get the data I need,

ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
 .getActivePage().getSelection();
 if (selection != null & selection instanceof IStructuredSelection)
 {
 IStructuredSelection strucSelection = (IStructuredSelection) selection;
.....

In the case of the editor, the selection is null, of course. So I added the following to handle the editor part,

IEditorPart editor = HandlerUtil.getActiveEditor(event);
IEditorInput input = editor.getEditorInput();
 IPath path = ((FileEditorInput)input).getPath();

But what happens is that even if I execute this command from my view, it always returns an active editor. Does this mean I have to write to separate handlers that will be active based on whether the view or the editor is in focus?

Thank you!

asked May 10, 2013 at 11:49
2
  • 1
    Please change HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection() to HandlerUtil.getCurrentSelectionChecked(event)... :-) Commented May 10, 2013 at 19:18
  • Oh thanks! Nice pointer! Commented May 10, 2013 at 23:43

2 Answers 2

1

Does this mean I have to write to separate handlers that will be active based on whether the view or the editor is in focus?

Yes it does. However, your separate handlers can be small classes that call a common class to do most of the work of your command. I don't know what data your command needs to function, but your separate handlers can prepare that data and pass it to the common class through one or more constructors.

answered May 10, 2013 at 12:44
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I just wanted to make sure there was no other way that I could have been missing.
I just need to get the path of the file in the active editor and the path of the file selected in my treeviewer.
0

You can have the same handler, and decide on the behaviour based on HandlerUtil.getActiveEditor(event) (which is only non-null when the active part is an editor) and HandlerUtil.getActiveView(event) (which is only non-null when the active part is a view).

Or you can let your handler implement IExecutableExtension and supply the method setInitializationData(IConfigurationElement config, String propertyName, Object data)' which is invoked when the handler is created. Heredatais usually given in theclassattribute of the handler declaration (see the Javadoc ofsetInitializationData`)...

In this case, I would prefer the former method, but I often use the later method whenever I cannot decide the behaviour at runtime.

answered May 11, 2013 at 8:40

6 Comments

I am not sure why but when I implemented the former method (as I have mentioned in my question), the getActiveEditor always returned an Editor even if a view was in focus.
I'll check. I'm very sure that getActiveView(...) only returns a view when the view is in fact active...
Yes the getActiveView is as you say; I am speak about the getActiveEditor. I get an Editor even when my view was in focus when I called the handler.
I can see that this is how it is coded... But I cannot explain the rationale behind this.
One may want the currently active 'file' aka the file on top, in the editor even from a view so that the view can perform operations on that file.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.