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

Papyrus/Papyrus Developer Guide/How To Code Examples

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 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 ;
   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 
  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 ;
        IEditorPart activeEditor = container.getActiveEditor();

How to get the list of visible nested editors

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 ;
 List<IEditorPart> visibleEditors = container .getVisibleIEditorParts();


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:

org.eclipse.mdt.papyrus/trunk/examples/others/org.eclipse.papyrus.example.diagram.simplediagram

[To be completed]

More Examples

Back to the top