Toolbar button in OutlineView using Command Framework and Menu Contributions? [message #333127] |
Wed, 26 November 2008 12:18 |
Per Mildner Messages: 9 Registered: July 2009 |
Junior Member |
|
|
I have MyEditor and MyOutlinePage and I want a button in the outline
view, similar to the usual Sort button, with a checked state
corresponing to some boolean property in the (model of the)
editor. Can this be done in Eclipse 3.4.1 using the new command
framework etc without resorting to the old Action based ways?
Except for one showstopper problem (see end) it kind
of works but not well enough, and not without far too much work.
I would very much appreciate if anyone could shed some light on this
issue.
Thanks.
Detailed questions follow:
I have encountered several problems:
I have a command with id "myCommandID", a handler implemented by the
class MyHandler which extends
org.eclipse.core.commands.AbstractHandler and implements
IElementUpdater and IExecutableExtension.
I can add a comand to the OutlineView
menuContribution
locationURI="toolbar:org.eclipse.ui.views.ContentOutline?after=additions "
command commandId="myCommandID"
style="toggle"
Question: what should I use for visibleWhen for the menu
constribution?
What I want is for it to be visible when the OutlineView is showing
an MyOutlinePage. The closest I could find was to use activeEditorId
but that is not perfect since this will hide the button if the
editor area is minimized.
Question: Similar for the handler, how do I specify its activeWhen?
Here too I use activeEditorId, even though that is not really
appropriate.
Question: Is there a (good) way to make the checked state reflect some
property of an underlying model where the model depends on which
MyOutlinePage the outline view is currently displaying? This problem
applies equally to any view toolbar command, I think.
My handler class implements IElementUpdater.updateElement() so it
can do element.setChecked(getFlag()) so the checked state reflects
the value (obtained by getFlag()) in the underlying model. This
works.
However, updateElement() needs to be called when either
a. The underlying model changes. All examples I have seen solves
this by assuming that the only way to change the model is through
the execute() method which then can do
commandService.refreshElements(event.getCommand().getId(),.. .);
This is neither sufficient nor necessary if there are other ways to
update the model. Instead the handler needs to listen for model
changes.
The problem then is that there is no way to request an update of the
handler, there is only a way to request update for any handler
associated with a certain command id. Of course, the handler does
not know or care about command ids (and I do not really want to
request an update for any other handlers of the command than this
particular handler-instance).
I solved this by letting MyHandler implement
IExecutableExtension.setInitializationData() where it gets a chance
to obtain and cache the command id. This works, but only since my
handler is instantiated from XML and it seems inelegant.
b. The outline view changes which MyOutlinePage it shows when
another editor is activated. I do not know how to force an update
when this happens. The closest I got was to force an update when the
active editor changes. I found no good way to track the active
editor and had to settle for a part listener installed by
updateElement() which seems overkill.
Question: How can I force updateElement() to be called when the
OutlineView changes the Page it shows?
All the above works, sort of, except for the final showstopper: The
buttons does not show if a MyEditor (and therefore a MyOutlinePage) is
active when Eclipse starts. This is especially strange since
MyHandler.updateElement() is called (twice) during Eclipse
startup. The button starts to show, and works as well as could be
expected given the above restrictions, if I first activates some other
non-MyEditor editor, or bring up a dialog.
I also did some experiments with
(IHandlerService) (getSite().getService(IHandlerService.class))
.activateHandler(myCommandID, new MyHandler(...), null,
true);
and also with myOutlinePage.getSite().getActionBars()
.getToolBarManager().update(true)
but the button still does not show initially.
There is nothing in the Error Log. I tried briefly with 3.5M3 but it
behaved the same.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02942 seconds