Difference between revisions of "Papyrus/Papyrus Developer Guide/How To Code Examples"
(Add How to get a well known Service) |
|||
Line 28: | Line 28: | ||
<br> See also 'Papyrus Cookbook/How to get a Papyrus Service or the ServiceRegistry' | <br> 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: | ||
+ | |||
+ | *<pre>org.eclipse.papyrus.core.utils.ServiceUtils</pre> | ||
+ | **When you have the instance of the ServiceRegistry | ||
+ | *<pre>org.eclipse.papyrus.diagram.common.util.ServiceUtilsForGMF | ||
+ | </pre> | ||
+ | **When you have a GMF EditPart | ||
+ | **When you have a GMF EditPolicies | ||
+ | *<pre>org.eclipse.papyrus.core.utils.ServiceUtilsForActionHandlers | ||
+ | </pre> | ||
+ | **from action handlers acting on the current active editor. This is the case for eclipse.ui.handler and similar code. | ||
+ | |||
+ | Examples - From the ServiceRegistry:<br> | ||
+ | <pre> ServicesRegistry serviceRegistry = getServiceRegistry(); // see How to get the ServiceRegistry | ||
+ | TransactionalEditingDomain editingDomain = ServiceUtils.getInstance().getTransactionalEditingDomain(serviceRegistry); | ||
+ | </pre> | ||
+ | Examples - From an Handler (and only from handler, check javadoc):<br> | ||
+ | |||
+ | 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 === | === How to be notified when active page change === | ||
Line 77: | Line 114: | ||
</pre> | </pre> | ||
− | === How to get the list of visible nested editors === | + | === How to get the list of visible nested editors === |
+ | |||
=== How to get the list of IEditorPart for the Opened Diagrams === | === How to get the list of IEditorPart for the Opened Diagrams === | ||
Line 85: | Line 123: | ||
List<IEditorPart> visibleEditors = container .getVisibleIEditorParts(); | List<IEditorPart> visibleEditors = container .getVisibleIEditorParts(); | ||
− | === How to get the IPageMngr === | + | === 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 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: | + | The IpageMngr can be retrieved from the ServiceRegistry: |
<pre> | <pre> | ||
ServicesRegistry serviceRegistry = getServiceRegistry(); // see How to get the ServiceRegistry | ServicesRegistry serviceRegistry = getServiceRegistry(); // see How to get the ServiceRegistry | ||
IPageMngr pageMngr = ServiceUtils.getInstance().getIPageMngr(serviceRegistry); | IPageMngr pageMngr = ServiceUtils.getInstance().getIPageMngr(serviceRegistry); | ||
− | </pre> | + | </pre> |
− | + | === How to open/close a nested editors === | |
− | === How to open/close a nested editors === | + | |
− | Nested editors can be opened and closed programatically.<br> | + | Nested editors can be opened and closed programatically.<br> |
− | 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.<br> | + | 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.<br> |
− | An instance of the class IPageMngr is used to open or close a diagram:<br> | + | An instance of the class IPageMngr is used to open or close a diagram:<br> |
<pre> | <pre> | ||
IPageMngr pageMngr = getIPageMngr(); // see How to get the IPageMngr | IPageMngr pageMngr = getIPageMngr(); // see How to get the IPageMngr | ||
// Open all closed diagrams | // Open all closed diagrams | ||
− | for( Object id : pageMngr.allPages() { | + | for( Object id : pageMngr.allPages() { |
− | if( ! pageMngr.isOpened(id) | + | if( ! pageMngr.isOpened(id) |
pageMngr.open(id); | pageMngr.open(id); | ||
} | } | ||
// Close all opened diagrams | // Close all opened diagrams | ||
− | for( Object id : pageMngr.allPages() { | + | for( Object id : pageMngr.allPages() { |
if( pageMngr.isOpened(id) | if( pageMngr.isOpened(id) | ||
pageMngr.close(id); | pageMngr.close(id); | ||
} | } | ||
− | </pre> | + | </pre> |
− | + | ||
== Diagram Examples == | == Diagram Examples == | ||
Revision as of 07:57, 18 July 2011
Contents
- 1 Core Examples
- 1.1 How to get the ServiceRegistry
- 1.2 How to get a Well Known Service
- 1.3 How to be notified when active page change
- 1.4 How to get the ISashWindowsContainer
- 1.5 How to get the currently active nested editor
- 1.6 How to get the list of visible nested editors
- 1.7 How to get the list of IEditorPart for the Opened Diagrams
- 1.8 How to get the IPageMngr
- 1.9 How to open/close a nested editors
- 2 Diagram Examples
- 3 More 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 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); }
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
- The following folder contains more examples (sometime deprecated).
- Check the cookbook (PapyrusCookBook.odt)