+++ /dev/null
-/**
-* Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy
-* http://www.e-taxonomy.eu
-*
-* The contents of this file are subject to the Mozilla Public License Version 1.1
-* See LICENSE.TXT at the top of this package for the full license terms.
-*/
-
-package eu.etaxonomy.taxeditor.editor.view.media.operation;
-
-import java.util.Collections;
-import java.util.List;
-import java.util.Set;
-
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.commands.operations.IUndoContext;
-import org.eclipse.core.runtime.IAdaptable;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-
-import eu.etaxonomy.cdm.model.description.DescriptionBase;
-import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
-import eu.etaxonomy.cdm.model.media.Media;
-import eu.etaxonomy.cdm.model.taxon.Taxon;
-import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
-import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
-import eu.etaxonomy.taxeditor.store.StoreUtil;
-
-/**
- * <p>MoveMediaInListOperation class.</p>
- *
- * @author p.ciardelli
- * @created 05.02.2009
- * @version 1.0
- */
-public class MoveMediaInListOperation extends AbstractPostOperation {
-
- /** Constant <code>UP=1</code> */
- public static final int UP = 1;
- /** Constant <code>DOWN=-1</code> */
- public static final int DOWN = -1;
-
- private DescriptionBase description;
- private Media media;
- private int direction;
-
- /**
- * <p>Constructor for MoveMediaInListOperation.</p>
- *
- * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
- * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
- * @param label a {@link java.lang.String} object.
- * @param description a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
- * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
- * @param direction a int.
- * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
- */
- public MoveMediaInListOperation(String label, IUndoContext undoContext,
- Taxon taxon, DescriptionBase description, Media media,
- int direction, IPostOperationEnabled postOperationEnabled) {
- super(label, undoContext, taxon, postOperationEnabled);
-
- this.description = description;
- this.media = media;
- this.direction = direction;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
- */
- /** {@inheritDoc} */
- @Override
- public IStatus execute(IProgressMonitor monitor, IAdaptable info)
- throws ExecutionException {
-
- monitor.worked(20);
-
- moveMedia(description, media, direction);
- monitor.worked(40);
-
- return postExecute(media);
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
- */
- /** {@inheritDoc} */
- @Override
- public IStatus redo(IProgressMonitor monitor, IAdaptable info)
- throws ExecutionException {
- return execute(monitor, info);
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
- */
- /** {@inheritDoc} */
- @Override
- public IStatus undo(IProgressMonitor monitor, IAdaptable info)
- throws ExecutionException {
- moveMedia(description, media, direction * -1);
- return postExecute(media);
- }
-
- private void moveMedia(DescriptionBase description, Media media, int direction){
- Set<DescriptionElementBase> elements = description.getElements();
-
- if(elements.size() != 1){
- StoreUtil.error(this.getClass(), "More than one description element in this image gallery", null);
- }
-
- DescriptionElementBase element = elements.iterator().next();
-
- List<Media> medias = element.getMedia();
-
- int index = medias.indexOf(media);
- int newIndex = index + direction;
-
- if(index < 0){
- return;
- }
-
- if (newIndex >= 0 && newIndex < medias.size()) {
- try{
- Collections.swap(medias, newIndex, index);
- }catch(ArrayIndexOutOfBoundsException e){
- StoreUtil.error(this.getClass(), e);
- }
- }
- }
-}