had to rename the packages to make them compliant with buckminster
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / operation / 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.editor.view.descriptive.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.model.description.CategoricalData;
19 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
20 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
21 import eu.etaxonomy.cdm.model.description.Distribution;
22 import eu.etaxonomy.cdm.model.description.Feature;
23 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
24 import eu.etaxonomy.cdm.model.description.QuantitativeData;
25 import eu.etaxonomy.cdm.model.description.TaxonDescription;
26 import eu.etaxonomy.cdm.model.description.TaxonInteraction;
27 import eu.etaxonomy.cdm.model.description.TextData;
28 import eu.etaxonomy.cdm.model.taxon.Taxon;
29 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
30 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
31
32 /**
33 * <p>CreateDescriptionElementOperation class.</p>
34 *
35 * @author p.ciardelli
36 * @author n.hoffmann
37 * @created 05.02.2009
38 * @version 1.0
39 */
40 public class CreateDescriptionElementOperation extends AbstractPostOperation {
41
42 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.descripti"{trunked}</code> */
43 public static final String ID = "eu.etaxonomy.taxeditor.editor.description.createDescriptionElement";
44
45 private TaxonDescription description;
46 private Feature feature;
47 private DescriptionElementBase element;
48
49 /**
50 * <p>Constructor for CreateDescriptionElementOperation.</p>
51 *
52 * @param label a {@link java.lang.String} object.
53 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
54 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
55 * @param description a {@link eu.etaxonomy.cdm.model.description.TaxonDescription} object.
56 * @param feature a {@link eu.etaxonomy.cdm.model.description.Feature} object.
57 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
58 */
59 public CreateDescriptionElementOperation(String label, IUndoContext undoContext,
60 Taxon taxon, TaxonDescription description, Feature feature, IPostOperationEnabled postOperationEnabled) {
61 super(label, undoContext, taxon, postOperationEnabled);
62
63 this.description = description;
64 this.feature = feature;
65 }
66
67 /**
68 * <p>Constructor for CreateDescriptionElementOperation.</p>
69 *
70 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
71 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
72 * @param description a {@link eu.etaxonomy.cdm.model.description.TaxonDescription} object.
73 * @param feature a {@link eu.etaxonomy.cdm.model.description.Feature} object.
74 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
75 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
76 * @param label a {@link java.lang.String} object.
77 */
78 public CreateDescriptionElementOperation(String label,
79 IUndoContext undoContext, Taxon taxon,
80 TaxonDescription description, Feature feature,
81 DescriptionElementBase element, IPostOperationEnabled postOperationEnabled) {
82 this(label, undoContext, taxon, description, feature, postOperationEnabled);
83
84 this.element = element;
85 }
86
87 /* (non-Javadoc)
88 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
89 */
90 /** {@inheritDoc} */
91 @Override
92 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
93 throws ExecutionException {
94
95 monitor.worked(20);
96 if (element == null) {
97 if (feature.isSupportsCommonTaxonName()) {
98 element = CommonTaxonName.NewInstance("", null);
99 }
100 else if (feature.isSupportsDistribution()) {
101 element = Distribution.NewInstance();
102 }
103 else if(feature.isSupportsTaxonInteraction()){
104 element = TaxonInteraction.NewInstance();
105 }
106 else if(feature.isSupportsIndividualAssociation()){
107 element = IndividualsAssociation.NewInstance();
108 }
109 else if(feature.isSupportsCategoricalData()){
110 element = CategoricalData.NewInstance();
111 }
112 else if(feature.isSupportsQuantitativeData()){
113 element = QuantitativeData.NewInstance();
114 }
115 else {
116 element = TextData.NewInstance();
117 }
118 }
119
120 element.setFeature(feature);
121 description.addElement(element);
122 monitor.worked(40);
123
124 return postExecute(element);
125 }
126
127 /* (non-Javadoc)
128 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
129 */
130 /** {@inheritDoc} */
131 @Override
132 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
133 throws ExecutionException {
134
135 description.addElement(element);
136
137 return postExecute(element);
138 }
139
140 /* (non-Javadoc)
141 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
142 */
143 /** {@inheritDoc} */
144 @Override
145 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
146 throws ExecutionException {
147
148 description.removeElement(element);
149
150 return postExecute(null);
151 }
152 }