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
.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
;
36 * TaxonNodeDetailElement class.
40 * @created Sep 27, 2010
43 public class TaxonNodeDetailElement
extends AbstractCdmDetailElement
<ITaxonTreeNode
> {
45 private EntitySelectionElement
<Classification
> selection_classification
;
47 private Classification classification
;
49 private TaxonNodeSelectionElement selection_parentTaxonNode
;
51 private ITaxonTreeNode parentTreeNode
;
53 private EntitySelectionElement
<Taxon
> selection_reuseExistingTaxon
;
57 private TextWithLabelElement textNewTaxonName
;
59 private CheckboxElement checkbox_openInEditor
;
61 private boolean openInEditor
;
63 private boolean complete
;
65 private EntitySelectionElement
<TaxonNameBase
> selection_reuseExistingName
;
69 * Constructor for TaxonNodeDetailElement.
73 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
76 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
79 public TaxonNodeDetailElement(CdmFormFactory formFactory
,
80 ICdmFormElement formElement
) {
81 super(formFactory
, formElement
);
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
);
101 selection_reuseExistingName
= formFactory
102 .createSelectionElement(TaxonNameBase
.class,
103 getConversationHolder(), formElement
,
104 "Reuse existing name", null,
105 EntitySelectionElement
.DELETABLE
, style
);
107 textNewTaxonName
= formFactory
.createTextWithLabelElement(formElement
,
108 "New Taxon", "", style
);
109 textNewTaxonName
.setFocus();
110 preFillParentTaxonName();
112 checkbox_openInEditor
= formFactory
.createCheckbox(formElement
,
113 "Open in Editor", true, style
);
114 setOpenInEditor(true);
115 setParentTreeNode(entity
);
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();
130 else if(name
.isSpecies() || name
.isInfraSpecific() ){
131 taxonName
= CdmUtils
.concat(" ", name
.getGenusOrUninomial(),name
.getSpecificEpithet());
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());
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
);
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
);
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
);
170 setTaxon(textNewTaxonName
.getText());
171 complete
= !textNewTaxonName
.getText().isEmpty();
172 } else if (eventSource
== checkbox_openInEditor
) {
173 setOpenInEditor(checkbox_openInEditor
.getSelection());
179 * Getter for the field <code>classification</code>.
182 * @return the classification
184 public Classification
getClassification() {
185 return classification
;
188 public boolean isOpenInEditor() {
194 * Getter for the field <code>parentTreeNode</code>.
197 * @return a {@link eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode} object.
199 public ITaxonTreeNode
getParentTreeNode() {
200 return parentTreeNode
;
205 * Getter for the field <code>taxon</code>.
210 public Taxon
getTaxon() {
214 private void setClassification(Classification classification
) {
215 this.classification
= classification
;
216 setParentTreeNode(classification
);
219 private void setParentTreeNode(ITaxonTreeNode parentTreeNode
) {
220 this.parentTreeNode
= parentTreeNode
;
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();
238 private void setTaxon(Taxon taxon
) {
242 private void setTaxon(String taxonNameString
) {
243 TaxonNameBase taxonName
= ParseHandler
.parseReferencedName(
244 taxonNameString
, null);
248 private void setTaxon(TaxonNameBase taxonName
) {
249 Reference secundum
= null;
250 if (getParentTreeNode() != null) {
251 if (getParentTreeNode() instanceof Classification
) {
252 secundum
= ((Classification
) getParentTreeNode())
254 } else if (getParentTreeNode() instanceof TaxonNode
) {
255 secundum
= ((TaxonNode
) getParentTreeNode()).getTaxon()
259 taxon
= Taxon
.NewInstance(taxonName
, secundum
);
262 private void setOpenInEditor(boolean openInEditor
) {
263 this.openInEditor
= openInEditor
;
266 public TextWithLabelElement
getTextNewTaxonName() {
267 return textNewTaxonName
;
270 public boolean isComplete() {