| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| <html> |
| |
| <head> |
| <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> |
| <title>Providing add connection functionality</title> |
| <link href="../book.css" rel="Stylesheet" type="text/css"> |
| <link href="../code.css" rel="Stylesheet" type="text/css"> |
| </head> |
| |
| <body> |
| |
| <h1>Providing Add Connection Functionality</h1> |
| <p>In a previous chapter it was explained how an <a href="add-feature.htm">add feature</a> |
| is used to create graphical representations (in this case shapes with rectangles) |
| for existing business-objects (EClasses). </p> |
| <p>In this chapter the same functionality shall be provided for connections. Concretely, |
| an add feature for connections shall be implemented, which creates graphical representations |
| (connections with polylines) for existing business objects (EReferences).</p> |
| <p>An add connection feature has to implement the interface |
| <a href="../../../javadoc/org/eclipse/graphiti/features/IAddFeature.html">IAddFeature</a>. |
| Instead of implementing it directly it should extend one of the available base classes. |
| In this example we extend the base class |
| <a href="../../../javadoc/org/eclipse/graphiti/features/impl/AbstractAddFeature.html"> |
| AbstractAddFeature</a>.</p> |
| <p>In this case we have to implement/overwrite two methods:</p> |
| <ul> |
| <li>The method |
| <a href="../../../javadoc/org/eclipse/graphiti/func/IAdd.html#canAdd(org.eclipse.graphiti.features.context.IAddContext)"> |
| canAdd</a> has to check the given context and therefore it decides if a business |
| object can be added.</li> |
| <li>The method |
| <a href="../../../javadoc/org/eclipse/graphiti/func/IAdd.html#add(org.eclipse.graphiti.features.context.IAddContext)"> |
| add</a> finally has to create the pictogram element (connection) and has to |
| establish the linkage to the business object. Additionally we have to create |
| the graphics algorithm (polyline). </li> |
| </ul> |
| <p>You can see the complete implementation of the add connection feature here:</p> |
| <!-- Begin code ------------------------------------------------------------------------------- --> |
| <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> TutorialAddEReferenceFeature |
| <span class="keyword">extends </span>AbstractAddFeature {<br> <br> |
| <span class="keyword">public </span>TutorialAddEReferenceFeature (IFeatureProvider |
| fp) {<br> |
| <span class="keyword">super</span>(fp);<br> }<br> <br> |
| <span class="keyword">public </span>PictogramElement add(IAddContext context) |
| {<br> IAddConnectionContext addConContext |
| = (IAddConnectionContext) context;<br> |
| EReference addedEReference = (EReference) context.getNewObject();<br> |
| IPeCreateService peCreateService = Graphiti.getPeCreateService();<br> |
| <br> <span class="comment">// |
| CONNECTION WITH POLYLINE</span><br> |
| Connection connection = peCreateService<br> |
| .createFreeFormConnection(getDiagram());<br> |
| connection.setStart(addConContext.getSourceAnchor());<br> |
| connection.setEnd(addConContext.getTargetAnchor());<br> <br> |
| IGaService gaService = Graphiti.getGaService();<br> |
| Polyline polyline = gaService.createPolyline(connection);<br> |
| polyline.setLineWidth(2);<br> |
| polyline.setForeground(manageColor(IColorConstant.<span class="string"><em>BLACK</em></span>));<br> <br> |
| <span class="comment">// create link and wire it</span><br> |
| link(connection, addedEReference);<br> <br> |
| <span class="keyword">return </span>connection;<br> }<br> <br> |
| <span class="keyword"> public boolean</span> canAdd(IAddContext context) |
| {<br> <span class="comment">// |
| return true if given business object is an EReference<br> |
| // note, that the context must be an instance of IAddConnectionContext</span><br> |
| <span class="keyword">if</span> (context <span class="keyword">instanceof |
| </span>IAddConnectionContext<br> |
| && context.getNewObject() <span class="keyword">instanceof </span>EReference) |
| {<br> |
| <span class="keyword">return true</span>;<br> |
| }<br> <span class="keyword"> return |
| false</span>;<br> }<br>}<br></p> |
| </div> |
| </div> |
| <p> </p> |
| <!-- End code ------------------------------------------------------------------------------- --> |
| <p>Additionally the feature provider has to deliver our newly created feature (overwrite |
| the method |
| <a href="../../../javadoc/org/eclipse/graphiti/features/IFeatureProvider.html#getAddFeature(org.eclipse.graphiti.features.context.IAddContext)"> |
| getAdd</a>).</p> |
| <p>This implementation can be seen here:</p> |
| <!-- Begin code ------------------------------------------------------------------------------- --> |
| <p> </p> |
| <div class="literallayout"> |
| <div class="incode"> |
| <p class="code">@Override<br><span class="keyword">public </span>IAddFeature |
| getAddFeature(IAddContext context) {<br> |
| <span class="comment">// is object for add request a EClass or EReference?</span><br> |
| <span class="keyword">if </span>(context.getNewObject() |
| <span class="keyword">instanceof </span>EClass) {<br> |
| <span class="keyword">return new</span> TutorialAddEClassFeature(<span class="keyword">this</span>);<br> |
| } <span class="keyword">else if</span> (context.getNewObject() |
| <span class="keyword">instanceof </span>EReference) {<br> |
| <span class="keyword">return new</span> TutorialAddEReferenceFeature(<span class="keyword">this</span>);<br> |
| }<br> <span class="keyword">return super</span>.getAddFeature(context);<br> |
| } </p> |
| </div> |
| </div> |
| <p> </p> |
| <!-- End code ------------------------------------------------------------------------------- --> |
| <p>Before this new add connection feature can be tested, we have to |
| <a href="create-connection-feature.htm">provide create connection functionality</a> |
| as described in the next chapter.</p> |
| <p> </p> |
| |
| </body> |
| |
| </html> |