589bab86b67a733cb1c625abde71f30a102618f2
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / editor / name / SynonymComposite.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.editor.name;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.core.commands.operations.IUndoContext;
14 import org.eclipse.core.commands.operations.IUndoableOperation;
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.swt.graphics.Font;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.ui.forms.IManagedForm;
20 import org.eclipse.ui.views.properties.IPropertySource;
21
22 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
23 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
24 import eu.etaxonomy.cdm.model.taxon.Synonym;
25 import eu.etaxonomy.cdm.model.taxon.Taxon;
26 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
27 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
28 import eu.etaxonomy.taxeditor.controller.EditorController;
29 import eu.etaxonomy.taxeditor.controller.GlobalController;
30 import eu.etaxonomy.taxeditor.editor.ContextMenu;
31 import eu.etaxonomy.taxeditor.model.CdmUtil;
32 import eu.etaxonomy.taxeditor.operations.name.ChangeHomotypicGroupOperation;
33 import eu.etaxonomy.taxeditor.operations.name.ChangeSynonymToConceptOperation;
34 import eu.etaxonomy.taxeditor.operations.name.ChangeSynonymToMisappliedNameOperation;
35 import eu.etaxonomy.taxeditor.operations.name.ChangeSynonymToTaxonOperation;
36 import eu.etaxonomy.taxeditor.operations.name.MakeSynonymAcceptedOperation;
37 import eu.etaxonomy.taxeditor.operations.name.MakeSynonymGroupBasionymOperation;
38 import eu.etaxonomy.taxeditor.operations.name.RemoveSynonymOperation;
39 import eu.etaxonomy.taxeditor.operations.name.UnmakeSynonymGroupBasionymOperation;
40 import eu.etaxonomy.taxeditor.propertysheet.name.TaxonBasePropertySource;
41
42 /**
43 * @author p.ciardelli
44 * @created 13.01.2009
45 * @version 1.0
46 */
47 public class SynonymComposite extends NameComposite {
48 @SuppressWarnings("unused")
49 private static final Logger logger = Logger
50 .getLogger(SynonymComposite.class);
51
52 private Synonym synonym;
53
54 private IUndoContext undoContext;
55
56 public SynonymComposite(Composite parent, IManagedForm form,
57 Taxon taxon, Synonym synonym) {
58 super(parent, form, null, synonym);
59
60 this.taxon = taxon;
61 this.synonym = synonym;
62
63 this.undoContext = EditorController.getUndoContext(taxon);
64
65 setIsDraggable(true);
66 setFont(getViewerFont());
67 setIndent(SYNONYM_INDENT);
68
69 if (isHomotypic()) {
70 if (CdmUtil.isNameGroupBasionym(synonym.getName())) {
71 setIcon(HOMOTYPIC_SYNONYM_ORIGINAL_COMBINATION_ICON);
72 } else {
73 setIcon(HOMOTYPIC_SYNONYM_ICON);
74 }
75 } else {
76 if (CdmUtil.isNameGroupBasionym(synonym.getName())) {
77 setIcon(HETEROTYPIC_SYNONYM_ORIGINAL_COMBINATION_ICON);
78 } else {
79 setIcon(HETEROTYPIC_SYNONYM_ICON);
80 }
81 }
82
83 createMenu();
84
85 initNameViewer(synonym);
86 }
87
88 private boolean isHomotypic() {
89 HomotypicalGroup group = synonym.getHomotypicGroup();
90 return group.equals(taxon.getHomotypicGroup());
91 }
92
93 public Synonym getSynonym() {
94 return synonym;
95 }
96
97 public boolean setParent(Composite parent) {
98
99 if (super.setParent(parent)) {
100
101 // Has this been moved to the misapplied names group?
102 if (parent instanceof MisappliedGroupComposite) {
103
104 IUndoableOperation operation = new ChangeSynonymToMisappliedNameOperation
105 ("change to misapplication", undoContext, taxon, synonym); //$NON-NLS-1$
106
107 GlobalController.executeOperation(operation);
108 }
109
110 // Has this been moved to a HomotypicalGroup?
111 if (parent instanceof HomotypicalGroupComposite) {
112
113 HomotypicalGroup homotypicalGroup =
114 ((HomotypicalGroupComposite)parent).getGroup();
115
116 // Make sure we are not dropping synonym on its own group
117 if (homotypicalGroup.equals(synonym.getHomotypicGroup())) {
118 return true;
119 }
120
121 IUndoableOperation operation = new ChangeHomotypicGroupOperation
122 ("change type", undoContext, taxon, synonym, homotypicalGroup); //$NON-NLS-1$
123
124 GlobalController.executeOperation(operation);
125 }
126 return true;
127
128 }
129 return false;
130 }
131
132 /**
133 * @param nameComposite
134 */
135 private void createMenu() {
136
137 ContextMenu contextMenu = createContextMenu();
138
139 // Change composite to a misapplied name
140 String text = "Change synonym to misapplied name"; //$NON-NLS-1$
141 ImageDescriptor image = TaxEditorPlugin.getDefault()
142 .getImageDescriptor(ITaxEditorConstants.MISAPPLIED_NAME_ICON);
143 contextMenu.addAction(new Action(text, image){
144
145 public void run() {
146 IUndoableOperation operation = new ChangeSynonymToMisappliedNameOperation
147 (this.getText(), undoContext, taxon, synonym);
148
149 GlobalController.executeOperation(operation);
150 }
151 });
152
153 // Change synonym to conccept relation
154 text = "Change synonym to concept relation"; //$NON-NLS-1$
155 image = TaxEditorPlugin.getDefault()
156 .getImageDescriptor(ITaxEditorConstants.CONCEPT_ICON);
157
158 contextMenu.addAction(new Action(text, image){
159
160 public void run() {
161 IUndoableOperation operation = new ChangeSynonymToConceptOperation
162 (this.getText(), undoContext, taxon, synonym);
163
164 GlobalController.executeOperation(operation);
165 }
166 });
167
168 // Remove composite
169 text = "Remove synonym from taxon"; //$NON-NLS-1$
170 image = TaxEditorPlugin.getDefault()
171 .getImageDescriptor(ITaxEditorConstants.ACTIVE_DELETE_ICON);
172 contextMenu.addAction(new Action(text, image){
173
174 public void run() {
175 IUndoableOperation operation = new RemoveSynonymOperation
176 (this.getText(), undoContext, taxon, synonym);
177
178 GlobalController.executeOperation(operation);
179 }
180 });
181
182 // Separator
183 contextMenu.addSeparator();
184
185 // Make the synonym the basionym of the homotypic group
186 text = Messages.getString
187 ("AddBasionymCompositeAction.0", synonym.getName()); //$NON-NLS-1$
188 image = TaxEditorPlugin.getDefault()
189 .getImageDescriptor(ITaxEditorConstants.BASIONYM_ICON);
190 Action makeBasionymAction = new Action(text, image){
191
192 public void run() {
193 IUndoableOperation operation = new MakeSynonymGroupBasionymOperation
194 (this.getText(), undoContext, taxon, synonym);
195
196 GlobalController.executeOperation(operation);
197 }
198 };
199 contextMenu.addAction(makeBasionymAction);
200
201 // Action to remove the composite as basionym of the homotypic group
202 text = Messages.getString
203 ("RemoveBasionymCompositeAction.0", synonym.getName()); //$NON-NLS-1$
204 image = TaxEditorPlugin.getDefault()
205 .getImageDescriptor(ITaxEditorConstants.BASIONYM_ICON);
206 Action unmakeBasionymAction = new Action(text, image){
207
208 public void run() {
209 IUndoableOperation operation = new UnmakeSynonymGroupBasionymOperation
210 (this.getText(), undoContext, taxon, synonym);
211
212 GlobalController.executeOperation(operation);
213 }
214 };
215 contextMenu.addAction(unmakeBasionymAction);
216
217 if (CdmUtil.isNameGroupBasionym(synonym.getName())) {
218
219 // Gray out action for "make synonym group basionym"
220 makeBasionymAction.setEnabled(false);
221 } else {
222
223 // Gray out action for "unmake synonym group basionym"
224 unmakeBasionymAction.setEnabled(false);
225 }
226
227 // Separator
228 contextMenu.addSeparator();
229
230 // Change this synonym to the taxon's accepted name
231 text = "Make synonym this taxon's accepted taxon"; //$NON-NLS-1$
232 image = TaxEditorPlugin.getDefault()
233 .getImageDescriptor(ITaxEditorConstants.SWAP_SYNONYM_AND_TAXON_ICON);
234 Action swapAction = new Action(text, image){
235
236 public void run() {
237 IUndoableOperation operation = new MakeSynonymAcceptedOperation
238 (this.getText(), undoContext, taxon, synonym);
239
240 GlobalController.executeOperation(operation);
241 }
242 };
243 contextMenu.addAction(swapAction);
244 // TODO fix operation
245 swapAction.setEnabled(false);
246
247 // Make a new taxon with this synonym as the accepted name
248 text = "Use synonym name to make a new taxon"; //$NON-NLS-1$
249 image = TaxEditorPlugin.getDefault()
250 .getImageDescriptor(ITaxEditorConstants.SYNONYM_TO_TAXON_ICON);
251
252 contextMenu.addAction(new Action(text, image){
253
254 public void run() {
255 IUndoableOperation operation = new ChangeSynonymToTaxonOperation
256 (this.getText(), undoContext, taxon, synonym);
257
258 GlobalController.executeOperation(operation);
259 }
260 });
261 }
262
263
264 // public IPropertySource getPropertySource() {
265 // if (getSynonym() == null) {
266 // return null;
267 // }
268 // return getPropertySourceByName(getSynonym().getName());
269 // }
270 @Override
271 public IPropertySource getPropertySource() {
272 return new TaxonBasePropertySource(synonym, "Synonym Name");
273 }
274
275 @Override
276 protected Font getViewerFont() {
277 return SYNONYM_FONT;
278 }
279
280 @Override
281 protected TaxonNameBase getName() {
282 return synonym.getName();
283 }
284 }