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