Project

General

Profile

Download (3.6 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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.view.media.operation;
10

    
11
import java.util.Collections;
12
import java.util.List;
13
import java.util.Set;
14

    
15
import org.eclipse.core.commands.ExecutionException;
16
import org.eclipse.core.commands.operations.IUndoContext;
17
import org.eclipse.core.runtime.IAdaptable;
18
import org.eclipse.core.runtime.IProgressMonitor;
19
import org.eclipse.core.runtime.IStatus;
20

    
21
import eu.etaxonomy.cdm.model.description.DescriptionBase;
22
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
23
import eu.etaxonomy.cdm.model.media.Media;
24
import eu.etaxonomy.cdm.model.taxon.Taxon;
25
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
26
import eu.etaxonomy.taxeditor.model.MessagingUtils;
27
import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
28
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
29

    
30
/**
31
 * <p>MoveMediaInListOperation class.</p>
32
 *
33
 * @author p.ciardelli
34
 * @created 05.02.2009
35
 */
36
public class MoveMediaInListOperation extends AbstractPostTaxonOperation {
37

    
38
	/** Constant <code>UP=1</code> */
39
	public static final int UP = -1;
40
	/** Constant <code>DOWN=-1</code> */
41
	public static final int DOWN = 1;
42

    
43
	private DescriptionBase<?> description;
44
	private Media media;
45
	private int direction;
46

    
47
	/**
48
	 * <p>Constructor for MoveMediaInListOperation.</p>
49
	 *
50
	 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
51
	 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
52
	 * @param label a {@link java.lang.String} object.
53
	 * @param description a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
54
	 * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
55
	 * @param direction a int.
56
	 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
57
	 */
58
	public MoveMediaInListOperation(String label, IUndoContext undoContext,
59
			Taxon taxon, DescriptionBase description, Media media,
60
			int direction, IPostOperationEnabled postOperationEnabled) {
61
		super(label, undoContext, taxon, postOperationEnabled);
62

    
63
		this.description = description;
64
		this.media = media;
65
		this.direction = direction;
66
	}
67

    
68
	@Override
69
	public IStatus execute(IProgressMonitor monitor, IAdaptable info)
70
			throws ExecutionException {
71

    
72
		monitor.worked(20);
73

    
74
		moveMedia(description, media, direction);
75
		monitor.worked(40);
76

    
77
		return postExecute(media);
78
	}
79

    
80
	@Override
81
	public IStatus redo(IProgressMonitor monitor, IAdaptable info)
82
			throws ExecutionException {
83
		return execute(monitor, info);
84
	}
85

    
86
	@Override
87
	public IStatus undo(IProgressMonitor monitor, IAdaptable info)
88
			throws ExecutionException {
89
		moveMedia(description, media, direction * -1);
90
		return postExecute(media);
91
	}
92

    
93
	private void moveMedia(DescriptionBase<?> description, Media media, int direction){
94
		Set<DescriptionElementBase> elements = description.getElements();
95

    
96
		if(elements.size() != 1){
97
			MessagingUtils.error(this.getClass(), Messages.MoveMediaInListOperation_MORE_DESC, null);
98
		}
99

    
100
		DescriptionElementBase element = elements.iterator().next();
101

    
102
		List<Media> medias = element.getMedia();
103

    
104
		int index = medias.indexOf(media);
105
		int newIndex = index + direction;
106

    
107
		if(index < 0){
108
			return;
109
		}
110

    
111
		if (newIndex >= 0 && newIndex < medias.size()) {
112
			try{
113
				Collections.swap(medias, newIndex, index);
114
			}catch(ArrayIndexOutOfBoundsException e){
115
				MessagingUtils.error(this.getClass(), e);
116
			}
117
		}
118
	}
119
}
(5-5/6)