AT: commiting Palm Use Data extension
[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 public class CreateTaxonUseOperation extends AbstractPostOperation {
24 private TaxonDescription description;
25 private boolean isImageGallery;
26 private Set<MarkerType> markerTypes = new HashSet<MarkerType>();
27 private Marker marker;
28
29 /**
30 * <p>Constructor for CreateTaxonDescriptionOperation.</p>
31 *
32 * @param label a {@link java.lang.String} object.
33 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
34 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
35 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
36 */
37 public CreateTaxonUseOperation(String label, IUndoContext undoContext,
38 Taxon taxon, IPostOperationEnabled postOperationEnabled) {
39 this(label, undoContext, taxon, postOperationEnabled, false);
40 }
41
42 /**
43 * <p>Constructor for CreateTaxonDescriptionOperation.</p>
44 *
45 * @param label a {@link java.lang.String} object.
46 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
47 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
48 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
49 * @param isImageGallery a boolean.
50 */
51 public CreateTaxonUseOperation(String label, IUndoContext undoContext,
52 Taxon taxon, IPostOperationEnabled postOperationEnabled, boolean isImageGallery) {
53 super(label, undoContext, taxon, postOperationEnabled);
54 this.isImageGallery = isImageGallery;
55 }
56
57 /* (non-Javadoc)
58 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
59 */
60 /** {@inheritDoc} */
61 @Override
62 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
63 throws ExecutionException {
64
65 description = TaxonDescription.NewInstance(taxon);
66 monitor.worked(20);
67 this.markerTypes.addAll(CdmStore.getTermManager().getPreferredMarkerTypes());
68 Set<MarkerType> typesToDisdplay = new HashSet<MarkerType>();
69 for (MarkerType markerType : markerTypes) {
70 if (markerType.getTitleCache().equals("use")) {
71 marker = Marker.NewInstance(markerType, true);
72 description.addMarker(marker);
73 }
74 }
75 if(isImageGallery){
76 description.setImageGallery(isImageGallery);
77 // add the description element to hold the media elements for this image gallery
78 TextData element = TextData.NewInstance(Feature.IMAGE());
79 element.addMedia(Media.NewInstance());
80 description.addElement(element);
81 }
82 monitor.worked(40);
83
84 return postExecute(description);
85 }
86
87 /* (non-Javadoc)
88 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
89 */
90 /** {@inheritDoc} */
91 @Override
92 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
93 throws ExecutionException {
94
95 taxon.addDescription(description);
96
97 return postExecute(description);
98 }
99
100 /* (non-Javadoc)
101 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
102 */
103 /** {@inheritDoc} */
104 @Override
105 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
106 throws ExecutionException {
107
108 taxon.removeDescription(description);
109
110 return postExecute(null);
111 }
112 }
113