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
;
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
.jface
.util
.IPropertyChangeListener
;
26 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
30 import eu
.etaxonomy
.taxeditor
.ITaxEditorConstants
;
31 import eu
.etaxonomy
.taxeditor
.UiUtil
;
32 import eu
.etaxonomy
.taxeditor
.actions
.cdm
.ChangeSynonymHomotypicGroupAction
;
33 import eu
.etaxonomy
.taxeditor
.actions
.cdm
.ChangeTaxonRelationToSynonymAction
;
34 import eu
.etaxonomy
.taxeditor
.model
.CdmUtil
;
35 import eu
.etaxonomy
.taxeditor
.view
.nameviewersupport
.GroupComposite
;
36 import eu
.etaxonomy
.taxeditor
.view
.nameviewersupport
.NameComposite
;
39 * Reassigns the synonym attached to the NameComposite's data object to the
40 * HomotypicalGroup specified in the GroupComposite's data object.
42 * If this is successful, new Synonym is attached to the NameComposite's data
43 * object, and NameComposite is cosmetically transformed according to its
50 public class AdaptCompositeToGroupAction
extends Action
{
51 private static final Logger logger
= Logger
52 .getLogger(AdaptCompositeToGroupAction
.class);
54 private static String text
= "Move name composite to new homotypic group";
55 private ImageDescriptor image
= null;
57 private IUndoableOperation operation
;
59 private NameComposite composite
;
60 // private Synonym synonym = null;
61 private TaxonBase taxonBase
= null;
62 private HomotypicalGroup newHomotypicGroup
;
65 private AdaptCompositeToGroupAction() {
67 setImageDescriptor(image
);
70 public AdaptCompositeToGroupAction(final NameComposite composite
,
71 GroupComposite groupComposite
) {
74 if (composite
.getData() instanceof TaxonBase
) {
75 this.taxonBase
= (TaxonBase
) composite
.getData();
77 throw new IllegalArgumentException(
78 "This action requires a NameComposite with a TaxonBase (Synonym or Taxon) in its data field.");
81 if (groupComposite
.getData() instanceof HomotypicalGroup
) {
82 this.newHomotypicGroup
= (HomotypicalGroup
) groupComposite
85 throw new IllegalArgumentException(
86 "This action requires a GroupComposite with a HomotypicalGroup in its data field.");
89 this.composite
= composite
;
91 if (groupComposite
.getParent().getData() instanceof Taxon
) {
92 this.taxon
= (Taxon
) groupComposite
.getParent().getData();
94 throw new IllegalArgumentException(
95 "This action requires a GroupComposite whose parent has a Taxon in its data field.");
98 operation
= new ChangeHomotypicGroupOperation();
103 IOperationHistory operationHistory
= UiUtil
.getOperationHistory();
104 IUndoContext undoContext
= UiUtil
.getWorkbenchUndoContext();
105 operation
.addContext(undoContext
);
107 operationHistory
.execute(operation
, null, null);
108 operationHistory
.add(operation
);
109 } catch (ExecutionException e
) {
114 class ChangeHomotypicGroupOperation
extends AbstractOperation
{
116 public ChangeHomotypicGroupOperation() {
117 super("'" + text
+ "'");
121 public IStatus
execute(IProgressMonitor monitor
, IAdaptable info
)
122 throws ExecutionException
{
123 Action changeHomotypicGroupAction
= null;
124 if (taxonBase
instanceof Synonym
) {
125 changeHomotypicGroupAction
= new ChangeSynonymHomotypicGroupAction(
126 (Synonym
) taxonBase
, taxon
, newHomotypicGroup
);
128 if (taxonBase
instanceof Taxon
) {
129 changeHomotypicGroupAction
= new ChangeTaxonRelationToSynonymAction(
130 (Taxon
) taxonBase
, taxon
, newHomotypicGroup
);
132 changeHomotypicGroupAction
133 .addPropertyChangeListener(new IPropertyChangeListener() {
135 public void propertyChange(
136 org
.eclipse
.jface
.util
.PropertyChangeEvent event
) {
137 if (event
.getProperty().equals(
138 ITaxEditorConstants
.HOMOTYPIC_GROUP
)) {
140 Synonym newSynonym
= null;
141 if (event
.getNewValue() instanceof Synonym
) {
142 newSynonym
= (Synonym
) event
.getNewValue();
144 throw new IllegalArgumentException(
145 "ChangeSynonymHomotypicGroupAction event did not return a Synonym in its newValue field.");
148 composite
.setData(newSynonym
);
150 if (newSynonym
.getName() == null) {
154 if (CdmUtil
.isNameHomotypic(newSynonym
.getName(), taxon
)) {
155 composite
.transform(NameComposite
.CHANGE_TO_HOMOTYPIC_SYNONYM
);
157 composite
.transform(NameComposite
.CHANGE_TO_HETEROTYPIC_SYNONYM
);
162 changeHomotypicGroupAction
.run();
163 return Status
.OK_STATUS
;
167 public IStatus
redo(IProgressMonitor monitor
, IAdaptable info
)
168 throws ExecutionException
{
169 return execute(monitor
, info
);
173 public IStatus
undo(IProgressMonitor monitor
, IAdaptable info
)
174 throws ExecutionException
{
175 return Status
.OK_STATUS
;