jpasch | 474ddbb | 2011-07-21 13:58:10 +0000 | [diff] [blame^] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <html> |
mwenz | b5dc2f9 | 2010-06-16 13:24:52 +0000 | [diff] [blame] | 3 | |
| 4 | <head> |
jpasch | 79955aa | 2011-06-29 08:04:06 +0000 | [diff] [blame] | 5 | <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> |
mwenz | b5dc2f9 | 2010-06-16 13:24:52 +0000 | [diff] [blame] | 6 | <title>Providing move functionality</title> |
jpasch | 79955aa | 2011-06-29 08:04:06 +0000 | [diff] [blame] | 7 | <link href="../book.css" rel="Stylesheet" type="text/css"> |
| 8 | <link href="../code.css" rel="Stylesheet" type="text/css"> |
mwenz | b5dc2f9 | 2010-06-16 13:24:52 +0000 | [diff] [blame] | 9 | </head> |
| 10 | |
jpasch | 79955aa | 2011-06-29 08:04:06 +0000 | [diff] [blame] | 11 | <body> |
mwenz | b5dc2f9 | 2010-06-16 13:24:52 +0000 | [diff] [blame] | 12 | |
jpasch | 79955aa | 2011-06-29 08:04:06 +0000 | [diff] [blame] | 13 | <h1>Providing Move Functionality</h1> |
| 14 | <h2>Create a Move Feature</h2> |
| 15 | <p>In our current editor the move functionality is already provided by the framework. |
| 16 | Nevertheless this tutorial should explain how the move behavior can be customized. |
| 17 | Therefore we constructed the following example behavior:</p> |
| 18 | <p>Moving EClasses should be restricted to EClasses whose name is longer than one |
| 19 | character (just an example without logical reason).</p> |
| 20 | <p>Another possibility would be to allow, that a EClass can be moved onto another |
| 21 | EClass (which is not allowed in the default implementation). This does not make |
| 22 | sense in our example, but in other scenarios this is a quite typical usage for a |
| 23 | move feature.</p> |
| 24 | <p>For that purpose we need a special move feature which is used by the editor if |
| 25 | a move gesture (a drag) is received as interaction. A move feature has to implement |
| 26 | the interface |
| 27 | <a href="../../../javadoc/org/eclipse/graphiti/features/IMoveFeature.html">IMoveFeature</a> |
| 28 | or one of the more specialized interfaces. Instead of implementing them directly |
| 29 | you should extend one of the available base classes. In this example we extend the |
jpasch | 474ddbb | 2011-07-21 13:58:10 +0000 | [diff] [blame^] | 30 | base class <a href="../../../javadoc/org/eclipse/graphiti/features/impl/DefaultMoveShapeFeature.html">DefaultMoveShapeFeature</a> |
jpasch | 79955aa | 2011-06-29 08:04:06 +0000 | [diff] [blame] | 31 | which implements the interface |
| 32 | <a href="../../../javadoc/org/eclipse/graphiti/features/IMoveShapeFeature.html"> |
| 33 | IMoveShapeFeature</a>.</p> |
| 34 | <p>In this case we only have to overwrite/implement one method:</p> |
jpasch | 474ddbb | 2011-07-21 13:58:10 +0000 | [diff] [blame^] | 35 | <ul> |
| 36 | <li>The method |
| 37 | <a href="../../../javadoc/org/eclipse/graphiti/func/IMoveShape.html#canMoveShape(org.eclipse.graphiti.features.context.IMoveShapeContext)"> |
| 38 | canMoveShape</a> has to check whether the current pictogram element of the given |
| 39 | context can be moved to the given location. In this case the location information |
| 40 | contained in the context consists of the coordinates and the target container. |
| 41 | With this information it is also possible to realize a move behavior depending |
| 42 | on the target the user wants to drag to.</li> |
| 43 | </ul> |
jpasch | 79955aa | 2011-06-29 08:04:06 +0000 | [diff] [blame] | 44 | <p>You can see the complete implementation of the move feature here:</p> |
jpasch | 474ddbb | 2011-07-21 13:58:10 +0000 | [diff] [blame^] | 45 | <!-- Begin code ------------------------------------------------------------------------------- --> |
jpasch | 79955aa | 2011-06-29 08:04:06 +0000 | [diff] [blame] | 46 | <p> </p> |
| 47 | <div class="literallayout"> |
| 48 | <div class="incode"> |
| 49 | <p class="code"><span class="keyword">package </span>org.eclipse.graphiti.examples.tutorial.features;<br> <br> |
| 50 | <span class="keyword">public class</span> TutorialMoveEClassFeature |
| 51 | <span class="keyword">extends </span>DefaultMoveShapeFeature {<br> <br> |
| 52 | <span class="keyword">public </span>TutorialMoveEClassFeature(IFeatureProvider |
| 53 | fp) {<br> |
| 54 | <span class="keyword">super</span>(fp);<br> }<br> <br> |
| 55 | @Override<br> <span class="keyword"> public boolean</span> |
| 56 | canMoveShape(IMoveShapeContext context) {<br> |
| 57 | <span class="keyword">boolean </span>canMove = <span class="keyword">super</span>.canMoveShape(context);<br> <br> |
| 58 | <span class="comment">// perform further check only if move allowed by default |
| 59 | feature</span><br> |
| 60 | <span class="keyword">if </span>(canMove) {<br> |
| 61 | <span class="comment">// don't allow move if the class name has the length |
| 62 | of 1</span><br> |
| 63 | Shape shape = context.getShape();<br> |
| 64 | Object bo = getBusinessObjectForPictogramElement(shape);<br> |
| 65 | <span class="keyword">if </span>(bo <span class="keyword">instanceof |
| 66 | </span>EClass) {<br> |
| 67 | EClass c = (EClass) bo;<br> |
| 68 | <span class="keyword">if </span>(c.getName() != <span class="keyword">null |
| 69 | </span>&& c.getName().length() == 1) {<br> |
| 70 | canMove = <span class="keyword">false</span>;<br> |
| 71 | }<br> |
| 72 | }<br> }<br> |
| 73 | <span class="keyword"> return</span> canMove;<br> |
| 74 | }<br>}<br></p> |
| 75 | </div> |
mwenz | b5dc2f9 | 2010-06-16 13:24:52 +0000 | [diff] [blame] | 76 | </div> |
jpasch | 79955aa | 2011-06-29 08:04:06 +0000 | [diff] [blame] | 77 | <p> </p> |
jpasch | 474ddbb | 2011-07-21 13:58:10 +0000 | [diff] [blame^] | 78 | <!-- End code ------------------------------------------------------------------------------- --> |
jpasch | 79955aa | 2011-06-29 08:04:06 +0000 | [diff] [blame] | 79 | <p>Additionally the feature provider has to deliver our newly created feature (overwrite |
| 80 | the method |
| 81 | <a href="../../../javadoc/org/eclipse/graphiti/features/IFeatureProvider.html#getMoveShapeFeature(org.eclipse.graphiti.features.context.IMoveShapeContext)"> |
| 82 | getMoveShapeFeature</a>.</p> |
| 83 | <p>This implementation can be seen here:</p> |
jpasch | 474ddbb | 2011-07-21 13:58:10 +0000 | [diff] [blame^] | 84 | <!-- Begin code ------------------------------------------------------------------------------- --> |
jpasch | 79955aa | 2011-06-29 08:04:06 +0000 | [diff] [blame] | 85 | <p> </p> |
| 86 | <div class="literallayout"> |
| 87 | <div class="incode"> |
| 88 | <p class="code">@Override<br><span class="keyword">public </span>IMoveShapeFeature |
| 89 | getMoveShapeFeature(IMoveShapeContext context) {<br> Shape |
| 90 | shape = context.getShape();<br> Object bo = getBusinessObjectForPictogramElement(shape);<br> |
| 91 | <span class="keyword">if </span>(bo <span class="keyword">instanceof |
| 92 | </span>EClass) {<br> |
| 93 | <span class="keyword">return new</span> TutorialMoveEClassFeature(this);<br> |
| 94 | }<br> <span class="keyword">return super</span>.getMoveShapeFeature(context);<br> |
| 95 | } </p> |
| 96 | </div> |
| 97 | </div> |
| 98 | <p> </p> |
jpasch | 474ddbb | 2011-07-21 13:58:10 +0000 | [diff] [blame^] | 99 | <!-- End code ------------------------------------------------------------------------------- --> |
jpasch | 79955aa | 2011-06-29 08:04:06 +0000 | [diff] [blame] | 100 | <h2>Test: Move EClass Is Restricted</h2> |
| 101 | <p>Now start the editor again and test it: Just create a EClass whose name is only |
| 102 | one character long. Moving this EClass should not be possible. Create another EClass |
| 103 | whose name is longer than one character. Moving this EClass must still be possible. |
| 104 | </p> |
jpasch | 474ddbb | 2011-07-21 13:58:10 +0000 | [diff] [blame^] | 105 | <p> </p> |
mwenz | b5dc2f9 | 2010-06-16 13:24:52 +0000 | [diff] [blame] | 106 | |
| 107 | </body> |
| 108 | |
| 109 | </html> |