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

Difference between revisions of "Papyrus/Papyrus Developer Guide/How To Code Examples"

m (How to adapt a Graphical Object to underlying Domain (UML))
(How to adapt a Graphical Object to underlying Domain (UML): improve code)
Line 197: Line 197:
 
   if( obj instanceof IAdaptable ) {
 
   if( obj instanceof IAdaptable ) {
 
     NamedElement ele = (NamedElement)((IAdaptable)obj).getAdapter(NamedElement.class);
 
     NamedElement ele = (NamedElement)((IAdaptable)obj).getAdapter(NamedElement.class);
 +
 +
    // Adapt object to NamedElement
 +
    NamedElement ele = null;
 +
    if( obj instanceof IAdaptable ) {
 +
      ele = (NamedElement)((IAdaptable)obj).getAdapter(NamedElement.class);
 +
    }
 +
    if( ele == null) {
 +
      ele = (NamedElement)Platform.getAdapterManager().getAdapter(obj, NamedElement.class);
 +
    }
 +
 
   }
 
   }
  
Line 213: Line 223:
 
   for( Object obj : selections) {
 
   for( Object obj : selections) {
 
     // Adapt object to NamedElement
 
     // Adapt object to NamedElement
 +
    NamedElement ele = null;
 
     if( obj instanceof IAdaptable ) {
 
     if( obj instanceof IAdaptable ) {
       NamedElement ele = (NamedElement)((IAdaptable)obj).getAdapter(NamedElement.class);
+
       ele = (NamedElement)((IAdaptable)obj).getAdapter(NamedElement.class);
       if(ele != null) {
+
    }
        results.add(ele);
+
    if(ele == null) {
      }
+
       ele = (NamedElement)Platform.getAdapterManager().getAdapter(obj, NamedElement.class);
 +
    }
 +
    if(ele != null) {
 +
      results.add(ele);
 
     }
 
     }
 
   }
 
   }

Revision as of 21:03, 4 September 2011

Core Examples

This section contains code examples related to the 'core'

How to get the ServiceRegistry

The ServiceRegistry is a central point in Papyrus. It allows to get nearly all objects or services used by papyrus.

There is one and only one ServiceRegistry for each Papyrus Editor.

When you create a Diagram or a Service, Papyrus always provide the ServiceRegistry as an argument. You usually need to store it in a way you could easily retrieve it.

 import org.eclipse.papyrus.core.services.ServicesRegistry;

If you are implementaing a Command Handler, you can get the ServiceRegistry of the currently active Papyrus editor like that:

   ServicesRegistry serviceRegistry = ServiceUtilsForActionHandlers.getInstance().getServiceRegistry();

If you are in a GMF related stuff and you have the IDiagramEditDomain :

   IDiagramEditDomain domain = getIDiagramEditingDomain(); // 
   ServicesRegistry serviceRegistry = ServiceUtilsForGMF.getInstance().getServiceRegistry(domain);

If you have the Papyrus main editor part (IEditorPart):

 IEditorPart editorPart = getIEditorPart() // The Papyrus editor, not a nested editorPart
 ServicesRegistry serviceRegistry = (ServicesRegistry)editor.getAdapter(ServicesRegistry.class);


See also 'Papyrus Cookbook/How to get a Papyrus Service or the ServiceRegistry'

How to get a Well Known Service

Papyrus has some well known services that can be easily retrieved. To get one of these services, you should use one of the XxxServiceUtils class. You should use the right ServiceUtils class, according to what kind of object you have at disposal. Use one of the following:

  • org.eclipse.papyrus.core.utils.ServiceUtils
    • When you have the instance of the ServiceRegistry
  • org.eclipse.papyrus.diagram.common.util.ServiceUtilsForGMF
    • When you have a GMF EditPart
    • When you have a GMF EditPolicies
  • org.eclipse.papyrus.core.utils.ServiceUtilsForActionHandlers
    • from action handlers acting on the current active editor. This is the case for eclipse.ui.handler and similar code.

Examples - From the ServiceRegistry:

  ServicesRegistry serviceRegistry = getServiceRegistry(); // see How to get the ServiceRegistry
  TransactionalEditingDomain editingDomain = ServiceUtils.getInstance().getTransactionalEditingDomain(serviceRegistry);

Examples - From an Handler (and only from handler, check javadoc):

 TransactionalEditingDomain editingDomain = ServiceUtilsForActionHandlers.getInstance().getTransactionalEditingDomain();
 

Well Known Services

  • ServiceRegistry
    • Allow to get any service by its name
  • ModelSet
    • Service allowing to get API to access the underlying 'Models' used by Papyrus
  • TransactionalEditingDomain
    • EMF object used to perform transactions on the Models
  • IPageMngr
    • Service used to open, close, add pages in the SashWindow
  • ISashWindowsContainer
    • This service manage the nested editors.
  • ILifeCycleEventsProvider
    • Service providing events when a 'save' actions is required by the user.

How to be notified when active page change

The SashWindows system send events when the current active page change (i.e. the nested editor). It is possible to listen on this event.

Your listener should implement the following interface:

org.eclipse.papyrus.sasheditor.editor.IPageChangedListener

Then you need to register your listener to the sashWindowContainer:


   ISashWindowsContainer container = getISashWindowsContainer(); // see  How to get the ISashWindowsContainer;
   IPageChangedListener listener = ... // Get your listener
   container.addPageChangedListener(listener);


How to get the ISashWindowsContainer

The ISashWindowContainer allows to listen on page changed events, or to refresh the windows. There is one ISashWindowsContainer for each Papyrus editor.

It is possible to get it in different ways:

