3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.ui
.section
.classification
;
13 import org
.apache
.commons
.lang
.StringUtils
;
14 import org
.eclipse
.swt
.widgets
.Text
;
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
.Rank
;
20 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
21 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
22 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
23 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
24 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
25 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
26 import eu
.etaxonomy
.taxeditor
.parser
.ParseHandler
;
27 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
28 import eu
.etaxonomy
.taxeditor
.ui
.element
.CheckboxElement
;
29 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.TextWithLabelElement
;
31 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractCdmDetailElement
;
32 import eu
.etaxonomy
.taxeditor
.ui
.selection
.EntitySelectionElement
;
33 import eu
.etaxonomy
.taxeditor
.ui
.selection
.TaxonNodeSelectionElement
;
37 * TaxonNodeDetailElement class.
41 * @created Sep 27, 2010
44 public class TaxonNodeDetailElement
extends AbstractCdmDetailElement
<ITaxonTreeNode
> {
46 private EntitySelectionElement
<Classification
> selection_classification
;
48 private Classification classification
;
50 private TaxonNodeSelectionElement selection_parentTaxonNode
;
52 private ITaxonTreeNode parentTreeNode
;
54 private EntitySelectionElement
<Taxon
> selection_reuseExistingTaxon
;
58 private TextWithLabelElement text_newTaxonName
;
60 private CheckboxElement checkbox_openInEditor
;
62 private boolean openInEditor
;
64 private boolean complete
;
66 private EntitySelectionElement
<TaxonNameBase
> selection_reuseExistingName
;
70 * Constructor for TaxonNodeDetailElement.
74 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
77 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
80 public TaxonNodeDetailElement(CdmFormFactory formFactory
,
81 ICdmFormElement formElement
) {
82 super(formFactory
, formElement
);
89 * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#createControls
90 * (eu.etaxonomy.taxeditor.forms.ICdmFormElement,
91 * eu.etaxonomy.cdm.model.common.IAnnotatableEntity, int)
95 protected void createControls(ICdmFormElement formElement
,
96 ITaxonTreeNode entity
, int style
) {
97 selection_classification
= formFactory
98 .createSelectionElement(Classification
.class,
99 getConversationHolder(), formElement
, "Classification",
100 null, EntitySelectionElement
.DELETABLE
, style
);
101 selection_parentTaxonNode
= formFactory
102 .createTaxonNodeSelectionElement(getConversationHolder(), formElement
, "Parent", null,
103 EntitySelectionElement
.DELETABLE
, style
);
104 selection_reuseExistingTaxon
= formFactory
105 .createSelectionElement(Taxon
.class,
106 getConversationHolder(), formElement
,
107 "Reuse existing taxon", null,
108 EntitySelectionElement
.DELETABLE
, style
);
110 selection_reuseExistingName
= formFactory
111 .createSelectionElement(TaxonNameBase
.class,
112 getConversationHolder(), formElement
,
113 "Reuse existing name", null,
114 EntitySelectionElement
.DELETABLE
, style
);
116 text_newTaxonName
= formFactory
.createTextWithLabelElement(formElement
,
117 "New Taxon", "", style
);
118 text_newTaxonName
.setFocus();
119 preFillParentTaxonName();
121 checkbox_openInEditor
= formFactory
.createCheckbox(formElement
,
122 "Open in Editor", true, style
);
123 setOpenInEditor(true);
124 setParentTreeNode(entity
);
127 private void preFillParentTaxonName() {
128 if(getEntity() instanceof TaxonNode
){
129 TaxonNode node
= (TaxonNode
)getEntity();
130 if(node
.getTaxon()!=null){
131 Taxon taxon
= HibernateProxyHelper
.deproxy(node
.getTaxon(), Taxon
.class);
132 if(taxon
.getName()!=null && taxon
.getName().isInstanceOf(NonViralName
.class)){
133 NonViralName
<?
> name
= HibernateProxyHelper
.deproxy(node
.getTaxon().getName(), NonViralName
.class);
134 if( ! name
.isSupraGeneric() && name
.getRank() != null){
135 String taxonName
= "";
136 if(name
.isGenus() || name
.isInfraGeneric()|| name
.isSpeciesAggregate() ){
137 taxonName
= name
.getGenusOrUninomial();
139 else if(name
.isSpecies() || name
.isInfraSpecific() ){
140 taxonName
= CdmUtils
.concat(" ", name
.getGenusOrUninomial(),name
.getSpecificEpithet());
142 if (StringUtils
.isNotBlank(taxonName
)){
143 text_newTaxonName
.setText(taxonName
+ " ");
144 if(text_newTaxonName
.getMainControl() instanceof Text
){
145 Text text
= (Text
)text_newTaxonName
.getMainControl();
146 text
.setSelection(text_newTaxonName
.getText().length());
159 * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleEvent(java
164 public void handleEvent(Object eventSource
) {
165 if (eventSource
== selection_classification
) {
166 setClassification(selection_classification
.getEntity());
167 } else if (eventSource
== selection_parentTaxonNode
) {
168 setParentTreeNode(selection_parentTaxonNode
.getEntity());
169 } else if (eventSource
== selection_reuseExistingTaxon
) {
170 boolean enabled
= selection_reuseExistingTaxon
.getEntity() == null;
171 selection_reuseExistingName
.setEnabled(enabled
);
172 text_newTaxonName
.setEnabled(enabled
);
174 setTaxon(selection_reuseExistingTaxon
.getEntity());
175 } else if (eventSource
== selection_reuseExistingName
) {
176 boolean enabled
= selection_reuseExistingName
.getEntity() == null;
177 selection_reuseExistingTaxon
.setEnabled(enabled
);
178 text_newTaxonName
.setEnabled(enabled
);
180 setTaxon(selection_reuseExistingName
.getEntity());
181 } else if (eventSource
== text_newTaxonName
) {
182 boolean enabled
= CdmUtils
.isEmpty(text_newTaxonName
.getText());
183 selection_reuseExistingTaxon
.setEnabled(enabled
);
184 selection_reuseExistingName
.setEnabled(enabled
);
186 setTaxon(text_newTaxonName
.getText());
187 } else if (eventSource
== checkbox_openInEditor
) {
188 setOpenInEditor(checkbox_openInEditor
.getSelection());
194 * Getter for the field <code>classification</code>.
197 * @return the classification
199 public Classification
getClassification() {
200 return classification
;
208 * @return the openInEditor
210 public boolean isOpenInEditor() {
216 * Getter for the field <code>parentTreeNode</code>.
219 * @return a {@link eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode} object.
221 public ITaxonTreeNode
getParentTreeNode() {
222 return parentTreeNode
;
227 * Getter for the field <code>taxon</code>.
232 public Taxon
getTaxon() {
237 * @param classification
238 * the classification to set
240 private void setClassification(Classification classification
) {
241 this.classification
= classification
;
242 setParentTreeNode(classification
);
246 * @param parentTreeNode
247 * the parentTreeNode to set
249 private void setParentTreeNode(ITaxonTreeNode parentTreeNode
) {
250 this.parentTreeNode
= parentTreeNode
;
252 if (parentTreeNode
instanceof Classification
) {
253 classification
= (Classification
) parentTreeNode
;
254 selection_classification
.setEntity(classification
);
255 selection_parentTaxonNode
.setEntity(null);
256 selection_parentTaxonNode
.setClassification(classification
);
257 } else if (parentTreeNode
instanceof TaxonNode
) {
258 classification
= (Classification
) HibernateProxyHelper
259 .deproxy(((TaxonNode
) parentTreeNode
).getClassification());
260 selection_classification
.setEntity(classification
);
261 selection_parentTaxonNode
.setEntity((TaxonNode
) parentTreeNode
);
262 selection_parentTaxonNode
.setClassification(classification
);
263 } else if(parentTreeNode
== null){
264 this.parentTreeNode
= selection_classification
.getEntity();
269 * @param reuseExistingTaxon
270 * the reuseExistingTaxon to set
272 private void setTaxon(Taxon taxon
) {
276 private void setTaxon(String taxonNameString
) {
277 TaxonNameBase taxonName
= ParseHandler
.parseReferencedName(
278 taxonNameString
, null);
282 private void setTaxon(TaxonNameBase taxonName
) {
283 Reference secundum
= null;
284 if (getParentTreeNode() != null) {
285 if (getParentTreeNode() instanceof Classification
) {
286 secundum
= ((Classification
) getParentTreeNode())
288 } else if (getParentTreeNode() instanceof TaxonNode
) {
289 secundum
= ((TaxonNode
) getParentTreeNode()).getTaxon()
293 taxon
= Taxon
.NewInstance(taxonName
, secundum
);
297 * @param openInEditor
298 * the openInEditor to set
300 private void setOpenInEditor(boolean openInEditor
) {
301 this.openInEditor
= openInEditor
;
309 * @return the complete
311 public boolean isComplete() {