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
.StructuredSelection
;
20 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
21 import org
.eclipse
.swt
.dnd
.TransferData
;
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
;
40 public class DefinedTermDropAdapterE4
extends EditViewerDropAdapter
{
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
;
46 public static final String MOVE_FAILED
= Messages
.DefinedTermDropAdapterE4_MOVE_FAILED
;
48 private static final String MOVE_FAILED_MESSAGE
= Messages
.DefinedTermDropAdapterE4_MOVE_FAILED_MESSAGE
;
50 private final DefinedTermEditorE4 editor
;
53 private UISynchronize sync
;
56 protected DefinedTermDropAdapterE4(DefinedTermEditorE4 editor
) {
57 super(editor
.getViewer());
62 public boolean performDrop(Object data
) {
63 if(StoreUtil
.promptCheckIsDirty(editor
) || data
== null){
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)
71 for (Object droppedTerm
: droppedTerms
.toList()) {
72 TermDto term
= (TermDto
) droppedTerm
;
73 sourceTerms
.add(term
);
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
);
82 if (droppedObject
.equals(target
)) {
83 MessagingUtils
.warningDialog(MOVE_FAILED
, this.getClass(),
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
);
95 TermDto partOfDto
= parentTerm
.getPartOfDto();
96 if(partOfDto
!=null && partOfDto
.equals(droppedObject
)){
97 MessagingUtils
.warningDialog(MOVE_FAILED
, this.getClass(),
101 TermDto kindOfDto
= parentTerm
.getKindOfDto();
102 if(kindOfDto
!=null && kindOfDto
.equals(droppedObject
)){
103 MessagingUtils
.warningDialog(MOVE_FAILED
, this.getClass(),
104 MOVE_FAILED_MESSAGE
);
108 else if(target
instanceof TermVocabularyDto
){
109 TermVocabularyDto vocDto
= (TermVocabularyDto
)target
;
110 //check term type compatibility
111 if(droppedObject
instanceof TermDto
&& !((TermDto
) droppedObject
).getTermType().equals(vocDto
.getTermType())){
112 MessagingUtils
.warningDialog(TERM_TYPE_ERROR_TITLE
, this, TERM_TYPE_ERROR_MESSAGE
);
117 AbstractPostOperation operation
= new MoveDefinedTermOperation(Messages
.DefinedTermDropAdapterE4_MOVE_DESCRIPTIONS
,
118 StoreUtil
.getUndoContext(),
123 AbstractUtility
.executeOperation(operation
, sync
);
124 // select the moved objects
125 editor
.getViewer().setSelection(new StructuredSelection(sourceTerms
));
131 public boolean validateDrop(Object target
, int operation
,
132 TransferData transferType
) {
133 boolean valid
= LocalSelectionTransfer
.getTransfer().isSupportedType(transferType
)
134 && target
instanceof AbstractTermDto
;
135 if(target
instanceof TermVocabularyDto
&& getCurrentLocation()!=ViewerDropAdapter
.LOCATION_ON
){