| <!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 create functionality</title> |
| <link href="../book.css" rel="Stylesheet" type="text/css"> |
| <link href="../code.css" rel="Stylesheet" type="text/css"> |
| </head> |
| |
| <body> |
| |
| <h1>Providing Create Functionality</h1> |
| <h2>Creating a Create Feature</h2> |
| <p>Usually a new business object is created graphically by using the corresponding |
| creation tool from the editor’s tool palette. When using Graphiti you only have |
| to provide a so called create feature. The integration into the platform’s UI is |
| done by the framework.</p> |
| <p>A create feature has to implement the interface |
| <a href="../../../javadoc/org/eclipse/graphiti/features/ICreateFeature.html">ICreateFeature</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/AbstractCreateFeature.html"> |
| AbstractCreateFeature</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/ICreate.html#canCreate(org.eclipse.graphiti.features.context.ICreateContext)"> |
| canCreate</a> has to check whether the business object can be created for the |
| given context. </li> |
| <li>The method |
| <a href="../../../javadoc/org/eclipse/graphiti/func/ICreate.html#create(org.eclipse.graphiti.features.context.ICreateContext)"> |
| create</a> has to create the business object and also has to add the graphical |
| representation of the newly created business object to the diagram. The creation |
| of the business object must be implemented here. For adding the graphical representation, |
| the existing add feature should be used (see <a href="add-feature.htm">providing |
| add functionality</a>). </li> |
| </ul> |
| <p>In this example we want create a EClass via the palette. The creation should |
| be possible anywhere on the diagram. </p> |
| <p>You can see the complete implementation of the create feature here: </p> |
| <!-- Begin code ------------------------------------------------------------------------------- --> |
| <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> TutorialCreateEClassFeature |
| <span class="keyword">extends </span>AbstractCreateFeature {<br> <br> |
| <span class="keyword">private static final</span> String |
| <span class="string"><em>TITLE</em> </span>= <span class="string">"Create |
| class"</span>;<br> <br> <span class="keyword"> private |
| static final</span> String <span class="string"><em>USER_QUESTION</em></span> |
| = <span class="string">"Enter new class name"</span>;<br> <br> |
| <span class="keyword">public </span>TutorialCreateEClassFeature(IFeatureProvider |
| fp) {<br> |
| <span class="comment">// set name and description of the creation feature</span><br> |
| <span class="keyword">super</span>(fp, "EClass", "Create EClass");<br> |
| }<br> <br> <span class="keyword"> public boolean</span> |
| canCreate(ICreateContext context) {<br> |
| <span class="keyword">return </span>context.getTargetContainer() |
| <span class="keyword">instanceof </span>Diagram;<br> }<br> <br> |
| <span class="keyword">public </span>Object[] create(ICreateContext context) |
| {<br> <span class="comment">// |
| ask user for EClass name</span><br> |
| String newClassName = ExampleUtil.askString(<span class="string"><em>TITLE</em></span>, |
| <span class="string"><em>USER_QUESTION</em></span>, <span class="string"> |
| ""</span>);<br> |
| <span class="keyword">if </span>(newClassName == null || newClassName.trim().length() |
| == 0) {<br> |
| <span class="keyword">return </span><span class="string"><em>EMPTY</em></span>;<br> |
| }<br> <br> |
| <span class="comment"> // create EClass</span><br> |
| EClass newClass = EcoreFactory.<span class="string"><em>eINSTANCE</em></span>.createEClass();<br> |
| <span class="comment">// Add model element to resource.<br> |
| // We add the model element to the resource of the diagram for<br> |
| // simplicity's sake. Normally, a customer would use its own<br> |
| // model persistence layer for storing the business model separately.</span><br> |
| getDiagram().eResource().getContents().add(newClass);<br> |
| newClass.setName(newClassName);<br> <br> |
| <span class="comment">// do the add</span><br> |
| addGraphicalRepresentation(context, newClass);<br> <br> |
| <span class="comment">// return newly created business object(s)</span><br> |
| <span class="keyword">return new</span> Object[] { newClass };<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#getCreateFeatures()"> |
| getCreateFeatures</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>ICreateFeature[] |
| getCreateFeatures() {<br> <span class="keyword">return new</span> |
| ICreateFeature[] { <span class="keyword">new </span>TutorialCreateEClassFeature(<span class="keyword">this</span>) |
| };<br>} </p> |
| </div> |
| </div> |
| <p> </p> |
| <!-- End code ------------------------------------------------------------------------------- --> |
| <h2>Test: Create a EClass</h2> |
| <p>Now start the editor again. "EClass" should be available in the palette of the |
| editor. It should be possible to create a EClass: Select the palette entry "EClass" |
| and then take the mouse and create size and location of the new object in the diagram. |
| Insert a name and press OK. The new EClass should be visible in the diagram.</p> |
| <h2>Correct Size of Created EClass</h2> |
| <p>As you can see the size of the created EClass is not correct. To correct this |
| it’s necessary to update the implementation of the add feature. The add method of |
| the add feature must use the given size and location. </p> |
| <p>The implementation of the changed add-method can be seen here:</p> |
| <!-- Begin code ------------------------------------------------------------------------------- --> |
| <p> </p> |
| <div class="literallayout"> |
| <div class="incode"> |
| <p class="code"><span class="keyword">public </span>PictogramElement add(IAddContext |
| context) {<br> EClass addedClass = (EClass) |
| context.getNewObject();<br> Diagram |
| targetDiagram = (Diagram) context.getTargetContainer();<br> <br> |
| <span class="comment">// CONTAINER SHAPE WITH ROUNDED RECTANGLE</span><br> |
| IPeCreateService peCreateService = Graphiti.getPeCreateService();<br> |
| ContainerShape containerShape =<br> |
| peCreateService.createContainerShape(targetDiagram, |
| <span class="keyword">true</span>);<br> <br> |
| <span class="comment">// check whether the context has a size (e.g. from |
| a create feature)<br> // otherwise define |
| a default size for the shape</span><br> |
| <span class="keyword">int </span>width = context.getWidth() <= 0 ? 100 : |
| context.getWidth();<br> |
| <span class="keyword">int </span>height = context.getHeight() <= 0 ? 50 |
| : context.getHeight();<br> <br> |
| <span class="comment">// ... EXISTING CODING COMES HERE ...</span><br> <br> |
| <span class="keyword">return </span>containerShape;<br> |
| }<br> </p> |
| </div> |
| </div> |
| <p> </p> |
| <!-- Begin code ------------------------------------------------------------------------------- --> |
| <p>Start the editor again and see if it works correctly.</p> |
| <p> </p> |
| |
| </body> |
| |
| </html> |