ref #6190 removing svn property place holder in first line of code - java files
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / editor / definedterm / operation / MoveDefinedTermOperation.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.taxeditor.editor.definedterm.operation;
10
11 import java.util.Collection;
12
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;
20
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;
30
31 /**
32 * @author l.morris
33 * @date 10 Jan 2012
34 *
35 */
36 public class MoveDefinedTermOperation extends AbstractPostTaxonOperation {
37
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;
41
42 /**
43 * @param label
44 * @param undoContext
45 * @param postOperationEnabled
46 */
47 public MoveDefinedTermOperation(String label,
48 IUndoContext undoContext,
49 TermBase target,
50 Collection<DefinedTermBase> sourceTerms,
51 IPostOperationEnabled postOperationEnabled,
52 int currentLocation) {
53 super(label, undoContext, postOperationEnabled);
54
55 this.targetTermOrVocabulary = target;
56 this.sourceTerms = sourceTerms;
57 this.currentLocation = currentLocation;
58 }
59
60 /* (non-Javadoc)
61 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
62 */
63 @Override
64 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
65 throws ExecutionException {
66
67 // need to make the moved DefinedTerm part of another DefinedTerm or Vocabulary (target)
68 // and remove it from old associations
69
70 //TODO move to ITermService
71
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);
77 return status;
78 }
79
80 if (targetTermOrVocabulary instanceof TermVocabulary) {
81 TermVocabulary termVocabulary = (TermVocabulary)targetTermOrVocabulary;
82
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);
87 return status;
88 }
89
90 cleanTerm(term);
91 termVocabulary.addTerm(term);
92
93 } else if (targetTermOrVocabulary instanceof DefinedTermBase) {
94 cleanTerm(term);
95 DefinedTermBase targetDefinedTerm = (DefinedTermBase) targetTermOrVocabulary;
96
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);
107 }
108
109 if(currentLocation == ViewerDropAdapter.LOCATION_AFTER) {
110 otVoc.addTermAbove((OrderedTermBase)term, (OrderedTermBase)targetTermOrVocabulary);
111 ((DefinedTermBase) targetTermOrVocabulary).getPartOf().addIncludes(term);
112 }
113 if(currentLocation == ViewerDropAdapter.LOCATION_ON) {
114 targetDefinedTerm.addIncludes(term);
115 targetDefinedTerm.getVocabulary().addTerm(term);
116 }
117 }
118 } else{
119 targetDefinedTerm.addIncludes(term);
120 targetDefinedTerm.getVocabulary().addTerm(term);
121 }
122
123 }
124
125 }
126 //return null;
127 return postExecute(targetTermOrVocabulary);
128 }
129
130 private DefinedTermBase cleanTerm(DefinedTermBase term){
131
132 DefinedTermBase partOf = term.getPartOf();
133 if(partOf != null){
134 partOf.removeIncludes(term);
135 }
136
137
138 TermVocabulary vocabulary = term.getVocabulary();
139 if(vocabulary != null){
140 vocabulary.removeTerm(term);
141 }
142
143 return term;
144 }
145
146
147 /* (non-Javadoc)
148 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
149 */
150 @Override
151 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
152 throws ExecutionException {
153 // TODO Auto-generated method stub
154 return null;
155 }
156
157 /* (non-Javadoc)
158 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
159 */
160 @Override
161 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
162 throws ExecutionException {
163 // TODO Auto-generated method stub
164 return null;
165 }
166
167 }