fix the merge conflicts of hotfix branch
[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 /** {@inheritDoc} */
153 @Override
154 public void handleEvent(Object eventSource) {
155 if (eventSource == selection_classification) {
156 setClassification(selection_classification.getEntity());
157 } else if (eventSource == selection_parentTaxonNode) {
158 setParentTreeNode(selection_parentTaxonNode.getEntity());
159 } else if (eventSource == selection_SecRef) {
160 setSecReference(selection_SecRef.getEntity());
161 } else if (eventSource == selection_reuseExistingTaxon) {
162 boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
163 selection_reuseExistingName.setEnabled(enabled);
164 textNewTaxonName.setEnabled(enabled);
165 if (!enabled){
166 setTaxon(selection_reuseExistingTaxon.getEntity());
167 } else{
168 textNewTaxonName.setText(null);
169 }
170 complete = !textNewTaxonName.getText().isEmpty();
171
172
173 } else if (eventSource == selection_reuseExistingName) {
174 boolean enabled = selection_reuseExistingName.getEntity() == null;
175 setTaxon(selection_reuseExistingName.getEntity());
176 selection_reuseExistingTaxon.setEnabled(enabled);
177 textNewTaxonName.setEnabled(enabled);
178
179 setTaxon(selection_reuseExistingName.getEntity());
180
181 complete = !textNewTaxonName.getText().isEmpty();
182 } else if (eventSource == textNewTaxonName) {
183 boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
184 selection_reuseExistingTaxon.setEnabled(enabled);
185 selection_reuseExistingName.setEnabled(enabled);
186
187 setTaxon(textNewTaxonName.getText());
188 complete = !textNewTaxonName.getText().isEmpty();
189 } else if (eventSource == checkbox_openInEditor) {
190 setOpenInEditor(checkbox_openInEditor.getSelection());
191 }
192 }
193
194 /**
195 * <p>
196 * Getter for the field <code>classification</code>.
197 * </p>
198 *
199 * @return the classification
200 */
201 public Classification getClassification() {
202 return classification;
203 }
204
205 public boolean isOpenInEditor() {
206 return openInEditor;
207 }
208
209 /**
210 * <p>
211 * Getter for the field <code>parentTreeNode</code>.
212 * </p>
213 *
214 * @return a {@link eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode} object.
215 */
216 public ITaxonTreeNode getParentTreeNode() {
217 return parentTreeNode;
218 }
219
220 /**
221 * <p>
222 * Getter for the field <code>taxon</code>.
223 * </p>
224 *
225 * @return the taxon
226 */
227 public Taxon getTaxon() {
228 return taxon;
229 }
230
231 private void setClassification(Classification classification) {
232 this.classification = classification;
233 setParentTreeNode(classification);
234 }
235
236 private void setParentTreeNode(ITaxonTreeNode parentTreeNode) {
237 this.parentTreeNode = parentTreeNode;
238
239 if (parentTreeNode instanceof Classification) {
240 classification = (Classification) parentTreeNode;
241 selection_classification.setEntity(classification);
242 selection_parentTaxonNode.setEntity(null);
243 selection_parentTaxonNode.setClassification(classification);
244 selection_SecRef.setEntity(classification.getReference());
245 } else if (parentTreeNode instanceof TaxonNode) {
246 classification = (Classification) HibernateProxyHelper
247 .deproxy(((TaxonNode) parentTreeNode).getClassification());
248 selection_classification.setEntity(classification);
249 selection_parentTaxonNode.setEntity((TaxonNode) parentTreeNode);
250 selection_parentTaxonNode.setClassification(classification);
251 selection_SecRef.setEntity(((TaxonNode)parentTreeNode).getTaxon().getSec());
252 } else if(parentTreeNode == null){
253 this.parentTreeNode = selection_classification.getEntity();
254 }
255 }
256
257 private void setSecReference(Reference secRef){
258 this.secReference = secRef;
259 setTaxon(textNewTaxonName.getText());
260 }
261
262 private void setTaxon(Taxon taxon) {
263 this.taxon = taxon;
264 textNewTaxonName.setText(taxon.getName().getTitleCache());
265 selection_SecRef.setEntity(taxon.getSec());
266 }
267
268 private void setTaxon(String taxonNameString) {
269 TaxonNameBase taxonName = ParseHandler.parseReferencedName(
270 taxonNameString, null);
271 setTaxon(taxonName);
272 }
273
274 private void setTaxon(TaxonNameBase taxonName) {
275 Reference secundum = null;
276 if (getParentTreeNode() != null) {
277 if (this.selection_SecRef.getEntity() != null){
278 secundum = this.selection_SecRef.getEntity();
279 } /*else if (getParentTreeNode() instanceof Classification) {
280 secundum = ((Classification) getParentTreeNode())
281 .getReference();
282 } else if (getParentTreeNode() instanceof TaxonNode) {
283 secundum = ((TaxonNode) getParentTreeNode()).getTaxon()
284 .getSec();
285 }*/
286 }
287 if (taxonName != null){
288 taxon = Taxon.NewInstance(taxonName, secundum);
289 if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
290 textNewTaxonName.setText(taxonName.getTitleCache());
291 }
292 } else {
293 textNewTaxonName.setText(null);
294 }
295 }
296
297 private void setOpenInEditor(boolean openInEditor) {
298 this.openInEditor = openInEditor;
299 }
300
301 public TextWithLabelElement getTextNewTaxonName() {
302 return textNewTaxonName;
303 }
304
305 public boolean isComplete() {
306 return complete;
307 }
308
309 }