2caf83275562a7597168e215035f3aeb2899e2d7
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / editor / name / MisappliedNameComposite.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.ExecutionException;
14 import org.eclipse.core.commands.operations.IOperationHistory;
15 import org.eclipse.core.commands.operations.IUndoContext;
16 import org.eclipse.core.commands.operations.IUndoableOperation;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.swt.graphics.Font;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.ui.forms.IManagedForm;
23 import org.eclipse.ui.views.properties.IPropertySource;
24
25 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
26 import eu.etaxonomy.cdm.model.taxon.Taxon;
27 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
28 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
29 import eu.etaxonomy.taxeditor.UiUtil;
30 import eu.etaxonomy.taxeditor.editor.ContextMenu;
31 import eu.etaxonomy.taxeditor.operations.name.ChangeMisappliedNameToSynonymOperation;
32 import eu.etaxonomy.taxeditor.operations.name.RemoveMisappliedNameOperation;
33 import eu.etaxonomy.taxeditor.propertysheet.name.TaxonBasePropertySource;
34
35 /**
36 * @author p.ciardelli
37 * @created 13.01.2009
38 * @version 1.0
39 */
40 public class MisappliedNameComposite extends NameComposite {
41 private static final Logger logger = Logger
42 .getLogger(MisappliedNameComposite.class);
43
44 private Taxon misappliedName;
45
46 public MisappliedNameComposite(Composite parent, IManagedForm form,
47 Taxon taxon, Taxon misappliedName) {
48 super(parent, form, NameComposite.MISAPPLIED_NAME, misappliedName);
49
50 this.taxon = taxon;
51 this.misappliedName = misappliedName;
52
53 setDraggable(true);
54 setIcon(MISAPPLIEDNAME_ICON);
55 setFont(getViewerFont());
56 setIndent(MISAPPLIEDNAME_INDENT);
57
58 createMenu();
59
60 initNameViewer(misappliedName);
61 }
62
63 public Taxon getMisappliedName() {
64 return misappliedName;
65 }
66
67 private void createMenu() {
68
69 ContextMenu contextMenu = createContextMenu();
70
71 final IOperationHistory operationHistory = UiUtil.getOperationHistory();
72 final IUndoContext undoContext = UiUtil.getTaxonNameEditorUndoContext(taxon);
73
74 // Remove misapplied name from taxon
75 String text = "Remove misapplied name from taxon"; //$NON-NLS-1$
76 ImageDescriptor image = TaxEditorPlugin.getDefault()
77 .getImageDescriptor(ITaxEditorConstants.ACTIVE_DELETE_ICON);
78 contextMenu.addAction(new Action(text, image){
79
80 public void run() {
81 IUndoableOperation operation = new RemoveMisappliedNameOperation
82 (this.getText(), undoContext, taxon, misappliedName);
83 try {
84 IStatus status = operationHistory.execute(operation, null, null);
85 } catch (ExecutionException e) {
86 // TODO Auto-generated catch block
87 e.printStackTrace();
88 }
89 }
90 });
91 }
92
93 public boolean setParent(Composite parent) {
94
95 if (super.setParent(parent)) {
96
97 // If this has been moved to the misapplied names group, do nothing
98 if (parent instanceof MisappliedGroupComposite) {
99 return true;
100 }
101
102 // Has this been moved to a HomotypicalGroup?
103 if (parent instanceof HomotypicalGroupComposite) {
104
105 HomotypicalGroup homotypicalGroup =
106 ((HomotypicalGroupComposite)parent).getGroup();
107
108 IOperationHistory operationHistory = UiUtil.getOperationHistory();
109 IUndoContext undoContext = UiUtil.getTaxonNameEditorUndoContext(taxon);
110 IUndoableOperation operation = new ChangeMisappliedNameToSynonymOperation
111 ("change misapplication to synonym", undoContext, taxon, misappliedName, homotypicalGroup);
112
113 try {
114 IStatus status = operationHistory.execute(operation, null, null);
115 } catch (ExecutionException e) {
116 // TODO Auto-generated catch block
117 e.printStackTrace();
118 }
119 }
120 return true;
121
122 }
123 return false;
124 }
125
126 public IPropertySource getPropertySource() {
127 return new TaxonBasePropertySource(misappliedName);
128 }
129
130 @Override
131 protected Font getViewerFont() {
132 return MISAPPLIEDNAME_FONT;
133 }
134 }