Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Capella/Tutorials/Extensibility/Add Menu

< Capella‎ | Tutorials
Revision as of 11:51, 13 February 2019 by Philippe.dul@thalesgroup.com (Talk) (Command Handler)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Example

An example importing elements from another DSL/metamodel to a Capella model with iterativity (based on diff/merge technology) APA to Capella Example

Extensions Points

org.eclipse.ui.commands

   <extension point="org.eclipse.ui.commands">
      
      <category
            id="org.polarsys.capella.xxxx.category1"
            name="Extension commands">
      </category>
      <command
            categoryId="org.polarsys.capella.xxxx.category1"
            description="Command 1"
            id="org.polarsys.capella.xxxx.command1"
            name="Command 1">
      </command>
   </extension>

org.eclipse.ui.handlers

   <extension point="org.eclipse.ui.handlers"> 
     <handler commandId="org.polarsys.capella.xxxx.command1"
            class="org.polarsys.capella.xxxx.handlers.Command1Handler">
     </handler>
   </extension>


org.eclipse.ui.menus


   <extension point="org.eclipse.ui.menu">
      <menuContribution locationURI="popup:org.eclipse.ui.popup.any?after=additions">
         
         <menu id="org.polarsys.capella.xxxx"
              icon="icons/full/etool16/plugin.gif"
              label="REC / RPL">

              <separator visible="true" name="mainCommands"/>
              <separator visible="true" name="additions"/>

         </menu>
      </menuContribution>


   </extension>

   <extension point="org.eclipse.ui.menus">
         
      <menuContribution locationURI="popup:org.polarsys.capella.xxxx?after=mainCommands">
         <command
               commandId="org.polarsys.capella.xxxx.command1"
               id="org.polarsys.capella.xxxx.command1"
               style="push">
               
               <visibleWhen
       		checkEnabled="false">
		   <with variable="activeMenuSelection">
		      <iterate
		           ifEmpty="false">
		         <or>
                           <!-- Enable on Diagram elements -->
                           <instanceof
                                 value="org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart">
                           </instanceof>

                           <!-- Enable on Model elements -->
                           <instanceof
                                 value="org.eclipse.emf.ecore.EObject">
                           </instanceof>

                           <!-- Enable on Aird file -->
                           <!--adapt type="org.eclipse.core.resources.IResource">
				 <test property="org.eclipse.core.resources.extension" value="aird" />
			   </adapt-->
                        </or>
		      </iterate>
		   </with>
		</visibleWhen>

         </command>
      </menuContribution>

   </extension>

org.eclipse.ui.commandImages


   <extension point="org.eclipse.ui.commandImages">
     <extension point="org.eclipse.ui.commandImages">
       
        <image commandId="org.polarsys.capella.xxxx.command1"
               icon="icons/full/etool16/add_element.gif">
        </image>

   </extension>

   </extension>


Command Handler

You can inherit this following class and create an AbstractReadWriteCommand where you perform model modifications.

import java.util.Collection;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;
import org.polarsys.capella.common.ef.command.ICommand;
import org.polarsys.capella.common.helpers.TransactionHelper;
import org.polarsys.capella.core.model.handler.helpers.CapellaAdapterHelper;

public abstract class CommandHandler extends AbstractHandler {

  protected abstract ICommand createCommand(Collection<EObject> selection);

  public Object execute(final ExecutionEvent event) throws ExecutionException {
    return execute(HandlerUtil.getCurrentSelection(event));
  }

  public Object execute(ISelection selection) {
    Collection<EObject> semanticElements = CapellaAdapterHelper
        .resolveSemanticObjects(((IStructuredSelection) selection).toList(), true);
    ICommand cmd = createCommand(semanticElements);
    TransactionHelper.getExecutionManager(semanticElements).execute(cmd);
    return null;
  }

}



public class Command1Handler extends CommandHandler {
	 
  @Override
  protected ICommand createCommand(final Collection<EObject> selection, IProgressMonitor progressMonitor) {
	 
    return new AbstractReadWriteCommand() {
	 
      @Override
      public void run() {
        System.out.println(selection.toString());
	 
        for (EObject object : selection) {
            if (object instanceof SystemFunction) {
              ((SystemFunction)object).setName("new name");
            }
        }
      }
	 
    };
  }
}	
 	

Back to the top