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