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