Project

General

Profile

Download (5.69 KB) Statistics
| Branch: | Tag: | Revision:
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
				        }
108

    
109
				        if(currentLocation == ViewerDropAdapter.LOCATION_AFTER) {
110
				            otVoc.addTermAbove((OrderedTermBase)term, (OrderedTermBase)targetTermOrVocabulary);
111
				        }
112
				    }
113
				}
114
				if(currentLocation == ViewerDropAdapter.LOCATION_ON) {
115
				    targetDefinedTerm.addIncludes(term);
116
				    targetDefinedTerm.getVocabulary().addTerm(term);
117
				}
118
			}
119

    
120
		}
121
		//return null;
122
		return postExecute(targetTermOrVocabulary);
123
	}
124

    
125
	private DefinedTermBase cleanTerm(DefinedTermBase term){
126

    
127
		DefinedTermBase partOf = term.getPartOf();
128
		if(partOf != null){
129
			partOf.removeIncludes(term);
130
		}
131

    
132

    
133
		TermVocabulary vocabulary = term.getVocabulary();
134
		if(vocabulary != null){
135
			vocabulary.removeTerm(term);
136
		}
137

    
138
		return term;
139
	}
140

    
141

    
142
	/* (non-Javadoc)
143
	 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
144
	 */
145
	@Override
146
	public IStatus redo(IProgressMonitor monitor, IAdaptable info)
147
			throws ExecutionException {
148
		// TODO Auto-generated method stub
149
		return null;
150
	}
151

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

    
162
}
(4-4/4)