merge-update from trunk
[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
55 private Taxon taxon;
56
57 private TextWithLabelElement textNewTaxonName;
58
59 private CheckboxElement checkbox_openInEditor;
60
61 private boolean openInEditor;
62
63 private boolean complete;
64
65 private EntitySelectionElement<TaxonNameBase> selection_reuseExistingName;
66
67 /**
68 * <p>
69 * Constructor for TaxonNodeDetailElement.
70 * </p>
71 *
72 * @param formFactory
73 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
74 * object.
75 * @param formElement
76 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
77 * object.
78 */
79 public TaxonNodeDetailElement(CdmFormFactory formFactory,
80 ICdmFormElement formElement) {
81 super(formFactory, formElement);
82 }
83
84 /** {@inheritDoc} */
85 @Override
86 protected void createControls(ICdmFormElement formElement,
87 ITaxonTreeNode entity, int style) {
88 selection_classification = formFactory
89 .createSelectionElement(Classification.class,
90 getConversationHolder(), formElement, "Classification",
91 null, EntitySelectionElement.DELETABLE, style);
92 selection_parentTaxonNode = formFactory
93 .createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", null,
94 EntitySelectionElement.DELETABLE, style);
95 selection_reuseExistingTaxon = formFactory
96 .createSelectionElement(Taxon.class,
97 getConversationHolder(), formElement,
98 "Reuse existing taxon", null,
99 EntitySelectionElement.DELETABLE, style);
100
101 selection_reuseExistingName = formFactory
102 .createSelectionElement(TaxonNameBase.class,
103 getConversationHolder(), formElement,
104 "Reuse existing name", null,
105 EntitySelectionElement.DELETABLE, style);
106
107 textNewTaxonName = formFactory.createTextWithLabelElement(formElement,
108 "New Taxon", "", style);
109 textNewTaxonName.setFocus();
110 preFillParentTaxonName();
111
112 checkbox_openInEditor = formFactory.createCheckbox(formElement,
113 "Open in Editor", true, style);
114 setOpenInEditor(true);
115 setParentTreeNode(entity);
116 }
117
118 private void preFillParentTaxonName() {
119 if(getEntity() instanceof TaxonNode){
120 TaxonNode node = (TaxonNode)getEntity();
121 if(node.getTaxon()!=null){
122 Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
123 if(taxon.getName()!=null && taxon.getName().isInstanceOf(NonViralName.class)){
124 NonViralName<?> name = HibernateProxyHelper.deproxy(node.getTaxon().getName(), NonViralName.class);
125 if( ! name.isSupraGeneric() && name.getRank() != null){
126 String taxonName = "";
127 if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
128 taxonName = name.getGenusOrUninomial();
129 }
130 else if(name.isSpecies() || name.isInfraSpecific() ){
131 taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
132 }
133 if (StringUtils.isNotBlank(taxonName)){
134 textNewTaxonName.setText(taxonName + " ");
135 if(textNewTaxonName.getMainControl() instanceof Text){
136 Text text = (Text)textNewTaxonName.getMainControl();
137 text.setSelection(textNewTaxonName.getText().length());
138 }
139 }
140 }
141 }
142 }
143 }
144 }
145
146 /** {@inheritDoc} */
147 @Override
148 public void handleEvent(Object eventSource) {
149 if (eventSource == selection_classification) {
150 setClassification(selection_classification.getEntity());
151 } else if (eventSource == selection_parentTaxonNode) {
152 setParentTreeNode(selection_parentTaxonNode.getEntity());
153 } else if (eventSource == selection_reuseExistingTaxon) {
154 boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
155 selection_reuseExistingName.setEnabled(enabled);
156 textNewTaxonName.setEnabled(enabled);
157
158 setTaxon(selection_reuseExistingTaxon.getEntity());
159 } else if (eventSource == selection_reuseExistingName) {
160 boolean enabled = selection_reuseExistingName.getEntity() == null;
161 selection_reuseExistingTaxon.setEnabled(enabled);
162 textNewTaxonName.setEnabled(enabled);
163
164 setTaxon(selection_reuseExistingName.getEntity());
165 } else if (eventSource == textNewTaxonName) {
166 boolean enabled = CdmUtils.isEmpty(textNewTaxonName.getText());
167 selection_reuseExistingTaxon.setEnabled(enabled);
168 selection_reuseExistingName.setEnabled(enabled);
169
170 setTaxon(textNewTaxonName.getText());
171 complete = !textNewTaxonName.getText().isEmpty();
172 } else if (eventSource == checkbox_openInEditor) {
173 setOpenInEditor(checkbox_openInEditor.getSelection());
174 }
175 }
176
177 /**
178 * <p>
179 * Getter for the field <code>classification</code>.
180 * </p>
181 *
182 * @return the classification
183 */
184 public Classification getClassification() {
185 return classification;
186 }
187
188 public boolean isOpenInEditor() {
189 return openInEditor;
190 }
191
192 /**
193 * <p>
194 * Getter for the field <code>parentTreeNode</code>.
195 * </p>
196 *
197 * @return a {@link eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode} object.
198 */
199 public ITaxonTreeNode getParentTreeNode() {
200 return parentTreeNode;
201 }
202
203 /**
204 * <p>
205 * Getter for the field <code>taxon</code>.
206 * </p>
207 *
208 * @return the taxon
209 */
210 public Taxon getTaxon() {
211 return taxon;
212 }
213
214 private void setClassification(Classification classification) {
215 this.classification = classification;
216 setParentTreeNode(classification);
217 }
218
219 private void setParentTreeNode(ITaxonTreeNode parentTreeNode) {
220 this.parentTreeNode = parentTreeNode;
221
222 if (parentTreeNode instanceof Classification) {
223 classification = (Classification) parentTreeNode;
224 selection_classification.setEntity(classification);
225 selection_parentTaxonNode.setEntity(null);
226 selection_parentTaxonNode.setClassification(classification);
227 } else if (parentTreeNode instanceof TaxonNode) {
228 classification = (Classification) HibernateProxyHelper
229 .deproxy(((TaxonNode) parentTreeNode).getClassification());
230 selection_classification.setEntity(classification);
231 selection_parentTaxonNode.setEntity((TaxonNode) parentTreeNode);
232 selection_parentTaxonNode.setClassification(classification);
233 } else if(parentTreeNode == null){
234 this.parentTreeNode = selection_classification.getEntity();
235 }
236 }
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 private void setOpenInEditor(boolean openInEditor) {
263 this.openInEditor = openInEditor;
264 }
265
266 public TextWithLabelElement getTextNewTaxonName() {
267 return textNewTaxonName;
268 }
269
270 public boolean isComplete() {
271 return complete;
272 }
273
274 }