Project

General

Profile

Download (6.22 KB) Statistics
| Branch: | Tag: | Revision:
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.addTermAbove((OrderedTermBase)term, (OrderedTermBase)targetTermOrVocabulary);
106
				            if (((DefinedTermBase) targetTermOrVocabulary).getPartOf() != null){
107
				                ((DefinedTermBase) targetTermOrVocabulary).getPartOf().addIncludes(term);
108
				            }
109
				        }
110

    
111
				        if(currentLocation == ViewerDropAdapter.LOCATION_AFTER) {
112
				            otVoc.addTermBelow((OrderedTermBase)term, (OrderedTermBase)targetTermOrVocabulary);
113
				            if (((DefinedTermBase) targetTermOrVocabulary).getPartOf() != null){
114
                                ((DefinedTermBase) targetTermOrVocabulary).getPartOf().addIncludes(term);
115
                            }
116
				        }
117
				        if(currentLocation == ViewerDropAdapter.LOCATION_ON) {
118
						    targetDefinedTerm.addIncludes(term);
119
						    targetDefinedTerm.getVocabulary().addTerm(term);
120
						}
121
				    }
122
				} else{
123
					targetDefinedTerm.addIncludes(term);
124
				    targetDefinedTerm.getVocabulary().addTerm(term);
125
				}
126

    
127
			}
128

    
129
		}
130
		//return null;
131
		return postExecute(targetTermOrVocabulary);
132
	}
133

    
134
	private DefinedTermBase cleanTerm(DefinedTermBase term){
135

    
136
		DefinedTermBase partOf = term.getPartOf();
137
		if(partOf != null){
138
			partOf.removeIncludes(term);
139
		}
140

    
141

    
142
		TermVocabulary vocabulary = term.getVocabulary();
143
		if(vocabulary != null){
144
			vocabulary.removeTerm(term);
145
		}
146

    
147
		return term;
148
	}
149

    
150

    
151
	/* (non-Javadoc)
152
	 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
153
	 */
154
	@Override
155
	public IStatus redo(IProgressMonitor monitor, IAdaptable info)
156
			throws ExecutionException {
157
		// TODO Auto-generated method stub
158
		return null;
159
	}
160

    
161
	/* (non-Javadoc)
162
	 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
163
	 */
164
	@Override
165
	public IStatus undo(IProgressMonitor monitor, IAdaptable info)
166
			throws ExecutionException {
167
		// TODO Auto-generated method stub
168
		return null;
169
	}
170

    
171
}
(4-4/4)