fb77f256089cd34845f22fe1f737e40b40bc4a28
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / editor / definedterm / e4 / DefinedTermDropAdapterE4.java
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
43 public static final String TERM_TYPE_ERROR_TITLE = Messages.DefinedTermDropAdapterE4_TERM_TYPE_ERROR_TITLE;
44 public static final String TERM_TYPE_ERROR_MESSAGE = Messages.DefinedTermDropAdapterE4_TERM_TYPE_ERROR_MESSAGE;
45
46 public static final String MOVE_FAILED = Messages.DefinedTermDropAdapterE4_MOVE_FAILED;
47
48 private static final String MOVE_FAILED_MESSAGE = Messages.DefinedTermDropAdapterE4_MOVE_FAILED_MESSAGE;
49
50 private final DefinedTermEditorE4 editor;
51
52 @Inject
53 private UISynchronize sync;
54
55 @Inject
56 protected DefinedTermDropAdapterE4(DefinedTermEditorE4 editor) {
57 super(editor.getViewer());
58 this.editor = editor;
59 }
60
61 @Override
62 public boolean performDrop(Object data) {
63 if(StoreUtil.promptCheckIsDirty(editor) || data == null){
64 return false;
65 }
66 AbstractTermDto target = (AbstractTermDto) getCurrentTarget();//can be vocab
67 int currentLocation = getCurrentLocation();
68 IStructuredSelection droppedTerms = (IStructuredSelection) data;
69 Collection<TermDto> sourceTerms = new ArrayList<>(); //Arrays.asList(droppedElements)
70
71 for (Object droppedTerm : droppedTerms.toList()) {
72 TermDto term = (TermDto) droppedTerm;
73 sourceTerms.add(term);
74 }
75 // cannot drop a term onto itself or at its own children
76 for (Object droppedObject : sourceTerms) {
77 if(droppedObject==null){
78 MessagingUtils.warningDialog(MOVE_FAILED, this.getClass(),
79 Messages.DefinedTermDropAdapterE4_MOVE_FAILED_SAVE_MESSAGE);
80 return false;
81 }
82 if (droppedObject.equals(target)) {
83 MessagingUtils.warningDialog(MOVE_FAILED, this.getClass(),
84 MOVE_FAILED_MESSAGE);
85 return false;
86 }
87 if(target instanceof TermDto){
88 TermDto parentTerm = (TermDto)target;
89 //check term type compatibility
90 if(droppedObject instanceof TermDto && !((TermDto) droppedObject).getTermType().equals(parentTerm.getTermType())){
91 MessagingUtils.warningDialog(TERM_TYPE_ERROR_TITLE, this, TERM_TYPE_ERROR_MESSAGE);
92 return false;
93 }
94
95 TermDto partOfDto = parentTerm.getPartOfDto();
96 while(partOfDto!=null){
97 if(partOfDto.equals(droppedObject)){
98 MessagingUtils.warningDialog(MOVE_FAILED, this.getClass(),
99 MOVE_FAILED_MESSAGE);
100 return false;
101 }
102 }
103 TermDto kindOfDto = parentTerm.getKindOfDto();
104 while(kindOfDto!=null){
105 if(kindOfDto.equals(droppedObject)){
106 MessagingUtils.warningDialog(MOVE_FAILED, this.getClass(),
107 MOVE_FAILED_MESSAGE);
108 return false;
109 }
110 }
111 }
112 else if(target instanceof TermVocabularyDto){
113 TermVocabularyDto vocDto = (TermVocabularyDto)target;
114 //check term type compatibility
115 if(droppedObject instanceof TermDto && !((TermDto) droppedObject).getTermType().equals(vocDto.getTermType())){
116 MessagingUtils.warningDialog(TERM_TYPE_ERROR_TITLE, this, TERM_TYPE_ERROR_MESSAGE);
117 return false;
118 }
119 }
120 }
121 AbstractPostOperation operation = new MoveDefinedTermOperation(Messages.DefinedTermDropAdapterE4_MOVE_DESCRIPTIONS,
122 StoreUtil.getUndoContext(),
123 target,
124 sourceTerms,
125 editor,
126 currentLocation);
127 AbstractUtility.executeOperation(operation, sync);
128 // select the moved objects
129 editor.getViewer().setSelection(new StructuredSelection(sourceTerms));
130
131 return true;
132 }
133
134 @Override
135 public boolean validateDrop(Object target, int operation,
136 TransferData transferType) {
137 boolean valid = LocalSelectionTransfer.getTransfer().isSupportedType(transferType)
138 && target instanceof AbstractTermDto;
139 if(target instanceof TermVocabularyDto && getCurrentLocation()!=ViewerDropAdapter.LOCATION_ON){
140 valid = false;
141 }
142 return valid;
143 }
144
145 }