1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.featuretree.e4;
|
11
|
|
12
|
import java.util.Arrays;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
|
17
|
import javax.annotation.PostConstruct;
|
18
|
import javax.annotation.PreDestroy;
|
19
|
import javax.inject.Inject;
|
20
|
|
21
|
import org.eclipse.core.runtime.IProgressMonitor;
|
22
|
import org.eclipse.e4.ui.di.Focus;
|
23
|
import org.eclipse.e4.ui.di.Persist;
|
24
|
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
|
25
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
26
|
import org.eclipse.e4.ui.services.EMenuService;
|
27
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
28
|
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
|
29
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
30
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
31
|
import org.eclipse.jface.viewers.SelectionChangedEvent;
|
32
|
import org.eclipse.jface.viewers.TreeViewer;
|
33
|
import org.eclipse.swt.dnd.DND;
|
34
|
import org.eclipse.swt.dnd.Transfer;
|
35
|
import org.eclipse.swt.layout.FillLayout;
|
36
|
import org.eclipse.swt.widgets.Composite;
|
37
|
import org.eclipse.ui.IMemento;
|
38
|
|
39
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
40
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
41
|
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
|
42
|
import eu.etaxonomy.cdm.model.description.FeatureNode;
|
43
|
import eu.etaxonomy.cdm.model.description.FeatureTree;
|
44
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
45
|
import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
|
46
|
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
|
47
|
import eu.etaxonomy.taxeditor.featuretree.FeatureTreeContentProvider;
|
48
|
import eu.etaxonomy.taxeditor.featuretree.FeatureTreeLabelProvider;
|
49
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
50
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
51
|
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
|
52
|
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
|
53
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
|
54
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
|
55
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
56
|
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
|
57
|
import eu.etaxonomy.taxeditor.workbench.part.IE4ViewerPart;
|
58
|
|
59
|
/**
|
60
|
*
|
61
|
* @author pplitzner
|
62
|
* @date 06.06.2017
|
63
|
*
|
64
|
*/
|
65
|
public class FeatureTreeEditor implements ICdmEntitySessionEnabled, ISelectionChangedListener,
|
66
|
IE4ViewerPart, IE4SavablePart, IPartContentHasDetails, IPartContentHasSupplementalData,
|
67
|
IContextListener, IConversationEnabled, IDirtyMarkable {
|
68
|
|
69
|
private ConversationHolder conversation;
|
70
|
|
71
|
private ICdmEntitySession cdmEntitySession;
|
72
|
|
73
|
@Inject
|
74
|
private ESelectionService selService;
|
75
|
|
76
|
@Inject
|
77
|
private MDirtyable dirty;
|
78
|
|
79
|
// private FeatureTreeEditorComposite composite;
|
80
|
|
81
|
@Inject
|
82
|
private MPart thisPart;
|
83
|
|
84
|
private TreeViewer viewer;
|
85
|
|
86
|
@Inject
|
87
|
public FeatureTreeEditor() {
|
88
|
CdmStore.getContextManager().addContextListener(this);
|
89
|
}
|
90
|
|
91
|
/** {@inheritDoc} */
|
92
|
@PostConstruct
|
93
|
public void createControl(Composite parent, EMenuService menuService){
|
94
|
if (CdmStore.isActive()){
|
95
|
initSession();
|
96
|
}
|
97
|
else{
|
98
|
return;
|
99
|
}
|
100
|
parent.setLayout(new FillLayout());
|
101
|
viewer = new TreeViewer(parent);
|
102
|
viewer.setContentProvider(new FeatureTreeContentProvider());
|
103
|
viewer.setLabelProvider(new FeatureTreeLabelProvider());
|
104
|
|
105
|
int ops = DND.DROP_COPY | DND.DROP_MOVE;
|
106
|
Transfer[] transfers = new Transfer[] { FeatureNodeTransfer
|
107
|
.getInstance(), TermTransfer.getInstance() };
|
108
|
viewer.addDragSupport(ops, transfers, new FeatureNodeDragListener(viewer));
|
109
|
viewer.addDropSupport(ops, transfers, new FeatureNodeDropAdapter(dirty, viewer));
|
110
|
viewer.addSelectionChangedListener(this);
|
111
|
|
112
|
List<FeatureTree> trees = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
|
113
|
viewer.setInput(trees);
|
114
|
|
115
|
// composite = new FeatureTreeEditorComposite(parent, SWT.NULL);
|
116
|
// composite.init(new FeatureNodeDragListener(composite.getViewer()),
|
117
|
// new FeatureNodeDropAdapter(dirty, composite.getViewer()), this, new SelectionAdapter() {
|
118
|
// @Override
|
119
|
// public void widgetSelected(SelectionEvent e) {
|
120
|
// if(isDirty()){
|
121
|
// MessageDialog dialog;
|
122
|
// String[] buttonLables = {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL};
|
123
|
// dialog = new MessageDialog(null, "Unsaved changes", null, "You have unsaved changes. Do you want to save?",
|
124
|
// MessageDialog.QUESTION_WITH_CANCEL, 0, buttonLables);
|
125
|
// int returnCode = dialog.open();
|
126
|
//
|
127
|
// if (returnCode == 0){
|
128
|
// //YES
|
129
|
// save(new NullProgressMonitor());
|
130
|
// } else if (returnCode == 1){
|
131
|
// //NO
|
132
|
// if(CdmStore.isActive()){
|
133
|
// clearSession();
|
134
|
// initSession();
|
135
|
// composite.setSelectedTree(null);
|
136
|
// }
|
137
|
// } else if (returnCode == 2){
|
138
|
// //CANCEL
|
139
|
// return;
|
140
|
// }
|
141
|
// }
|
142
|
// FeatureTree tree = FeatureTreeSelectionDialog.select(composite.getDisplay().getActiveShell(), //conversation,
|
143
|
// null);
|
144
|
// if (tree != null) {
|
145
|
// composite.setSelectedTree(tree);
|
146
|
// }
|
147
|
// }
|
148
|
// }, this);
|
149
|
|
150
|
//create context menu
|
151
|
menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.store.popupmenu.featureTreeEditor");
|
152
|
}
|
153
|
|
154
|
private void initSession(){
|
155
|
if(conversation == null){
|
156
|
conversation = CdmStore.createConversation();
|
157
|
}
|
158
|
if(cdmEntitySession!=null){
|
159
|
cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
|
160
|
}
|
161
|
}
|
162
|
|
163
|
private void clearSession() {
|
164
|
if(conversation!=null){
|
165
|
conversation.close();
|
166
|
conversation = null;
|
167
|
}
|
168
|
if(cdmEntitySession != null) {
|
169
|
cdmEntitySession.dispose();
|
170
|
cdmEntitySession = null;
|
171
|
}
|
172
|
dirty.setDirty(false);
|
173
|
}
|
174
|
|
175
|
public void setDirty(boolean isDirty){
|
176
|
this.dirty.setDirty(isDirty);
|
177
|
}
|
178
|
|
179
|
public boolean isDirty(){
|
180
|
return dirty.isDirty();
|
181
|
}
|
182
|
|
183
|
/** {@inheritDoc} */
|
184
|
@Override
|
185
|
public void selectionChanged(SelectionChangedEvent event) {
|
186
|
//propagate selection
|
187
|
selService.setSelection(event.getSelection());
|
188
|
}
|
189
|
|
190
|
@Focus
|
191
|
public void focus(){
|
192
|
if(viewer!=null){
|
193
|
viewer.getControl().setFocus();
|
194
|
}
|
195
|
if(conversation!=null && !conversation.isBound()){
|
196
|
conversation.bind();
|
197
|
}
|
198
|
if(cdmEntitySession != null) {
|
199
|
cdmEntitySession.bind();
|
200
|
}
|
201
|
}
|
202
|
|
203
|
@Override
|
204
|
public void refresh(){
|
205
|
viewer.refresh();
|
206
|
}
|
207
|
|
208
|
public TreeViewer getViewer(){
|
209
|
return viewer;
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* {@inheritDoc}
|
214
|
*/
|
215
|
@Override
|
216
|
public IStructuredSelection getSelection() {
|
217
|
return (IStructuredSelection) viewer.getSelection();
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* {@inheritDoc}
|
222
|
*/
|
223
|
@Override
|
224
|
public ConversationHolder getConversationHolder() {
|
225
|
return conversation;
|
226
|
}
|
227
|
|
228
|
@Override
|
229
|
@Persist
|
230
|
public void save(IProgressMonitor monitor){
|
231
|
if (!conversation.isBound()) {
|
232
|
conversation.bind();
|
233
|
}
|
234
|
|
235
|
// commit the conversation and start a new transaction immediately
|
236
|
conversation.commit(true);
|
237
|
|
238
|
List<FeatureTree> trees = (List<FeatureTree>) viewer.getInput();
|
239
|
CdmStore.getService(IFeatureTreeService.class).merge(trees);
|
240
|
|
241
|
this.setDirty(false);
|
242
|
}
|
243
|
|
244
|
@PreDestroy
|
245
|
public void dispose(){
|
246
|
selService.setSelection(null);
|
247
|
clearSession();
|
248
|
}
|
249
|
|
250
|
@Override
|
251
|
public ICdmEntitySession getCdmEntitySession() {
|
252
|
return cdmEntitySession;
|
253
|
}
|
254
|
|
255
|
@Override
|
256
|
public Map<Object, List<String>> getPropertyPathsMap() {
|
257
|
List<String> propertyPaths = Arrays.asList(new String[] {
|
258
|
"children", //$NON-NLS-1$
|
259
|
"feature", //$NON-NLS-1$
|
260
|
"featureTree", //$NON-NLS-1$
|
261
|
});
|
262
|
Map<Object, List<String>> propertyPathMap =
|
263
|
new HashMap<Object, List<String>>();
|
264
|
propertyPathMap.put(FeatureNode.class,propertyPaths);
|
265
|
return propertyPathMap;
|
266
|
}
|
267
|
|
268
|
/**
|
269
|
* {@inheritDoc}
|
270
|
*/
|
271
|
@Override
|
272
|
public List<FeatureTree> getRootEntities() {
|
273
|
return (List<FeatureTree>) viewer.getInput();
|
274
|
}
|
275
|
|
276
|
/**
|
277
|
* {@inheritDoc}
|
278
|
*/
|
279
|
@Override
|
280
|
public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
|
281
|
}
|
282
|
|
283
|
/**
|
284
|
* {@inheritDoc}
|
285
|
*/
|
286
|
@Override
|
287
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
288
|
//close view when workbench closes
|
289
|
try{
|
290
|
thisPart.getContext().get(EPartService.class).hidePart(thisPart);
|
291
|
}
|
292
|
catch(Exception e){
|
293
|
//nothing
|
294
|
}
|
295
|
}
|
296
|
|
297
|
/**
|
298
|
* {@inheritDoc}
|
299
|
*/
|
300
|
@Override
|
301
|
public void contextStart(IMemento memento, IProgressMonitor monitor) {
|
302
|
}
|
303
|
|
304
|
/**
|
305
|
* {@inheritDoc}
|
306
|
*/
|
307
|
@Override
|
308
|
public void contextRefresh(IProgressMonitor monitor) {
|
309
|
}
|
310
|
|
311
|
/**
|
312
|
* {@inheritDoc}
|
313
|
*/
|
314
|
@Override
|
315
|
public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
|
316
|
}
|
317
|
|
318
|
/**
|
319
|
* {@inheritDoc}
|
320
|
*/
|
321
|
@Override
|
322
|
public void update(CdmDataChangeMap arg0) {
|
323
|
}
|
324
|
|
325
|
/**
|
326
|
* {@inheritDoc}
|
327
|
*/
|
328
|
@Override
|
329
|
public void changed(Object element) {
|
330
|
dirty.setDirty(true);
|
331
|
}
|
332
|
|
333
|
/**
|
334
|
* {@inheritDoc}
|
335
|
*/
|
336
|
@Override
|
337
|
public void forceDirty() {
|
338
|
dirty.setDirty(true);
|
339
|
}
|
340
|
|
341
|
}
|