-
Notifications
You must be signed in to change notification settings - Fork 0
/
07-Eclipse4.xdoc
162 lines (117 loc) · 5.62 KB
/
07-Eclipse4.xdoc
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
chapter:Eclipse4[Eclipse 4.x]
Instead of using the Extension Point mechanism, EMF Parsley leverages from DSL and Google Guice Injection.
Because of this, it is very easy to use it with Eclipse 4.x (e4).
section:GetFirstExample[First Example Setup]
If you followed the steps described in section ref:FirstExample[First Example] you will have already
what we need to begin. Otherwise the following wizard will bring you to that point.
ol[
item[File -> New... -> Example...]
item[from Category "Emf Parsley Examples", select "Emf Parsley First Example"]
item[press Next and Finish]
]
You will end up with three plug-ins:
ul[
item[org.eclipse.emf.parsley.examples.firstexample (the EMF Parsley example plug-in)]
item[org.eclipse.emf.examples.library (the model plug-in)]
item[org.eclipse.emf.examples.library.edit (the model.edit plug-in)]
]
As a reminder, in section ref:FirstExample[First Example] 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.
section:PrepareForEclipse4Application[Preparing for a pure e4 Application]
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.
In order to do this we need to export the e["org.eclipse.emf.parsley.examples.firstexample"] package from the first plug-in.
section:CreateEclipse4Application[Create an e4 Application]
Now let's create a new, empty, e4 application, e.g. e["org.eclipse.emf.parsley.examples.firstexample.application"]
(you can find details on how to create e4 applications in link[http://www.rcp-vision.com/?p=4694&lang=en][our
tutorials]).
Create a Part and ensure that the application starts.
section:Eclipse4ApplicationAndEMFParsley[Using a TreeComposite into an e4 Part]
In the just created plug-in we need dependencies from the previous plug-ins: so open the e[org.eclipse.emf.parsley.examples.firstexample.application/MANIFEST.MF] file, go to e[Dependencies]
tab and add the three previous plug-ins. Add also e["org.eclipse.emf.parsley"] plug-in.
Don't forget to add the previous, and the required plug-ins, also to the Product.
Open the Part java class and make the following changes:
code[Java][
// Use these imports during Organizing Imports operation
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.swt.widgets.Composite;
// The part implements IMenuListener for context menu handling
public class MyEclipse4Part {
//the EMF Parley composite for showing a tree and a detail form
private TreeFormComposite treeFormComposite;
//the EMF Resource
private Resource resource;
//URI for EMF Resource
private URI uri = URI.createFileURI(System.getProperty("user.home")
+ "/MyLibrary.library");
]
Modify the e[@PostConstruct] method with this code:
code[Java][
@PostConstruct
public void postConstruct(Composite parent) throws Exception {
// Guice injector
private Injector injector = FirstexampleInjectorProvider.getInjector();
// The EditingDomain is needed for context menu and drag and drop
EditingDomain editingDomain = injector.getInstance(EditingDomain.class);
ResourceLoader resourceLoader = injector.getInstance(ResourceLoader.class);
//load the resource
resource = resourceLoader.getResource(editingDomain, uri).getResource();
TreeFormFactory treeFormFactory = injector.getInstance(TreeFormFactory.class);
//create the tree-form composite
treeFormComposite = treeFormFactory.createTreeFormComposite(parent, SWT.BORDER);
// Guice injected viewer context menu helper
ViewerContextMenuHelper contextMenuHelper = injector.getInstance(ViewerContextMenuHelper.class);
// Guice injected viewer drag and drop helper
ViewerDragAndDropHelper dragAndDropHelper = injector.getInstance(ViewerDragAndDropHelper.class);
// set context menu and drag and drop
contextMenuHelper.addViewerContextMenu(treeFormComposite.getViewer(), editingDomain);
dragAndDropHelper.addDragAndDrop(treeFormComposite.getViewer(), editingDomain);
//update the composite
treeFormComposite.update(resource);
}
]
If you now run the application you will be able to manage the model:
img[images/07-eclipse4-part.png][][ ][]
but you will notice that it is not possible to persist the changes to the model.
section:Eclipse4Save[Adding the dirty state and Save command]
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
code[Java][
@Inject
MDirtyable dirty;
]
add to e[@PostConstruct] method the following code in order to update the dirty state
code[Java][
editingDomain.getCommandStack().addCommandStackListener(
new CommandStackListener() {
public void commandStackChanged(EventObject event) {
if (dirty != null)
dirty.setDirty(true);
}
});
]
and add the e[@Persist] method, which will be called when the part is saved
code[Java][
@Persist
public void save(MDirtyable dirty) throws IOException {
resource.save(null);
if (dirty != null) {
dirty.setDirty(false);
}
}
]
and, in the end, add the e[Save] handler along with the correspondent e[Command] and e[Menu]
(you can find how to create handlers, commands and menus in an e4 applications in link[http://www.rcp-vision.com/?p=4972&lang=en][our
tutorials])
code[Java][
import javax.inject.Named;
public class SaveHandler {
@Execute
void execute(EPartService partService, @Named(IServiceConstants.ACTIVE_PART) MPart part) {
partService.savePart(part, false);
}
}
]