Project

General

Profile

Download (4.42 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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.definedterm.e4;
10

    
11
import java.util.ArrayList;
12
import java.util.Collection;
13

    
14
import javax.inject.Inject;
15

    
16
import org.eclipse.e4.ui.di.UISynchronize;
17
import org.eclipse.jface.util.LocalSelectionTransfer;
18
import org.eclipse.jface.viewers.IStructuredSelection;
19
import org.eclipse.jface.viewers.StructuredSelection;
20
import org.eclipse.swt.dnd.TransferData;
21

    
22
import eu.etaxonomy.cdm.persistence.dto.AbstractTermDto;
23
import eu.etaxonomy.cdm.persistence.dto.TermDto;
24
import eu.etaxonomy.taxeditor.editor.definedterm.operation.MoveDefinedTermOperation;
25
import eu.etaxonomy.taxeditor.l10n.Messages;
26
import eu.etaxonomy.taxeditor.model.AbstractUtility;
27
import eu.etaxonomy.taxeditor.model.MessagingUtils;
28
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
29
import eu.etaxonomy.taxeditor.store.StoreUtil;
30
import eu.etaxonomy.taxeditor.ui.EditViewerDropAdapter;
31

    
32
/**
33
 *
34
 * @author pplitzner
35
 * @since Aug 22, 2017
36
 *
37
 */
38
public class DefinedTermDropAdapterE4 extends EditViewerDropAdapter {
39

    
40
    public static final String MOVE_FAILED = Messages.DefinedTermDropAdapterE4_MOVE_FAILED;
41

    
42
    private static final String MOVE_FAILED_MESSAGE = Messages.DefinedTermDropAdapterE4_MOVE_FAILED_MESSAGE;
43

    
44
	private final DefinedTermEditorE4 editor;
45

    
46
	@Inject
47
    private UISynchronize sync;
48

    
49
	@Inject
50
	protected DefinedTermDropAdapterE4(DefinedTermEditorE4 editor) {
51
		super(editor.getViewer());
52
		this.editor = editor;
53
	}
54

    
55
	@Override
56
	public boolean performDrop(Object data) {
57
	    if(StoreUtil.promptCheckIsDirty(editor)){
58
	        return false;
59
	    }
60
		AbstractTermDto target = (AbstractTermDto) getCurrentTarget();//can be vocab
61
		int currentLocation = getCurrentLocation();
62
		IStructuredSelection droppedTerms = (IStructuredSelection) data;
63
		Collection<TermDto> sourceTerms = new ArrayList<>(); //Arrays.asList(droppedElements)
64

    
65
		for (Object droppedTerm : droppedTerms.toList()) {
66
		    TermDto term = (TermDto) droppedTerm;
67
			sourceTerms.add(term);
68
		}
69
        // cannot drop a term onto itself or at its own children
70
        for (Object droppedObject : sourceTerms) {
71
            if(droppedObject==null){
72
                MessagingUtils.warningDialog(MOVE_FAILED, this.getClass(),
73
                        Messages.DefinedTermDropAdapterE4_MOVE_FAILED_SAVE_MESSAGE);
74
                return false;
75
            }
76
            if (droppedObject.equals(target)) {
77
                MessagingUtils.warningDialog(MOVE_FAILED, this.getClass(),
78
                        MOVE_FAILED_MESSAGE);
79
                return false;
80
            }
81
            // check if target is a child of the dropped term
82
            if(target instanceof TermDto){
83
                TermDto parentTerm = (TermDto)target;
84
                TermDto partOfDto = parentTerm.getPartOfDto();
85
                while(partOfDto!=null){
86
                    if(partOfDto.equals(droppedObject)){
87
                        MessagingUtils.warningDialog(MOVE_FAILED, this.getClass(),
88
                                MOVE_FAILED_MESSAGE);
89
                        return false;
90
                    }
91
                    partOfDto = partOfDto.getPartOfDto();
92
                }
93
                TermDto kindOfDto = parentTerm.getKindOfDto();
94
                while(kindOfDto!=null){
95
                    if(kindOfDto.equals(droppedObject)){
96
                        MessagingUtils.warningDialog(MOVE_FAILED, this.getClass(),
97
                                MOVE_FAILED_MESSAGE);
98
                        return false;
99
                    }
100
                    kindOfDto = kindOfDto.getKindOfDto();
101
                }
102
            }
103
        }
104
		AbstractPostOperation operation = new MoveDefinedTermOperation(Messages.DefinedTermDropAdapterE4_MOVE_DESCRIPTIONS,
105
		        StoreUtil.getUndoContext(),
106
		        target,
107
		        sourceTerms,
108
		        editor,
109
		        currentLocation);
110
		AbstractUtility.executeOperation(operation, sync);
111
		// select the newly moved objects
112
		editor.getViewer().setSelection(new StructuredSelection(sourceTerms));
113

    
114
		return true;
115
	}
116

    
117
	@Override
118
	public boolean validateDrop(Object target, int operation,
119
			TransferData transferType) {
120
        return LocalSelectionTransfer.getTransfer().isSupportedType(transferType) && target instanceof AbstractTermDto;
121
	}
122

    
123
}
(2-2/4)