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