Thursday, December 20, 2012

Conditional menu contribution relying on java code

Context

Last few days I need to build a contextual menu on a TreeViewer provides by antother plugin. This TreeView containing EClass instance (comming from ecore and genmodel).

What I want to do is implement functions allowing  :
  • the create a new Resource containing a particular selected object
  • to absord the contend of a ressource containing a particular selected object
To build those functions, I rely on a provide popup menu (comming from EMF Client Plateform : ECP)

Developments

So I add :
  • command (extension org.eclipse.ui.commands)
  • menu entries using contributionItems (extension org.eclipse.ui.menus)
    • contribution item refers a command 
  • handler (extension org.eclipse.ui.handlers)
    • refers a command
    • describs "visiblewhen" using 
      • iterate
        • and
          • instanceof
          • test refers a propertyTester
      • count
  • propertyTester (extension org.eclipse.core.expressions.propertyTesters)

Plugin.xml fragment

here I just show how to detect that the selected "MyObject" is not already in a particular resource.

Command


<extension
         point="org.eclipse.ui.commands">
      <command
            id="franckys.gui.command.makeFragment"
            name="Make fragment">
      </command>
   </extension>

Contribution item


   <extension
         point="org.eclipse.ui.menus">
       <menuContribution
            allPopups="false"
            locationURI="popup:org.eclipse.emf.ecp.navigator.viewer?after=additions">
         <command
               commandId="franckys.gui.command.makeFragment"
               style="push">
         </command>
      </menuContribution>
   </extension>


handler

Here the expected behavior is obtain using "test" in java and "instanceof" checking.

   <extension point="org.eclipse.ui.handlers">
  <handler
   class="franckys.gui.handlers.MakeFragment"
   commandId="franckys.gui.command.makeFragment">
   <enabledWhen>
    <with variable="selection">
     <iterate>
      <and>
       <instanceof
       value="franckys.model.MyObject">
       </instanceof>
       <test
       property="franckys.isFragment"
       value="false">
       </test>
      </and>
     </iterate>
     <count
      value="1">
     </count>
    </with>
   </enabledWhen>
  </handler>
 </extension>


propertyTester


   <extension
         point="org.eclipse.core.expressions.propertyTesters">
      <propertyTester
            class="franckys.propertytester.IsFragmentTester"
            id="franckys.propertytester.isfragment"
            namespace="franckys"
            properties="isFragment"
            type="org.eclipse.emf.ecore.EObject">
      </propertyTester>
   </extension>


source code


propertyTester


import org.eclipse.core.expressions.PropertyTester;

import franckys.model.MyObject;

public class IsFragmentTester extends PropertyTester {

 public IsFragmentTester() {
 }

 @Override
 public boolean test(Object receiver, String property, Object[] args,
  Object expectedValue) {
 if (receiver instanceof MyObject) {
  MyObject myo = (MyObject) receiver;
  if("isFragment".equals(property)) {
   Boolean value = (Boolean)expectedValue;
   boolean bool = myo.eResource() == myo.eContainer().eResource();
   if(value.booleanValue()==false) {
    return bool;
   } else {
    return !bool;
   }
  }
 }
 return false;
 }
}


Handler


public class MakeFragment extends AbstractHandler {
 @Override
 public Object execute(ExecutionEvent event) throws ExecutionException {
 //Do the job in emf transaction
 }
}

No comments:

Post a Comment