Refactoring of selection elements. Additional minor refactoring. Fixed a bug with...
[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.ITreeNode;
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<ITreeNode> {
40
41 private EntitySelectionElement<Classification> selection_classification;
42
43 private Classification classification;
44
45 private TaxonNodeSelectionElement selection_parentTaxonNode;
46
47 private ITreeNode 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 ITreeNode 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.NOTHING, style);
104
105 selection_reuseExistingName = formFactory
106 .createSelectionElement(TaxonNameBase.class,
107 getConversationHolder(), formElement,
108 "Reuse existing name", null,
109 EntitySelectionElement.ALL, style);
110
111 text_newTaxonName = formFactory.createTextWithLabelElement(formElement,
112 "New Taxon", "", style);
113
114 checkbox_openInEditor = formFactory.createCheckbox(formElement,
115 "Open in Editor", true, style);
116 setOpenInEditor(true);
117 setParentTreeNode(entity);
118 }
119
120 /*
121 * (non-Javadoc)
122 *
123 * @see
124 * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleEvent(java
125 * .lang.Object)
126 */
127 /** {@inheritDoc} */
128 @Override
129 public void handleEvent(Object eventSource) {
130 if (eventSource == selection_classification) {
131 setClassification(selection_classification.getEntity());
132 } else if (eventSource == selection_parentTaxonNode) {
133 setParentTreeNode(selection_parentTaxonNode.getEntity());
134 } else if (eventSource == selection_reuseExistingTaxon) {
135 boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
136 selection_reuseExistingName.setEnabled(enabled);
137 text_newTaxonName.setEnabled(enabled);
138
139 setTaxon(selection_reuseExistingTaxon.getEntity());
140 } else if (eventSource == selection_reuseExistingName) {
141 boolean enabled = selection_reuseExistingName.getEntity() == null;
142 selection_reuseExistingTaxon.setEnabled(enabled);
143 text_newTaxonName.setEnabled(enabled);
144
145 setTaxon(selection_reuseExistingName.getEntity());
146 } else if (eventSource == text_newTaxonName) {
147 boolean enabled = CdmUtils.isEmpty(text_newTaxonName.getText());
148 selection_reuseExistingTaxon.setEnabled(enabled);
149 selection_reuseExistingName.setEnabled(enabled);
150
151 setTaxon(text_newTaxonName.getText());
152 } else if (eventSource == checkbox_openInEditor) {
153 setOpenInEditor(checkbox_openInEditor.getSelection());
154 }
155 }
156
157 /**
158 * <p>
159 * Getter for the field <code>classification</code>.
160 * </p>
161 *
162 * @return the classification
163 */
164 public Classification getClassification() {
165 return classification;
166 }
167
168 /**
169 * <p>
170 * isOpenInEditor
171 * </p>
172 *
173 * @return the openInEditor
174 */
175 public boolean isOpenInEditor() {
176 return openInEditor;
177 }
178
179 /**
180 * <p>
181 * Getter for the field <code>parentTreeNode</code>.
182 * </p>
183 *
184 * @return a {@link eu.etaxonomy.cdm.model.taxon.ITreeNode} object.
185 */
186 public ITreeNode getParentTreeNode() {
187 return parentTreeNode;
188 }
189
190 /**
191 * <p>
192 * Getter for the field <code>taxon</code>.
193 * </p>
194 *
195 * @return the taxon
196 */
197 public Taxon getTaxon() {
198 return taxon;
199 }
200
201 /**
202 * @param classification
203 * the classification to set
204 */
205 private void setClassification(Classification classification) {
206 this.classification = classification;
207 setParentTreeNode(classification);
208 }
209
210 /**
211 * @param parentTreeNode
212 * the parentTreeNode to set
213 */
214 private void setParentTreeNode(ITreeNode parentTreeNode) {
215 this.parentTreeNode = parentTreeNode;
216
217 if (parentTreeNode instanceof Classification) {
218 classification = (Classification) parentTreeNode;
219 selection_classification.setEntity(classification);
220 selection_parentTaxonNode.setEntity(null);
221 selection_parentTaxonNode.setClassification(classification);
222 } else if (parentTreeNode instanceof TaxonNode) {
223 classification = (Classification) HibernateProxyHelper
224 .deproxy(((TaxonNode) parentTreeNode).getClassification());
225 selection_classification.setEntity(classification);
226 selection_parentTaxonNode.setEntity((TaxonNode) parentTreeNode);
227 selection_parentTaxonNode.setClassification(classification);
228 } else if(parentTreeNode == null){
229 this.parentTreeNode = selection_classification.getEntity();
230 }
231 }
232
233 /**
234 * @param reuseExistingTaxon
235 * the reuseExistingTaxon to set
236 */
237 private void setTaxon(Taxon taxon) {
238 this.taxon = taxon;
239 }
240
241 private void setTaxon(String taxonNameString) {
242 TaxonNameBase taxonName = ParseHandler.parseReferencedName(
243 taxonNameString, null);
244 setTaxon(taxonName);
245 }
246
247 private void setTaxon(TaxonNameBase taxonName) {
248 Reference secundum = null;
249 if (getParentTreeNode() != null) {
250 if (getParentTreeNode() instanceof Classification) {
251 secundum = ((Classification) getParentTreeNode())
252 .getReference();
253 } else if (getParentTreeNode() instanceof TaxonNode) {
254 secundum = ((TaxonNode) getParentTreeNode()).getTaxon()
255 .getSec();
256 }
257 }
258 taxon = Taxon.NewInstance(taxonName, secundum);
259 }
260
261 /**
262 * @param openInEditor
263 * the openInEditor to set
264 */
265 private void setOpenInEditor(boolean openInEditor) {
266 this.openInEditor = openInEditor;
267 }
268
269 /**
270 * <p>
271 * isComplete
272 * </p>
273 *
274 * @return the complete
275 */
276 public boolean isComplete() {
277 return complete;
278 }
279
280 }