860f76b5ff33e85300d9fcd110a9bc74a9fccfab
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / ui / AdaptCompositeToGroupAction.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;
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.jface.util.IPropertyChangeListener;
25
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;
37
38 /**
39 * Reassigns the synonym attached to the NameComposite's data object to the
40 * HomotypicalGroup specified in the GroupComposite's data object.
41 *
42 * If this is successful, new Synonym is attached to the NameComposite's data
43 * object, and NameComposite is cosmetically transformed according to its
44 * synonymy type.
45 *
46 * @author p.ciardelli
47 * @created 02.06.2008
48 * @version 1.0
49 */
50 public class AdaptCompositeToGroupAction extends Action {
51 private static final Logger logger = Logger
52 .getLogger(AdaptCompositeToGroupAction.class);
53
54 private static String text = "Move name composite to new homotypic group";
55 private ImageDescriptor image = null;
56
57 private IUndoableOperation operation;
58
59 private NameComposite composite;
60 // private Synonym synonym = null;
61 private TaxonBase taxonBase = null;
62 private HomotypicalGroup newHomotypicGroup;
63 private Taxon taxon;
64
65 private AdaptCompositeToGroupAction() {
66 super(text);
67 setImageDescriptor(image);
68 }
69
70 public AdaptCompositeToGroupAction(final NameComposite composite,
71 GroupComposite groupComposite) {
72 this();
73
74 if (composite.getData() instanceof TaxonBase) {
75 this.taxonBase = (TaxonBase) composite.getData();
76 } else {
77 throw new IllegalArgumentException(
78 "This action requires a NameComposite with a TaxonBase (Synonym or Taxon) in its data field.");
79 }
80
81 if (groupComposite.getData() instanceof HomotypicalGroup) {
82 this.newHomotypicGroup = (HomotypicalGroup) groupComposite
83 .getData();
84 } else {
85 throw new IllegalArgumentException(
86 "This action requires a GroupComposite with a HomotypicalGroup in its data field.");
87 }
88
89 this.composite = composite;
90
91 if (groupComposite.getParent().getData() instanceof Taxon) {
92 this.taxon = (Taxon) groupComposite.getParent().getData();
93 } else {
94 throw new IllegalArgumentException(
95 "This action requires a GroupComposite whose parent has a Taxon in its data field.");
96 }
97
98 operation = new ChangeHomotypicGroupOperation();
99
100 }
101
102 public void run() {
103 IOperationHistory operationHistory = UiUtil.getOperationHistory();
104 IUndoContext undoContext = UiUtil.getWorkbenchUndoContext();
105 operation.addContext(undoContext);
106 try {
107 operationHistory.execute(operation, null, null);
108 operationHistory.add(operation);
109 } catch (ExecutionException e) {
110 e.printStackTrace();
111 }
112 }
113
114 class ChangeHomotypicGroupOperation extends AbstractOperation {
115
116 public ChangeHomotypicGroupOperation() {
117 super("'" + text + "'");
118 }
119
120 @Override
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);
127 }
128 if (taxonBase instanceof Taxon) {
129 changeHomotypicGroupAction = new ChangeTaxonRelationToSynonymAction(
130 (Taxon) taxonBase, taxon, newHomotypicGroup);
131 }
132 changeHomotypicGroupAction
133 .addPropertyChangeListener(new IPropertyChangeListener() {
134 @Override
135 public void propertyChange(
136 org.eclipse.jface.util.PropertyChangeEvent event) {
137 if (event.getProperty().equals(
138 ITaxEditorConstants.HOMOTYPIC_GROUP)) {
139
140 Synonym newSynonym = null;
141 if (event.getNewValue() instanceof Synonym) {
142 newSynonym = (Synonym) event.getNewValue();
143 } else {
144 throw new IllegalArgumentException(
145 "ChangeSynonymHomotypicGroupAction event did not return a Synonym in its newValue field.");
146 }
147
148 composite.setData(newSynonym);
149
150 if (newSynonym.getName() == null) {
151 return;
152 }
153
154 if (CdmUtil.isNameHomotypic(newSynonym.getName(), taxon)) {
155 composite.transform(NameComposite.CHANGE_TO_HOMOTYPIC_SYNONYM);
156 } else {
157 composite.transform(NameComposite.CHANGE_TO_HETEROTYPIC_SYNONYM);
158 }
159 }
160 }
161 });
162 changeHomotypicGroupAction.run();
163 return Status.OK_STATUS;
164 }
165
166 @Override
167 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
168 throws ExecutionException {
169 return execute(monitor, info);
170 }
171
172 @Override
173 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
174 throws ExecutionException {
175 return Status.OK_STATUS;
176 }
177 }
178 }