310db2bbdc199dba2e3e172cc7e156b85a75ade0
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / operations / AddImageToDescriptionElementOperation.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.operations;
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 import org.eclipse.core.runtime.OperationCanceledException;
18
19 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
20 import eu.etaxonomy.cdm.model.media.ImageFile;
21 import eu.etaxonomy.cdm.model.taxon.Taxon;
22 import eu.etaxonomy.taxeditor.model.ImagesHelper;
23 import eu.etaxonomy.taxeditor.store.StoreUtil;
24
25 /**
26 * @author p.ciardelli
27 * @created 05.02.2009
28 * @version 1.0
29 * @author n.hoffmann
30 */
31 public class AddImageToDescriptionElementOperation extends AbstractPostOperation {
32
33 private DescriptionElementBase element;
34 private ImageFile image;
35
36 /**
37 * @param name
38 * @param undoContext
39 * @param taxon
40 * @param element
41 * @param postOperationEnabled
42 */
43 public AddImageToDescriptionElementOperation(String label,
44 IUndoContext undoContext, Taxon taxon,
45 DescriptionElementBase element, IPostOperationEnabled postOperationEnabled) {
46 super(label, undoContext, taxon, postOperationEnabled);
47
48 this.element = element;
49 }
50
51 /* (non-Javadoc)
52 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
53 */
54 @Override
55 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
56 throws ExecutionException {
57
58 // Start the main progress monitor.
59 IProgressMonitor newMonitor = StoreUtil.startMainMonitor(monitor,"Adding Image to Description Element", 2);
60
61 // Do one step
62 newMonitor.worked(1);
63
64 try {
65 // Operation steps
66 image = ImagesHelper.addImagePart(element);
67 StoreUtil.isCanceled(newMonitor, 1);
68 }
69 finally {
70
71 // Stop the progress monitor.
72 newMonitor.done();
73 }
74
75 return postExecute(image);
76 }
77
78 /* (non-Javadoc)
79 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
80 */
81 @Override
82 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
83 throws ExecutionException {
84 return execute(monitor, info);
85 }
86
87 /* (non-Javadoc)
88 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
89 */
90 @Override
91 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
92 throws ExecutionException {
93 ImagesHelper.removeImage(element, image);
94 return postExecute(element);
95 }
96 }