Massive refactoring of the methodology in former class UiUtils
[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.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.taxon.Taxon;
24 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
25 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
26 import eu.etaxonomy.taxeditor.controller.EditorController;
27 import eu.etaxonomy.taxeditor.controller.GlobalController;
28 import eu.etaxonomy.taxeditor.editor.ContextMenu;
29 import eu.etaxonomy.taxeditor.operations.name.ChangeMisappliedNameToSynonymOperation;
30 import eu.etaxonomy.taxeditor.operations.name.RemoveMisappliedNameOperation;
31 import eu.etaxonomy.taxeditor.propertysheet.name.TaxonBasePropertySource;
32
33 /**
34 * @author p.ciardelli
35 * @created 13.01.2009
36 * @version 1.0
37 */
38 public class MisappliedNameComposite extends NameComposite {
39 private static final Logger logger = Logger
40 .getLogger(MisappliedNameComposite.class);
41
42 private Taxon misappliedName;
43
44 public MisappliedNameComposite(Composite parent, IManagedForm form,
45 Taxon taxon, Taxon misappliedName) {
46 super(parent, form, NameComposite.MISAPPLIED_NAME, misappliedName);
47
48 this.taxon = taxon;
49 this.misappliedName = misappliedName;
50
51 setDraggable(true);
52 setIcon(MISAPPLIEDNAME_ICON);
53 setFont(getViewerFont());
54 setIndent(MISAPPLIEDNAME_INDENT);
55
56 createMenu();
57
58 initNameViewer(misappliedName);
59 }
60
61 public Taxon getMisappliedName() {
62 return misappliedName;
63 }
64
65 private void createMenu() {
66
67 ContextMenu contextMenu = createContextMenu();
68
69 final IUndoContext undoContext = EditorController.getUndoContext(taxon);
70
71 // Remove misapplied name from taxon
72 String text = "Remove misapplied name from taxon"; //$NON-NLS-1$
73 ImageDescriptor image = TaxEditorPlugin.getDefault()
74 .getImageDescriptor(ITaxEditorConstants.ACTIVE_DELETE_ICON);
75 contextMenu.addAction(new Action(text, image){
76
77 public void run() {
78 IUndoableOperation operation = new RemoveMisappliedNameOperation
79 (this.getText(), undoContext, taxon, misappliedName);
80
81 GlobalController.executeOperation(operation);
82 }
83 });
84 }
85
86 public boolean setParent(Composite parent) {
87
88 if (super.setParent(parent)) {
89
90 // If this has been moved to the misapplied names group, do nothing
91 if (parent instanceof MisappliedGroupComposite) {
92 return true;
93 }
94
95 // Has this been moved to a HomotypicalGroup?
96 if (parent instanceof HomotypicalGroupComposite) {
97
98 HomotypicalGroup homotypicalGroup =
99 ((HomotypicalGroupComposite)parent).getGroup();
100
101 IUndoContext undoContext = EditorController.getUndoContext(taxon);
102 IUndoableOperation operation = new ChangeMisappliedNameToSynonymOperation
103 ("change misapplication to synonym", undoContext, taxon, misappliedName, homotypicalGroup);
104
105 GlobalController.executeOperation(operation);
106 }
107 return true;
108
109 }
110 return false;
111 }
112
113 public IPropertySource getPropertySource() {
114 return new TaxonBasePropertySource(misappliedName);
115 }
116
117 @Override
118 protected Font getViewerFont() {
119 return MISAPPLIEDNAME_FONT;
120 }
121 }