Project

General

Profile

Download (5.28 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.ViewerDropAdapter;
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.cdm.persistence.dto.TermVocabularyDto;
25
import eu.etaxonomy.taxeditor.editor.definedterm.operation.MoveDefinedTermOperation;
26
import eu.etaxonomy.taxeditor.l10n.Messages;
27
import eu.etaxonomy.taxeditor.model.AbstractUtility;
28
import eu.etaxonomy.taxeditor.model.MessagingUtils;
29
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
30
import eu.etaxonomy.taxeditor.store.StoreUtil;
31
import eu.etaxonomy.taxeditor.ui.EditViewerDropAdapter;
32

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

    
41

    
42
    public static final String TERM_TYPE_ERROR_TITLE = Messages.DefinedTermDropAdapterE4_TERM_TYPE_ERROR_TITLE;
43
    public static final String TERM_TYPE_ERROR_MESSAGE = Messages.DefinedTermDropAdapterE4_TERM_TYPE_ERROR_MESSAGE;
44

    
45
    public static final String MOVE_FAILED = Messages.DefinedTermDropAdapterE4_MOVE_FAILED;
46

    
47
    private static final String MOVE_FAILED_MESSAGE = Messages.DefinedTermDropAdapterE4_MOVE_FAILED_MESSAGE;
48

    
49
	private final DefinedTermEditorE4 editor;
50

    
51
	@Inject
52
    private UISynchronize sync;
53

    
54
	@Inject
55
	protected DefinedTermDropAdapterE4(DefinedTermEditorE4 editor) {
56
		super(editor.getViewer());
57
		this.editor = editor;
58
	}
59

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

    
70
		for (Object droppedTerm : droppedTerms.toList()) {
71
		    TermDto term = (TermDto) droppedTerm;
72
			sourceTerms.add(term);
73
		}
74
        // cannot drop a term onto itself or at its own children
75
        for (Object droppedObject : sourceTerms) {
76
            if(droppedObject==null){
77
                MessagingUtils.warningDialog(MOVE_FAILED, this.getClass(),
78
                        Messages.DefinedTermDropAdapterE4_MOVE_FAILED_SAVE_MESSAGE);
79
                return false;
80
            }
81
            if (droppedObject.equals(target)) {
82
                MessagingUtils.warningDialog(MOVE_FAILED, this.getClass(),
83
                        MOVE_FAILED_MESSAGE);
84
                return false;
85
            }
86
            if(target instanceof TermDto){
87
                TermDto parentTerm = (TermDto)target;
88
                //check term type compatibility
89
                if(droppedObject instanceof TermDto && !((TermDto) droppedObject).getTermType().equals(parentTerm.getTermType())){
90
                    MessagingUtils.warningDialog(TERM_TYPE_ERROR_TITLE, this, TERM_TYPE_ERROR_MESSAGE);
91
                    return false;
92
                }
93

    
94
                TermDto partOfDto = parentTerm.getPartOfDto();
95
                if(partOfDto!=null && partOfDto.equals(droppedObject)){
96
                    MessagingUtils.warningDialog(MOVE_FAILED, this.getClass(),
97
                            MOVE_FAILED_MESSAGE);
98
                    return false;
99
                }
100
                TermDto kindOfDto = parentTerm.getKindOfDto();
101
                if(kindOfDto!=null && kindOfDto.equals(droppedObject)){
102
                    MessagingUtils.warningDialog(MOVE_FAILED, this.getClass(),
103
                            MOVE_FAILED_MESSAGE);
104
                    return false;
105
                }
106
            }
107
            else if(target instanceof TermVocabularyDto){
108
                TermVocabularyDto vocDto = (TermVocabularyDto)target;
109
                //check term type compatibility
110
                if(droppedObject instanceof TermDto && !((TermDto) droppedObject).getTermType().equals(vocDto.getTermType())){
111
                    MessagingUtils.warningDialog(TERM_TYPE_ERROR_TITLE, this, TERM_TYPE_ERROR_MESSAGE);
112
                    return false;
113
                }
114
            }
115
        }
116
		AbstractPostOperation operation = new MoveDefinedTermOperation(Messages.DefinedTermDropAdapterE4_MOVE_DESCRIPTIONS,
117
		        StoreUtil.getUndoContext(),
118
		        target,
119
		        sourceTerms,
120
		        editor,
121
		        currentLocation);
122
		AbstractUtility.executeOperation(operation, sync);
123

    
124
		return true;
125
	}
126

    
127
	@Override
128
	public boolean validateDrop(Object target, int operation,
129
			TransferData transferType) {
130
	    boolean valid = LocalSelectionTransfer.getTransfer().isSupportedType(transferType)
131
                && target instanceof AbstractTermDto;
132
	    if(target instanceof TermVocabularyDto && getCurrentLocation()!=ViewerDropAdapter.LOCATION_ON){
133
	        valid = false;
134
	    }
135
        return valid;
136
	}
137

    
138
}
(2-2/4)