jpasch | d696dcd | 2011-07-21 12:36:20 +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 | adf11af | 2011-06-30 13:44:46 +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 direct editing functionality</title> |
jpasch | adf11af | 2011-06-30 13:44:46 +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 | adf11af | 2011-06-30 13:44:46 +0000 | [diff] [blame] | 11 | <body> |
mwenz | b5dc2f9 | 2010-06-16 13:24:52 +0000 | [diff] [blame] | 12 | |
jpasch | adf11af | 2011-06-30 13:44:46 +0000 | [diff] [blame] | 13 | <h1>Providing Direct Editing Functionality</h1> |
| 14 | <p>Direct editing means the possibility to change values directly in the graphical |
| 15 | editor. Technically the user clicks on a pictogram-element and an editor is shown, |
| 16 | where the user can change the values of this pictogram element.</p> |
| 17 | <p>A typical use case is, that the user clicks on a text (either in a shape or a |
| 18 | connection decorator) and then the text is overlaid with a text-edit-field, where |
| 19 | the user can change the text value. To the user this actually looks as if the text |
| 20 | is replaced with the text-edit-field.</p> |
| 21 | <p> </p> |
jpasch | da3428a | 2012-05-25 11:10:26 +0200 | [diff] [blame^] | 22 | <p><img alt="" height="160" src="visio/direct-editing.png" width="259"></p> |
jpasch | adf11af | 2011-06-30 13:44:46 +0000 | [diff] [blame] | 23 | <p><strong>Figure: Direct editing of a text</strong></p> |
jpasch | adf11af | 2011-06-30 13:44:46 +0000 | [diff] [blame] | 24 | <h2>Creating a Direct Editing Feature</h2> |
| 25 | <p>In this example we want to enable the users to edit the name of a EClass directly |
| 26 | in the diagram. Therefore we have to create a direct editing feature and make it |
| 27 | available in the feature provider. A direct editing feature has to implement the |
jpasch | d696dcd | 2011-07-21 12:36:20 +0000 | [diff] [blame] | 28 | interface |
jpasch | adf11af | 2011-06-30 13:44:46 +0000 | [diff] [blame] | 29 | <a href="../../../javadoc/org/eclipse/graphiti/features/IDirectEditingFeature.html"> |
jpasch | d696dcd | 2011-07-21 12:36:20 +0000 | [diff] [blame] | 30 | IDirectEditingFeature</a>. Instead of implementing it directly it should extend |
| 31 | one of the available base classes. In this example we extend the base class |
jpasch | adf11af | 2011-06-30 13:44:46 +0000 | [diff] [blame] | 32 | <a href="../../../javadoc/org/eclipse/graphiti/features/impl/AbstractDirectEditingFeature.html"> |
jpasch | d696dcd | 2011-07-21 12:36:20 +0000 | [diff] [blame] | 33 | AbstractDirectEditingFeature</a>.</p> |
jpasch | adf11af | 2011-06-30 13:44:46 +0000 | [diff] [blame] | 34 | <p> </p> |
| 35 | <p>In this case we have to implement/overwrite several methods:</p> |
| 36 | <ul> |
| 37 | <li>The method |
| 38 | <a href="../../../javadoc/org/eclipse/graphiti/func/IDirectEditing.html#getEditingType()"> |
| 39 | getEditingType</a> has to return the editor type which shall be used to edit |
| 40 | the value, in this example a text editor.</li> |
| 41 | <li>The method |
| 42 | <a href="../../../javadoc/org/eclipse/graphiti/func/IDirectEditing.html#canDirectEdit(org.eclipse.graphiti.features.context.IDirectEditingContext)"> |
| 43 | canDirectEdit</a> has to check the given context and therefore it decides if |
| 44 | direct editing is supported.</li> |
| 45 | <li>The method |
| 46 | <a href="../../../javadoc/org/eclipse/graphiti/func/IDirectEditing.html#getInitialValue(org.eclipse.graphiti.features.context.IDirectEditingContext)"> |
| 47 | getInitialValue</a> has to return the initial value with which the editor is |
| 48 | initialized, which is usually the currently displayed value.</li> |
| 49 | <li>The method |
| 50 | <a href="../../../javadoc/org/eclipse/graphiti/func/IDirectEditing.html#checkValueValid(java.lang.String, org.eclipse.graphiti.features.context.IDirectEditingContext)"> |
| 51 | checkValueValid</a> performs a check of the current editor value on each value |
| 52 | change.</li> |
| 53 | <li>The method |
| 54 | <a href="../../../javadoc/org/eclipse/graphiti/func/IDirectEditing.html#setValue(java.lang.String, org.eclipse.graphiti.features.context.IDirectEditingContext)"> |
| 55 | setValue</a> has to set the edited value to the model at the end of the editing |
| 56 | process.</li> |
| 57 | </ul> |
| 58 | <p>You can see the complete implementation of the direct editing feature here:</p> |
| 59 | <p> </p> |
jpasch | d696dcd | 2011-07-21 12:36:20 +0000 | [diff] [blame] | 60 | <!-- Begin code ------------------------------------------------------------------------------- --> |
jpasch | adf11af | 2011-06-30 13:44:46 +0000 | [diff] [blame] | 61 | <div class="literallayout"> |
| 62 | <div class="incode"> |
| 63 | <p class="code"><span class="keyword">package </span>org.eclipse.graphiti.examples.tutorial.features;<br> <br> |
| 64 | <span class="keyword">public class</span> TutorialDirectEditEClassFeature |
| 65 | <span class="keyword">extends</span><br> AbstractDirectEditingFeature |
| 66 | {<br> <br> <span class="keyword">public </span>TutorialDirectEditEClassFeature(IFeatureProvider |
| 67 | fp) {<br> |
| 68 | <span class="keyword">super</span>(fp);<br> }<br> <br> |
| 69 | <span class="keyword">public int</span> getEditingType() {<br> |
| 70 | <span class="comment">// there are several possible editor-types supported:<br> |
| 71 | // text-field, checkbox, color-chooser, combobox, ...</span><br> |
| 72 | <span class="keyword">return </span><span class="string"><em>TYPE_TEXT</em></span>;<br> |
| 73 | }<br> <br> @Override<br> |
| 74 | <span class="keyword">public boolean</span> canDirectEdit(IDirectEditingContext |
| 75 | context) {<br> PictogramElement |
| 76 | pe = context.getPictogramElement();<br> |
| 77 | Object bo = getBusinessObjectForPictogramElement(pe);<br> |
| 78 | GraphicsAlgorithm ga = context.getGraphicsAlgorithm();<br> |
| 79 | <span class="comment">// support direct editing, if it is a EClass, and |
| 80 | the user clicked<br> // directly |
| 81 | on the text and not somewhere else in the rectangle</span><br> |
| 82 | <span class="keyword">if</span> (bo <span class="keyword">instanceof |
| 83 | </span>EClass && ga <span class="keyword">instanceof </span>Text) {<br> |
| 84 | <span class="keyword">return true</span>;<br> |
| 85 | }<br> <span class="comment">// |
| 86 | direct editing not supported in all other cases</span><br> |
| 87 | <span class="keyword"> return false</span>;<br> }<br> <br> |
| 88 | <span class="keyword">public </span>String getInitialValue(IDirectEditingContext |
| 89 | context) {<br> |
| 90 | <span class="comment">// return the current name of the EClass</span><br> |
| 91 | PictogramElement pe = context.getPictogramElement();<br> |
| 92 | EClass eClass = (EClass) getBusinessObjectForPictogramElement(pe);<br> |
| 93 | <span class="keyword">return </span>eClass.getName();<br> |
| 94 | }<br> <br> @Override<br> |
| 95 | <span class="keyword">public </span>String checkValueValid(String value, |
| 96 | IDirectEditingContext context) {<br> |
| 97 | <span class="keyword">if</span> (value.length() < 1)<br> |
| 98 | <span class="keyword">return </span><span class="string">"Please enter any |
| 99 | text as class name."</span>;<br> |
| 100 | <span class="keyword">if</span> (value.contains(<span class="string">" "</span>))<br> |
| 101 | <span class="keyword">return </span><span class="string">"Spaces are not |
| 102 | allowed in class names."</span>;<br> |
| 103 | <span class="keyword">if</span> (value.contains(<span class="string">"\n"</span>))<br> |
| 104 | <span class="keyword">return </span><span class="string">"Line breakes are |
| 105 | not allowed in class names."</span>;<br> <br> |
| 106 | <span class="comment">// null means, that the value is valid</span><br> |
| 107 | <span class="keyword">return null</span>;<br> }<br> <br> |
| 108 | <span class="keyword">public </span>void setValue(String value, IDirectEditingContext |
| 109 | context) {<br> |
jpasch | b3fc719 | 2011-08-18 11:16:33 +0200 | [diff] [blame] | 110 | <span class="comment">// set the new name for the EClass</span><br> |
jpasch | adf11af | 2011-06-30 13:44:46 +0000 | [diff] [blame] | 111 | PictogramElement pe = context.getPictogramElement();<br> |
| 112 | EClass eClass = (EClass) getBusinessObjectForPictogramElement(pe);<br> |
| 113 | eClass.setName(value);<br> <br> |
| 114 | <span class="comment">// Explicitly update the shape to display the new |
| 115 | value in the diagram<br> // Note, |
| 116 | that this might not be necessary in future versions of Graphiti<br> |
| 117 | // (currently in discussion)</span><br> <br> |
| 118 | <span class="comment">// we know, that pe is the Shape of the Text, so its |
| 119 | container is the<br> // main shape |
| 120 | of the EClass</span><br> updatePictogramElement(((Shape) |
| 121 | pe).getContainer());<br> }<br>}<br></p> |
| 122 | </div> |
mwenz | b5dc2f9 | 2010-06-16 13:24:52 +0000 | [diff] [blame] | 123 | </div> |
jpasch | adf11af | 2011-06-30 13:44:46 +0000 | [diff] [blame] | 124 | <p> </p> |
jpasch | d696dcd | 2011-07-21 12:36:20 +0000 | [diff] [blame] | 125 | <!-- End code ------------------------------------------------------------------------------- --> |
jpasch | adf11af | 2011-06-30 13:44:46 +0000 | [diff] [blame] | 126 | <p>Additionally the feature provider has to deliver our newly created feature (overwrite |
| 127 | the method |
| 128 | <a href="../../../javadoc/org/eclipse/graphiti/features/IFeatureProvider.html#getDirectEditingFeature(org.eclipse.graphiti.features.context.IDirectEditingContext)"> |
| 129 | getDirectEditingFeature</a>). This implementation can be seen here: </p> |
jpasch | d696dcd | 2011-07-21 12:36:20 +0000 | [diff] [blame] | 130 | <!-- Begin code ------------------------------------------------------------------------------- --> |
jpasch | adf11af | 2011-06-30 13:44:46 +0000 | [diff] [blame] | 131 | <p> </p> |
| 132 | <div class="literallayout"> |
| 133 | <div class="incode"> |
| 134 | <p class="code">@Override<br><span class="keyword">public </span>IDirectEditingFeature |
| 135 | getDirectEditingFeature(<br> IDirectEditingContext context) |
| 136 | {<br> PictogramElement pe = context.getPictogramElement();<br> |
| 137 | Object bo = getBusinessObjectForPictogramElement(pe);<br> |
| 138 | <span class="keyword">if</span> (bo <span class="keyword">instanceof |
| 139 | </span>EClass) {<br> |
| 140 | <span class="keyword">return new</span> TutorialDirectEditEClassFeature(<span class="keyword">this</span>);<br> |
| 141 | }<br> <span class="keyword">return super</span>.getDirectEditingFeature(context);<br> |
| 142 | } </p> |
| 143 | </div> |
| 144 | </div> |
| 145 | <p> </p> |
jpasch | d696dcd | 2011-07-21 12:36:20 +0000 | [diff] [blame] | 146 | <!-- End code ------------------------------------------------------------------------------- --> |
| 147 | <h2>Test: Edit the Name of a Class Directly In the Diagram</h2> |
jpasch | adf11af | 2011-06-30 13:44:46 +0000 | [diff] [blame] | 148 | <p>Now start the editor and test this new direct editing feature:</p> |
| 149 | <ol> |
| 150 | <li>Create or open a diagram and create an EClass "Address"</li> |
| 151 | <li>Click on the class name and the value "Address" should become editable in |
| 152 | the text-editor</li> |
| 153 | <li>Change the value to "Customer Address". Now the editor should be highlighted |
| 154 | and an error-message ("no space allowed") should be displayed in the status-bar.</li> |
| 155 | <li>Change the value to "CustomerAddress". The highlight and error-message should |
| 156 | disappear. </li> |
| 157 | <li>Press return or make the editor loose focus otherwise to overtake the edited |
| 158 | value, or press ESC to cancel the editing.</li> |
| 159 | </ol> |
jpasch | d696dcd | 2011-07-21 12:36:20 +0000 | [diff] [blame] | 160 | <p> </p> |
mwenz | b5dc2f9 | 2010-06-16 13:24:52 +0000 | [diff] [blame] | 161 | |
| 162 | </body> |
| 163 | |
| 164 | </html> |