Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / media / operation / AddMediaToImageGalleryOperation.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 org.eclipse.core.commands.ExecutionException;
13 import org.eclipse.core.commands.operations.IUndoContext;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17
18 import eu.etaxonomy.cdm.api.utility.ImagesUtility;
19 import eu.etaxonomy.cdm.model.description.DescriptionBase;
20 import eu.etaxonomy.cdm.model.media.Media;
21 import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
22 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
23 /**
24 * This operation creates a new {@link Media} and adds it to the currently selected {@link DescriptionBase}.
25 *
26 * @author p.ciardelli
27 * @author n.hoffmann
28 * @created 05.02.2009
29 */
30 public class AddMediaToImageGalleryOperation extends AbstractPostTaxonOperation {
31
32 private final DescriptionBase<?> description;
33 private Media media;
34
35 /**
36 * <p>Constructor for AddMediaToImageGalleryOperation.</p>
37 *
38 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
39 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
40 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
41 * @param label a {@link java.lang.String} object.
42 * @param description a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
43 */
44 public AddMediaToImageGalleryOperation(String label,
45 IUndoContext undoContext, DescriptionBase<?> description, IPostOperationEnabled postOperationEnabled) {
46 this(null, label, undoContext, description, postOperationEnabled);
47 media = Media.NewInstance();
48 }
49
50 protected AddMediaToImageGalleryOperation(Media media, String label,
51 IUndoContext undoContext, DescriptionBase<?> description, IPostOperationEnabled postOperationEnabled) {
52 super(label, undoContext, postOperationEnabled);
53
54 this.description = description;
55 this.media = media;
56 }
57
58 @Override
59 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
60 throws ExecutionException {
61
62 monitor.worked(20);
63
64 ImagesUtility.addMediaToGallery(description, media);
65
66 monitor.worked(40);
67
68 return postExecute(media);
69 }
70
71 @Override
72 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
73 throws ExecutionException {
74 return execute(monitor, info);
75 }
76
77 @Override
78 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
79 throws ExecutionException {
80 ImagesUtility.removeMediaFromGallery(description, media);
81 return postExecute(description);
82 }
83 }