Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Toggle command with state for every editor instance
Toggle command with state for every editor instance [message #1005776] Mon, 28 January 2013 17:02 Go to next message
Jan Krakora is currently offline Jan Krakora
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
Hi all,

I would like to have toggle command in main menu and in main toolbar. Following this tutorial i can easily create the command and contribute it to the menu.

This menu contribution is visible only when a particular editor is active.

The problem is, that when I have more instances of that editor, every instance must have its own state of that command. So the state isn't related to the command but to the editor instance.

How could I do that?

Thanks
Re: Toggle command with state for every editor instance [message #1005790 is a reply to message #1005776] Mon, 28 January 2013 18:10 Go to previous messageGo to next message
John Steele is currently offline John Steele
Messages: 49
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 Go to previous messageGo to next message
Jan Krakora is currently offline 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 Smile).

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.

Re: Toggle command with state for every editor instance [message #1005925 is a reply to message #1005896] Tue, 29 January 2013 10:22 Go to previous messageGo to next message
John Steele is currently offline John Steele
Messages: 49
Registered: January 2010
Member
How about making the menu dynamic, so that when each editor is activated a new set of menu items is loaded into the menu, which will have their own state, hence belonging to that particular editor?

[Updated on: Tue, 29 January 2013 10:24]

Report message to a moderator

Re: Toggle command with state for every editor instance [message #1005928 is a reply to message #1005925] Tue, 29 January 2013 10:36 Go to previous message
Jan Krakora is currently offline Jan Krakora
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
Sounds good, but I believe the dynamic menu is created when opened (main menu, context menu). So I don't know if it would work for the toolbar.
When the second editor instance is activated and the menu is already here, it isn't loaded? I could try that.

I can realize that by using retarget actions, but they are deprecated. I would expect some likewise simple solution also with commands.
Previous Topic:what's the mean of class parameter in handle
Next Topic:RCP EventBus?
Goto Forum:
  


Current Time: Wed Oct 22 06:46:03 GMT 2014

Powered by FUDForum. Page generated in 0.01816 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software