merging in latest changes from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / uses / operation / CreateUseRecordOperation.java
1 /**
2 * Copyright (C) 2011 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 package eu.etaxonomy.taxeditor.editor.view.uses.operation;
10
11 import org.eclipse.core.commands.ExecutionException;
12 import org.eclipse.core.commands.operations.IUndoContext;
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16
17 import eu.etaxonomy.cdm.model.description.CategoricalData;
18 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
19 import eu.etaxonomy.cdm.model.description.Feature;
20 import eu.etaxonomy.cdm.model.description.TaxonDescription;
21 import eu.etaxonomy.cdm.model.description.TextData;
22 import eu.etaxonomy.cdm.model.taxon.Taxon;
23 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
24 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
25
26
27 /**
28 * CreateUseRecordOperation Class
29 * @author a.theys
30 * @created mar 13, 2012
31 * @version 1.0
32 */
33 public class CreateUseRecordOperation extends AbstractPostOperation {
34
35 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.use."{trunked}</code> */
36 public static final String ID = "eu.etaxonomy.taxeditor.editor.use.createUseRecord";
37 private TaxonDescription description;
38 private Feature feature;
39 private DescriptionElementBase element;
40
41 /**
42 * <p>Constructor for CreateUseRecordOperation.</p>
43 *
44 * @param label a {@link java.lang.String} object.
45 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
46 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
47 * @param description a {@link eu.etaxonomy.cdm.model.description.TaxonDescription} object.
48 * @param feature a {@link eu.etaxonomy.cdm.model.description.Feature} object.
49 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
50 */
51 public CreateUseRecordOperation(String label, IUndoContext undoContext,
52 Taxon taxon, TaxonDescription description, Feature feature, IPostOperationEnabled postOperationEnabled) {
53 super(label, undoContext, taxon, postOperationEnabled);
54
55 this.description = description;
56 this.feature = feature;
57 }
58
59
60 /**
61 * <p>Constructor for CreateUseRecordOperation.</p>
62 *
63 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
64 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
65 * @param description a {@link eu.etaxonomy.cdm.model.description.TaxonDescription} object.
66 * @param feature a {@link eu.etaxonomy.cdm.model.description.Feature} object.
67 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
68 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
69 * @param label a {@link java.lang.String} object.
70 */
71 public CreateUseRecordOperation(String label,
72 IUndoContext undoContext, Taxon taxon,
73 TaxonDescription description, Feature feature,
74 DescriptionElementBase element, IPostOperationEnabled postOperationEnabled) {
75 this(label, undoContext, taxon, description, feature, postOperationEnabled);
76
77 this.element = element;
78 }
79
80 @Override
81 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
82 throws ExecutionException {
83 monitor.worked(20);
84 if (element == null) {
85 if(feature.isSupportsCategoricalData()){
86 element = CategoricalData.NewInstance();
87 }
88 else {
89 element = TextData.NewInstance();
90 }
91 }
92
93
94 element.setFeature(feature);
95 description.addElement(element);
96 monitor.worked(40);
97
98 return postExecute(element);
99 }
100
101 @Override
102 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
103 throws ExecutionException {
104
105 description.addElement(element);
106
107 return postExecute(element);
108 }
109
110 @Override
111 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
112 throws ExecutionException {
113 description.removeElement(element);
114
115 return postExecute(null);
116 }
117 }