1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.editor.name.e4.dnd;
|
5
|
|
6
|
import java.util.UUID;
|
7
|
|
8
|
import org.eclipse.core.runtime.Assert;
|
9
|
import org.eclipse.e4.ui.di.UISynchronize;
|
10
|
import org.eclipse.swt.dnd.DND;
|
11
|
import org.eclipse.swt.dnd.DropTargetAdapter;
|
12
|
import org.eclipse.swt.dnd.DropTargetEvent;
|
13
|
|
14
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
15
|
import eu.etaxonomy.cdm.model.metadata.SecReferenceHandlingEnum;
|
16
|
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
|
17
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
18
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
19
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
20
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
21
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
22
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
23
|
import eu.etaxonomy.taxeditor.editor.name.e4.IDropTargetableE4;
|
24
|
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
|
25
|
import eu.etaxonomy.taxeditor.editor.name.e4.container.AbstractHomotypicalGroupContainerE4;
|
26
|
import eu.etaxonomy.taxeditor.editor.name.e4.container.MisappliedGroupE4;
|
27
|
import eu.etaxonomy.taxeditor.editor.name.e4.operation.MoveSynonymToAnotherAcceptedTaxonOperationE4;
|
28
|
import eu.etaxonomy.taxeditor.editor.name.operation.ChangeConceptRelationshipTypeOperation;
|
29
|
import eu.etaxonomy.taxeditor.editor.name.operation.ChangeConceptToSynonymOperation;
|
30
|
import eu.etaxonomy.taxeditor.editor.name.operation.ChangeHomotypicGroupOperation;
|
31
|
import eu.etaxonomy.taxeditor.editor.name.operation.ChangeSynonymToMisapplicationOperation;
|
32
|
import eu.etaxonomy.taxeditor.event.EventUtility;
|
33
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
34
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
35
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
36
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
37
|
import eu.etaxonomy.taxeditor.ui.dialog.selection.ReferenceSelectionDialog;
|
38
|
|
39
|
/**
|
40
|
* @author pplitzner
|
41
|
* @date Aug 24, 2017
|
42
|
*/
|
43
|
public class NameEditorDropTargetListenerE4 extends DropTargetAdapter {
|
44
|
|
45
|
private IDropTargetableE4 target;
|
46
|
|
47
|
public NameEditorDropTargetListenerE4(IDropTargetableE4 target){
|
48
|
this.target = target;
|
49
|
}
|
50
|
|
51
|
@Override
|
52
|
public void dragEnter(DropTargetEvent dropTargetEvent) {
|
53
|
target.dragEntered();
|
54
|
}
|
55
|
|
56
|
@Override
|
57
|
public void dragLeave(DropTargetEvent dropTargetEvent) {
|
58
|
target.dragLeft();
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
public void drop(DropTargetEvent dropTargetEvent) {
|
63
|
|
64
|
TaxonBase<?> taxonBase = getDroppedTaxonBase(dropTargetEvent);
|
65
|
|
66
|
Assert.isNotNull(taxonBase);
|
67
|
|
68
|
if(taxonBase.getHomotypicGroup().equals(getHomotypicalGroup())){
|
69
|
// do nothing when dragged to the same group
|
70
|
dropTargetEvent.detail = DND.DROP_NONE;
|
71
|
return;
|
72
|
}
|
73
|
|
74
|
if (target instanceof MisappliedGroupE4){
|
75
|
dropTargetEvent.detail = DND.DROP_NONE;
|
76
|
return;
|
77
|
}
|
78
|
|
79
|
// Execute operations at end of drag event
|
80
|
AbstractPostOperation<?> operation = createOperation(taxonBase);
|
81
|
|
82
|
// Execute operation if it has been initiated
|
83
|
if (operation == null) {
|
84
|
MessagingUtils.warn(getClass(), String.format(Messages.NameEditorDropTargetListener_UNSUCCESSFULL_DROP, taxonBase.getClass(), target));
|
85
|
dropTargetEvent.detail = DND.DROP_NONE;
|
86
|
} else {
|
87
|
AbstractUtility.executeOperation(operation, target.getContext().get(UISynchronize.class));
|
88
|
}
|
89
|
|
90
|
}
|
91
|
|
92
|
private AbstractPostOperation createOperation(TaxonBase taxonBase) {
|
93
|
|
94
|
if(taxonBase instanceof Synonym){
|
95
|
Synonym synonym = (Synonym) taxonBase;
|
96
|
|
97
|
if(target instanceof AbstractHomotypicalGroupContainerE4){
|
98
|
HomotypicalGroup homotypicalGroup = ((AbstractHomotypicalGroupContainerE4) target).getGroup();
|
99
|
if (((AbstractHomotypicalGroupContainerE4) target).getEditor().getTaxon().equals(synonym.getAcceptedTaxon())){
|
100
|
//the actual accepted taxon is the same as the taxon of the target editor
|
101
|
try{
|
102
|
return new ChangeHomotypicGroupOperation(Messages.NameEditorDropTargetListener_CHANGE_HOMOTYPICAL_GROUP, getEditor().getUndoContext(),
|
103
|
((AbstractHomotypicalGroupContainerE4) target).getEditor().getTaxon(), synonym, homotypicalGroup, target);
|
104
|
}catch (NullPointerException e){
|
105
|
return null;
|
106
|
}
|
107
|
} else{
|
108
|
UUID newSecUuid = null;
|
109
|
try{
|
110
|
newSecUuid = getNewSecRef(synonym);
|
111
|
}catch (Exception e){
|
112
|
return null;
|
113
|
}
|
114
|
return new MoveSynonymToAnotherAcceptedTaxonOperationE4(Messages.NameEditorDropTargetListener_CHANGE_ACC_TAXON, getEditor().getUndoContext(), synonym.getUuid(), ((TaxonNameEditorE4) target).getTaxon(), newSecUuid, (TaxonNameEditorE4) target);
|
115
|
|
116
|
}
|
117
|
|
118
|
}
|
119
|
else if(target instanceof MisappliedGroupE4){
|
120
|
return new ChangeSynonymToMisapplicationOperation(Messages.NameEditorDropTargetListener_CHANGE_SYNONYM_TO_MISAPP, getEditor().getUndoContext(),
|
121
|
getEditor().getTaxon(), synonym, target);
|
122
|
}
|
123
|
else if(target instanceof TaxonNameEditorE4){
|
124
|
if (((TaxonNameEditorE4) target).getTaxon().equals(synonym.getAcceptedTaxon())){
|
125
|
return new ChangeHomotypicGroupOperation(Messages.NameEditorDropTargetListener_CHANGE_HOMOTYPICAL_GROUP, getEditor().getUndoContext(),
|
126
|
((TaxonNameEditorE4) target).getTaxon(), synonym, null, target);
|
127
|
} else{
|
128
|
UUID newSecUuid = null;
|
129
|
try{
|
130
|
newSecUuid = getNewSecRef(synonym);
|
131
|
}catch (Exception e){
|
132
|
return null;
|
133
|
}
|
134
|
return new MoveSynonymToAnotherAcceptedTaxonOperationE4(Messages.NameEditorDropTargetListener_CHANGE_ACC_TAXON, getEditor().getUndoContext(), synonym.getUuid(), ((TaxonNameEditorE4) target).getTaxon(), newSecUuid, (TaxonNameEditorE4) target);
|
135
|
|
136
|
}
|
137
|
}
|
138
|
|
139
|
}else if(taxonBase instanceof Taxon){
|
140
|
Taxon concept = (Taxon) taxonBase;
|
141
|
|
142
|
if(target instanceof AbstractHomotypicalGroupContainerE4){
|
143
|
HomotypicalGroup homotypicalGroup = ((AbstractHomotypicalGroupContainerE4) target).getGroup();
|
144
|
|
145
|
return new ChangeConceptToSynonymOperation(Messages.NameEditorDropTargetListener_CHANGE_SYNONYM, getEditor().getUndoContext(),
|
146
|
getEditor().getTaxon(), concept, homotypicalGroup, target);
|
147
|
}
|
148
|
else if(target instanceof MisappliedGroupE4){
|
149
|
return new ChangeConceptRelationshipTypeOperation(Messages.NameEditorDropTargetListener_CHANGE, getEditor().getUndoContext(),
|
150
|
getEditor().getTaxon(), concept, TaxonRelationshipType.MISAPPLIED_NAME_FOR(), target);
|
151
|
}
|
152
|
}
|
153
|
|
154
|
|
155
|
return null;
|
156
|
}
|
157
|
|
158
|
public UUID getNewSecRef(Synonym synonym) throws Exception{
|
159
|
Reference synSecRef = synonym.getSec();
|
160
|
UUID newSecUuid = null;
|
161
|
SecReferenceHandlingEnum secHandling = PreferencesUtil.getSecReferenceHandlingPreference();
|
162
|
Reference parentSecRef = ((AbstractHomotypicalGroupContainerE4) target).getEditor().getTaxon() != null? ((AbstractHomotypicalGroupContainerE4) target).getEditor().getTaxon().getSec():null;
|
163
|
if ((synSecRef != parentSecRef && secHandling.equals(SecReferenceHandlingEnum.KeepOrWarn) )|| secHandling.equals(SecReferenceHandlingEnum.KeepOrSelect)){
|
164
|
|
165
|
if ((parentSecRef != synSecRef && secHandling.equals(SecReferenceHandlingEnum.KeepOrWarn) )|| secHandling.equals(SecReferenceHandlingEnum.KeepOrSelect)){
|
166
|
int result = MessagingUtils.confirmDialog(Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Handling_title, Messages.MoveSynonymToAcceptedHandler_Select_Sec_Reference_Handling_message,
|
167
|
new String[]{Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Keep, Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Parent, Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Select});
|
168
|
if (result == 2){
|
169
|
Reference sec = ReferenceSelectionDialog.select(null, null);
|
170
|
newSecUuid = sec != null? sec.getUuid(): null;
|
171
|
}else if (result == 1){
|
172
|
newSecUuid = parentSecRef != null? parentSecRef.getUuid(): null;
|
173
|
}else if (result == 0){
|
174
|
newSecUuid = synSecRef != null? synSecRef.getUuid(): null;
|
175
|
}else{
|
176
|
return null;
|
177
|
}
|
178
|
|
179
|
}
|
180
|
|
181
|
}
|
182
|
return newSecUuid;
|
183
|
}
|
184
|
|
185
|
@Override
|
186
|
public void dropAccept(DropTargetEvent event) {
|
187
|
// pass
|
188
|
}
|
189
|
|
190
|
private HomotypicalGroup getHomotypicalGroup() {
|
191
|
if(target instanceof AbstractHomotypicalGroupContainerE4){
|
192
|
return ((AbstractHomotypicalGroupContainerE4) target).getGroup();
|
193
|
}
|
194
|
return null;
|
195
|
}
|
196
|
|
197
|
private TaxonBase getDroppedTaxonBase(DropTargetEvent dropTargetEvent){
|
198
|
if(dropTargetEvent.data instanceof ICdmBase[]){
|
199
|
|
200
|
ICdmBase[] cdmBaseObjects = (ICdmBase[]) dropTargetEvent.data;
|
201
|
|
202
|
if(cdmBaseObjects[0] instanceof TaxonBase){
|
203
|
return (TaxonBase) cdmBaseObjects[0];
|
204
|
}
|
205
|
}
|
206
|
return null;
|
207
|
}
|
208
|
|
209
|
private TaxonNameEditorE4 getEditor() {
|
210
|
return (TaxonNameEditorE4) EventUtility.getTaxonEditor();
|
211
|
}
|
212
|
}
|