From the ServiceRegistry:

  ServicesRegistry serviceRegistry = getServiceRegistry(); // see How to get the ServiceRegistry
  ISashWindowsContainer container = ServiceUtils.getInstance().getISashWindowsContainer(serviceRegistry);

From an Handler (and only from handler, check javadoc):

 ISashWindowsContainer container = ServiceUtilsForActionHandlers.getInstance().getISashWindowsContainer();
 

From the Papyrus IEditorPart ():

	
        IEditorPart editorPart = getIEditorPart() // The Papyrus editor, not a nested editorPart
        ISashWindowsContainer container = (ISashWindowsContainer)editorPart.getAdapter(ISashWindowsContainer.class);

How to get the currently active nested editor

You can get the nested editor that is currently active from the ISashWindowContainer.

First, get the ISashWindowsContainer (see ).

Second get the Active nested editor:

        ISashWindowsContainer container = getISashWindowsContainer(); // see  How to get the ISashWindowsContainer;
        IEditorPart activeEditor = container.getActiveEditor();

How to get the list of visible nested editors

How to get the list of IEditorPart for the Opened Diagrams

A visible nested editor is one that is visible from the user. There can be several nested editors visible at one time. You can get the list of visible editors:

 ISashWindowsContainer container = getISashWindowsContainer(); // see  How to get the ISashWindowsContainer;
 List<IEditorPart> visibleEditors = container .getVisibleIEditorParts();

How to get the IPageMngr

The IPageMngr class allows to open, close, page in the sash window system. A page is generally a diagram. The IPageMngr also allows to list existing pages, or to check if a page is open. See the javadoc for more.

The IpageMngr can be retrieved from the ServiceRegistry:

 
 ServicesRegistry serviceRegistry = getServiceRegistry(); // see How to get the ServiceRegistry
 IPageMngr pageMngr = ServiceUtils.getInstance().getIPageMngr(serviceRegistry);

How to open/close a nested editors

Nested editors can be opened and closed programatically.

For that, you need to have the 'diagram identifier': this is the object that you provide when you open the diagram. The ID. For GMF diagrams, this is usually the notation.Diagram element used as the root of the diagram.

An instance of the class IPageMngr is used to open or close a diagram:

 
  IPageMngr pageMngr = getIPageMngr(); // see How to get the IPageMngr
 
  // Open all closed diagrams
  for( Object id : pageMngr.allPages() {
    if( ! pageMngr.isOpened(id)
      pageMngr.open(id);
  } 
  
  // Close all opened diagrams
  for( Object id : pageMngr.allPages() {
    if( pageMngr.isOpened(id)
      pageMngr.close(id);
  } 
  

How to Get the Current Selection from Java code

You can get the current selection inside the active nested editor by using the usual Eclipse way:

 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
 ISelection selection = page.getActiveEditor().getSite().getSelectionProvider().getSelection();

This will give you a Selection object containing a collection of Graphical Objects (ex: GMF class, Common Navigator class ...). If you want to get the associated "Domain" object (i.e. the UML objects), you should adapt your Graphical Object to a UML one (see #How to adapt a Graphical Object to underlying Domain (UML)). See the two next methods for example.


This first method return a list of selected elements:

 
  /**
   * Lookup selected objects in UI. 
   * @return
   */
  protected List<Object> lookupSelectedElements() {
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    ISelection selection = page.getSelection();
    if(selection instanceof IStructuredSelection) {
	IStructuredSelection structuredSelection = (IStructuredSelection)selection;
	return structuredSelection.toList();
      }
      else if( selection instanceof TreeSelection) {
	TreeSelection treeSelection = (TreeSelection)selection;
	return treeSelection.toList();
      }
    return null;
}

How to adapt a Graphical Object to underlying Domain (UML)

When you ask the current selection, you get the selected Graphical Object. Often, you really want the underlying domain object (i.e. the UML object). As there is several graphical technology used to in Papyrus (GMF, GEF, Common Navigator ...), you shouldn't test for the type of the selected object, but rather you should "adapt" it to the requested type.

 Object obj = ... // The Graphical Object
 if( obj instanceof IAdaptable ) {
   NamedElement ele = (NamedElement)((IAdaptable)obj).getAdapter(NamedElement.class);
   // Adapt object to NamedElement
   NamedElement ele = null;
   if( obj instanceof IAdaptable ) {
     ele = (NamedElement)((IAdaptable)obj).getAdapter(NamedElement.class);
   }
   if( ele == null) {
     ele = (NamedElement)Platform.getAdapterManager().getAdapter(obj, NamedElement.class);
   }
 }

The following method get the list of selections, adapt each selected element to get its underlying UML type, and return a list of the found UML types.

/**
 * Return a list of selected domain (UML) elements.
 * @return
 */
protected List<NamedElement> getSelectedUmlObject() {
  List<Object> selections = lookupSelectedElements(); // see "How to Get the Current Selection from Java code"
		
  List<NamedElement> results = new ArrayList<NamedElement>();

  // create model with EList<EObject> objects
  for( Object obj : selections) {
    // Adapt object to NamedElement
    NamedElement ele = null;
    if( obj instanceof IAdaptable ) {
      ele = (NamedElement)((IAdaptable)obj).getAdapter(NamedElement.class);
    }
    if(ele == null) {
      ele = (NamedElement)Platform.getAdapterManager().getAdapter(obj, NamedElement.class);
    }				
    if(ele != null) {
      results.add(ele);
    }
  }
  return results;
}

Diagram Examples

This section contains code example related to diagrams.

How to develop my own UML diagram

Papyrus allow to add new kind of UML diagrams. A plugin example with a doc can be found in the SVN:

[To be completed]

More Examples

Back to the top