Merge branch 'develop' into LibrAlign
[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.jface.viewers.Viewer;
16 import org.eclipse.jface.viewers.ViewerDropAdapter;
17 import org.eclipse.swt.dnd.DND;
18 import org.eclipse.swt.dnd.TransferData;
19
20 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
21 import eu.etaxonomy.cdm.model.description.TaxonDescription;
22 import eu.etaxonomy.taxeditor.editor.EditorUtil;
23 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
24 import eu.etaxonomy.taxeditor.model.MessagingUtils;
25 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
26
27 /**
28 * @author n.hoffmann
29 * @created Feb 8, 2011
30 * @version 1.0
31 */
32 public class DescriptionElementDropAdapter extends ViewerDropAdapter {
33
34 /**
35 * @param viewer
36 */
37 public DescriptionElementDropAdapter(Viewer viewer) {
38 super(viewer);
39 }
40
41 /* (non-Javadoc)
42 * @see org.eclipse.jface.viewers.ViewerDropAdapter#performDrop(java.lang.Object)
43 */
44 @Override
45 public boolean performDrop(Object data) {
46 TaxonDescription target = (TaxonDescription) 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
53 // cannot drop a feature node onto itself
54 if (droppedElements != null){
55 for (Object droppedElement : droppedElements) {
56 if (droppedElement == null){
57 MessagingUtils.warningDialog("Operation not supported yet", this, "We are currently unable to drag and drop a newly created element. Please save the editor to make this work.");
58 return false;
59 }
60 if(! (droppedElement instanceof DescriptionElementBase)){
61 return false;
62 }else{
63 DescriptionElementBase descriptionElement = (DescriptionElementBase) droppedElement;
64
65 if (descriptionElement.getInDescription().equals(target)) {
66 return false;
67 }
68
69 descriptionElements.add(descriptionElement);
70 }
71 }
72
73 AbstractPostOperation operation = new MoveDescriptionElementsOperation("Move Descriptions", EditorUtil.getUndoContext(), target, descriptionElements, isCopy, null);
74
75 EditorUtil.executeOperation(operation);
76 return true;
77 }
78 MessagingUtils.warningDialog("Operation not supported yet", this, "We are unable to drag and drop empty descriptions");
79
80 return false;
81
82 }
83
84 /* (non-Javadoc)
85 * @see org.eclipse.jface.viewers.ViewerDropAdapter#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
86 */
87 @Override
88 public boolean validateDrop(Object target, int operation,
89 TransferData transferData) {
90 boolean transferDataIsSupported = DescriptionElementTransfer.getInstance().isSupportedType(
91 transferData);
92 return target instanceof TaxonDescription && transferDataIsSupported;
93 }
94
95 }