fixes #888 and #889
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / ConceptComposite.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.eclipse.core.commands.operations.IUndoableOperation;
13 import org.eclipse.swt.graphics.Font;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.ui.views.properties.IPropertySource;
16
17 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
18 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
19 import eu.etaxonomy.cdm.model.taxon.Taxon;
20 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
21 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
22 import eu.etaxonomy.taxeditor.editor.EditorUtil;
23 import eu.etaxonomy.taxeditor.operations.ChangeConceptRelationshipTypeOperation;
24 import eu.etaxonomy.taxeditor.operations.ChangeConceptToSynonymOperation;
25 import eu.etaxonomy.taxeditor.propertysheet.name.ConceptPropertySource;
26
27 /**
28 * @author p.ciardelli
29 * @created 26.01.2009
30 * @version 1.0
31 */
32 public class ConceptComposite extends NameComposite<Taxon> {
33
34 private Taxon relatedTaxon;
35 private TaxonRelationshipType relationshipType;
36
37 /**
38 *
39 */
40 private boolean acceptedTaxonIsFromTaxon;
41
42 private ConceptComposite(TaxonNameEditor editor, Composite parent,
43 Taxon relatedTaxon, TaxonRelationshipType relationshipType,
44 boolean acceptedTaxonIsFromTaxon) {
45 super(editor, parent, relatedTaxon);
46
47 this.relatedTaxon = relatedTaxon;
48 this.relationshipType = relationshipType;
49 this.setAcceptedTaxonIsFromTaxon(acceptedTaxonIsFromTaxon);
50
51 initializeComposite();
52 }
53
54 /* (non-Javadoc)
55 * @see eu.etaxonomy.taxeditor.editor.name.NameComposite#initializeComposite()
56 */
57 @Override
58 protected void initializeComposite() {
59
60 setIsDraggable(true);
61 setIcon(CONCEPT_ICON);
62 setFont(getViewerFont());
63 setIndent(CONCEPT_INDENT);
64
65 showRelationshipType();
66 showSec();
67
68 initTextViewer(relatedTaxon);
69 }
70
71 private void showRelationshipType() {
72 setNonEditableInfo(relationshipType.getLabel());
73 }
74
75 private void showSec() {
76 if (relatedTaxon == null) {
77 return;
78 }
79
80 if (relatedTaxon.getSec() == null) {
81 setNonEditableInfo("sec. ???");
82 } else {
83 setNonEditableInfo("sec. " + relatedTaxon.getSec().getTitleCache());
84 }
85 }
86
87 public static ConceptComposite getNewInstance(TaxonNameEditor editor, Composite parent, TaxonRelationship relationship){
88
89 TaxonRelationshipType relationshipType = relationship.getType();
90
91 if (relationship.getToTaxon().equals(editor.getTaxon())) {
92 return new ConceptComposite (editor, parent,
93 relationship.getFromTaxon(), relationshipType, false);
94 } else {
95 return new ConceptComposite (editor, parent,
96 relationship.getToTaxon(), relationshipType, true);
97 }
98 }
99
100 /* (non-Javadoc)
101 * @see eu.etaxonomy.taxeditor.editor.GroupedComposite#getViewerFont()
102 */
103 @Override
104 protected Font getViewerFont() {
105 return CONCEPT_FONT;
106 }
107
108 /* (non-Javadoc)
109 * @see eu.etaxonomy.taxeditor.editor.IHasPropertySource#getPropertySource()
110 */
111
112 public IPropertySource getPropertySource() {
113 return new ConceptPropertySource(relatedTaxon);
114 }
115
116 public Taxon getRelatedTaxon() {
117 return relatedTaxon;
118 }
119
120 protected TaxonNameBase<?, ?> getName() {
121 return relatedTaxon.getName();
122 }
123
124 protected void calculateErrors() {
125 super.calculateErrors();
126
127 textViewer.setShowSecError(relatedTaxon);
128 }
129
130 public Object getData () {
131 return getRelatedTaxon();
132 }
133
134 /**
135 * @param acceptedTaxonIsFromTaxon the acceptedTaxonIsFromTaxon to set
136 */
137 public void setAcceptedTaxonIsFromTaxon(boolean acceptedTaxonIsFromTaxon) {
138 this.acceptedTaxonIsFromTaxon = acceptedTaxonIsFromTaxon;
139 }
140
141 /**
142 * @return the acceptedTaxonIsFromTaxon
143 */
144 public boolean isAcceptedTaxonIsFromTaxon() {
145 return acceptedTaxonIsFromTaxon;
146 }
147
148 public boolean setParent(Composite parent) {
149 boolean doSetParent = false;
150
151 if (parent instanceof MisappliedGroupComposite) {
152 IUndoableOperation operation = new ChangeConceptRelationshipTypeOperation("change to misapplication",
153 editor.getUndoContext(), taxon, relatedTaxon, TaxonRelationshipType.MISAPPLIED_NAME_FOR(), editor); //$NON-NLS-1$
154 EditorUtil.executeOperation(operation);
155
156 doSetParent = true;
157 }
158
159 if (parent instanceof HomotypicalGroupComposite) {
160 HomotypicalGroup homotypicalGroup =
161 ((HomotypicalGroupComposite)parent).getGroup();
162 IUndoableOperation operation = new ChangeConceptToSynonymOperation
163 ("change concept to synonym", editor.getUndoContext(), taxon, relatedTaxon, homotypicalGroup, editor);
164 EditorUtil.executeOperation(operation);
165
166 doSetParent = true;
167 }
168
169 return doSetParent;
170 }
171 }
172
173
174
175
176
177