performed javacscript:fix and worked on documentation
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / operations / MoveMediaInListOperation.java
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
10 package eu.etaxonomy.taxeditor.operations;
11
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.core.commands.operations.IUndoContext;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21
22 import eu.etaxonomy.cdm.model.description.DescriptionBase;
23 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
24 import eu.etaxonomy.cdm.model.media.Media;
25 import eu.etaxonomy.cdm.model.taxon.Taxon;
26 import eu.etaxonomy.taxeditor.store.StoreUtil;
27
28 /**
29 * <p>MoveMediaInListOperation class.</p>
30 *
31 * @author p.ciardelli
32 * @created 05.02.2009
33 * @version 1.0
34 */
35 public class MoveMediaInListOperation extends AbstractPostOperation {
36
37 /** Constant <code>UP=1</code> */
38 public static final int UP = 1;
39 /** Constant <code>DOWN=-1</code> */
40 public static final int DOWN = -1;
41
42 private DescriptionBase description;
43 private Media media;
44 private int direction;
45
46 /**
47 * <p>Constructor for MoveMediaInListOperation.</p>
48 *
49 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
50 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
51 * @param label a {@link java.lang.String} object.
52 * @param description a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
53 * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
54 * @param direction a int.
55 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operations.IPostOperationEnabled} object.
56 */
57 public MoveMediaInListOperation(String label, IUndoContext undoContext,
58 Taxon taxon, DescriptionBase description, Media media,
59 int direction, IPostOperationEnabled postOperationEnabled) {
60 super(label, undoContext, taxon, postOperationEnabled);
61
62 this.description = description;
63 this.media = media;
64 this.direction = direction;
65 }
66
67 /* (non-Javadoc)
68 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
69 */
70 /** {@inheritDoc} */
71 @Override
72 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
73 throws ExecutionException {
74
75 monitor.worked(20);
76
77 moveMedia(description, media, direction);
78 monitor.worked(40);
79
80 return postExecute(media);
81 }
82
83 /* (non-Javadoc)
84 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
85 */
86 /** {@inheritDoc} */
87 @Override
88 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
89 throws ExecutionException {
90 return execute(monitor, info);
91 }
92
93 /* (non-Javadoc)
94 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
95 */
96 /** {@inheritDoc} */
97 @Override
98 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
99 throws ExecutionException {
100 moveMedia(description, media, direction * -1);
101 return postExecute(media);
102 }
103
104 private void moveMedia(DescriptionBase description, Media media, int direction){
105 Set<DescriptionElementBase> elements = description.getElements();
106
107 if(elements.size() != 1){
108 StoreUtil.error(this.getClass(), "More than one description element in this image gallery", null);
109 }
110
111 DescriptionElementBase element = elements.iterator().next();
112
113 List<Media> medias = element.getMedia();
114
115 int index = medias.indexOf(media);
116 int newIndex = index + direction;
117
118 if(index < 0){
119 return;
120 }
121
122 if (newIndex >= 0 && newIndex < medias.size()) {
123 try{
124 Collections.swap(medias, newIndex, index);
125 }catch(ArrayIndexOutOfBoundsException e){
126 StoreUtil.error(this.getClass(), e);
127 }
128 }
129 }
130 }