Merge branch 'release/4.0.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / description / operation / ChangeDescriptionElementType.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.ui.section.description.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 import org.eclipse.core.runtime.Status;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.swt.widgets.Display;
20
21 import eu.etaxonomy.cdm.model.description.CategoricalData;
22 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
23 import eu.etaxonomy.cdm.model.description.DescriptionBase;
24 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
25 import eu.etaxonomy.cdm.model.description.Distribution;
26 import eu.etaxonomy.cdm.model.description.Feature;
27 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
28 import eu.etaxonomy.cdm.model.description.QuantitativeData;
29 import eu.etaxonomy.cdm.model.description.TaxonInteraction;
30 import eu.etaxonomy.cdm.model.description.TextData;
31 import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
32 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
33
34 /**
35 * <p>ChangeDescriptionElementType class.</p>
36 *
37 * @author e.-m.lee
38 * @date 25.05.2010
39 * @version $Id: $
40 */
41 public class ChangeDescriptionElementType extends AbstractPostTaxonOperation {
42
43 private DescriptionElementBase sourceElement;
44 private DescriptionElementBase destinationElement;
45
46 /**
47 * <p>Constructor for ChangeDescriptionElementType.</p>
48 *
49 * @param label a {@link java.lang.String} object.
50 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
51 * @param sourceType a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
52 * @param postOperation a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
53 */
54 public ChangeDescriptionElementType(String label, DescriptionElementBase sourceType, IPostOperationEnabled postOperation,
55 IUndoContext undoContext) {
56 super(label, undoContext, postOperation);
57 this.sourceElement = sourceType;
58 }
59
60 /* (non-Javadoc)
61 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
62 */
63 /** {@inheritDoc} */
64 @Override
65 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
66 throws ExecutionException {
67 DescriptionBase inDescription = sourceElement.getInDescription();
68 Feature feature = sourceElement.getFeature();
69
70 if(sourceElement instanceof TextData){
71 // FIXME I guess there are features that support more that one of these
72 // and that case is not handled correctly at the moment
73 if(feature.isSupportsDistribution()){
74 destinationElement = Distribution.NewInstance();
75 }
76 else if(feature.isSupportsCommonTaxonName()){
77 destinationElement = CommonTaxonName.NewInstance(null, null);
78 }
79 else if(feature.isSupportsTaxonInteraction()){
80 destinationElement = TaxonInteraction.NewInstance();
81 }
82 else if(feature.isSupportsIndividualAssociation()){
83 destinationElement = IndividualsAssociation.NewInstance();
84 }
85 else if(feature.isSupportsCategoricalData()){
86 destinationElement = CategoricalData.NewInstance();
87 }
88 else if(feature.isSupportsQuantitativeData()){
89 destinationElement = QuantitativeData.NewInstance();
90 }
91 }
92 else{
93 if(feature.isSupportsTextData()){
94 destinationElement = TextData.NewInstance();
95 }
96 }
97 if(destinationElement==null){
98 MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Change not possible", "Feature does not have another type");
99 return Status.CANCEL_STATUS;
100 }
101 destinationElement.setFeature(feature);
102 inDescription.addElement(destinationElement);
103 inDescription.removeElement(sourceElement);
104 return postExecute(destinationElement);
105 }
106
107 /* (non-Javadoc)
108 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
109 */
110 /** {@inheritDoc} */
111 @Override
112 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
113 throws ExecutionException {
114 return null;
115 }
116
117 /* (non-Javadoc)
118 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
119 */
120 /** {@inheritDoc} */
121 @Override
122 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
123 throws ExecutionException {
124 return null;
125 }
126
127 }