fix remaining issues for taxon field prefill #4860
[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.Rank;
20 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
21 import eu.etaxonomy.cdm.model.reference.Reference;
22 import eu.etaxonomy.cdm.model.taxon.Classification;
23 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
24 import eu.etaxonomy.cdm.model.taxon.Taxon;
25 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
26 import eu.etaxonomy.taxeditor.parser.ParseHandler;
27 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
28 import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
29 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
30 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
31 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
32 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
33 import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
34
35 /**
36 * <p>
37 * TaxonNodeDetailElement class.
38 * </p>
39 *
40 * @author n.hoffmann
41 * @created Sep 27, 2010
42 * @version 1.0
43 */
44 public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeNode> {
45
46 private EntitySelectionElement<Classification> selection_classification;
47
48 private Classification classification;
49
50 private TaxonNodeSelectionElement selection_parentTaxonNode;
51
52 private ITaxonTreeNode parentTreeNode;
53
54 private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
55
56 private Taxon taxon;
57
58 private TextWithLabelElement text_newTaxonName;
59
60 private CheckboxElement checkbox_openInEditor;
61
62 private boolean openInEditor;
63
64 private boolean complete;
65
66 private EntitySelectionElement<TaxonNameBase> selection_reuseExistingName;
67
68 /**
69 * <p>
70 * Constructor for TaxonNodeDetailElement.
71 * </p>
72 *
73 * @param formFactory
74 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
75 * object.
76 * @param formElement
77 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
78 * object.
79 */
80 public TaxonNodeDetailElement(CdmFormFactory formFactory,
81 ICdmFormElement formElement) {
82 super(formFactory, formElement);
83 }
84
85 /*
86 * (non-Javadoc)
87 *
88 * @see
89 * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#createControls
90 * (eu.etaxonomy.taxeditor.forms.ICdmFormElement,
91 * eu.etaxonomy.cdm.model.common.IAnnotatableEntity, int)
92 */
93 /** {@inheritDoc} */
94 @Override
95 protected void createControls(ICdmFormElement formElement,
96 ITaxonTreeNode entity, int style) {
97 selection_classification = formFactory
98 .createSelectionElement(Classification.class,
99 getConversationHolder(), formElement, "Classification",
100 null, EntitySelectionElement.DELETABLE, style);
101 selection_parentTaxonNode = formFactory
102 .createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", null,
103 EntitySelectionElement.DELETABLE, style);
104 selection_reuseExistingTaxon = formFactory
105 .createSelectionElement(Taxon.class,
106 getConversationHolder(), formElement,
107 "Reuse existing taxon", null,
108 EntitySelectionElement.DELETABLE, style);
109
110 selection_reuseExistingName = formFactory
111 .createSelectionElement(TaxonNameBase.class,
112 getConversationHolder(), formElement,
113 "Reuse existing name", null,
114 EntitySelectionElement.DELETABLE, style);
115
116 text_newTaxonName = formFactory.createTextWithLabelElement(formElement,
117 "New Taxon", "", style);
118 text_newTaxonName.setFocus();
119 preFillParentTaxonName();
120
121 checkbox_openInEditor = formFactory.createCheckbox(formElement,
122 "Open in Editor", true, style);
123 setOpenInEditor(true);
124 setParentTreeNode(entity);
125 }
126
127 private void preFillParentTaxonName() {
128 if(getEntity() instanceof TaxonNode){
129 TaxonNode node = (TaxonNode)getEntity();
130 if(node.getTaxon()!=null){
131 Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
132 if(taxon.getName()!=null && taxon.getName().isInstanceOf(NonViralName.class)){
133 NonViralName<?> name = HibernateProxyHelper.deproxy(node.getTaxon().getName(), NonViralName.class);
134 if( ! name.isSupraGeneric() && name.getRank() != null){
135 String taxonName = "";
136 if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
137 taxonName = name.getGenusOrUninomial();
138 }
139 else if(name.isSpecies() || name.isInfraSpecific() ){
140 taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
141 }
142 if (StringUtils.isNotBlank(taxonName)){
143 text_newTaxonName.setText(taxonName + " ");
144 if(text_newTaxonName.getMainControl() instanceof Text){
145 Text text = (Text)text_newTaxonName.getMainControl();
146 text.setSelection(text_newTaxonName.getText().length());
147 }
148 }
149 }
150 }
151 }
152 }
153 }
154
155 /*
156 * (non-Javadoc)
157 *
158 * @see
159 * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleEvent(java
160 * .lang.Object)
161 */
162 /** {@inheritDoc} */
163 @Override
164 public void handleEvent(Object eventSource) {
165 if (eventSource == selection_classification) {
166 setClassification(selection_classification.getEntity());
167 } else if (eventSource == selection_parentTaxonNode) {
168 setParentTreeNode(selection_parentTaxonNode.getEntity());
169 } else if (eventSource == selection_reuseExistingTaxon) {
170 boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
171 selection_reuseExistingName.setEnabled(enabled);
172 text_newTaxonName.setEnabled(enabled);
173
174 setTaxon(selection_reuseExistingTaxon.getEntity());
175 } else if (eventSource == selection_reuseExistingName) {
176 boolean enabled = selection_reuseExistingName.getEntity() == null;
177 selection_reuseExistingTaxon.setEnabled(enabled);
178 text_newTaxonName.setEnabled(enabled);
179
180 setTaxon(selection_reuseExistingName.getEntity());
181 } else if (eventSource == text_newTaxonName) {
182 boolean enabled = CdmUtils.isEmpty(text_newTaxonName.getText());
183 selection_reuseExistingTaxon.setEnabled(enabled);
184 selection_reuseExistingName.setEnabled(enabled);
185
186 setTaxon(text_newTaxonName.getText());
187 } else if (eventSource == checkbox_openInEditor) {
188 setOpenInEditor(checkbox_openInEditor.getSelection());
189 }
190 }
191
192 /**
193 * <p>
194 * Getter for the field <code>classification</code>.
195 * </p>
196 *
197 * @return the classification
198 */
199 public Classification getClassification() {
200 return classification;
201 }
202
203 /**
204 * <p>
205 * isOpenInEditor
206 * </p>
207 *
208 * @return the openInEditor
209 */
210 public boolean isOpenInEditor() {
211 return openInEditor;
212 }
213
214 /**
215 * <p>
216 * Getter for the field <code>parentTreeNode</code>.
217 * </p>
218 *
219 * @return a {@link eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode} object.
220 */
221 public ITaxonTreeNode getParentTreeNode() {
222 return parentTreeNode;
223 }
224
225 /**
226 * <p>
227 * Getter for the field <code>taxon</code>.
228 * </p>
229 *
230 * @return the taxon
231 */
232 public Taxon getTaxon() {
233 return taxon;
234 }
235
236 /**
237 * @param classification
238 * the classification to set
239 */
240 private void setClassification(Classification classification) {
241 this.classification = classification;
242 setParentTreeNode(classification);
243 }
244
245 /**
246 * @param parentTreeNode
247 * the parentTreeNode to set
248 */
249 private void setParentTreeNode(ITaxonTreeNode parentTreeNode) {
250 this.parentTreeNode = parentTreeNode;
251
252 if (parentTreeNode instanceof Classification) {
253 classification = (Classification) parentTreeNode;
254 selection_classification.setEntity(classification);
255 selection_parentTaxonNode.setEntity(null);
256 selection_parentTaxonNode.setClassification(classification);
257 } else if (parentTreeNode instanceof TaxonNode) {
258 classification = (Classification) HibernateProxyHelper
259 .deproxy(((TaxonNode) parentTreeNode).getClassification());
260 selection_classification.setEntity(classification);
261 selection_parentTaxonNode.setEntity((TaxonNode) parentTreeNode);
262 selection_parentTaxonNode.setClassification(classification);
263 } else if(parentTreeNode == null){
264 this.parentTreeNode = selection_classification.getEntity();
265 }
266 }
267
268 /**
269 * @param reuseExistingTaxon
270 * the reuseExistingTaxon to set
271 */
272 private void setTaxon(Taxon taxon) {
273 this.taxon = taxon;
274 }
275
276 private void setTaxon(String taxonNameString) {
277 TaxonNameBase taxonName = ParseHandler.parseReferencedName(
278 taxonNameString, null);
279 setTaxon(taxonName);
280 }
281
282 private void setTaxon(TaxonNameBase taxonName) {
283 Reference secundum = null;
284 if (getParentTreeNode() != null) {
285 if (getParentTreeNode() instanceof Classification) {
286 secundum = ((Classification) getParentTreeNode())
287 .getReference();
288 } else if (getParentTreeNode() instanceof TaxonNode) {
289 secundum = ((TaxonNode) getParentTreeNode()).getTaxon()
290 .getSec();
291 }
292 }
293 taxon = Taxon.NewInstance(taxonName, secundum);
294 }
295
296 /**
297 * @param openInEditor
298 * the openInEditor to set
299 */
300 private void setOpenInEditor(boolean openInEditor) {
301 this.openInEditor = openInEditor;
302 }
303
304 /**
305 * <p>
306 * isComplete
307 * </p>
308 *
309 * @return the complete
310 */
311 public boolean isComplete() {
312 return complete;
313 }
314
315 }