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
.editor
.view
.media
.operation
;
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
.operation
.AbstractPostTaxonOperation
;
27 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
28 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
31 * <p>MoveMediaInListOperation class.</p>
37 public class MoveMediaInListOperation
extends AbstractPostTaxonOperation
{
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;
44 private DescriptionBase description
;
46 private int direction
;
49 * <p>Constructor for MoveMediaInListOperation.</p>
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.
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
);
64 this.description
= description
;
66 this.direction
= direction
;
70 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
74 public IStatus
execute(IProgressMonitor monitor
, IAdaptable info
)
75 throws ExecutionException
{
79 moveMedia(description
, media
, direction
);
82 return postExecute(media
);
86 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
90 public IStatus
redo(IProgressMonitor monitor
, IAdaptable info
)
91 throws ExecutionException
{
92 return execute(monitor
, info
);
96 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
100 public IStatus
undo(IProgressMonitor monitor
, IAdaptable info
)
101 throws ExecutionException
{
102 moveMedia(description
, media
, direction
* -1);
103 return postExecute(media
);
106 private void moveMedia(DescriptionBase description
, Media media
, int direction
){
107 Set
<DescriptionElementBase
> elements
= description
.getElements();
109 if(elements
.size() != 1){
110 StoreUtil
.error(this.getClass(), "More than one description element in this image gallery", null);
113 DescriptionElementBase element
= elements
.iterator().next();
115 List
<Media
> medias
= element
.getMedia();
117 int index
= medias
.indexOf(media
);
118 int newIndex
= index
+ direction
;
124 if (newIndex
>= 0 && newIndex
< medias
.size()) {
126 Collections
.swap(medias
, newIndex
, index
);
127 }catch(ArrayIndexOutOfBoundsException e
){
128 StoreUtil
.error(this.getClass(), e
);