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