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.
9 package eu
.etaxonomy
.taxeditor
.editor
.view
.media
.operation
;
11 import java
.util
.Collections
;
12 import java
.util
.List
;
15 import org
.eclipse
.core
.commands
.ExecutionException
;
16 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
17 import org
.eclipse
.core
.runtime
.IAdaptable
;
18 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
19 import org
.eclipse
.core
.runtime
.IStatus
;
21 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
22 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
23 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
24 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
25 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
26 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
27 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostTaxonOperation
;
28 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
31 * <p>MoveMediaInListOperation class.</p>
36 public class MoveMediaInListOperation
extends AbstractPostTaxonOperation
{
38 /** Constant <code>UP=1</code> */
39 public static final int UP
= -1;
40 /** Constant <code>DOWN=-1</code> */
41 public static final int DOWN
= 1;
43 private DescriptionBase
<?
> description
;
45 private int direction
;
48 * <p>Constructor for MoveMediaInListOperation.</p>
50 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
51 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
52 * @param label a {@link java.lang.String} object.
53 * @param description a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
54 * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
55 * @param direction a int.
56 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
58 public MoveMediaInListOperation(String label
, IUndoContext undoContext
,
59 Taxon taxon
, DescriptionBase description
, Media media
,
60 int direction
, IPostOperationEnabled postOperationEnabled
) {
61 super(label
, undoContext
, taxon
, postOperationEnabled
);
63 this.description
= description
;
65 this.direction
= direction
;
69 public IStatus
execute(IProgressMonitor monitor
, IAdaptable info
)
70 throws ExecutionException
{
74 moveMedia(description
, media
, direction
);
77 return postExecute(media
);
81 public IStatus
redo(IProgressMonitor monitor
, IAdaptable info
)
82 throws ExecutionException
{
83 return execute(monitor
, info
);
87 public IStatus
undo(IProgressMonitor monitor
, IAdaptable info
)
88 throws ExecutionException
{
89 moveMedia(description
, media
, direction
* -1);
90 return postExecute(media
);
93 private void moveMedia(DescriptionBase
<?
> description
, Media media
, int direction
){
94 Set
<DescriptionElementBase
> elements
= description
.getElements();
96 if(elements
.size() != 1){
97 MessagingUtils
.error(this.getClass(), Messages
.MoveMediaInListOperation_MORE_DESC
, null);
100 DescriptionElementBase element
= elements
.iterator().next();
102 List
<Media
> medias
= element
.getMedia();
104 int index
= medias
.indexOf(media
);
105 int newIndex
= index
+ direction
;
111 if (newIndex
>= 0 && newIndex
< medias
.size()) {
113 Collections
.swap(medias
, newIndex
, index
);
114 }catch(ArrayIndexOutOfBoundsException e
){
115 MessagingUtils
.error(this.getClass(), e
);