-
Notifications
You must be signed in to change notification settings - Fork 0
/
07-Eclipse4.html
241 lines (239 loc) · 10.6 KB
/
07-Eclipse4.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" >
<title>Eclipse 4.x</title>
<link href="book.css" rel="stylesheet" type="text/css">
<link href="code.css" rel="stylesheet" type="text/css">
<link rel="home" href="00-Main.html" title="">
</head>
<body>
<a name="Eclipse4"></a>
<h1>Eclipse 4.x</h1>
<p>
Instead of using the Extension Point mechanism, EMF Parsley leverages from DSL and Google Guice Injection.
</p>
<p>
Because of this, it is very easy to use it with Eclipse 4.x (e4).
</p>
<a name="GetFirstExample"></a>
<h2>First Example Setup</h2>
<p>
If you followed the steps described in section <a href="02-FirstExample.html#FirstExample" title="Go to "First Example"">First Example</a> you will have already
what we need to begin. Otherwise the following wizard will bring you to that point.
</p>
<p>
<ol>
<li>
File -> New... -> Example...
</li>
<li>
from Category "Emf Parsley Examples", select "Emf Parsley First Example"
</li>
<li>
press Next and Finish
</li>
</ol>
</p>
<p>
You will end up with three plug-ins:
</p>
<p>
<ul>
<li>
org.eclipse.emf.parsley.examples.firstexample (the EMF Parsley example plug-in)
</li>
<li>
org.eclipse.emf.examples.library (the model plug-in)
</li>
<li>
org.eclipse.emf.examples.library.edit (the model.edit plug-in)
</li>
</ul>
</p>
<p>
As a reminder, in section <a href="02-FirstExample.html#FirstExample" title="Go to "First Example"">First Example</a> we reached the point where we launched a second Eclipse
instance (but, of course, just defining a product you could have a standalone 3.x application) with a
view (called "My Library Tree Form") that allowed to manage the model.
</p>
<a name="PrepareForEclipse4Application"></a>
<h2>Preparing for a pure e4 Application</h2>
<p>
What we will do now is starting from the previous step and create an e4 Application (on top of
the previous plug-ins) that gets to the same result, but now with a pure e4 Part.
</p>
<p>
In order to do this we need to export the <em>"org.eclipse.emf.parsley.examples.firstexample"</em> package from the first plug-in.
</p>
<a name="CreateEclipse4Application"></a>
<h2>Create an e4 Application</h2>
<p>
Now let's create a new, empty, e4 application, e.g. <em>"org.eclipse.emf.parsley.examples.firstexample.application"</em>
(you can find details on how to create e4 applications in <a href="http://www.rcp-vision.com/?p=4694&lang=en">our
tutorials</a>).
</p>
<p>
Create a Part and ensure that the application starts.
</p>
<a name="Eclipse4ApplicationAndEMFParsley"></a>
<h2>Using a TreeComposite into an e4 Part</h2>
<p>
In the just created plug-in we need dependencies from the previous plug-ins: so open the <em>org.eclipse.emf.parsley.examples.firstexample.application/MANIFEST.MF</em> file, go to <em>Dependencies</em>
tab and add the three previous plug-ins. Add also <em>"org.eclipse.emf.parsley"</em> plug-in.
Don't forget to add the previous, and the required plug-ins, also to the Product.
</p>
<p>
Open the Part java class and make the following changes:
</p>
<p>
<div class="literallayout">
<div class="incode">
<p class="code">
<span class="comment">// Use these imports during Organizing Imports operation<br/>
</span><span class="keyword">import</span> org.eclipse.emf.common.util.URI;<br/>
<span class="keyword">import</span> org.eclipse.emf.ecore.resource.Resource;<br/>
<span class="keyword">import</span> org.eclipse.swt.widgets.Composite;<br/>
<br/>
<span class="comment">// The part implements IMenuListener for context menu handling<br/>
</span><span class="keyword">public</span> <span class="keyword">class</span> MyEclipse4Part {<br/>
<br/>
<span class="comment">//the EMF Parley composite for showing a tree and a detail form<br/>
</span> <span class="keyword">private</span> TreeFormComposite treeFormComposite;<br/>
<span class="comment">//the EMF Resource<br/>
</span> <span class="keyword">private</span> Resource resource;<br/>
<span class="comment">//URI for EMF Resource<br/>
</span> <span class="keyword">private</span> URI uri = URI.createFileURI(System.getProperty(<span class="string">"user.home"</span>)<br/>
+ <span class="string">"/MyLibrary.library"</span>);<br/>
<br/>
</p>
</div>
</div>
</p>
<p>
Modify the <em>@PostConstruct</em> method with this code:
</p>
<p>
<div class="literallayout">
<div class="incode">
<p class="code">
@PostConstruct<br/>
<span class="keyword">public</span> <span class="keyword">void</span> postConstruct(Composite parent) <span class="keyword">throws</span> Exception {<br/>
<span class="comment">// Guice injector<br/>
</span> <span class="keyword">private</span> Injector injector = FirstexampleInjectorProvider.getInjector();<br/>
<br/>
AdapterFactoryEditingDomain editingDomain = injector.getInstance(AdapterFactoryEditingDomain.<span class="keyword">class</span>);<br/>
<br/>
ResourceLoader resourceLoader = injector.getInstance(ResourceLoader.<span class="keyword">class</span>);<br/>
<span class="comment">//load the resource<br/>
</span> resource = resourceLoader.getResource(editingDomain, uri).getResource();<br/>
<br/>
TreeFormFactory treeFormFactory = injector.getInstance(TreeFormFactory.<span class="keyword">class</span>);<br/>
<span class="comment">//create the tree-form composite<br/>
</span> treeFormComposite = treeFormFactory.createTreeFormComposite(parent, SWT.BORDER);<br/>
<br/>
<span class="comment">// Guice injected viewer context menu helper<br/>
</span> ViewerContextMenuHelper contextMenuHelper = injector.getInstance(ViewerContextMenuHelper.<span class="keyword">class</span>);<br/>
<span class="comment">// Guice injected viewer drag and drop helper<br/>
</span> ViewerDragAndDropHelper dragAndDropHelper = injector.getInstance(ViewerDragAndDropHelper.<span class="keyword">class</span>);<br/>
<br/>
<span class="comment">// set context menu and drag and drop<br/>
</span> contextMenuHelper.addViewerContextMenu(treeFormComposite.getViewer(), editingDomain);<br/>
dragAndDropHelper.addDragAndDrop(treeFormComposite.getViewer(), editingDomain);<br/>
<br/>
<span class="comment">//update the composite<br/>
</span> treeFormComposite.update(resource);<br/>
}<br/>
</p>
</div>
</div>
</p>
<p>
If you now run the application you will be able to manage the model:
</p>
<p>
<div class="image" >
<img src="images/07-eclipse4-part.png" class=" "
/>
<div class="caption">
</div>
</div>
</p>
<p>
but you will notice that it is not possible to persist the changes to the model.
</p>
<a name="Eclipse4Save"></a>
<h2>Adding the dirty state and Save command</h2>
<p>
In order to allow persisting the model changes we have to add the dirty state handling to the part and
the Save command to the application.
Let's start with adding the following attribute to the part
</p>
<p>
<div class="literallayout">
<div class="incode">
<p class="code">
@Inject<br/>
MDirtyable dirty;<br/>
</p>
</div>
</div>
</p>
<p>
add to <em>@PostConstruct</em> method the following code in order to update the dirty state
</p>
<p>
<div class="literallayout">
<div class="incode">
<p class="code">
editingDomain.getCommandStack().addCommandStackListener(<br/>
<span class="keyword">new</span> CommandStackListener() {<br/>
<span class="keyword">public</span> <span class="keyword">void</span> commandStackChanged(EventObject event) {<br/>
<span class="keyword">if</span> (dirty != null)<br/>
dirty.setDirty(<span class="keyword">true</span>);<br/>
}<br/>
});<br/>
</p>
</div>
</div>
</p>
<p>
and add the <em>@Persist</em> method, which will be called when the part is saved
</p>
<p>
<div class="literallayout">
<div class="incode">
<p class="code">
@Persist<br/>
<span class="keyword">public</span> <span class="keyword">void</span> save(MDirtyable dirty) <span class="keyword">throws</span> IOException {<br/>
resource.save(null);<br/>
<span class="keyword">if</span> (dirty != null) {<br/>
dirty.setDirty(<span class="keyword">false</span>);<br/>
}<br/>
}<br/>
</p>
</div>
</div>
</p>
<p>
and, in the end, add the <em>Save</em> handler along with the correspondent <em>Command</em> and <em>Menu</em>
(you can find how to create handlers, commands and menus in an e4 applications in <a href="http://www.rcp-vision.com/?p=4972&lang=en">our
tutorials</a>)
</p>
<p>
<div class="literallayout">
<div class="incode">
<p class="code">
<span class="keyword">import</span> javax.inject.Named;<br/>
<br/>
<span class="keyword">public</span> <span class="keyword">class</span> SaveHandler {<br/>
<br/>
@Execute<br/>
<span class="keyword">void</span> execute(EPartService partService, @Named(IServiceConstants.ACTIVE_PART) MPart part) {<br/>
partService.savePart(part, <span class="keyword">false</span>);<br/>
}<br/>
}<br/>
</p>
</div>
</div>
</p>
</body>
</html>