|
Re: Toggle command with state for every editor instance [message #1005790 is a reply to message #1005776] |
Mon, 28 January 2013 18:10 |
John Steele Messages: 50 Registered: January 2010 |
Member |
|
|
If I understand this correctly, you essentially want to refresh the state of the variable used by a command to determine whether or not a handler should be enabled each time an editor is activated. Is that somewhat correct? If so, you can use a source provider and property tester.
IWorkbenchPage page = ...;
//the active part
IWorkbenchPart active = page.getActivePart();
//adding a listener
IPartListener2 pl = new IPartListener2() {
public void partActivated(IWorkbenchPartReference ref)
System.out.println("Active: "+ref.getTitle());
}
... other listener methods ...
};
page.addPartListener(pl);
In the part activated method get your source provider:
(ISourceProviderService) PlatformUI.getWorkbench().getService(ISourceProviderService.class);
(ActiveEditorSourceProvider) sourceProviderService.getSourceProvider(ActiveEditorSourceProvider.ACTIVE_EDITOR);
sourceProvider.setActiveEditor (...);
Each time an editor is activated set the state of the source provider and fire a source changed event. This will cause all expressions using that specific source provider to re-evaluate. The property tester can then evaluate whether or not the command should be enabled/active or whatever else you want. Pretty straight forward.
public class ActiveEditorSourceProvider extends AbstractSourceProvider {
...
public void setActiveEditor (Editor editor) {
map.put(ACTIVE_EDITOR, editor);
fireSourceChanged(ISources.WORKBENCH, ACTIVE_EDITOR, editor);
}
}
Then the property tester will get called to test:
public class ActiveEditorPropertyTester extends PropertyTester {
@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
boolean result = false;
if (receiver instanceof Editor) {
if (PROP_SELECTION.equals(property)) {
Editor support = (Editor) receiver;
... // do your evaluation
}
}
Hope this helps or at least stirs up some ideas.
j
[Updated on: Mon, 28 January 2013 18:17] Report message to a moderator
|
|
|
Re: Toggle command with state for every editor instance [message #1005896 is a reply to message #1005790] |
Tue, 29 January 2013 09:02 |
Jan Krakora Messages: 477 Registered: December 2009 Location: Prague |
Senior Member |
|
|
Hi thanks for your answer. But this is not what I need to help with (if I understand you correctly ).
My problem is not about enabling/disabling handler. It's about command's state.
The command has its state, because it's toggle command.
When I have open more instances of my editor, the command should have its state paired with active editor, but unfortunately it has just one state.
So when I set focus to the first editor and toggle the command in toolbar and then set focus to the second editor, the command shouldn't be toggled,
but due to the fact it has just one state, it's toggled.
I just need a toggle command and menu contribution like this:
- menu contribution with that command is visibile only when my editor is active (done)
- when an instance of my editor (there could be more instance of it) is activated, the command says "hey, for you my state is false, so I'm toggled out"
- when another instance of my editor is activated it says "hey, for you my state is true, so I'm toggled in"
Problem is, that command has just one state and can't map different state for different editor instances.
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04897 seconds