a6d3fd1df18570c18ce5eb714051d12fe95f2906
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / ui / description / AddDescriptionElementCompositeAction.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.actions.ui.description;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.core.commands.ExecutionException;
14 import org.eclipse.core.commands.operations.AbstractOperation;
15 import org.eclipse.core.commands.operations.IOperationHistory;
16 import org.eclipse.core.commands.operations.IUndoContext;
17 import org.eclipse.core.commands.operations.IUndoableOperation;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.jface.action.Action;
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.ui.forms.IManagedForm;
27
28 import eu.etaxonomy.cdm.model.common.Language;
29 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
30 import eu.etaxonomy.cdm.model.description.Feature;
31 import eu.etaxonomy.cdm.model.description.TaxonDescription;
32 import eu.etaxonomy.cdm.model.description.TextData;
33 import eu.etaxonomy.cdm.model.description.TextFormat;
34 import eu.etaxonomy.taxeditor.controller.GlobalController;
35 import eu.etaxonomy.taxeditor.editor.description.DescriptionElementComposite;
36 import eu.etaxonomy.taxeditor.editor.description.FeatureGroupComposite;
37
38 /**
39 * Checks whether the <code>TaxonDescriptionComposite</code> contains a
40 * a <code>FeatureGroupComposite</code> for the <code>Feature</code>; creates a
41 * new <code>FeatureGroupComposite</code> if necessary. Then adds a new
42 * <code>DescriptionElementComposite</code> to the <code>FeatureGroupComposite</code>.
43 *
44 * @author p.ciardelli
45 * @created 04.06.2008
46 * @version 1.0
47 */
48 public class AddDescriptionElementCompositeAction extends Action {
49 private static final Logger logger = Logger
50 .getLogger(AddDescriptionElementCompositeAction.class);
51
52 private String text;
53 private ImageDescriptor image = null;
54
55 private IUndoableOperation operation;
56
57 private TaxonDescription taxonDescription;
58 private Composite taxonDescriptionComposite;
59 private IManagedForm managedForm;
60 private Feature feature;
61
62 /**
63 * descriptionElement and descriptionElementComposite are created when
64 * the action is executed, and disposed as part of any undo.
65 */
66 private DescriptionElementBase descriptionElement;
67 private DescriptionElementComposite descriptionElementComposite;
68
69 private AddDescriptionElementCompositeAction(String text) {
70 super(text);
71 this.text = text;
72 if (image != null) {
73 setImageDescriptor(image);
74 }
75 }
76
77 public AddDescriptionElementCompositeAction(Composite taxonDescriptionComposite,
78 IManagedForm managedForm, Feature feature) {
79
80 // The feature's label will be shown in the menu entry
81 this(feature.getLabel());
82
83 // Make sure TaxonDescriptionComposite has a TaxonDescription in its data field
84 if (taxonDescriptionComposite.getData() instanceof TaxonDescription) {
85 this.taxonDescription = (TaxonDescription) taxonDescriptionComposite.getData();
86 } else {
87 throw new IllegalArgumentException(
88 "This action requires a composite with a TaxonDescription in its data field.");
89 }
90
91 this.taxonDescriptionComposite = taxonDescriptionComposite;
92 this.managedForm = managedForm;
93 this.feature = feature;
94
95 operation = new AddDescriptionElementOperation();
96
97 }
98
99 public void run() {
100 IOperationHistory operationHistory = GlobalController.getOperationHistory();
101 IUndoContext undoContext = GlobalController.getWorkbenchUndoContext();
102 operation.addContext(undoContext);
103 try {
104 operationHistory.execute(operation, null, null);
105 operationHistory.add(operation);
106 } catch (ExecutionException e) {
107 e.printStackTrace();
108 }
109 }
110
111 class AddDescriptionElementOperation extends AbstractOperation {
112
113 public AddDescriptionElementOperation() {
114 super("'" + text + "'");
115 }
116
117 @Override
118 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
119 throws ExecutionException {
120
121 // Look for a FeatureGroupComposite containing the current Feature
122 FeatureGroupComposite featureGroupComposite = null;
123 for (Control child : taxonDescriptionComposite.getChildren()) {
124 if (child instanceof FeatureGroupComposite && child.getData() != null
125 && child.getData().equals(feature)) {
126 featureGroupComposite = (FeatureGroupComposite) child;
127 break;
128 }
129 }
130
131 // Otherwise, create a new FeatureGroupComposite
132 if (featureGroupComposite == null) {
133 featureGroupComposite = new FeatureGroupComposite(taxonDescriptionComposite,
134 managedForm, feature);
135 }
136
137 // Create new element, set it as the data for a new element composite
138 descriptionElement = TextData.NewInstance("", Language.DEFAULT(), null);
139 descriptionElement.setFeature(feature);
140 taxonDescription.addElement(descriptionElement);
141 descriptionElementComposite = new DescriptionElementComposite(
142 featureGroupComposite, managedForm, descriptionElement);
143
144 return Status.OK_STATUS;
145 }
146
147 @Override
148 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
149 throws ExecutionException {
150 // TODO Auto-generated method stub
151 return Status.OK_STATUS;
152 }
153
154 @Override
155 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
156 throws ExecutionException {
157 // TODO Auto-generated method stub
158 return Status.OK_STATUS;
159 }
160 }
161 }