Merge branch 'hotfix/5.45.1'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / DescriptionElementDropAdapter.java
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 package eu.etaxonomy.taxeditor.editor.view.descriptive;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13
14 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
15 import org.eclipse.jface.viewers.Viewer;
16 import org.eclipse.swt.dnd.DND;
17 import org.eclipse.swt.dnd.TransferData;
18
19 import eu.etaxonomy.cdm.model.description.DescriptionBase;
20 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
21 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
22 import eu.etaxonomy.taxeditor.editor.EditorUtil;
23 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
24 import eu.etaxonomy.taxeditor.editor.name.e4.TaxonEditor;
25 import eu.etaxonomy.taxeditor.editor.view.descriptive.e4.FactualDataPartE4;
26 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
27 import eu.etaxonomy.taxeditor.model.MessagingUtils;
28 import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
29 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
30 import eu.etaxonomy.taxeditor.ui.EditViewerDropAdapter;
31
32 /**
33 * @author n.hoffmann
34 * @created Feb 8, 2011
35 */
36 public class DescriptionElementDropAdapter extends EditViewerDropAdapter {
37
38 private static final String OPERATION_NOT_SUPPORTED_YET = Messages.DescriptionElementDropAdapter_NOT_SUPPORTED;
39
40 public DescriptionElementDropAdapter(Viewer viewer) {
41 super(viewer);
42 }
43
44 @Override
45 public boolean performDrop(Object data) {
46 DescriptionBase target = (DescriptionBase) getCurrentTarget();
47 Object[] droppedElements = (Object[]) data;
48
49 Collection<DescriptionElementBase> descriptionElements = new ArrayList<DescriptionElementBase>();
50
51 boolean isCopy = getCurrentOperation() == DND.DROP_COPY ? true : false;
52 DescriptionBase<?> description = null;
53 boolean containsSpecimen = false;
54 // cannot drop a feature node onto itself
55 if (droppedElements != null){
56 for (Object droppedElement : droppedElements) {
57 if (droppedElement == null){
58 MessagingUtils.warningDialog(OPERATION_NOT_SUPPORTED_YET, this, Messages.DescriptionElementDropAdapter_NOT_SUPPORTED_NEW_ELEMENT);
59 return false;
60 }
61 if(! (droppedElement instanceof DescriptionElementBase)){
62 return false;
63 }else{
64 DescriptionElementBase descriptionElement = (DescriptionElementBase) droppedElement;
65
66 if (descriptionElement.getInDescription().equals(target)) {
67 return false;
68 }
69 for (Object element : target.getElements()){
70 if (element instanceof DescriptionElementBase){
71 if( !((DescriptionElementBase)element).isPersisted()){
72 MessagingUtils.warningDialog(OPERATION_NOT_SUPPORTED_YET, this, Messages.DescriptionElementDropAdapter_NOT_SUPPORTED_NEW_ELEMENT_IN_DESCRIPTION);
73 return false;
74 }
75
76 }
77 }
78 for (Object element : descriptionElement.getInDescription().getElements()){
79 if (element instanceof DescriptionElementBase){
80 if( !((DescriptionElementBase)element).isPersisted()){
81 MessagingUtils.warningDialog(OPERATION_NOT_SUPPORTED_YET, this, Messages.DescriptionElementDropAdapter_NOT_SUPPORTED_NEW_ELEMENT_IN_DESCRIPTION);
82 return false;
83 }
84
85 }
86 }
87 description = descriptionElement.getInDescription();
88 description.removeElement(descriptionElement);
89 target.addElement(descriptionElement);
90 if (descriptionElement instanceof IndividualsAssociation && PreferencesUtil.getBooleanValue("AskForNewCurrentDetermination")) {
91 if (((IndividualsAssociation)descriptionElement).getAssociatedSpecimenOrObservation() != null && !((IndividualsAssociation)descriptionElement).getAssociatedSpecimenOrObservation().getDeterminations().isEmpty()) {
92 containsSpecimen = true;
93 }
94 }
95 descriptionElements.add(descriptionElement);
96 }
97 }
98 TaxonEditor editor = null;
99 Object activePart = EditorUtil.getActivePart();
100 if (activePart instanceof FactualDataPartE4){
101 MPart selectionProvidingPart =((FactualDataPartE4)activePart).getSelectionProvidingPart();
102 Object obj = selectionProvidingPart.getObject();
103 if (obj instanceof TaxonEditor){
104 editor = (TaxonEditor) obj;
105 }
106 }
107
108
109 AbstractPostTaxonOperation operation = new MoveDescriptionElementsOperation(Messages.DescriptionElementDropAdapter_MOVE_DESC, EditorUtil.getUndoContext(), target, description, descriptionElements, isCopy, null, sync, false, containsSpecimen);
110
111 editor.getEditorInput().addOperation(operation);
112 editor.setDirty();
113 // EditorUtil.executeOperation(operation, sync);
114 return true;
115 }
116 MessagingUtils.warningDialog(OPERATION_NOT_SUPPORTED_YET, this, Messages.DescriptionElementDropAdapter_NOT_SUPPORTED_EMPTY_ELEMENT);
117
118 return false;
119
120 }
121
122 @Override
123 public boolean validateDrop(Object target, int operation,
124 TransferData transferData) {
125 boolean transferDataIsSupported = true;
126 if (target instanceof DescriptionBase){
127 if((((DescriptionBase)target).isComputed() || ((DescriptionBase)target).isCloneForSource())&& PreferencesUtil.isComputedDesciptionHandlingDisabled()){
128 transferDataIsSupported = false;
129 }
130 }
131 transferDataIsSupported = DescriptionElementTransfer.getInstance().isSupportedType(
132 transferData) && transferDataIsSupported;
133 return target instanceof DescriptionBase && transferDataIsSupported;
134 }
135 }