Skip to main content
Jump to: navigation, search

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

Line 77: Line 77:
  
 
</pre>  
 
</pre>  
=== How to get the list of visible nested editors<br> ===
+
=== 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:<br>  
 
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:<br>  

Revision as of 10:08, 21 June 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 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);
  } 
  

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

Copyright © Eclipse Foundation, Inc. All Rights Reserved.