90706eb8b265d80d239665053ae1fabafcef4739
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / classification / TaxonNodeDetailElement.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.section.classification;
12
13 import org.apache.commons.lang.StringUtils;
14 import org.eclipse.swt.widgets.Text;
15
16 import eu.etaxonomy.cdm.common.CdmUtils;
17 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
18 import eu.etaxonomy.cdm.model.name.NonViralName;
19 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
20 import eu.etaxonomy.cdm.model.reference.Reference;
21 import eu.etaxonomy.cdm.model.taxon.Classification;
22 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
23 import eu.etaxonomy.cdm.model.taxon.Taxon;
24 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
25 import eu.etaxonomy.taxeditor.parser.ParseHandler;
26 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
27 import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
28 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
29 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
30 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
31 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
32 import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
33
34 /**
35 *
36 * @author n.hoffmann
37 * @created Sep 27, 2010
38 * @version 1.0
39 */
40 public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeNode> {
41
42 private EntitySelectionElement<Classification> selection_classification;
43
44 private Classification classification;
45
46 private TaxonNodeSelectionElement selection_parentTaxonNode;
47
48 private ITaxonTreeNode parentTreeNode;
49
50 private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
51 private EntitySelectionElement<Reference> selection_SecRef;
52
53 private Taxon taxon;
54
55 private Reference secReference;
56
57 private TextWithLabelElement textNewTaxonName;
58
59 private CheckboxElement checkbox_openInEditor;
60
61 private boolean openInEditor;
62
63 private boolean complete;
64
65 private EntitySelectionElement<TaxonNameBase> selection_reuseExistingName;
66
67 public TaxonNodeDetailElement(CdmFormFactory formFactory,
68 ICdmFormElement formElement) {
69 super(formFactory, formElement);
70 }
71
72 /** {@inheritDoc} */
73 @Override
74 protected void createControls(ICdmFormElement formElement,
75 ITaxonTreeNode entity, int style) {
76 selection_classification = formFactory
77 .createSelectionElement(Classification.class,
78 getConversationHolder(), formElement, "Classification",
79 null, EntitySelectionElement.DELETABLE, style);
80 selection_parentTaxonNode = formFactory
81 .createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", null,
82 EntitySelectionElement.DELETABLE, style);
83
84 selection_reuseExistingTaxon = formFactory
85 .createSelectionElement(Taxon.class,
86 getConversationHolder(), formElement,
87 "Reuse existing taxon", null,
88 EntitySelectionElement.DELETABLE, style);
89
90 selection_reuseExistingName = formFactory
91 .createSelectionElement(TaxonNameBase.class,
92 getConversationHolder(), formElement,
93 "Reuse existing name", null,
94 EntitySelectionElement.DELETABLE, style);
95 selection_SecRef = formFactory
96 .createSelectionElement(Reference.class,getConversationHolder(), formElement, "Secundum Reference", null,
97 EntitySelectionElement.DELETABLE, style);
98 textNewTaxonName = formFactory.createTextWithLabelElement(formElement,
99 "New Taxon", "", style);
100 textNewTaxonName.setFocus();
101 preFillParentTaxonName();
102
103 checkbox_openInEditor = formFactory.createCheckbox(formElement,
104 "Open in Editor", true, style);
105 setOpenInEditor(true);
106 setParentTreeNode(entity);
107 }
108
109 private void preFillParentTaxonName() {
110 if(getEntity() instanceof TaxonNode){
111 TaxonNode node = (TaxonNode)getEntity();
112 if(node.getTaxon()!=null){
113 Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
114 if(taxon.getName()!=null && taxon.getName().isInstanceOf(NonViralName.class)){
115 NonViralName<?> name = HibernateProxyHelper.deproxy(node.getTaxon().getName(), NonViralName.class);
116 if( ! name.isSupraGeneric() && name.getRank() != null){
117 String taxonName = "";
118 if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
119 taxonName = name.getGenusOrUninomial();
120 }
121 else if(name.isSpecies() || name.isInfraSpecific() ){
122 taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
123 }
124 if (StringUtils.isNotBlank(taxonName)){
125 textNewTaxonName.setText(taxonName + " ");
126 if(textNewTaxonName.getMainControl() instanceof Text){
127 Text text = (Text)textNewTaxonName.getMainControl();
128 text.setSelection(textNewTaxonName.getText().length());
129 }
130 }
131 }
132 }
133
134 }
135 }
136 }
137
138 /** {@inheritDoc} */
139 @Override
140 public void handleEvent(Object eventSource) {
141 if (eventSource == selection_classification) {
142 setClassification(selection_classification.getEntity());
143 } else if (eventSource == selection_parentTaxonNode) {
144 setParentTreeNode(selection_parentTaxonNode.getEntity());
145 } else if (eventSource == selection_SecRef) {
146 setSecReference(selection_SecRef.getEntity());
147 } else if (eventSource == selection_reuseExistingTaxon) {
148 boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
149 selection_reuseExistingName.setEnabled(enabled);
150 textNewTaxonName.setEnabled(enabled);
151 if (!enabled){
152 setTaxon(selection_reuseExistingTaxon.getEntity());
153 } else{
154 textNewTaxonName.setText(null);
155 }
156 complete = !textNewTaxonName.getText().isEmpty();
157
158
159 } else if (eventSource == selection_reuseExistingName) {
160 boolean enabled = selection_reuseExistingName.getEntity() == null;
161 setTaxon(selection_reuseExistingName.getEntity());
162 selection_reuseExistingTaxon.setEnabled(enabled);
163 textNewTaxonName.setEnabled(enabled);
164
165 setTaxon(selection_reuseExistingName.getEntity());
166
167 complete = !textNewTaxonName.getText().isEmpty();
168 } else if (eventSource == textNewTaxonName) {
169 boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
170 selection_reuseExistingTaxon.setEnabled(enabled);
171 selection_reuseExistingName.setEnabled(enabled);
172
173 setTaxon(textNewTaxonName.getText());
174 complete = !textNewTaxonName.getText().isEmpty();
175 } else if (eventSource == checkbox_openInEditor) {
176 setOpenInEditor(checkbox_openInEditor.getSelection());
177 }
178 }
179
180 public Classification getClassification() {
181 return classification;
182 }
183
184 public boolean isOpenInEditor() {
185 return openInEditor;
186 }
187
188 public ITaxonTreeNode getParentTreeNode() {
189 return parentTreeNode;
190 }
191
192 public Taxon getTaxon() {
193 return taxon;
194 }
195
196 private void setClassification(Classification classification) {
197 this.classification = classification;
198 setParentTreeNode(classification);
199 }
200
201 private void setParentTreeNode(ITaxonTreeNode parentTreeNode) {
202 this.parentTreeNode = parentTreeNode;
203
204 if (parentTreeNode instanceof Classification) {
205 classification = (Classification) parentTreeNode;
206 selection_classification.setEntity(classification);
207 selection_parentTaxonNode.setEntity(null);
208 selection_parentTaxonNode.setClassification(classification);
209 selection_SecRef.setEntity(classification.getReference());
210 } else if (parentTreeNode instanceof TaxonNode) {
211 classification = (Classification) HibernateProxyHelper
212 .deproxy(((TaxonNode) parentTreeNode).getClassification());
213 selection_classification.setEntity(classification);
214 selection_parentTaxonNode.setEntity((TaxonNode) parentTreeNode);
215 selection_parentTaxonNode.setClassification(classification);
216 selection_SecRef.setEntity(((TaxonNode)parentTreeNode).getTaxon().getSec());
217 } else if(parentTreeNode == null){
218 this.parentTreeNode = selection_classification.getEntity();
219 }
220 }
221
222 private void setSecReference(Reference secRef){
223 this.secReference = secRef;
224 setTaxon(textNewTaxonName.getText());
225 }
226
227 private void setTaxon(Taxon taxon) {
228 this.taxon = taxon;
229 textNewTaxonName.setText(taxon.getName().getTitleCache());
230 selection_SecRef.setEntity(taxon.getSec());
231 }
232
233 private void setTaxon(String taxonNameString) {
234 TaxonNameBase taxonName = ParseHandler.parseReferencedName(
235 taxonNameString, null);
236 setTaxon(taxonName);
237 }
238
239 private void setTaxon(TaxonNameBase taxonName) {
240 Reference secundum = null;
241 if (getParentTreeNode() != null) {
242 if (this.selection_SecRef.getEntity() != null){
243 secundum = this.selection_SecRef.getEntity();
244 } /*else if (getParentTreeNode() instanceof Classification) {
245 secundum = ((Classification) getParentTreeNode())
246 .getReference();
247 } else if (getParentTreeNode() instanceof TaxonNode) {
248 secundum = ((TaxonNode) getParentTreeNode()).getTaxon()
249 .getSec();
250 }*/
251 }
252 if (taxonName != null){
253 taxon = Taxon.NewInstance(taxonName, secundum);
254 if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
255 textNewTaxonName.setText(taxonName.getTitleCache());
256 }
257 } else {
258 textNewTaxonName.setText(null);
259 }
260 }
261
262 private void setOpenInEditor(boolean openInEditor) {
263 this.openInEditor = openInEditor;
264 }
265
266 public TextWithLabelElement getTextNewTaxonName() {
267 return textNewTaxonName;
268 }
269
270 public boolean isComplete() {
271 return complete;
272 }
273
274 }