1
|
/**
|
2
|
* Copyright (C) 2017 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
|
package eu.etaxonomy.taxeditor.editor.descriptiveDataSet;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Collection;
|
13
|
import java.util.Collections;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.Iterator;
|
16
|
import java.util.List;
|
17
|
import java.util.Map;
|
18
|
import java.util.Set;
|
19
|
import java.util.UUID;
|
20
|
import java.util.stream.Collectors;
|
21
|
|
22
|
import javax.annotation.PostConstruct;
|
23
|
import javax.annotation.PreDestroy;
|
24
|
import javax.inject.Inject;
|
25
|
import javax.inject.Named;
|
26
|
|
27
|
import org.eclipse.core.runtime.IProgressMonitor;
|
28
|
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
|
29
|
import org.eclipse.e4.core.contexts.IEclipseContext;
|
30
|
import org.eclipse.e4.ui.di.Focus;
|
31
|
import org.eclipse.e4.ui.di.Persist;
|
32
|
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
|
33
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
34
|
import org.eclipse.e4.ui.services.EMenuService;
|
35
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
36
|
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
|
37
|
import org.eclipse.jface.util.LocalSelectionTransfer;
|
38
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
39
|
import org.eclipse.jface.viewers.StructuredSelection;
|
40
|
import org.eclipse.jface.viewers.Viewer;
|
41
|
import org.eclipse.jface.window.Window;
|
42
|
import org.eclipse.swt.SWT;
|
43
|
import org.eclipse.swt.dnd.DND;
|
44
|
import org.eclipse.swt.dnd.Transfer;
|
45
|
import org.eclipse.swt.events.FocusAdapter;
|
46
|
import org.eclipse.swt.events.FocusEvent;
|
47
|
import org.eclipse.swt.events.KeyAdapter;
|
48
|
import org.eclipse.swt.events.KeyEvent;
|
49
|
import org.eclipse.swt.events.ModifyEvent;
|
50
|
import org.eclipse.swt.events.ModifyListener;
|
51
|
import org.eclipse.swt.events.SelectionAdapter;
|
52
|
import org.eclipse.swt.widgets.Composite;
|
53
|
import org.eclipse.swt.widgets.Display;
|
54
|
import org.eclipse.swt.widgets.Shell;
|
55
|
|
56
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
57
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
58
|
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
|
59
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
60
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
61
|
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
|
62
|
import eu.etaxonomy.cdm.model.description.Feature;
|
63
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
64
|
import eu.etaxonomy.cdm.model.name.Rank;
|
65
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
66
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
67
|
import eu.etaxonomy.cdm.model.term.TermTree;
|
68
|
import eu.etaxonomy.cdm.model.term.TermType;
|
69
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
|
70
|
import eu.etaxonomy.cdm.persistence.dto.TermDto;
|
71
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
72
|
import eu.etaxonomy.taxeditor.editor.IDescriptiveDataSetEditor;
|
73
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
74
|
import eu.etaxonomy.taxeditor.event.EventUtility;
|
75
|
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
|
76
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
77
|
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
|
78
|
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
|
79
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
80
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
|
81
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
|
82
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
83
|
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
|
84
|
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
|
85
|
|
86
|
/**
|
87
|
* Editor for configuring DescriptiveDataSets
|
88
|
* @author pplitzner
|
89
|
* @since Nov 21, 2017
|
90
|
*
|
91
|
*/
|
92
|
public class DescriptiveDataSetEditor implements IE4SavablePart, IConversationEnabled, ICdmEntitySessionEnabled,
|
93
|
IPartContentHasDetails, IPartContentHasSupplementalData, IDirtyMarkable, IDescriptiveDataSetEditor {
|
94
|
|
95
|
private DescriptiveDataSetComposite composite;
|
96
|
|
97
|
private ConversationHolder conversation;
|
98
|
|
99
|
private ICdmEntitySession cdmEntitySession;
|
100
|
|
101
|
private DescriptiveDataSet descriptiveDataSet;
|
102
|
|
103
|
private final int dndOperations = DND.DROP_MOVE;
|
104
|
|
105
|
@Inject
|
106
|
private MDirtyable dirty;
|
107
|
|
108
|
@Inject
|
109
|
private MPart thisPart;
|
110
|
|
111
|
@Inject
|
112
|
private ESelectionService selectionService;
|
113
|
|
114
|
private ModifyListener labelModifyListener;
|
115
|
|
116
|
@PostConstruct
|
117
|
public void create(Composite parent, IEclipseContext context, @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
|
118
|
EMenuService menuService){
|
119
|
if (CdmStore.isActive()){
|
120
|
if(conversation == null){
|
121
|
conversation = CdmStore.createConversation();
|
122
|
}
|
123
|
if(cdmEntitySession == null){
|
124
|
cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
|
125
|
}
|
126
|
}
|
127
|
else{
|
128
|
return;
|
129
|
}
|
130
|
CdmFormFactory cdmFormFactory = new CdmFormFactory(Display.getCurrent());
|
131
|
ContextInjectionFactory.inject(cdmFormFactory, context);
|
132
|
|
133
|
composite = new DescriptiveDataSetComposite(parent, SWT.NONE);
|
134
|
|
135
|
labelModifyListener = new ModifyListener() {
|
136
|
@Override
|
137
|
public void modifyText(ModifyEvent e) {
|
138
|
descriptiveDataSet.setLabel(composite.getTxt_label().getText());
|
139
|
EventUtility.postSyncEvent(WorkbenchEventConstants.REFRESH_DATASET_NAVIGATOR, descriptiveDataSet);
|
140
|
dirty.setDirty(true);
|
141
|
}
|
142
|
};
|
143
|
composite.getRankMin().addSelectionChangedListener(event->dirty.setDirty(true));
|
144
|
composite.getRankMin().setText("Select min rank...");
|
145
|
composite.getRankMax().addSelectionChangedListener(event->dirty.setDirty(true));
|
146
|
composite.getRankMax().setText("Select max rank...");
|
147
|
composite.getBtnRemoveRankMin().addSelectionListener(new SelectionAdapter() {
|
148
|
@Override
|
149
|
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
|
150
|
composite.getRankMin().setElement(null);
|
151
|
dirty.setDirty(true);
|
152
|
}
|
153
|
});
|
154
|
composite.getBtnRemoveRankMax().addSelectionListener(new SelectionAdapter() {
|
155
|
@Override
|
156
|
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
|
157
|
composite.getRankMax().setElement(null);
|
158
|
dirty.setDirty(true);
|
159
|
}
|
160
|
});
|
161
|
|
162
|
composite.getBtnChooseArea().addSelectionListener(new SelectionAdapter() {
|
163
|
|
164
|
@Override
|
165
|
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
|
166
|
AreasSelectionDialog areasSelectionDialog = new AreasSelectionDialog(composite.getShell(), composite.getAreas());
|
167
|
if(areasSelectionDialog.open()==Window.OK){
|
168
|
List<TermDto> selectedAreas = areasSelectionDialog.getSelectedAreas();
|
169
|
if(selectedAreas!=null){
|
170
|
composite.setAreas(selectedAreas);
|
171
|
dirty.setDirty(true);
|
172
|
}
|
173
|
}
|
174
|
}
|
175
|
});
|
176
|
|
177
|
composite.getFeatureTreeEditorComposite().init("Character Tree",
|
178
|
TermType.Character,
|
179
|
null,
|
180
|
null,
|
181
|
e->selectionService.setSelection(e.getSelection()),
|
182
|
this,
|
183
|
null);
|
184
|
|
185
|
//add drag'n'drop support
|
186
|
Transfer[] transfers = new Transfer[] {LocalSelectionTransfer.getTransfer()};
|
187
|
composite.getTaxonNodeTree().addDropSupport(dndOperations, transfers, new TaxonNodeDropAdapter(this));
|
188
|
|
189
|
composite.getTaxonNodeTree().getTree().addKeyListener(new KeyAdapter() {
|
190
|
@Override
|
191
|
public void keyPressed(KeyEvent e) {
|
192
|
if(e.character==SWT.DEL){
|
193
|
removeSelectedTaxonNodes();
|
194
|
}
|
195
|
}
|
196
|
});
|
197
|
|
198
|
|
199
|
//create context menu
|
200
|
menuService.registerContextMenu(composite.getTaxonNodeTree().getControl(), "eu.etaxonomy.taxeditor.editor.descriptiveDataSet.DescriptiveDataSetEditor.popupmenu.specimeneditor");
|
201
|
|
202
|
}
|
203
|
|
204
|
public void init(UUID descriptiveDataSetUuid) {
|
205
|
this.descriptiveDataSet = cdmEntitySession.remoteLoad(CdmStore.getService(IDescriptiveDataSetService.class), descriptiveDataSetUuid);
|
206
|
if(descriptiveDataSet.getLabel()!=null){
|
207
|
composite.getTxt_label().setText(descriptiveDataSet.getLabel());
|
208
|
}
|
209
|
if(descriptiveDataSet.getDescriptiveSystem()!=null){
|
210
|
composite.setCharacters(descriptiveDataSet.getDescriptiveSystem());
|
211
|
}
|
212
|
Rank maxRank = descriptiveDataSet.getMaxRank();
|
213
|
if(maxRank!=null){
|
214
|
composite.setRankMax(maxRank);
|
215
|
}
|
216
|
Rank minRank = descriptiveDataSet.getMinRank();
|
217
|
if(minRank!=null){
|
218
|
composite.setRankMin(minRank);
|
219
|
}
|
220
|
Set<NamedArea> geoFilter = descriptiveDataSet.getGeoFilter();
|
221
|
if(geoFilter!=null && !geoFilter.isEmpty()){
|
222
|
Set<TermDto> terms = geoFilter.stream().map(filter->TermDto.fromTerm(filter, true)).collect(Collectors.toSet());
|
223
|
composite.setAreas(new ArrayList<>(terms));
|
224
|
}
|
225
|
Set<TaxonNode> taxonSubtreeFilter = descriptiveDataSet.getTaxonSubtreeFilter();
|
226
|
if(taxonSubtreeFilter!=null){
|
227
|
Set<TaxonNodeDto> dtoSet = new HashSet<>();
|
228
|
for (TaxonNode node: taxonSubtreeFilter){
|
229
|
dtoSet.add(new TaxonNodeDto(node));
|
230
|
}
|
231
|
composite.getTaxonNodeTree().setInput(dtoSet);
|
232
|
}
|
233
|
composite.getTxt_label().addModifyListener(labelModifyListener);
|
234
|
composite.getTxt_label().addFocusListener(new FocusAdapter() {
|
235
|
@Override
|
236
|
public void focusGained(FocusEvent e) {
|
237
|
super.focusGained(e);
|
238
|
selectionService.setSelection(new StructuredSelection(descriptiveDataSet));
|
239
|
}
|
240
|
});
|
241
|
selectionService.setSelection(new StructuredSelection(descriptiveDataSet));
|
242
|
thisPart.setLabel(descriptiveDataSet.getLabel());
|
243
|
}
|
244
|
|
245
|
public void addTaxonNode(TaxonNodeDto taxonNodeDto){
|
246
|
// TaxonNode taxonNode = cdmEntitySession.remoteLoad(CdmStore.getService(ITaxonNodeService.class), taxonNodeDto.getUuid());
|
247
|
//check if node belongs to same classification
|
248
|
Set<TaxonNode> taxonSubtreeFilter = descriptiveDataSet.getTaxonSubtreeFilter();
|
249
|
if(taxonSubtreeFilter!=null && !taxonSubtreeFilter.isEmpty()){
|
250
|
if(!taxonSubtreeFilter.iterator().next().getClassification().getUuid().equals(taxonNodeDto.getClassificationUUID())){
|
251
|
MessagingUtils.warningDialog(Messages.TaxonNodeDropAdapter_CLASSIFICATIONS_NO_MATCH, this.getClass(),
|
252
|
Messages.TaxonNodeDropAdapter_CLASSIFICATIONS_NO_MATCH_MESSAGE);
|
253
|
return;
|
254
|
}
|
255
|
}
|
256
|
Viewer taxonTreeViewer = getTaxonTreeViewer();
|
257
|
Object input = taxonTreeViewer.getInput();
|
258
|
Collection<TaxonNodeDto> treeNodes;
|
259
|
if(input==null){
|
260
|
treeNodes = new ArrayList<>();
|
261
|
}
|
262
|
else{
|
263
|
treeNodes = (Collection<TaxonNodeDto>) input;
|
264
|
}
|
265
|
treeNodes.add(taxonNodeDto);
|
266
|
taxonTreeViewer.setInput(treeNodes);
|
267
|
dirty.setDirty(true);
|
268
|
}
|
269
|
|
270
|
public void removeSelectedTaxonNodes(){
|
271
|
IStructuredSelection selection = (IStructuredSelection) composite.getTaxonNodeTree().getSelection();
|
272
|
if(selection.toList().stream().anyMatch(object->!(object instanceof TaxonNodeDto))){
|
273
|
MessagingUtils.warningDialog(Messages.DescriptiveDataSetEditor_DELETE_FAIL_TITLE, this.getClass(), Messages.DescriptiveDataSetEditor_DELETE_FAIL_MESSAGE);
|
274
|
return;
|
275
|
}
|
276
|
Iterator<Object> iterator = selection.iterator();
|
277
|
Viewer taxonTreeViewer = getTaxonTreeViewer();
|
278
|
Collection<TaxonNodeDto> input = (Collection<TaxonNodeDto>) taxonTreeViewer.getInput();
|
279
|
while(iterator.hasNext()){
|
280
|
Object next = iterator.next();
|
281
|
TaxonNodeDto taxonNode = (TaxonNodeDto) next;
|
282
|
input.remove(taxonNode);
|
283
|
TaxonNode node = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid());
|
284
|
descriptiveDataSet.removeTaxonSubtree(node);
|
285
|
}
|
286
|
taxonTreeViewer.setInput(input);
|
287
|
dirty.setDirty(true);
|
288
|
}
|
289
|
|
290
|
@Persist
|
291
|
@Override
|
292
|
public void save(IProgressMonitor monitor) {
|
293
|
|
294
|
if (!conversation.isBound()) {
|
295
|
conversation.bind();
|
296
|
|
297
|
}
|
298
|
conversation.commit(true);
|
299
|
Collection<TermDto> areas = composite.getAreas();
|
300
|
Object input = composite.getTaxonNodeTree().getInput();
|
301
|
if(input!=null){
|
302
|
descriptiveDataSet.setTaxonSubtreeFilter(new HashSet<>());//clear existing filter
|
303
|
Collection<TaxonNodeDto> taxonNodes = (Collection<TaxonNodeDto>) input;
|
304
|
for (TaxonNodeDto taxonTreeNode : taxonNodes) {
|
305
|
TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonTreeNode.getUuid());
|
306
|
descriptiveDataSet.addTaxonSubtree(taxonNode);
|
307
|
}
|
308
|
}
|
309
|
TermTree<Feature> characters = composite.getCharacters();
|
310
|
if(characters!=null){
|
311
|
//save characters because they can be modified in this editor
|
312
|
characters.getDistinctTerms().forEach(character->CdmStore.getService(ITermService.class).merge(character,true));
|
313
|
}
|
314
|
|
315
|
|
316
|
DefinedTermBase rankMaxSelection = composite.getRankMax().getSelection();
|
317
|
Rank rankMax = null;
|
318
|
if(rankMaxSelection instanceof Rank){
|
319
|
rankMax = (Rank) rankMaxSelection;
|
320
|
}
|
321
|
DefinedTermBase rankMinSelection = composite.getRankMin().getSelection();
|
322
|
Rank rankMin = null;
|
323
|
if(rankMinSelection instanceof Rank){
|
324
|
rankMin = (Rank) rankMinSelection;
|
325
|
}
|
326
|
|
327
|
descriptiveDataSet.setMaxRank(rankMax);
|
328
|
descriptiveDataSet.setMinRank(rankMin);
|
329
|
descriptiveDataSet.setDescriptiveSystem(characters);
|
330
|
List<DefinedTermBase> terms = CdmStore.getService(ITermService.class)
|
331
|
.load(areas.stream().map(area -> area.getUuid()).collect(Collectors.toList()), null);
|
332
|
Set<NamedArea> areaTerms = new HashSet<>();
|
333
|
terms.forEach(term->areaTerms.add((NamedArea) term));
|
334
|
descriptiveDataSet.setGeoFilter(areaTerms);
|
335
|
|
336
|
// conversation.commit();
|
337
|
CdmStore.getService(IDescriptiveDataSetService.class).merge(descriptiveDataSet, true);
|
338
|
//event for updating matrix
|
339
|
EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DESCRIPTIVE_DATASET, descriptiveDataSet.getUuid());
|
340
|
dirty.setDirty(false);
|
341
|
}
|
342
|
|
343
|
@Override
|
344
|
public boolean isDirty() {
|
345
|
return dirty.isDirty();
|
346
|
}
|
347
|
|
348
|
@PreDestroy
|
349
|
public void dispose() {
|
350
|
if (conversation != null) {
|
351
|
conversation.close();
|
352
|
conversation = null;
|
353
|
}
|
354
|
if(cdmEntitySession != null) {
|
355
|
cdmEntitySession.dispose();
|
356
|
cdmEntitySession = null;
|
357
|
}
|
358
|
dirty.setDirty(false);
|
359
|
selectionService.setSelection(null);
|
360
|
}
|
361
|
|
362
|
@Focus
|
363
|
public void setFocus() {
|
364
|
if(composite!=null){
|
365
|
composite.setFocus();
|
366
|
}
|
367
|
if (getConversationHolder() != null) {
|
368
|
getConversationHolder().bind();
|
369
|
}
|
370
|
if(cdmEntitySession != null) {
|
371
|
cdmEntitySession.bind();
|
372
|
}
|
373
|
}
|
374
|
|
375
|
public Viewer getTaxonTreeViewer() {
|
376
|
return composite.getTaxonNodeTree();
|
377
|
}
|
378
|
|
379
|
public DescriptiveDataSet getDescriptiveDataSet() {
|
380
|
return descriptiveDataSet;
|
381
|
}
|
382
|
|
383
|
@Override
|
384
|
public void update(CdmDataChangeMap arg0) {
|
385
|
}
|
386
|
|
387
|
@Override
|
388
|
public ICdmEntitySession getCdmEntitySession() {
|
389
|
return cdmEntitySession;
|
390
|
}
|
391
|
|
392
|
@Override
|
393
|
public Collection<DescriptiveDataSet> getRootEntities() {
|
394
|
return Collections.singleton(descriptiveDataSet);
|
395
|
}
|
396
|
|
397
|
@Override
|
398
|
public Map<Object, List<String>> getPropertyPathsMap() {
|
399
|
return null;
|
400
|
}
|
401
|
|
402
|
@Override
|
403
|
public ConversationHolder getConversationHolder() {
|
404
|
return conversation;
|
405
|
}
|
406
|
|
407
|
@Override
|
408
|
public void changed(Object element) {
|
409
|
dirty.setDirty(true);
|
410
|
}
|
411
|
|
412
|
@Override
|
413
|
public void forceDirty() {
|
414
|
dirty.setDirty(true);
|
415
|
}
|
416
|
|
417
|
}
|