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
.operation
;
11 import java
.util
.Collection
;
13 import org
.eclipse
.core
.commands
.ExecutionException
;
14 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
15 import org
.eclipse
.core
.runtime
.IAdaptable
;
16 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
17 import org
.eclipse
.core
.runtime
.IStatus
;
18 import org
.eclipse
.core
.runtime
.Status
;
19 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
21 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
22 import eu
.etaxonomy
.cdm
.model
.common
.OrderedTermBase
;
23 import eu
.etaxonomy
.cdm
.model
.common
.OrderedTermVocabulary
;
24 import eu
.etaxonomy
.cdm
.model
.common
.TermBase
;
25 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
26 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
27 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostTaxonOperation
;
28 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
29 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
36 public class MoveDefinedTermOperation
extends AbstractPostTaxonOperation
{
38 private final Collection
<DefinedTermBase
> sourceTerms
;// the actual DefinedTermBase(s) we are moving
39 private final TermBase targetTermOrVocabulary
;// the target VOCABULARY or DefinedTerm we are moving these to
40 private final int currentLocation
;
45 * @param postOperationEnabled
47 public MoveDefinedTermOperation(String label
,
48 IUndoContext undoContext
,
50 Collection
<DefinedTermBase
> sourceTerms
,
51 IPostOperationEnabled postOperationEnabled
,
52 int currentLocation
) {
53 super(label
, undoContext
, postOperationEnabled
);
55 this.targetTermOrVocabulary
= target
;
56 this.sourceTerms
= sourceTerms
;
57 this.currentLocation
= currentLocation
;
61 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
64 public IStatus
execute(IProgressMonitor monitor
, IAdaptable info
)
65 throws ExecutionException
{
67 // need to make the moved DefinedTerm part of another DefinedTerm or Vocabulary (target)
68 // and remove it from old associations
70 //TODO move to ITermService
72 for (DefinedTermBase term
: sourceTerms
) {
73 // do nothing when moving it on itself
74 if(targetTermOrVocabulary
.equals(term
)){
75 Status status
= new Status(IStatus
.CANCEL
, StoreUtil
.getPluginId(), "Term can not be added to itself");
76 MessagingUtils
.informationDialog("", status
);
80 if (targetTermOrVocabulary
instanceof TermVocabulary
) {
81 TermVocabulary termVocabulary
= (TermVocabulary
)targetTermOrVocabulary
;
83 // do nothing when term is top level and gets added to the same vocabulary
84 if(term
.getPartOf() == null && termVocabulary
.equals(term
.getVocabulary())){
85 Status status
= new Status(IStatus
.CANCEL
, StoreUtil
.getPluginId(), "Term is already in this vocabulary");
86 MessagingUtils
.informationDialog("", status
);
91 termVocabulary
.addTerm(term
);
93 } else if (targetTermOrVocabulary
instanceof DefinedTermBase
) {
95 DefinedTermBase targetDefinedTerm
= (DefinedTermBase
) targetTermOrVocabulary
;
97 if(targetTermOrVocabulary
instanceof OrderedTermBase
&& term
instanceof OrderedTermBase
) {
98 TermVocabulary tVoc
= ((DefinedTermBase
) targetTermOrVocabulary
).getVocabulary();
99 if(tVoc
instanceof OrderedTermVocabulary
) {
100 OrderedTermVocabulary otVoc
= (OrderedTermVocabulary
)tVoc
;
101 // the link between the location and the add term (below / above)
102 // method is determined by the compare method in the
103 // DefinedTermEditor's ViewerSorter (DefinedTermSorter) class
104 if(currentLocation
== ViewerDropAdapter
.LOCATION_BEFORE
) {
105 otVoc
.addTermBelow((OrderedTermBase
)term
, (OrderedTermBase
)targetTermOrVocabulary
);
106 ((DefinedTermBase
) targetTermOrVocabulary
).getPartOf().addIncludes(term
);
109 if(currentLocation
== ViewerDropAdapter
.LOCATION_AFTER
) {
110 otVoc
.addTermAbove((OrderedTermBase
)term
, (OrderedTermBase
)targetTermOrVocabulary
);
111 ((DefinedTermBase
) targetTermOrVocabulary
).getPartOf().addIncludes(term
);
113 if(currentLocation
== ViewerDropAdapter
.LOCATION_ON
) {
114 targetDefinedTerm
.addIncludes(term
);
115 targetDefinedTerm
.getVocabulary().addTerm(term
);
119 targetDefinedTerm
.addIncludes(term
);
120 targetDefinedTerm
.getVocabulary().addTerm(term
);
127 return postExecute(targetTermOrVocabulary
);
130 private DefinedTermBase
cleanTerm(DefinedTermBase term
){
132 DefinedTermBase partOf
= term
.getPartOf();
134 partOf
.removeIncludes(term
);
138 TermVocabulary vocabulary
= term
.getVocabulary();
139 if(vocabulary
!= null){
140 vocabulary
.removeTerm(term
);
148 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
151 public IStatus
redo(IProgressMonitor monitor
, IAdaptable info
)
152 throws ExecutionException
{
153 // TODO Auto-generated method stub
158 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
161 public IStatus
undo(IProgressMonitor monitor
, IAdaptable info
)
162 throws ExecutionException
{
163 // TODO Auto-generated method stub