blob: 1601f00198a07eab2d6425209fe1d44e0359983b [file] [log] [blame]
jpaschd696dcd2011-07-21 12:36:20 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html>
mwenzb5dc2f92010-06-16 13:24:52 +00003
4<head>
jpaschadf11af2011-06-30 13:44:46 +00005<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
mwenzb5dc2f92010-06-16 13:24:52 +00006<title>Providing direct editing functionality</title>
jpaschadf11af2011-06-30 13:44:46 +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
jpaschadf11af2011-06-30 13:44:46 +000011<body>
mwenzb5dc2f92010-06-16 13:24:52 +000012
jpaschadf11af2011-06-30 13:44:46 +000013<h1>Providing Direct Editing Functionality</h1>
14<p>Direct editing means the possibility to change values directly in the graphical
15editor. Technically the user clicks on a pictogram-element and an editor is shown,
16where 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
18connection decorator) and then the text is overlaid with a text-edit-field, where
19the user can change the text value. To the user this actually looks as if the text
20is replaced with the text-edit-field.</p>
21<p>&nbsp;</p>
jpaschda3428a2012-05-25 11:10:26 +020022<p><img alt="" height="160" src="visio/direct-editing.png" width="259"></p>
jpaschadf11af2011-06-30 13:44:46 +000023<p><strong>Figure: Direct editing of a text</strong></p>
jpaschadf11af2011-06-30 13:44:46 +000024<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
26in the diagram. Therefore we have to create a direct editing feature and make it
27available in the feature provider. A direct editing feature has to implement the
jpaschd696dcd2011-07-21 12:36:20 +000028interface
jpaschadf11af2011-06-30 13:44:46 +000029<a href="../../../javadoc/org/eclipse/graphiti/features/IDirectEditingFeature.html">
jpaschd696dcd2011-07-21 12:36:20 +000030IDirectEditingFeature</a>. Instead of implementing it directly it should extend
31one of the available base classes. In this example we extend the base class
jpaschadf11af2011-06-30 13:44:46 +000032<a href="../../../javadoc/org/eclipse/graphiti/features/impl/AbstractDirectEditingFeature.html">
jpaschd696dcd2011-07-21 12:36:20 +000033AbstractDirectEditingFeature</a>.</p>
jpaschadf11af2011-06-30 13:44:46 +000034<p>&nbsp;</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>&nbsp;</p>
jpaschd696dcd2011-07-21 12:36:20 +000060<!-- Begin code ------------------------------------------------------------------------------- -->
jpaschadf11af2011-06-30 13:44:46 +000061<div class="literallayout">
62 <div class="incode">
63 <p class="code"><span class="keyword">package </span>org.eclipse.graphiti.examples.tutorial.features;<br>&nbsp;<br>
64 <span class="keyword">public class</span> TutorialDirectEditEClassFeature
65 <span class="keyword">extends</span><br>&nbsp;&nbsp;&nbsp; AbstractDirectEditingFeature
66 {<br>&nbsp;<br>&nbsp;&nbsp;&nbsp; <span class="keyword">public </span>TutorialDirectEditEClassFeature(IFeatureProvider
67 fp) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
68 <span class="keyword">super</span>(fp);<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;
69 <span class="keyword">public int</span> getEditingType() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
70 <span class="comment">// there are several possible editor-types supported:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
71 // text-field, checkbox, color-chooser, combobox, ...</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
72 <span class="keyword">return </span><span class="string"><em>TYPE_TEXT</em></span>;<br>&nbsp;&nbsp;&nbsp;
73 }<br>&nbsp;<br>&nbsp;&nbsp;&nbsp; @Override<br>&nbsp;&nbsp;&nbsp;
74 <span class="keyword">public boolean</span> canDirectEdit(IDirectEditingContext
75 context) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PictogramElement
76 pe = context.getPictogramElement();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
77 Object bo = getBusinessObjectForPictogramElement(pe);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
78 GraphicsAlgorithm ga = context.getGraphicsAlgorithm();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
79 <span class="comment">// support direct editing, if it is a EClass, and
80 the user clicked<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // directly
81 on the text and not somewhere else in the rectangle</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
82 <span class="keyword">if</span> (bo <span class="keyword">instanceof
83 </span>EClass &amp;&amp; ga <span class="keyword">instanceof </span>Text) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
84 <span class="keyword">return true</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
85 }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="comment">//
86 direct editing not supported in all other cases</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
87 <span class="keyword">&nbsp;return false</span>;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;
88 <span class="keyword">public </span>String getInitialValue(IDirectEditingContext
89 context) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
90 <span class="comment">// return the current name of the EClass</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
91 PictogramElement pe = context.getPictogramElement();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
92 EClass eClass = (EClass) getBusinessObjectForPictogramElement(pe);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
93 <span class="keyword">return </span>eClass.getName();<br>&nbsp;&nbsp;&nbsp;
94 }<br>&nbsp;<br>&nbsp;&nbsp;&nbsp; @Override<br>&nbsp;&nbsp;&nbsp;
95 <span class="keyword">public </span>String checkValueValid(String value,
96 IDirectEditingContext context) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
97 <span class="keyword">if</span> (value.length() &lt; 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
98 <span class="keyword">return </span><span class="string">&quot;Please enter any
99 text as class name.&quot;</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
100 <span class="keyword">if</span> (value.contains(<span class="string">&quot; &quot;</span>))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
101 <span class="keyword">return </span><span class="string">&quot;Spaces are not
102 allowed in class names.&quot;</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
103 <span class="keyword">if</span> (value.contains(<span class="string">&quot;\n&quot;</span>))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
104 <span class="keyword">return </span><span class="string">&quot;Line breakes are
105 not allowed in class names.&quot;</span>;<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
106 <span class="comment">// null means, that the value is valid</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
107 <span class="keyword">return null</span>;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;
108 <span class="keyword">public </span>void setValue(String value, IDirectEditingContext
109 context) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
jpaschb3fc7192011-08-18 11:16:33 +0200110 <span class="comment">// set the new name for the EClass</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
jpaschadf11af2011-06-30 13:44:46 +0000111 PictogramElement pe = context.getPictogramElement();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
112 EClass eClass = (EClass) getBusinessObjectForPictogramElement(pe);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
113 eClass.setName(value);<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
114 <span class="comment">// Explicitly update the shape to display the new
115 value in the diagram<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Note,
116 that this might not be necessary in future versions of Graphiti<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
117 // (currently in discussion)</span><br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
118 <span class="comment">// we know, that pe is the Shape of the Text, so its
119 container is the<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // main shape
120 of the EClass</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; updatePictogramElement(((Shape)
121 pe).getContainer());<br>&nbsp;&nbsp;&nbsp; }<br>}<br></p>
122 </div>
mwenzb5dc2f92010-06-16 13:24:52 +0000123</div>
jpaschadf11af2011-06-30 13:44:46 +0000124<p>&nbsp;</p>
jpaschd696dcd2011-07-21 12:36:20 +0000125<!-- End code ------------------------------------------------------------------------------- -->
jpaschadf11af2011-06-30 13:44:46 +0000126<p>Additionally the feature provider has to deliver our newly created feature (overwrite
127the method
128<a href="../../../javadoc/org/eclipse/graphiti/features/IFeatureProvider.html#getDirectEditingFeature(org.eclipse.graphiti.features.context.IDirectEditingContext)">
129getDirectEditingFeature</a>). This implementation can be seen here: </p>
jpaschd696dcd2011-07-21 12:36:20 +0000130<!-- Begin code ------------------------------------------------------------------------------- -->
jpaschadf11af2011-06-30 13:44:46 +0000131<p>&nbsp;</p>
132<div class="literallayout">
133 <div class="incode">
134 <p class="code">@Override<br><span class="keyword">public </span>IDirectEditingFeature
135 getDirectEditingFeature(<br>&nbsp;&nbsp;&nbsp; IDirectEditingContext context)
136 {<br>&nbsp;&nbsp;&nbsp; PictogramElement pe = context.getPictogramElement();<br>&nbsp;&nbsp;&nbsp;
137 Object bo = getBusinessObjectForPictogramElement(pe);<br>&nbsp;&nbsp;&nbsp;
138 <span class="keyword">if</span> (bo <span class="keyword">instanceof
139 </span>EClass) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
140 <span class="keyword">return new</span> TutorialDirectEditEClassFeature(<span class="keyword">this</span>);<br>&nbsp;&nbsp;&nbsp;
141 }<br>&nbsp;&nbsp;&nbsp; <span class="keyword">return super</span>.getDirectEditingFeature(context);<br>
142 } </p>
143 </div>
144</div>
145<p>&nbsp;</p>
jpaschd696dcd2011-07-21 12:36:20 +0000146<!-- End code ------------------------------------------------------------------------------- -->
147<h2>Test: Edit the Name of a Class Directly In the Diagram</h2>
jpaschadf11af2011-06-30 13:44:46 +0000148<p>Now start the editor and test this new direct editing feature:</p>
149<ol>
150 <li>Create or open a&nbsp; diagram and create an EClass &quot;Address&quot;</li>
151 <li>Click on the class name and the value &quot;Address&quot; should become editable in
152 the text-editor</li>
153 <li>Change the value to &quot;Customer Address&quot;. Now the editor should be highlighted
154 and an error-message (&quot;no space allowed&quot;) should be displayed in the status-bar.</li>
155 <li>Change the value to &quot;CustomerAddress&quot;. 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>
jpaschd696dcd2011-07-21 12:36:20 +0000160<p>&nbsp;</p>
mwenzb5dc2f92010-06-16 13:24:52 +0000161
162</body>
163
164</html>