(no commit message)
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / operations / CreateDescriptionElementOperation.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
18 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
19 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
20 import eu.etaxonomy.cdm.model.description.Distribution;
21 import eu.etaxonomy.cdm.model.description.Feature;
22 import eu.etaxonomy.cdm.model.description.TaxonDescription;
23 import eu.etaxonomy.cdm.model.description.TaxonInteraction;
24 import eu.etaxonomy.cdm.model.description.TextData;
25 import eu.etaxonomy.cdm.model.media.ImageFile;
26 import eu.etaxonomy.cdm.model.taxon.Taxon;
27 import eu.etaxonomy.taxeditor.model.ImagesHelper;
28 import eu.etaxonomy.taxeditor.store.CdmStore;
29
30 /**
31 * @author p.ciardelli
32 * @created 05.02.2009
33 * @version 1.0
34 * @author n.hoffmann
35 */
36 public class CreateDescriptionElementOperation extends AbstractPostOperation {
37
38 private TaxonDescription description;
39 private Feature feature;
40 private DescriptionElementBase element;
41
42 public CreateDescriptionElementOperation(String label, IUndoContext undoContext,
43 Taxon taxon, TaxonDescription description, Feature feature, IPostOperationEnabled postOperationEnabled) {
44 super(label, undoContext, taxon, postOperationEnabled);
45
46 this.description = description;
47 this.feature = feature;
48 }
49
50 /**
51 * @param name
52 * @param undoContext
53 * @param taxon
54 * @param description
55 * @param feature
56 * @param element
57 * @param postOperationEnabled
58 */
59 public CreateDescriptionElementOperation(String label,
60 IUndoContext undoContext, Taxon taxon,
61 TaxonDescription description, Feature feature,
62 DescriptionElementBase element, IPostOperationEnabled postOperationEnabled) {
63 this(label, undoContext, taxon, description, feature, postOperationEnabled);
64
65 this.element = element;
66 }
67
68 /* (non-Javadoc)
69 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
70 */
71 @Override
72 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
73 throws ExecutionException {
74
75 if (element == null) {
76 if (feature.isSupportsCommonTaxonName()) {
77 element = CommonTaxonName.NewInstance("", null);
78 } else if (feature.isSupportsDistribution()) {
79 element = Distribution.NewInstance();
80 } else if (feature.equals(Feature.IMAGE())){
81 element = ImagesHelper.createImageElement(ImageFile.NewInstance(null, null));
82 } else if(feature.isSupportsTaxonInteraction()){
83 element = TaxonInteraction.NewInstance(feature);
84 } else {
85 element = TextData.NewInstance(feature);
86 }
87 }
88 description.addElement(element);
89
90 return postExecute(element);
91 }
92
93 /* (non-Javadoc)
94 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
95 */
96 @Override
97 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
98 throws ExecutionException {
99
100 description.addElement(element);
101
102 return postExecute(element);
103 }
104
105 /* (non-Javadoc)
106 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
107 */
108 @Override
109 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
110 throws ExecutionException {
111
112 description.removeElement(element);
113
114 return postExecute(null);
115 }
116 }