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
;
54 private EntitySelectionElement
<Reference
> selection_SecRef
;
58 private Reference secReference
;
60 private TextWithLabelElement textNewTaxonName
;
62 private CheckboxElement checkbox_openInEditor
;
64 private boolean openInEditor
;
66 private boolean complete
;
68 private EntitySelectionElement
<TaxonNameBase
> selection_reuseExistingName
;
72 * Constructor for TaxonNodeDetailElement.
76 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
79 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
82 public TaxonNodeDetailElement(CdmFormFactory formFactory
,
83 ICdmFormElement formElement
) {
84 super(formFactory
, formElement
);
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 selection_SecRef
= formFactory
99 .createSelectionElement(Reference
.class,getConversationHolder(), formElement
, "Secundum Reference", null,
100 EntitySelectionElement
.DELETABLE
, style
);
101 selection_reuseExistingTaxon
= formFactory
102 .createSelectionElement(Taxon
.class,
103 getConversationHolder(), formElement
,
104 "Reuse existing taxon", null,
105 EntitySelectionElement
.DELETABLE
, style
);
107 selection_reuseExistingName
= formFactory
108 .createSelectionElement(TaxonNameBase
.class,
109 getConversationHolder(), formElement
,
110 "Reuse existing name", null,
111 EntitySelectionElement
.DELETABLE
, style
);
113 textNewTaxonName
= formFactory
.createTextWithLabelElement(formElement
,
114 "New Taxon", "", style
);
115 textNewTaxonName
.setFocus();
116 preFillParentTaxonName();
118 checkbox_openInEditor
= formFactory
.createCheckbox(formElement
,
119 "Open in Editor", true, style
);
120 setOpenInEditor(true);
121 setParentTreeNode(entity
);
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();
136 else if(name
.isSpecies() || name
.isInfraSpecific() ){
137 taxonName
= CdmUtils
.concat(" ", name
.getGenusOrUninomial(),name
.getSpecificEpithet());
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());
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
);
166 setTaxon(selection_reuseExistingTaxon
.getEntity());
168 complete
= !textNewTaxonName
.getText().isEmpty();
171 } else if (eventSource
== selection_reuseExistingName
) {
172 boolean enabled
= selection_reuseExistingName
.getEntity() == null;
173 selection_reuseExistingTaxon
.setEnabled(enabled
);
174 textNewTaxonName
.setEnabled(enabled
);
176 setTaxon(selection_reuseExistingName
.getEntity());
177 } else if (eventSource
== textNewTaxonName
) {
178 boolean enabled
= CdmUtils
.isEmpty(textNewTaxonName
.getText());
179 selection_reuseExistingTaxon
.setEnabled(enabled
);
180 selection_reuseExistingName
.setEnabled(enabled
);
182 setTaxon(textNewTaxonName
.getText());
183 complete
= !textNewTaxonName
.getText().isEmpty();
184 } else if (eventSource
== checkbox_openInEditor
) {
185 setOpenInEditor(checkbox_openInEditor
.getSelection());
191 * Getter for the field <code>classification</code>.
194 * @return the classification
196 public Classification
getClassification() {
197 return classification
;
200 public boolean isOpenInEditor() {
206 * Getter for the field <code>parentTreeNode</code>.
209 * @return a {@link eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode} object.
211 public ITaxonTreeNode
getParentTreeNode() {
212 return parentTreeNode
;
217 * Getter for the field <code>taxon</code>.
222 public Taxon
getTaxon() {
226 private void setClassification(Classification classification
) {
227 this.classification
= classification
;
228 setParentTreeNode(classification
);
231 private void setParentTreeNode(ITaxonTreeNode parentTreeNode
) {
232 this.parentTreeNode
= parentTreeNode
;
234 if (parentTreeNode
instanceof Classification
) {
235 classification
= (Classification
) parentTreeNode
;
236 selection_classification
.setEntity(classification
);
237 selection_parentTaxonNode
.setEntity(null);
238 selection_parentTaxonNode
.setClassification(classification
);
239 } else if (parentTreeNode
instanceof TaxonNode
) {
240 classification
= (Classification
) HibernateProxyHelper
241 .deproxy(((TaxonNode
) parentTreeNode
).getClassification());
242 selection_classification
.setEntity(classification
);
243 selection_parentTaxonNode
.setEntity((TaxonNode
) parentTreeNode
);
244 selection_parentTaxonNode
.setClassification(classification
);
245 } else if(parentTreeNode
== null){
246 this.parentTreeNode
= selection_classification
.getEntity();
250 private void setSecReference(Reference secRef
){
251 this.secReference
= secRef
;
252 setTaxon(textNewTaxonName
.getText());
255 private void setTaxon(Taxon taxon
) {
257 textNewTaxonName
.setText(taxon
.getName().getTitleCache());
261 private void setTaxon(String taxonNameString
) {
262 TaxonNameBase taxonName
= ParseHandler
.parseReferencedName(
263 taxonNameString
, null);
267 private void setTaxon(TaxonNameBase taxonName
) {
268 Reference secundum
= null;
269 if (getParentTreeNode() != null) {
270 if (this.secReference
!= null){
271 secundum
= this.secReference
;
272 } else if (getParentTreeNode() instanceof Classification
) {
273 secundum
= ((Classification
) getParentTreeNode())
275 } else if (getParentTreeNode() instanceof TaxonNode
) {
276 secundum
= ((TaxonNode
) getParentTreeNode()).getTaxon()
280 taxon
= Taxon
.NewInstance(taxonName
, secundum
);
283 private void setOpenInEditor(boolean openInEditor
) {
284 this.openInEditor
= openInEditor
;
287 public TextWithLabelElement
getTextNewTaxonName() {
288 return textNewTaxonName
;
291 public boolean isComplete() {