2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.definedterm
.e4
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collection
;
14 import javax
.inject
.Inject
;
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
;
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
;
39 public class DefinedTermDropAdapterE4
extends EditViewerDropAdapter
{
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
;
45 public static final String MOVE_FAILED
= Messages
.DefinedTermDropAdapterE4_MOVE_FAILED
;
47 private static final String MOVE_FAILED_MESSAGE
= Messages
.DefinedTermDropAdapterE4_MOVE_FAILED_MESSAGE
;
49 private final DefinedTermEditorE4 editor
;
52 private UISynchronize sync
;
55 protected DefinedTermDropAdapterE4(DefinedTermEditorE4 editor
) {
56 super(editor
.getViewer());
61 public boolean performDrop(Object data
) {
62 if(StoreUtil
.promptCheckIsDirty(editor
) || data
== null){
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)
70 for (Object droppedTerm
: droppedTerms
.toList()) {
71 TermDto term
= (TermDto
) droppedTerm
;
72 sourceTerms
.add(term
);
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
);
81 if (droppedObject
.equals(target
)) {
82 MessagingUtils
.warningDialog(MOVE_FAILED
, this.getClass(),
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
);
94 TermDto partOfDto
= parentTerm
.getPartOfDto();
95 if(partOfDto
!=null && partOfDto
.equals(droppedObject
)){
96 MessagingUtils
.warningDialog(MOVE_FAILED
, this.getClass(),
100 TermDto kindOfDto
= parentTerm
.getKindOfDto();
101 if(kindOfDto
!=null && kindOfDto
.equals(droppedObject
)){
102 MessagingUtils
.warningDialog(MOVE_FAILED
, this.getClass(),
103 MOVE_FAILED_MESSAGE
);
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
);
116 AbstractPostOperation operation
= new MoveDefinedTermOperation(Messages
.DefinedTermDropAdapterE4_MOVE_DESCRIPTIONS
,
117 StoreUtil
.getUndoContext(),
122 AbstractUtility
.executeOperation(operation
, sync
);
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
){