Project

General

Profile

Download (4.92 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.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
					description = descriptionElement.getInDescription();
80
					description.removeElement(descriptionElement);
81
					target.addElement(descriptionElement);
82

    
83
					if (description.getElements().isEmpty() ){
84
					    if (description instanceof TaxonDescription){
85
					        ((TaxonDescription)description).getTaxon().removeDescription((TaxonDescription)description, false);
86
					    }else if (description instanceof TaxonNameDescription){
87
					        ((TaxonNameDescription)description).getTaxonName().removeDescription((TaxonNameDescription)description);
88
					    } else if (description instanceof SpecimenDescription){
89
                            ((SpecimenDescription)description).getDescribedSpecimenOrObservation().removeDescription(description);
90
                        }
91
					}
92
					descriptionElements.add(descriptionElement);
93
				}
94
			}
95
			TaxonNameEditorE4 editor = null;
96
			Object activePart = EditorUtil.getActivePart();
97
			if (activePart instanceof FactualDataPartE4){
98
			    MPart selectionProvidingPart =((FactualDataPartE4)activePart).getSelectionProvidingPart();
99
			    Object obj = selectionProvidingPart.getObject();
100
			    if (obj instanceof TaxonNameEditorE4){
101
			        editor = (TaxonNameEditorE4) obj;
102
			    }
103
			}
104
			AbstractPostTaxonOperation operation = new MoveDescriptionElementsOperation(Messages.DescriptionElementDropAdapter_MOVE_DESC, EditorUtil.getUndoContext(), target, description, descriptionElements, isCopy, null, sync);
105

    
106
			editor.getEditorInput().addOperation(operation);
107
			editor.setDirty();
108
//			EditorUtil.executeOperation(operation, sync);
109
			return true;
110
		}
111
		MessagingUtils.warningDialog(OPERATION_NOT_SUPPORTED_YET, this, Messages.DescriptionElementDropAdapter_NOT_SUPPORTED_EMPTY_ELEMENT);
112

    
113
		return false;
114

    
115
	}
116

    
117
	/* (non-Javadoc)
118
	 * @see org.eclipse.jface.viewers.ViewerDropAdapter#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
119
	 */
120
	@Override
121
	public boolean validateDrop(Object target, int operation,
122
			TransferData transferData) {
123
		boolean transferDataIsSupported = DescriptionElementTransfer.getInstance().isSupportedType(
124
				transferData);
125
		return target instanceof DescriptionBase && transferDataIsSupported;
126
	}
127

    
128
}
(2-2/6)