| <html> |
| |
| <head> |
| <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> |
| <title>Providing move functionality</title> |
| <link href="../book.css" rel="Stylesheet" type="text/css"> |
| <link href="../code.css" rel="Stylesheet" type="text/css"> |
| </head> |
| |
| <body> |
| |
| <h1>Providing Move Functionality</h1> |
| <h2>Create a Move Feature</h2> |
| <p>In our current editor the move functionality is already provided by the framework. |
| Nevertheless this tutorial should explain how the move behavior can be customized. |
| Therefore we constructed the following example behavior:</p> |
| <p>Moving EClasses should be restricted to EClasses whose name is longer than one |
| character (just an example without logical reason).</p> |
| <p>Another possibility would be to allow, that a EClass can be moved onto another |
| EClass (which is not allowed in the default implementation). This does not make |
| sense in our example, but in other scenarios this is a quite typical usage for a |
| move feature.</p> |
| <p>For that purpose we need a special move feature which is used by the editor if |
| a move gesture (a drag) is received as interaction. A move feature has to implement |
| the interface |
| <a href="../../../javadoc/org/eclipse/graphiti/features/IMoveFeature.html">IMoveFeature</a> |
| or one of the more specialized interfaces. Instead of implementing them directly |
| you should extend one of the available base classes. In this example we extend the |
| base class<a href="../../../javadoc/org/eclipse/graphiti/features/impl/DefaultMoveShapeFeature.html">DefaultMoveShapeFeature</a> |
| which implements the interface |
| <a href="../../../javadoc/org/eclipse/graphiti/features/IMoveShapeFeature.html"> |
| IMoveShapeFeature</a>.</p> |
| <p>In this case we only have to overwrite/implement one method:</p> |
| <p>The method |
| <a href="../../../javadoc/org/eclipse/graphiti/func/IMoveShape.html#canMoveShape(org.eclipse.graphiti.features.context.IMoveShapeContext)"> |
| canMoveShape</a> has to check whether the current pictogram element of the given |
| context can be moved to the given location. In this case the location information |
| contained in the context consists of the coordinates and the target container. With |
| this information it is also possible to realize a move behavior depending on the |
| target the user wants to drag to.</p> |
| <p>You can see the complete implementation of the move feature here:</p> |
| <p> </p> |
| <div class="literallayout"> |
| <div class="incode"> |
| <p class="code"><span class="keyword">package </span>org.eclipse.graphiti.examples.tutorial.features;<br> <br> |
| <span class="keyword">public class</span> TutorialMoveEClassFeature |
| <span class="keyword">extends </span>DefaultMoveShapeFeature {<br> <br> |
| <span class="keyword">public </span>TutorialMoveEClassFeature(IFeatureProvider |
| fp) {<br> |
| <span class="keyword">super</span>(fp);<br> }<br> <br> |
| @Override<br> <span class="keyword"> public boolean</span> |
| canMoveShape(IMoveShapeContext context) {<br> |
| <span class="keyword">boolean </span>canMove = <span class="keyword">super</span>.canMoveShape(context);<br> <br> |
| <span class="comment">// perform further check only if move allowed by default |
| feature</span><br> |
| <span class="keyword">if </span>(canMove) {<br> |
| <span class="comment">// don't allow move if the class name has the length |
| of 1</span><br> |
| Shape shape = context.getShape();<br> |
| Object bo = getBusinessObjectForPictogramElement(shape);<br> |
| <span class="keyword">if </span>(bo <span class="keyword">instanceof |
| </span>EClass) {<br> |
| EClass c = (EClass) bo;<br> |
| <span class="keyword">if </span>(c.getName() != <span class="keyword">null |
| </span>&& c.getName().length() == 1) {<br> |
| canMove = <span class="keyword">false</span>;<br> |
| }<br> |
| }<br> }<br> |
| <span class="keyword"> return</span> canMove;<br> |
| }<br>}<br></p> |
| </div> |
| </div> |
| <p> </p> |
| <p>Additionally the feature provider has to deliver our newly created feature (overwrite |
| the method |
| <a href="../../../javadoc/org/eclipse/graphiti/features/IFeatureProvider.html#getMoveShapeFeature(org.eclipse.graphiti.features.context.IMoveShapeContext)"> |
| getMoveShapeFeature</a>.</p> |
| <p>This implementation can be seen here:</p> |
| <p> </p> |
| <div class="literallayout"> |
| <div class="incode"> |
| <p class="code">@Override<br><span class="keyword">public </span>IMoveShapeFeature |
| getMoveShapeFeature(IMoveShapeContext context) {<br> Shape |
| shape = context.getShape();<br> Object bo = getBusinessObjectForPictogramElement(shape);<br> |
| <span class="keyword">if </span>(bo <span class="keyword">instanceof |
| </span>EClass) {<br> |
| <span class="keyword">return new</span> TutorialMoveEClassFeature(this);<br> |
| }<br> <span class="keyword">return super</span>.getMoveShapeFeature(context);<br> |
| } </p> |
| </div> |
| </div> |
| <p> </p> |
| <h2>Test: Move EClass Is Restricted</h2> |
| <p>Now start the editor again and test it: Just create a EClass whose name is only |
| one character long. Moving this EClass should not be possible. Create another EClass |
| whose name is longer than one character. Moving this EClass must still be possible. |
| </p> |
| |
| </body> |
| |
| </html> |