3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.editor
.definedterm
.operation
;
12 import java
.util
.Collection
;
14 import org
.eclipse
.core
.commands
.ExecutionException
;
15 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
16 import org
.eclipse
.core
.runtime
.IAdaptable
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.core
.runtime
.Status
;
20 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
22 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
23 import eu
.etaxonomy
.cdm
.model
.common
.OrderedTermBase
;
24 import eu
.etaxonomy
.cdm
.model
.common
.OrderedTermVocabulary
;
25 import eu
.etaxonomy
.cdm
.model
.common
.TermBase
;
26 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
27 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
28 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostTaxonOperation
;
29 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
30 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
37 public class MoveDefinedTermOperation
extends AbstractPostTaxonOperation
{
39 private final Collection
<DefinedTermBase
> sourceTerms
;// the actual DefinedTermBase(s) we are moving
40 private final TermBase targetTermOrVocabulary
;// the target VOCABULARY or DefinedTerm we are moving these to
41 private final int currentLocation
;
46 * @param postOperationEnabled
48 public MoveDefinedTermOperation(String label
,
49 IUndoContext undoContext
,
51 Collection
<DefinedTermBase
> sourceTerms
,
52 IPostOperationEnabled postOperationEnabled
,
53 int currentLocation
) {
54 super(label
, undoContext
, postOperationEnabled
);
56 this.targetTermOrVocabulary
= target
;
57 this.sourceTerms
= sourceTerms
;
58 this.currentLocation
= currentLocation
;
62 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
65 public IStatus
execute(IProgressMonitor monitor
, IAdaptable info
)
66 throws ExecutionException
{
68 // need to make the moved DefinedTerm part of another DefinedTerm or Vocabulary (target)
69 // and remove it from old associations
71 //TODO move to ITermService
73 for (DefinedTermBase term
: sourceTerms
) {
74 // do nothing when moving it on itself
75 if(targetTermOrVocabulary
.equals(term
)){
76 Status status
= new Status(IStatus
.CANCEL
, StoreUtil
.getPluginId(), "Term can not be added to itself");
77 MessagingUtils
.informationDialog("", status
);
81 if (targetTermOrVocabulary
instanceof TermVocabulary
) {
82 TermVocabulary termVocabulary
= (TermVocabulary
)targetTermOrVocabulary
;
84 // do nothing when term is top level and gets added to the same vocabulary
85 if(term
.getPartOf() == null && termVocabulary
.equals(term
.getVocabulary())){
86 Status status
= new Status(IStatus
.CANCEL
, StoreUtil
.getPluginId(), "Term is already in this vocabulary");
87 MessagingUtils
.informationDialog("", status
);
92 termVocabulary
.addTerm(term
);
94 } else if (targetTermOrVocabulary
instanceof DefinedTermBase
) {
96 DefinedTermBase targetDefinedTerm
= (DefinedTermBase
) targetTermOrVocabulary
;
98 if(targetTermOrVocabulary
instanceof OrderedTermBase
&& term
instanceof OrderedTermBase
) {
99 TermVocabulary tVoc
= ((DefinedTermBase
) targetTermOrVocabulary
).getVocabulary();
100 if(tVoc
instanceof OrderedTermVocabulary
) {
101 OrderedTermVocabulary otVoc
= (OrderedTermVocabulary
)tVoc
;
102 // the link between the location and the add term (below / above)
103 // method is determined by the compare method in the
104 // DefinedTermEditor's ViewerSorter (DefinedTermSorter) class
105 if(currentLocation
== ViewerDropAdapter
.LOCATION_BEFORE
) {
106 otVoc
.addTermBelow((OrderedTermBase
)term
, (OrderedTermBase
)targetTermOrVocabulary
);
109 if(currentLocation
== ViewerDropAdapter
.LOCATION_AFTER
) {
110 otVoc
.addTermAbove((OrderedTermBase
)term
, (OrderedTermBase
)targetTermOrVocabulary
);
114 if(currentLocation
== ViewerDropAdapter
.LOCATION_ON
) {
115 targetDefinedTerm
.addIncludes(term
);
116 targetDefinedTerm
.getVocabulary().addTerm(term
);
122 return postExecute(targetTermOrVocabulary
);
125 private DefinedTermBase
cleanTerm(DefinedTermBase term
){
127 DefinedTermBase partOf
= term
.getPartOf();
129 partOf
.removeIncludes(term
);
133 TermVocabulary vocabulary
= term
.getVocabulary();
134 if(vocabulary
!= null){
135 vocabulary
.removeTerm(term
);
143 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
146 public IStatus
redo(IProgressMonitor monitor
, IAdaptable info
)
147 throws ExecutionException
{
148 // TODO Auto-generated method stub
153 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
156 public IStatus
undo(IProgressMonitor monitor
, IAdaptable info
)
157 throws ExecutionException
{
158 // TODO Auto-generated method stub