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 / CreateTaxonUseOperation.java
1 package eu.etaxonomy.taxeditor.editor.view.uses.operation;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import org.eclipse.core.commands.ExecutionException;
7 import org.eclipse.core.commands.operations.IUndoContext;
8 import org.eclipse.core.runtime.IAdaptable;
9 import org.eclipse.core.runtime.IProgressMonitor;
10 import org.eclipse.core.runtime.IStatus;
11
12 import eu.etaxonomy.cdm.model.common.Marker;
13 import eu.etaxonomy.cdm.model.common.MarkerType;
14 import eu.etaxonomy.cdm.model.description.Feature;
15 import eu.etaxonomy.cdm.model.description.TaxonDescription;
16 import eu.etaxonomy.cdm.model.description.TextData;
17 import eu.etaxonomy.cdm.model.media.Media;
18 import eu.etaxonomy.cdm.model.taxon.Taxon;
19 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
20 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
21 import eu.etaxonomy.taxeditor.store.CdmStore;
22
23 /**
24 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
25 *
26 * @author a.theys
27 * @created mar 13, 2012
28 * @version 1.0
29 */
30 public class CreateTaxonUseOperation extends AbstractPostOperation {
31 private TaxonDescription description;
32 private boolean isImageGallery;
33 private Set<MarkerType> markerTypes = new HashSet<MarkerType>();
34 private Marker marker;
35
36 /**
37 * <p>Constructor for CreateTaxonDescriptionOperation.</p>
38 *
39 * @param label a {@link java.lang.String} object.
40 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
41 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
42 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
43 */
44 public CreateTaxonUseOperation(String label, IUndoContext undoContext,
45 Taxon taxon, IPostOperationEnabled postOperationEnabled) {
46 this(label, undoContext, taxon, postOperationEnabled, false);
47 }
48
49 /**
50 * <p>Constructor for CreateTaxonDescriptionOperation.</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 postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
56 * @param isImageGallery a boolean.
57 */
58 public CreateTaxonUseOperation(String label, IUndoContext undoContext,
59 Taxon taxon, IPostOperationEnabled postOperationEnabled, boolean isImageGallery) {
60 super(label, undoContext, taxon, postOperationEnabled);
61 this.isImageGallery = isImageGallery;
62 }
63
64 /* (non-Javadoc)
65 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
66 */
67 /** {@inheritDoc} */
68 @Override
69 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
70 throws ExecutionException {
71
72 description = TaxonDescription.NewInstance(taxon);
73 monitor.worked(20);
74 //this.markerTypes.addAll(CdmStore.getTermManager().getPreferredMarkerTypes());
75 this.markerTypes.addAll(CdmStore.getTermManager().getPreferredTerms(MarkerType.class));
76 for (MarkerType markerType : markerTypes) {
77 if (markerType.getTitleCache().equals("use")) {
78 marker = Marker.NewInstance(markerType, true);
79 description.addMarker(marker);
80 }
81 }
82 if(isImageGallery){
83 description.setImageGallery(isImageGallery);
84 // add the description element to hold the media elements for this image gallery
85 TextData element = TextData.NewInstance(Feature.IMAGE());
86 element.addMedia(Media.NewInstance());
87 description.addElement(element);
88 }
89 monitor.worked(40);
90
91 return postExecute(description);
92 }
93
94 /* (non-Javadoc)
95 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
96 */
97 /** {@inheritDoc} */
98 @Override
99 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
100 throws ExecutionException {
101
102 taxon.addDescription(description);
103
104 return postExecute(description);
105 }
106
107 /* (non-Javadoc)
108 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
109 */
110 /** {@inheritDoc} */
111 @Override
112 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
113 throws ExecutionException {
114
115 taxon.removeDescription(description);
116
117 return postExecute(null);
118 }
119 }
120