| <html> |
| |
| <head> |
| <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> |
| <title>Providing a context menu</title> |
| <link href="../book.css" rel="Stylesheet" type="text/css"> |
| <link href="../code.css" rel="Stylesheet" type="text/css"> |
| </head> |
| |
| <body> |
| |
| <h1>Providing a Context Menu</h1> |
| <h2>Enhancing the Tool Behavior Provider</h2> |
| <p>Context menus are defined in the tool behavior provider.</p> |
| <p>If you didn’t do so already you must <strong>first create a tool behavior provider |
| and add it to the diagram type provider as described </strong> |
| <a href="tool-behavior-provider.htm"><strong>here</strong></a>.</p> |
| <p>There is one method of the tool behavior provider to overwrite: </p> |
| <p>The method |
| <a href="../../../javadoc/org/eclipse/graphiti/tb/IToolBehaviorProvider.html#getContextMenu(org.eclipse.graphiti.features.context.ICustomContext)"> |
| getContextMenu</a> has to return the context menu entries for the given context |
| (which implement |
| <a href="../../../javadoc/org/eclipse/graphiti/tb/IContextMenuEntry.html">IContextMenuEntry</a>)</p> |
| <p>The functionality of the context menu entries is always provided by features. |
| </p> |
| <p>It is possible to create a context menu entry with “children”, which build a |
| group of other context menu entries. Such a group can be shown in the context menu |
| either as a sub-menu or as a flat list separated by separator-lines. This behaviour |
| can be switched with a simple flag. You can also use the |
| <a href="../../../javadoc/org/eclipse/graphiti/tb/DynamicContextMenuEntry.html"> |
| DynamicContextMenuEntry</a>, which will automatically create a sub-menu, if there |
| are more than a predefined number of children, and a flat list otherwise.</p> |
| <p>In this example we want to create one context menu entry, which offers all available |
| <a href="custom-feature.htm">custom features</a> as menu entries in a new sub-menu. |
| </p> |
| <p>You can see the complete implementation of the context menu here:</p> |
| <!-- Begin code ------------------------------------------------------------------------------- --> |
| <p> </p> |
| <div class="literallayout"> |
| <div class="incode"> |
| <p class="code">@Override<br><span class="keyword">public</span> IContextMenuEntry[] |
| getContextMenu(ICustomContext context) {<br> |
| <span class="comment">// create a sub-menu for all custom features</span><br> |
| ContextMenuEntry subMenu = <span class="keyword">new</span> ContextMenuEntry(<span class="keyword">null</span>, |
| context);<br> subMenu.setText(<span class="string">"Custom"</span>);<br> |
| subMenu.setDescription(<span class="string">"Custom features submenu"</span>);<br> |
| <span class="comment">// display sub-menu hierarchical or flat</span><br> |
| subMenu.setSubmenu(<span class="keyword">true</span>);<br><br> |
| <span class="comment"> // create a menu-entry in the sub-menu for each |
| custom feature</span><br> ICustomFeature[] customFeatures |
| = getFeatureProvider().getCustomFeatures(context);<br> |
| <span class="keyword">for</span> (<span class="keyword">int</span> i = 0; |
| i < customFeatures.<span class="string">length</span>; i++) {<br> |
| ICustomFeature customFeature = customFeatures[i];<br> |
| <span class="keyword">if</span> (customFeature.isAvailable(context)) {<br> |
| ContextMenuEntry menuEntry = <span class="keyword">new</span> ContextMenuEntry(customFeature, |
| context);<br> |
| subMenu.add(menuEntry);<br> |
| }<br> }<br><br> IContextMenuEntry |
| ret[] =<span class="keyword"> new</span> IContextMenuEntry[] { subMenu };<br> |
| <span class="keyword">return</span> ret;<br>}</p> |
| </div> |
| </div> |
| <p> </p> |
| <!-- End code ------------------------------------------------------------------------------- --> |
| <h2>Test: Create a Connection by Drag & Drop from a Context Button</h2> |
| <p>Note that previously we implemented one "<a href="custom-feature.htm">Rename |
| EClass" feature</a> which allows renaming a EClass in a popup dialog.</p> |
| <p>Now start the editor and test this new context menu:</p> |
| <ol> |
| <li>Create or open a diagram and create a EClass in the diagram.</li> |
| <li>Open the context-menu on the diagram, and verify that the "Rename EClass" |
| feature is available but disabled in the custom sub-menu.</li> |
| <li>Open the context-menu on the EClass, and verify that the "Rename EClass" |
| feature is available and enabled in the custom sub-menu.</li> |
| </ol> |
| <p>Next you may change the hierarchical sub-menu to a flat sub-menu (change the |
| coding to <em>"subMenu.setSubmenu(false)</em>"). Start the editor and repeat the |
| test to view the difference in the context-menu.</p> |
| |
| </body> |
| |
| </html> |