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.di.UISynchronize;
|
25
|
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
|
26
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
27
|
import org.eclipse.e4.ui.services.EMenuService;
|
28
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
29
|
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
|
30
|
import org.eclipse.jface.util.LocalSelectionTransfer;
|
31
|
import org.eclipse.jface.viewers.ISelection;
|
32
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
33
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
34
|
import org.eclipse.jface.viewers.SelectionChangedEvent;
|
35
|
import org.eclipse.jface.viewers.StructuredSelection;
|
36
|
import org.eclipse.jface.viewers.TreeViewer;
|
37
|
import org.eclipse.swt.SWT;
|
38
|
import org.eclipse.swt.dnd.DND;
|
39
|
import org.eclipse.swt.dnd.Transfer;
|
40
|
import org.eclipse.swt.events.KeyAdapter;
|
41
|
import org.eclipse.swt.events.KeyEvent;
|
42
|
import org.eclipse.swt.layout.FillLayout;
|
43
|
import org.eclipse.swt.widgets.Composite;
|
44
|
import org.eclipse.ui.IMemento;
|
45
|
|
46
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
47
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
48
|
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
|
49
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
50
|
import eu.etaxonomy.cdm.model.description.Character;
|
51
|
import eu.etaxonomy.cdm.model.description.Feature;
|
52
|
import eu.etaxonomy.cdm.model.term.TermNode;
|
53
|
import eu.etaxonomy.cdm.model.term.TermTree;
|
54
|
import eu.etaxonomy.cdm.model.term.TermType;
|
55
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
56
|
import eu.etaxonomy.taxeditor.editor.definedterm.FeatureTreeViewerComparator;
|
57
|
import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
|
58
|
import eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer;
|
59
|
import eu.etaxonomy.taxeditor.featuretree.FeatureTreeContentProvider;
|
60
|
import eu.etaxonomy.taxeditor.featuretree.FeatureTreeLabelProvider;
|
61
|
import eu.etaxonomy.taxeditor.featuretree.e4.operation.AddFeatureOperation;
|
62
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
63
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
64
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
65
|
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
|
66
|
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
|
67
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
|
68
|
import eu.etaxonomy.taxeditor.store.AppModelId;
|
69
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
70
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
71
|
import eu.etaxonomy.taxeditor.workbench.part.IE4ViewerPart;
|
72
|
|
73
|
/**
|
74
|
*
|
75
|
* @author pplitzner
|
76
|
* @date 06.06.2017
|
77
|
*
|
78
|
*/
|
79
|
public class FeatureTreeEditor implements IFeatureTreeEditor, ISelectionChangedListener,
|
80
|
IE4ViewerPart, IPartContentHasDetails, IPartContentHasSupplementalData,
|
81
|
IContextListener, IConversationEnabled, IDirtyMarkable {
|
82
|
|
83
|
private ConversationHolder conversation;
|
84
|
|
85
|
private ICdmEntitySession cdmEntitySession;
|
86
|
|
87
|
@Inject
|
88
|
private ESelectionService selService;
|
89
|
|
90
|
@Inject
|
91
|
private MDirtyable dirty;
|
92
|
|
93
|
@Inject
|
94
|
private UISynchronize sync;
|
95
|
|
96
|
@Inject
|
97
|
private MPart thisPart;
|
98
|
|
99
|
private TreeViewer viewer;
|
100
|
|
101
|
@Inject
|
102
|
public FeatureTreeEditor() {
|
103
|
CdmStore.getContextManager().addContextListener(this);
|
104
|
}
|
105
|
|
106
|
@PostConstruct
|
107
|
public void createControl(Composite parent, EMenuService menuService){
|
108
|
if (CdmStore.isActive()){
|
109
|
initSession();
|
110
|
}
|
111
|
else{
|
112
|
return;
|
113
|
}
|
114
|
parent.setLayout(new FillLayout());
|
115
|
viewer = new TreeViewer(parent);
|
116
|
viewer.setContentProvider(new FeatureTreeContentProvider());
|
117
|
viewer.setLabelProvider(new FeatureTreeLabelProvider());
|
118
|
|
119
|
int ops = DND.DROP_COPY | DND.DROP_MOVE;
|
120
|
Transfer[] transfers = new Transfer[] {
|
121
|
FeatureNodeTransfer.getInstance(),
|
122
|
TermTransfer.getInstance(),
|
123
|
LocalSelectionTransfer.getTransfer()};
|
124
|
viewer.addDragSupport(ops, transfers, new FeatureNodeDragListener(viewer));
|
125
|
viewer.addDropSupport(ops, transfers, new FeatureTreeDropAdapter(this, viewer, sync));
|
126
|
viewer.addSelectionChangedListener(this);
|
127
|
viewer.getTree().addKeyListener(new KeyAdapter() {
|
128
|
@Override
|
129
|
public void keyPressed(KeyEvent e) {
|
130
|
if(e.stateMask == SWT.MOD1 && e.keyCode == 'c'){
|
131
|
copy(viewer.getStructuredSelection());
|
132
|
}
|
133
|
else if(e.stateMask == SWT.MOD1 && e.keyCode == 'v'){
|
134
|
paste(viewer.getStructuredSelection());
|
135
|
}
|
136
|
}
|
137
|
});
|
138
|
|
139
|
List<TermTree> trees = CdmStore.getService(IFeatureTreeService.class).list(TermTree.class, null, null, null, null);
|
140
|
viewer.setComparator(new FeatureTreeViewerComparator());
|
141
|
viewer.setInput(trees);
|
142
|
|
143
|
//create context menu
|
144
|
menuService.registerContextMenu(viewer.getControl(), AppModelId.POPUPMENU_EU_ETAXONOMY_TAXEDITOR_STORE_POPUPMENU_FEATURETREEEDITOR);
|
145
|
}
|
146
|
|
147
|
public void paste(IStructuredSelection selection) {
|
148
|
if (StoreUtil.promptCheckIsDirty(this)) {
|
149
|
return;
|
150
|
}
|
151
|
|
152
|
ISelection clipBoardSelection = LocalSelectionTransfer.getTransfer().getSelection();
|
153
|
Object firstElement = selection.getFirstElement();
|
154
|
TermNode<Feature> parentNode = null;
|
155
|
if(firstElement instanceof TermNode){
|
156
|
parentNode = (TermNode<Feature>) firstElement;
|
157
|
}
|
158
|
else if(firstElement instanceof TermTree){
|
159
|
parentNode = ((TermTree<Feature>)firstElement).getRoot();
|
160
|
}
|
161
|
if(parentNode!=null){
|
162
|
TermNode<Feature> copiedNode = (TermNode<Feature>) ((IStructuredSelection)clipBoardSelection).getFirstElement();
|
163
|
|
164
|
AddFeatureOperation operation = new AddFeatureOperation(copiedNode.getTerm().getUuid(), parentNode, this, this);
|
165
|
AbstractUtility.executeOperation(operation, sync);
|
166
|
}
|
167
|
|
168
|
}
|
169
|
|
170
|
public void copy(IStructuredSelection selection) {
|
171
|
LocalSelectionTransfer.getTransfer().setSelection(selection);
|
172
|
}
|
173
|
|
174
|
private void initSession(){
|
175
|
if(conversation == null){
|
176
|
conversation = CdmStore.createConversation();
|
177
|
}
|
178
|
if(cdmEntitySession==null){
|
179
|
cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
|
180
|
}
|
181
|
}
|
182
|
|
183
|
private void clearSession() {
|
184
|
if(conversation!=null){
|
185
|
conversation.close();
|
186
|
conversation = null;
|
187
|
}
|
188
|
if(cdmEntitySession != null) {
|
189
|
cdmEntitySession.dispose();
|
190
|
cdmEntitySession = null;
|
191
|
}
|
192
|
dirty.setDirty(false);
|
193
|
}
|
194
|
|
195
|
public void setDirty(boolean isDirty){
|
196
|
this.dirty.setDirty(isDirty);
|
197
|
}
|
198
|
|
199
|
@Override
|
200
|
public boolean isDirty(){
|
201
|
return dirty.isDirty();
|
202
|
}
|
203
|
|
204
|
@Override
|
205
|
public void selectionChanged(SelectionChangedEvent event) {
|
206
|
//propagate selection
|
207
|
selService.setSelection(event.getSelection());
|
208
|
}
|
209
|
|
210
|
@Focus
|
211
|
public void focus(){
|
212
|
if(viewer!=null){
|
213
|
viewer.getControl().setFocus();
|
214
|
}
|
215
|
if(conversation!=null && !conversation.isBound()){
|
216
|
conversation.bind();
|
217
|
}
|
218
|
if(cdmEntitySession != null) {
|
219
|
cdmEntitySession.bind();
|
220
|
}
|
221
|
}
|
222
|
|
223
|
@Override
|
224
|
public void refresh(){
|
225
|
viewer.refresh();
|
226
|
}
|
227
|
|
228
|
public TreeViewer getViewer(){
|
229
|
return viewer;
|
230
|
}
|
231
|
|
232
|
@Override
|
233
|
public IStructuredSelection getSelection() {
|
234
|
return (IStructuredSelection) viewer.getSelection();
|
235
|
}
|
236
|
|
237
|
@Override
|
238
|
public ConversationHolder getConversationHolder() {
|
239
|
return conversation;
|
240
|
}
|
241
|
|
242
|
@Override
|
243
|
@Persist
|
244
|
public void save(IProgressMonitor monitor){
|
245
|
if (!conversation.isBound()) {
|
246
|
conversation.bind();
|
247
|
}
|
248
|
|
249
|
// commit the conversation and start a new transaction immediately
|
250
|
conversation.commit(true);
|
251
|
|
252
|
CdmStore.getService(IFeatureTreeService.class).saveOrUpdate((List)getRootEntities());
|
253
|
|
254
|
List<TermTree<Feature>> rootEntities = getRootEntities();
|
255
|
for (TermTree featureTree : rootEntities) {
|
256
|
if(featureTree.getTermType().equals(TermType.Character)){
|
257
|
TermTree<Character> characterTree = featureTree;
|
258
|
//save characters because they can be modified in this editor
|
259
|
characterTree.getDistinctTerms().forEach(character->CdmStore.getService(ITermService.class).merge(character,true));
|
260
|
}
|
261
|
}
|
262
|
|
263
|
initializeTrees();
|
264
|
|
265
|
this.setDirty(false);
|
266
|
}
|
267
|
|
268
|
private void initializeTrees() {
|
269
|
Object[] expandedElements = viewer.getExpandedElements();
|
270
|
viewer.getTree().removeAll();
|
271
|
List<TermTree> trees = CdmStore.getService(IFeatureTreeService.class).list(TermTree.class, null, null, null, null);
|
272
|
viewer.setInput(trees);
|
273
|
viewer.setExpandedElements(expandedElements);
|
274
|
}
|
275
|
|
276
|
@PreDestroy
|
277
|
public void dispose(){
|
278
|
selService.setSelection(null);
|
279
|
clearSession();
|
280
|
}
|
281
|
|
282
|
@Override
|
283
|
public ICdmEntitySession getCdmEntitySession() {
|
284
|
return cdmEntitySession;
|
285
|
}
|
286
|
|
287
|
@Override
|
288
|
public Map<Object, List<String>> getPropertyPathsMap() {
|
289
|
List<String> propertyPaths = Arrays.asList(new String[] {
|
290
|
"children", //$NON-NLS-1$
|
291
|
"feature", //$NON-NLS-1$
|
292
|
"featureTree", //$NON-NLS-1$
|
293
|
});
|
294
|
Map<Object, List<String>> propertyPathMap =
|
295
|
new HashMap<Object, List<String>>();
|
296
|
propertyPathMap.put(TermNode.class,propertyPaths);
|
297
|
return propertyPathMap;
|
298
|
}
|
299
|
|
300
|
@Override
|
301
|
public List<TermTree<Feature>> getRootEntities() {
|
302
|
return (List<TermTree<Feature>>) viewer.getInput();
|
303
|
}
|
304
|
|
305
|
@Override
|
306
|
public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
|
307
|
}
|
308
|
|
309
|
@Override
|
310
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
311
|
//close view when workbench closes
|
312
|
try{
|
313
|
thisPart.getContext().get(EPartService.class).hidePart(thisPart);
|
314
|
}
|
315
|
catch(Exception e){
|
316
|
//nothing
|
317
|
}
|
318
|
}
|
319
|
|
320
|
@Override
|
321
|
public void contextStart(IMemento memento, IProgressMonitor monitor) {
|
322
|
}
|
323
|
|
324
|
@Override
|
325
|
public void contextRefresh(IProgressMonitor monitor) {
|
326
|
}
|
327
|
|
328
|
@Override
|
329
|
public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
|
330
|
}
|
331
|
|
332
|
@Override
|
333
|
public void update(CdmDataChangeMap arg0) {
|
334
|
}
|
335
|
|
336
|
@Override
|
337
|
public void changed(Object element) {
|
338
|
dirty.setDirty(true);
|
339
|
viewer.refresh();
|
340
|
}
|
341
|
|
342
|
@Override
|
343
|
public void forceDirty() {
|
344
|
dirty.setDirty(true);
|
345
|
}
|
346
|
|
347
|
@Override
|
348
|
public boolean postOperation(Object objectAffectedByOperation) {
|
349
|
initializeTrees();
|
350
|
viewer.refresh();
|
351
|
if(objectAffectedByOperation instanceof TermNode){
|
352
|
TermNode<Feature> node = (TermNode)objectAffectedByOperation;
|
353
|
viewer.expandToLevel(node.getGraph(), 1);
|
354
|
}
|
355
|
if(objectAffectedByOperation!=null){
|
356
|
StructuredSelection selection = new StructuredSelection(objectAffectedByOperation);
|
357
|
viewer.setSelection(selection);
|
358
|
}
|
359
|
return true;
|
360
|
}
|
361
|
|
362
|
@Override
|
363
|
public boolean onComplete() {
|
364
|
return false;
|
365
|
}
|
366
|
|
367
|
}
|