2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.operations
;
12 import java
.util
.Collections
;
13 import java
.util
.List
;
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
;
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
;
29 * <p>MoveMediaInListOperation class.</p>
35 public class MoveMediaInListOperation
extends AbstractPostOperation
{
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;
42 private DescriptionBase description
;
44 private int direction
;
47 * <p>Constructor for MoveMediaInListOperation.</p>
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.
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
);
62 this.description
= description
;
64 this.direction
= direction
;
68 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
72 public IStatus
execute(IProgressMonitor monitor
, IAdaptable info
)
73 throws ExecutionException
{
77 moveMedia(description
, media
, direction
);
80 return postExecute(media
);
84 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
88 public IStatus
redo(IProgressMonitor monitor
, IAdaptable info
)
89 throws ExecutionException
{
90 return execute(monitor
, info
);
94 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
98 public IStatus
undo(IProgressMonitor monitor
, IAdaptable info
)
99 throws ExecutionException
{
100 moveMedia(description
, media
, direction
* -1);
101 return postExecute(media
);
104 private void moveMedia(DescriptionBase description
, Media media
, int direction
){
105 Set
<DescriptionElementBase
> elements
= description
.getElements();
107 if(elements
.size() != 1){
108 StoreUtil
.error(this.getClass(), "More than one description element in this image gallery", null);
111 DescriptionElementBase element
= elements
.iterator().next();
113 List
<Media
> medias
= element
.getMedia();
115 int index
= medias
.indexOf(media
);
116 int newIndex
= index
+ direction
;
122 if (newIndex
>= 0 && newIndex
< medias
.size()) {
124 Collections
.swap(medias
, newIndex
, index
);
125 }catch(ArrayIndexOutOfBoundsException e
){
126 StoreUtil
.error(this.getClass(), e
);