blob: 100912ca0fbe289a698e901da64d998ca4d5a759 [file] [log] [blame]
jpasch474ddbb2011-07-21 13:58:10 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html>
mwenzb5dc2f92010-06-16 13:24:52 +00003
4<head>
jpasch79955aa2011-06-29 08:04:06 +00005<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
mwenzb5dc2f92010-06-16 13:24:52 +00006<title>Providing move functionality</title>
jpasch79955aa2011-06-29 08:04:06 +00007<link href="../book.css" rel="Stylesheet" type="text/css">
8<link href="../code.css" rel="Stylesheet" type="text/css">
mwenzb5dc2f92010-06-16 13:24:52 +00009</head>
10
jpasch79955aa2011-06-29 08:04:06 +000011<body>
mwenzb5dc2f92010-06-16 13:24:52 +000012
jpasch79955aa2011-06-29 08:04:06 +000013<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.
16Nevertheless this tutorial should explain how the move behavior can be customized.
17Therefore we constructed the following example behavior:</p>
18<p>Moving EClasses should be restricted to EClasses whose name is longer than one
19character (just an example without logical reason).</p>
20<p>Another possibility would be to allow, that a EClass can be moved onto another
21EClass (which is not allowed in the default implementation). This does not make
22sense in our example, but in other scenarios this is a quite typical usage for a
23move feature.</p>
24<p>For that purpose we need a special move feature which is used by the editor if
25a move gesture (a drag) is received as interaction. A move feature has to implement
26the interface
27<a href="../../../javadoc/org/eclipse/graphiti/features/IMoveFeature.html">IMoveFeature</a>
28or one of the more specialized interfaces. Instead of implementing them directly
29you should extend one of the available base classes. In this example we extend the
jpasch474ddbb2011-07-21 13:58:10 +000030base class <a href="../../../javadoc/org/eclipse/graphiti/features/impl/DefaultMoveShapeFeature.html">DefaultMoveShapeFeature</a>
jpasch79955aa2011-06-29 08:04:06 +000031which implements the interface
32<a href="../../../javadoc/org/eclipse/graphiti/features/IMoveShapeFeature.html">
33IMoveShapeFeature</a>.</p>
34<p>In this case we only have to overwrite/implement one method:</p>
jpasch474ddbb2011-07-21 13:58:10 +000035<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>
jpasch79955aa2011-06-29 08:04:06 +000044<p>You can see the complete implementation of the move feature here:</p>
jpasch474ddbb2011-07-21 13:58:10 +000045<!-- Begin code ------------------------------------------------------------------------------- -->
jpasch79955aa2011-06-29 08:04:06 +000046<p>&nbsp;</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>&nbsp;<br>
50 <span class="keyword">public class</span> TutorialMoveEClassFeature
51 <span class="keyword">extends </span>DefaultMoveShapeFeature {<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;
52 <span class="keyword">public </span>TutorialMoveEClassFeature(IFeatureProvider
53 fp) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
54 <span class="keyword">super</span>(fp);<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;
55 @Override<br>&nbsp;&nbsp; <span class="keyword">&nbsp;public boolean</span>
56 canMoveShape(IMoveShapeContext context) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
57 <span class="keyword">boolean </span>canMove = <span class="keyword">super</span>.canMoveShape(context);<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
58 <span class="comment">// perform further check only if move allowed by default
59 feature</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
60 <span class="keyword">if </span>(canMove) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
61 <span class="comment">// don&#39;t allow move if the class name has the length
62 of 1</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
63 Shape shape = context.getShape();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
64 Object bo = getBusinessObjectForPictogramElement(shape);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
65 <span class="keyword">if </span>(bo <span class="keyword">instanceof
66 </span>EClass) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
67 EClass c = (EClass) bo;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
68 <span class="keyword">if </span>(c.getName() != <span class="keyword">null
69 </span>&amp;&amp; c.getName().length() == 1) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
70 canMove = <span class="keyword">false</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
71 }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
72 }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
73 <span class="keyword">&nbsp;return</span> canMove;<br>&nbsp;&nbsp;&nbsp;
74 }<br>}<br></p>
75 </div>
mwenzb5dc2f92010-06-16 13:24:52 +000076</div>
jpasch79955aa2011-06-29 08:04:06 +000077<p>&nbsp;</p>
jpasch474ddbb2011-07-21 13:58:10 +000078<!-- End code ------------------------------------------------------------------------------- -->
jpasch79955aa2011-06-29 08:04:06 +000079<p>Additionally the feature provider has to deliver our newly created feature (overwrite
80the method
81<a href="../../../javadoc/org/eclipse/graphiti/features/IFeatureProvider.html#getMoveShapeFeature(org.eclipse.graphiti.features.context.IMoveShapeContext)">
82getMoveShapeFeature</a>.</p>
83<p>This implementation can be seen here:</p>
jpasch474ddbb2011-07-21 13:58:10 +000084<!-- Begin code ------------------------------------------------------------------------------- -->
jpasch79955aa2011-06-29 08:04:06 +000085<p>&nbsp;</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>&nbsp;&nbsp;&nbsp; Shape
90 shape = context.getShape();<br>&nbsp;&nbsp;&nbsp; Object bo = getBusinessObjectForPictogramElement(shape);<br>&nbsp;&nbsp;&nbsp;
91 <span class="keyword">if </span>(bo <span class="keyword">instanceof
92 </span>EClass) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
93 <span class="keyword">return new</span> TutorialMoveEClassFeature(this);<br>&nbsp;&nbsp;&nbsp;
94 }<br>&nbsp;&nbsp;&nbsp; <span class="keyword">return super</span>.getMoveShapeFeature(context);<br>
95 } </p>
96 </div>
97</div>
98<p>&nbsp;</p>
jpasch474ddbb2011-07-21 13:58:10 +000099<!-- End code ------------------------------------------------------------------------------- -->
jpasch79955aa2011-06-29 08:04:06 +0000100<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
102one character long. Moving this EClass should not be possible. Create another EClass
103whose name is longer than one character. Moving this EClass must still be possible.
104</p>
jpasch474ddbb2011-07-21 13:58:10 +0000105<p>&nbsp;</p>
mwenzb5dc2f92010-06-16 13:24:52 +0000106
107</body>
108
109</html>