2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.actions
.ui
.description
;
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
;
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
;
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>.
48 public class AddDescriptionElementCompositeAction
extends Action
{
49 private static final Logger logger
= Logger
50 .getLogger(AddDescriptionElementCompositeAction
.class);
53 private ImageDescriptor image
= null;
55 private IUndoableOperation operation
;
57 private TaxonDescription taxonDescription
;
58 private Composite taxonDescriptionComposite
;
59 private IManagedForm managedForm
;
60 private Feature feature
;
63 * descriptionElement and descriptionElementComposite are created when
64 * the action is executed, and disposed as part of any undo.
66 private DescriptionElementBase descriptionElement
;
67 private DescriptionElementComposite descriptionElementComposite
;
69 private AddDescriptionElementCompositeAction(String text
) {
73 setImageDescriptor(image
);
77 public AddDescriptionElementCompositeAction(Composite taxonDescriptionComposite
,
78 IManagedForm managedForm
, Feature feature
) {
80 // The feature's label will be shown in the menu entry
81 this(feature
.getLabel());
83 // Make sure TaxonDescriptionComposite has a TaxonDescription in its data field
84 if (taxonDescriptionComposite
.getData() instanceof TaxonDescription
) {
85 this.taxonDescription
= (TaxonDescription
) taxonDescriptionComposite
.getData();
87 throw new IllegalArgumentException(
88 "This action requires a composite with a TaxonDescription in its data field.");
91 this.taxonDescriptionComposite
= taxonDescriptionComposite
;
92 this.managedForm
= managedForm
;
93 this.feature
= feature
;
95 operation
= new AddDescriptionElementOperation();
100 IOperationHistory operationHistory
= GlobalController
.getOperationHistory();
101 IUndoContext undoContext
= GlobalController
.getWorkbenchUndoContext();
102 operation
.addContext(undoContext
);
104 operationHistory
.execute(operation
, null, null);
105 operationHistory
.add(operation
);
106 } catch (ExecutionException e
) {
111 class AddDescriptionElementOperation
extends AbstractOperation
{
113 public AddDescriptionElementOperation() {
114 super("'" + text
+ "'");
118 public IStatus
execute(IProgressMonitor monitor
, IAdaptable info
)
119 throws ExecutionException
{
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
;
131 // Otherwise, create a new FeatureGroupComposite
132 if (featureGroupComposite
== null) {
133 featureGroupComposite
= new FeatureGroupComposite(taxonDescriptionComposite
,
134 managedForm
, feature
);
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
);
144 return Status
.OK_STATUS
;
148 public IStatus
redo(IProgressMonitor monitor
, IAdaptable info
)
149 throws ExecutionException
{
150 // TODO Auto-generated method stub
151 return Status
.OK_STATUS
;
155 public IStatus
undo(IProgressMonitor monitor
, IAdaptable info
)
156 throws ExecutionException
{
157 // TODO Auto-generated method stub
158 return Status
.OK_STATUS
;