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