AT: committing latest changes to the Tax Editor after a first round of Code review
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / uses / operation / CreateUseRecordOperation.java
1 package eu.etaxonomy.taxeditor.editor.view.uses.operation;
2
3 import org.eclipse.core.commands.ExecutionException;
4 import org.eclipse.core.commands.operations.IUndoContext;
5 import org.eclipse.core.runtime.IAdaptable;
6 import org.eclipse.core.runtime.IProgressMonitor;
7 import org.eclipse.core.runtime.IStatus;
8
9 import eu.etaxonomy.cdm.model.description.CategoricalData;
10 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
11 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
12 import eu.etaxonomy.cdm.model.description.Distribution;
13 import eu.etaxonomy.cdm.model.description.Feature;
14 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
15 import eu.etaxonomy.cdm.model.description.QuantitativeData;
16 import eu.etaxonomy.cdm.model.description.TaxonDescription;
17 import eu.etaxonomy.cdm.model.description.TaxonInteraction;
18 import eu.etaxonomy.cdm.model.description.TextData;
19 import eu.etaxonomy.cdm.model.taxon.Taxon;
20 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
21 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
22
23
24 /**
25 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
26 *
27 * @author a.theys
28 * @created mar 13, 2012
29 * @version 1.0
30 */
31 public class CreateUseRecordOperation extends AbstractPostOperation {
32
33 public static final String ID = "eu.etaxonomy.taxeditor.editor.use.createUseRecord";
34
35 private TaxonDescription description;
36 private Feature feature;
37 private DescriptionElementBase element;
38
39
40 public CreateUseRecordOperation(String label, IUndoContext undoContext,
41 Taxon taxon, TaxonDescription description, Feature feature, IPostOperationEnabled postOperationEnabled) {
42 super(label, undoContext, taxon, postOperationEnabled);
43
44 this.description = description;
45 this.feature = feature;
46 }
47
48 public CreateUseRecordOperation(String label,
49 IUndoContext undoContext, Taxon taxon,
50 TaxonDescription description, Feature feature,
51 DescriptionElementBase element, IPostOperationEnabled postOperationEnabled) {
52 this(label, undoContext, taxon, description, feature, postOperationEnabled);
53
54 this.element = element;
55 }
56
57 @Override
58 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
59 throws ExecutionException {
60 monitor.worked(20);
61 if (element == null) {
62 if (feature.isSupportsCommonTaxonName()) {
63 element = CommonTaxonName.NewInstance("", null);
64 }
65 else if (feature.isSupportsDistribution()) {
66 element = Distribution.NewInstance();
67 }
68 else if(feature.isSupportsTaxonInteraction()){
69 element = TaxonInteraction.NewInstance();
70 }
71 else if(feature.isSupportsIndividualAssociation()){
72 element = IndividualsAssociation.NewInstance();
73 }
74 else if(feature.isSupportsCategoricalData()){
75 element = CategoricalData.NewInstance();
76 }
77 else if(feature.isSupportsQuantitativeData()){
78 element = QuantitativeData.NewInstance();
79 }
80 else {
81 element = TextData.NewInstance();
82 }
83 }
84
85
86 element.setFeature(feature);
87 description.addElement(element);
88 monitor.worked(40);
89
90 return postExecute(element);
91 }
92
93 @Override
94 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
95 throws ExecutionException {
96
97 description.addElement(element);
98
99 return postExecute(element);
100 }
101
102 @Override
103 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
104 throws ExecutionException {
105 description.removeElement(element);
106
107 return postExecute(null);
108 }
109 }