Project

General

Profile

Download (4.71 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.jface.viewers.ViewerDropAdapter;
21
import org.eclipse.swt.dnd.TransferData;
22

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

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

    
42
    public static final String MOVE_FAILED = Messages.DefinedTermDropAdapterE4_MOVE_FAILED;
43

    
44
    private static final String MOVE_FAILED_MESSAGE = Messages.DefinedTermDropAdapterE4_MOVE_FAILED_MESSAGE;
45

    
46
	private final DefinedTermEditorE4 editor;
47

    
48
	@Inject
49
    private UISynchronize sync;
50

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

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

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

    
116
		return true;
117
	}
118

    
119
	@Override
120
	public boolean validateDrop(Object target, int operation,
121
			TransferData transferType) {
122
	    boolean valid = LocalSelectionTransfer.getTransfer().isSupportedType(transferType)
123
                && target instanceof AbstractTermDto;
124
	    if(target instanceof TermVocabularyDto && getCurrentLocation()!=ViewerDropAdapter.LOCATION_ON){
125
	        valid = false;
126
	    }
127
        return valid;
128
	}
129

    
130
}
(2-2/4)