Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Command selected state
Command selected state [message #993118] Sun, 23 December 2012 12:43 Go to next message
Frank Benoit is currently offline Frank BenoitFriend
Messages: 51
Registered: July 2009
Member
Hi

when I create a checkbox style command ("Cmd1") which shall be visible in the menu bar and the toolbar, the state is not synchronized.

What do I miss, to get the "checked" state maintained by a single state only?

  

// Command decl
<commands xmi:id="_2EdVYEr2EeK2l8Ho1g9J8Q" elementId="e4test2.command.0" commandName="Cmd1" description="Cmd1 Description"/>

// Handler decl
<handlers xmi:id="_bGiy4Er1EeK2l8Ho1g9J8Q" elementId="e4test2.handler.0" contributionURI="bundleclass://E4Test2/e4test2.StdHandler" command="_2EdVYEr2EeK2l8Ho1g9J8Q"/>
  

// main toolbar decl
<trimBars xmi:id="_VOV6ANTAEeGTY-uOtVc6Mg">
      <children xsi:type="menu:ToolBar" xmi:id="toolbar:org.eclipse.ui.main.toolbar" elementId="">
        <children xsi:type="menu:HandledToolItem" xmi:id="_bcx2sEr3EeK2l8Ho1g9J8Q" elementId="e4test2.handleditem.cmd1" label="cmd1lbl" iconURI="platform:/plugin/E4Test2/icons/alt_window_16.gif" type="Check" command="_2EdVYEr2EeK2l8Ho1g9J8Q"/>
   
// main menu decl
    <mainMenu xmi:id="_ydmDwErzEeK2l8Ho1g9J8Q" elementId="e4test2.menu.0">
      <children xsi:type="menu:Menu" xmi:id="_wP9uAEz1EeKq5-u9QAXssw" elementId="e4test2.menu.file" label="File" mnemonics="">
        <children xsi:type="menu:HandledMenuItem" xmi:id="_3EQsUEz1EeKq5-u9QAXssw" elementId="org.eclipse.ui.file.exit" label="Exit" command="_MBOOcEz2EeKq5-u9QAXssw"/>
      </children>
      <children xsi:type="menu:Menu" xmi:id="_7W0uQErzEeK2l8Ho1g9J8Q" elementId="e4test2.menu.0" label="Example">
        <children xsi:type="menu:HandledMenuItem" xmi:id="_809Y0Er2EeK2l8Ho1g9J8Q" elementId="e4test2.handleditem.cmd1" selected="true" type="Check" command="_2EdVYEr2EeK2l8Ho1g9J8Q"/>
      </children>
    </mainMenu>


thanks
Frank
Re: Command selected state [message #994915 is a reply to message #993118] Fri, 28 December 2012 16:20 Go to previous messageGo to next message
Frank Benoit is currently offline Frank BenoitFriend
Messages: 51
Registered: July 2009
Member
Now I found a solution. But I want to use pure E4 functionality. I am not sure what is E4 and what is compatibility layer.

To what I found out.
For this i need to add "org.eclipse.ui.workbench" to the dependencies


private static final String TOGGLE_STATE = "org.eclipse.jface.commands.ToggleState";
private static final String CMD_ID = "my.command";
@Inject 
private ECommandService cmdServ;
private boolean isSelected;


Get the command object and make it have a ToggleState object.
Command cmd = cmdServ.getCommand(CMD_ID);
State state = cmd.getState(TOGGLE_STATE);
if( state == null ){
	state = new org.eclipse.jface.commands.ToggleState();
	cmd.addState(TOGGLE_STATE, state);
}


When the command is executed, toggle the state
Object value = state.getValue();
if( value == null ) value = false;
value = !(Boolean)value;
state.setValue(value);
isSelected = (Boolean)value;


and propagate the current state to all UI elements that show the state as well. E.g. Toolbar and Menu.
ICommandService cmdServ = (ICommandService) workbench.getActiveWorkbenchWindow().getService(ICommandService.class);
cmdServ.refreshElements(cmd.getId(), null);


The handler implements the interface IElementUpdater
@Override
public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
	element.setChecked(isSelected);
}

@Inject
public void updatePart( @Optional @Active MPart part, @Optional IWorkbench workbench ){
	// do initial update, and/or change the state depending on selected part (editor)
}


Is this the right way to do it in a E4 application or what is the right way?

thanks
Frank


Re: Command selected state [message #994964 is a reply to message #994915] Fri, 28 December 2012 18:34 Go to previous messageGo to next message
Marco Descher is currently offline Marco DescherFriend
Messages: 107
Registered: October 2010
Location: Austria
Senior Member
This is very interesting, I am currently trying to figure out a general solution for Check and Radio items, see http://www.eclipse.org/forums/index.php/t/444326/ - The problem with your solution is that you introduce Eclipse 3.x dependency; but you may seem right, as lot of the work seems yet not to be realized in Eclipse 4, e.g. there does not seem to be a HandlerUtil as presented in http://blog.eclipse-tips.com/2009/03/commands-part-6-toggle-radio-menu.html
Re: Command selected state [message #994973 is a reply to message #994964] Fri, 28 December 2012 19:01 Go to previous messageGo to next message
Frank Benoit is currently offline Frank BenoitFriend
Messages: 51
Registered: July 2009
Member
Now I found a E4 solution.

I mark the HandledMenuItem and HandledToolbarItem with "MyTag" tag.
When the event occurs, I get the handledItem injected, to get the selected state. The previously ToggleState is no more needed.

Then find all UI contribs with EModelService.findElements and update them with setSelected.

@Execute
public void run( @Active MPart part, @Active MWindow window, MHandledItem handledItem ){
	// handle the event ...

	// find the other UI contribs and update the checked state
	List<String> tags = new LinkedList<>();
	tags.add("MyTag");
		
	List<MHandledItem> elements = modelServ.findElements(window.getMainMenu(), null, MHandledItem.class, tags );
	elements.addAll( modelServ.findElements(window, null, MHandledItem.class, tags ) );
			
	for( MHandledItem hi : elements ){
		System.out.println("element : "+hi.getElementId());
		hi.setSelected(handledItem.isSelected());
	}
}


What is not nice about this are two things:

1. the tags should not be needed. Is there a way with just refering to the command ID?

2. the findElements does not include the main menu. Is this a bug?

thanks Frank

Re: Command selected state [message #995010 is a reply to message #994973] Fri, 28 December 2012 21:41 Go to previous messageGo to next message
Marco Descher is currently offline Marco DescherFriend
Messages: 107
Registered: October 2010
Location: Austria
Senior Member
Frank, I was thinking about the solution you presented! I see it the same, the tags should not be needed.

But one problem is not yet covered. How do you synchronize the state of your handler prior to a selection occuring? I was fiddling around with @PostConstruct but was not even capable of finding the resp. MMenuElements there! Although I think this is much too complicated. I would think something like this could be a good solution:

Think of a DirectMenuItem of CHECK style and the following class being referenced using class Uri:

@PostConstruct
public void initialize(MDirectMenuItem me) {
   // cover initialization stuff, sync it with code
   boolean currentlySelected = me.isSelected();
}

@Execute
public void execute(MDirectMenuItem me) {
   // do the stuff to handle it
   execute.blafoo();
   // toggle the state
   me.setSelected(!me.isSelected());
} 


The problem now is that @PostConstruct is not working. I see this as a bug. And generally I see detection of elements in the Application model to be somehow unreliable?!
Re: Command selected state [message #995178 is a reply to message #994973] Sat, 29 December 2012 09:27 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5462
Registered: July 2009
Senior Member
[...]

> 2. the findElements does not include the main menu. Is this a bug?
>

yes and no see https://bugs.eclipse.org/bugs/show_bug.cgi?id=383403
Previous Topic:Workbench Eventing
Next Topic:Close part programmatically
Goto Forum:
  


Current Time: Sat Nov 22 16:56:33 GMT 2014

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

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