Project

General

Profile

Download (4.32 KB) Statistics
| Branch: | Tag: | Revision:
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.TaxonDescription;
23
import eu.etaxonomy.taxeditor.editor.EditorUtil;
24
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
25
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
26
import eu.etaxonomy.taxeditor.editor.view.descriptive.e4.FactualDataPartE4;
27
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
28
import eu.etaxonomy.taxeditor.model.MessagingUtils;
29
import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
30
import eu.etaxonomy.taxeditor.ui.EditViewerDropAdapter;
31

    
32
/**
33
 * @author n.hoffmann
34
 * @created Feb 8, 2011
35
 * @version 1.0
36
 */
37
public class DescriptionElementDropAdapter extends EditViewerDropAdapter {
38

    
39
    private static final String OPERATION_NOT_SUPPORTED_YET = Messages.DescriptionElementDropAdapter_NOT_SUPPORTED;
40

    
41

    
42

    
43
    /**
44
	 * @param viewer
45
	 */
46
	public DescriptionElementDropAdapter(Viewer viewer) {
47
		super(viewer);
48
	}
49

    
50
	/* (non-Javadoc)
51
	 * @see org.eclipse.jface.viewers.ViewerDropAdapter#performDrop(java.lang.Object)
52
	 */
53
	@Override
54
	public boolean performDrop(Object data) {
55
		TaxonDescription target = (TaxonDescription) getCurrentTarget();
56
		Object[] droppedElements = (Object[]) data;
57

    
58
		Collection<DescriptionElementBase> descriptionElements = new ArrayList<DescriptionElementBase>();
59

    
60
		boolean isCopy = getCurrentOperation() == DND.DROP_COPY ? true : false;
61

    
62
		// cannot drop a feature node onto itself
63
		if (droppedElements != null){
64
			for (Object droppedElement : droppedElements) {
65
				if (droppedElement == null){
66
					MessagingUtils.warningDialog(OPERATION_NOT_SUPPORTED_YET, this, Messages.DescriptionElementDropAdapter_NOT_SUPPORTED_NEW_ELEMENT);
67
					return false;
68
				}
69
				if(! (droppedElement instanceof DescriptionElementBase)){
70
					return false;
71
				}else{
72
					DescriptionElementBase descriptionElement = (DescriptionElementBase) droppedElement;
73

    
74
					if (descriptionElement.getInDescription().equals(target)) {
75
						return false;
76
					}
77
					DescriptionBase<?> description = descriptionElement.getInDescription();
78
					description.removeElement(descriptionElement);
79
					target.addElement(descriptionElement);
80

    
81
					if (description.getElements().isEmpty()){
82
					    ((TaxonDescription)description).getTaxon().removeDescription((TaxonDescription)description, false);
83
					}
84
					descriptionElements.add(descriptionElement);
85
				}
86
			}
87
			TaxonNameEditorE4 editor = null;
88
			Object activePart = EditorUtil.getActivePart();
89
			if (activePart instanceof FactualDataPartE4){
90
			    MPart selectionProvidingPart =(MPart) ((FactualDataPartE4)activePart).getSelectionProvidingPart();
91
			    Object obj = selectionProvidingPart.getObject();
92
			    if (obj instanceof TaxonNameEditorE4){
93
			        editor = (TaxonNameEditorE4) obj;
94
			    }
95
			}
96
			AbstractPostTaxonOperation operation = new MoveDescriptionElementsOperation(Messages.DescriptionElementDropAdapter_MOVE_DESC, EditorUtil.getUndoContext(), target, descriptionElements, isCopy, null, sync);
97

    
98
			editor.getEditorInput().addOperation(operation);
99
			editor.setDirty();
100
//			EditorUtil.executeOperation(operation, sync);
101
			return true;
102
		}
103
		MessagingUtils.warningDialog(OPERATION_NOT_SUPPORTED_YET, this, Messages.DescriptionElementDropAdapter_NOT_SUPPORTED_EMPTY_ELEMENT);
104

    
105
		return false;
106

    
107
	}
108

    
109
	/* (non-Javadoc)
110
	 * @see org.eclipse.jface.viewers.ViewerDropAdapter#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
111
	 */
112
	@Override
113
	public boolean validateDrop(Object target, int operation,
114
			TransferData transferData) {
115
		boolean transferDataIsSupported = DescriptionElementTransfer.getInstance().isSupportedType(
116
				transferData);
117
		return target instanceof TaxonDescription && transferDataIsSupported;
118
	}
119

    
120
}
(2-2/7)