2 * Copyright (C) 2017 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
.featuretree
.e4
;
12 import java
.util
.Arrays
;
13 import java
.util
.Collection
;
14 import java
.util
.Collections
;
16 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
17 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
18 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
19 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
20 import org
.eclipse
.jface
.viewers
.TreeViewer
;
21 import org
.eclipse
.jface
.viewers
.Viewer
;
22 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
23 import org
.eclipse
.swt
.dnd
.DND
;
24 import org
.eclipse
.swt
.dnd
.TransferData
;
26 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureNodeService
;
27 import eu
.etaxonomy
.cdm
.model
.common
.OrderedTermVocabulary
;
28 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
29 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
30 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
31 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
32 import eu
.etaxonomy
.taxeditor
.editor
.definedterm
.TermTransfer
;
33 import eu
.etaxonomy
.taxeditor
.featuretree
.FeatureNodeTransfer
;
34 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
35 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
36 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
37 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.TermVocabularySelectionDialog
;
38 import eu
.etaxonomy
.taxeditor
.view
.webimport
.termimport
.wrapper
.OntologyTermWrapper
;
39 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
41 public class FeatureNodeDropAdapter
extends ViewerDropAdapter
{
43 public static final String SAVE_CHANGES_TITLE
= "Unsaved changes";
44 public static final String SAVE_CHANGES_MESSAGE
= "The editor has unsaved changes. You need to save before performind this operation. Save now?";
46 private MDirtyable dirtyable
;
47 private IE4SavablePart savablePart
;
49 public FeatureNodeDropAdapter(MDirtyable dirtyable
, IE4SavablePart savablePart
, Viewer viewer
) {
51 this.dirtyable
= dirtyable
;
52 this.savablePart
= savablePart
;
56 public boolean performDrop(Object data
) {
57 if(dirtyable
.isDirty()){
58 if(MessagingUtils
.confirmDialog(SAVE_CHANGES_TITLE
, SAVE_CHANGES_MESSAGE
)){
59 savablePart
.save(new NullProgressMonitor());
65 Object currentTarget
= getCurrentTarget();
66 FeatureNode target
= null;
67 if(currentTarget
instanceof FeatureTree
){
68 target
= ((FeatureTree
) currentTarget
).getRoot();
70 else if(currentTarget
instanceof FeatureNode
){
71 target
= (FeatureNode
) currentTarget
;
76 int location
= getCurrentLocation();
77 FeatureNode parent
= target
.getParent();
79 if (location
== LOCATION_BEFORE
) {
80 position
= Math
.max(0, parent
.getIndex(target
) - 1);
84 if (location
== LOCATION_AFTER
) {
85 position
= parent
.getIndex(target
);
92 MessagingUtils
.warningDialog(Messages
.FeatureNodeDropAdapter_INVALID_TARGET
, this, Messages
.FeatureNodeDropAdapter_INVALID_TARGET_MESSAGE
);
95 Collection
<Object
> droppedObjects
= Collections
.emptyList();
96 if(data
instanceof Object
[]){
97 droppedObjects
= Arrays
.asList((Object
[])data
);
99 else if(data
instanceof IStructuredSelection
){
100 droppedObjects
= ((IStructuredSelection
) data
).toList();
102 TreeViewer viewer
= (TreeViewer
) getViewer();
104 // cannot drop a feature node onto itself
105 for (Object droppedObject
: droppedObjects
) {
106 if(droppedObject
==null){
107 MessagingUtils
.warningDialog("Move failed", this.getClass(),
108 "Moving the feature node failed. Try saving before.");
111 if (droppedObject
.equals(target
)) {
115 for (Object droppedObject
: droppedObjects
) {
116 if(droppedObject
instanceof FeatureNode
){
117 FeatureNode droppedNode
= (FeatureNode
) droppedObject
;
119 if(getCurrentOperation()==DND
.DROP_MOVE
){
120 CdmStore
.getService(IFeatureNodeService
.class).moveFeatureNode(droppedNode
.getUuid(), target
.getUuid(), position
);
123 else if(getCurrentOperation()==DND
.DROP_COPY
){
124 CdmStore
.getService(IFeatureNodeService
.class).addChildFeatureNode(target
.getUuid(), droppedNode
.getFeature().getUuid());
126 viewer
.reveal(droppedNode
);
128 else if(droppedObject
instanceof Feature
){
129 Feature droppedFeature
= (Feature
) droppedObject
;
130 CdmStore
.getService(IFeatureNodeService
.class).addChildFeatureNode(target
.getUuid(), droppedFeature
.getUuid());
132 else if(droppedObject
instanceof OntologyTermWrapper
){
133 OntologyTermWrapper wrapper
= (OntologyTermWrapper
)droppedObject
;
134 TermVocabulary vocabulary
= wrapper
.getTermVocabulary();
135 if(vocabulary
==null){
136 vocabulary
= TermVocabularySelectionDialog
.select(
137 "Choose vocabulary for import", viewer
.getControl().getShell(), null);
138 if(vocabulary
instanceof OrderedTermVocabulary
){
139 MessagingUtils
.warningDialog("Import not possible", this,
140 "The chosen vocabulary is an ordered vocabulary.\n"
141 + "Importing into ordered vocabularies is currently not supported.");
145 if(vocabulary
==null){
149 Feature feature
= Feature
.NewInstance(wrapper
.getDescription(), wrapper
.getLabel(), null);
150 feature
.setUri(URI
.create(wrapper
.getUri()));
151 vocabulary
.addTerm(feature
);
152 CdmStore
.getService(IFeatureNodeService
.class).createChildFeatureNode(target
, feature
);
161 public boolean validateDrop(Object target
, int operation
, TransferData transferData
) {
162 boolean isSupported
= FeatureNodeTransfer
.getInstance().isSupportedType(transferData
);
163 isSupported
|= TermTransfer
.getInstance().isSupportedType(transferData
);
164 isSupported
|= LocalSelectionTransfer
.getTransfer().isSupportedType(transferData
);
165 isSupported
&= getViewer().getInput()!=null;