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