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
;
37 * @created Sep 27, 2010
40 public class TaxonNodeDetailElement
extends AbstractCdmDetailElement
<ITaxonTreeNode
> {
42 private EntitySelectionElement
<Classification
> selection_classification
;
44 private Classification classification
;
46 private TaxonNodeSelectionElement selection_parentTaxonNode
;
48 private ITaxonTreeNode parentTreeNode
;
50 private EntitySelectionElement
<Taxon
> selection_reuseExistingTaxon
;
51 private EntitySelectionElement
<Reference
> selection_SecRef
;
55 private Reference secReference
;
57 private TextWithLabelElement textNewTaxonName
;
59 private CheckboxElement checkbox_openInEditor
;
61 private boolean openInEditor
;
63 private boolean complete
;
65 private EntitySelectionElement
<TaxonNameBase
> selection_reuseExistingName
;
67 public TaxonNodeDetailElement(CdmFormFactory formFactory
,
68 ICdmFormElement formElement
) {
69 super(formFactory
, formElement
);
74 protected void createControls(ICdmFormElement formElement
,
75 ITaxonTreeNode entity
, int style
) {
76 selection_classification
= formFactory
77 .createSelectionElement(Classification
.class,
78 getConversationHolder(), formElement
, "Classification",
79 null, EntitySelectionElement
.DELETABLE
, style
);
80 selection_parentTaxonNode
= formFactory
81 .createTaxonNodeSelectionElement(getConversationHolder(), formElement
, "Parent", null,
82 EntitySelectionElement
.DELETABLE
, style
);
84 selection_reuseExistingTaxon
= formFactory
85 .createSelectionElement(Taxon
.class,
86 getConversationHolder(), formElement
,
87 "Reuse existing taxon", null,
88 EntitySelectionElement
.DELETABLE
, style
);
90 selection_reuseExistingName
= formFactory
91 .createSelectionElement(TaxonNameBase
.class,
92 getConversationHolder(), formElement
,
93 "Reuse existing name", null,
94 EntitySelectionElement
.DELETABLE
, style
);
95 selection_SecRef
= formFactory
96 .createSelectionElement(Reference
.class,getConversationHolder(), formElement
, "Secundum Reference", null,
97 EntitySelectionElement
.DELETABLE
, style
);
98 textNewTaxonName
= formFactory
.createTextWithLabelElement(formElement
,
99 "New Taxon", "", style
);
100 textNewTaxonName
.setFocus();
101 preFillParentTaxonName();
103 checkbox_openInEditor
= formFactory
.createCheckbox(formElement
,
104 "Open in Editor", true, style
);
105 setOpenInEditor(true);
106 setParentTreeNode(entity
);
109 private void preFillParentTaxonName() {
110 if(getEntity() instanceof TaxonNode
){
111 TaxonNode node
= (TaxonNode
)getEntity();
112 if(node
.getTaxon()!=null){
113 Taxon taxon
= HibernateProxyHelper
.deproxy(node
.getTaxon(), Taxon
.class);
114 if(taxon
.getName()!=null && taxon
.getName().isInstanceOf(NonViralName
.class)){
115 NonViralName
<?
> name
= HibernateProxyHelper
.deproxy(node
.getTaxon().getName(), NonViralName
.class);
116 if( ! name
.isSupraGeneric() && name
.getRank() != null){
117 String taxonName
= "";
118 if(name
.isGenus() || name
.isInfraGeneric()|| name
.isSpeciesAggregate() ){
119 taxonName
= name
.getGenusOrUninomial();
121 else if(name
.isSpecies() || name
.isInfraSpecific() ){
122 taxonName
= CdmUtils
.concat(" ", name
.getGenusOrUninomial(),name
.getSpecificEpithet());
124 if (StringUtils
.isNotBlank(taxonName
)){
125 textNewTaxonName
.setText(taxonName
+ " ");
126 if(textNewTaxonName
.getMainControl() instanceof Text
){
127 Text text
= (Text
)textNewTaxonName
.getMainControl();
128 text
.setSelection(textNewTaxonName
.getText().length());
140 public void handleEvent(Object eventSource
) {
141 if (eventSource
== selection_classification
) {
142 setClassification(selection_classification
.getEntity());
143 } else if (eventSource
== selection_parentTaxonNode
) {
144 setParentTreeNode(selection_parentTaxonNode
.getEntity());
145 } else if (eventSource
== selection_SecRef
) {
146 setSecReference(selection_SecRef
.getEntity());
147 } else if (eventSource
== selection_reuseExistingTaxon
) {
148 boolean enabled
= selection_reuseExistingTaxon
.getEntity() == null;
149 selection_reuseExistingName
.setEnabled(enabled
);
150 textNewTaxonName
.setEnabled(enabled
);
152 setTaxon(selection_reuseExistingTaxon
.getEntity());
154 textNewTaxonName
.setText(null);
156 complete
= !textNewTaxonName
.getText().isEmpty();
159 } else if (eventSource
== selection_reuseExistingName
) {
160 boolean enabled
= selection_reuseExistingName
.getEntity() == null;
161 setTaxon(selection_reuseExistingName
.getEntity());
162 selection_reuseExistingTaxon
.setEnabled(enabled
);
163 textNewTaxonName
.setEnabled(enabled
);
165 setTaxon(selection_reuseExistingName
.getEntity());
167 complete
= !textNewTaxonName
.getText().isEmpty();
168 } else if (eventSource
== textNewTaxonName
) {
169 boolean enabled
= CdmUtils
.isBlank(textNewTaxonName
.getText());
170 selection_reuseExistingTaxon
.setEnabled(enabled
);
171 selection_reuseExistingName
.setEnabled(enabled
);
173 setTaxon(textNewTaxonName
.getText());
174 complete
= !textNewTaxonName
.getText().isEmpty();
175 } else if (eventSource
== checkbox_openInEditor
) {
176 setOpenInEditor(checkbox_openInEditor
.getSelection());
180 public Classification
getClassification() {
181 return classification
;
184 public boolean isOpenInEditor() {
188 public ITaxonTreeNode
getParentTreeNode() {
189 return parentTreeNode
;
192 public Taxon
getTaxon() {
196 private void setClassification(Classification classification
) {
197 this.classification
= classification
;
198 setParentTreeNode(classification
);
201 private void setParentTreeNode(ITaxonTreeNode parentTreeNode
) {
202 this.parentTreeNode
= parentTreeNode
;
204 if (parentTreeNode
instanceof Classification
) {
205 classification
= (Classification
) parentTreeNode
;
206 selection_classification
.setEntity(classification
);
207 selection_parentTaxonNode
.setEntity(null);
208 selection_parentTaxonNode
.setClassification(classification
);
209 selection_SecRef
.setEntity(classification
.getReference());
210 } else if (parentTreeNode
instanceof TaxonNode
) {
211 classification
= (Classification
) HibernateProxyHelper
212 .deproxy(((TaxonNode
) parentTreeNode
).getClassification());
213 selection_classification
.setEntity(classification
);
214 selection_parentTaxonNode
.setEntity((TaxonNode
) parentTreeNode
);
215 selection_parentTaxonNode
.setClassification(classification
);
216 selection_SecRef
.setEntity(((TaxonNode
)parentTreeNode
).getTaxon().getSec());
217 } else if(parentTreeNode
== null){
218 this.parentTreeNode
= selection_classification
.getEntity();
222 private void setSecReference(Reference secRef
){
223 this.secReference
= secRef
;
224 setTaxon(textNewTaxonName
.getText());
227 private void setTaxon(Taxon taxon
) {
229 textNewTaxonName
.setText(taxon
.getName().getTitleCache());
230 selection_SecRef
.setEntity(taxon
.getSec());
233 private void setTaxon(String taxonNameString
) {
234 TaxonNameBase taxonName
= ParseHandler
.parseReferencedName(
235 taxonNameString
, null);
239 private void setTaxon(TaxonNameBase taxonName
) {
240 Reference secundum
= null;
241 if (getParentTreeNode() != null) {
242 if (this.selection_SecRef
.getEntity() != null){
243 secundum
= this.selection_SecRef
.getEntity();
244 } /*else if (getParentTreeNode() instanceof Classification) {
245 secundum = ((Classification) getParentTreeNode())
247 } else if (getParentTreeNode() instanceof TaxonNode) {
248 secundum = ((TaxonNode) getParentTreeNode()).getTaxon()
252 if (taxonName
!= null){
253 taxon
= Taxon
.NewInstance(taxonName
, secundum
);
254 if (!taxonName
.getTitleCache().trim().equals(textNewTaxonName
.getText().trim())){
255 textNewTaxonName
.setText(taxonName
.getTitleCache());
258 textNewTaxonName
.setText(null);
262 private void setOpenInEditor(boolean openInEditor
) {
263 this.openInEditor
= openInEditor
;
266 public TextWithLabelElement
getTextNewTaxonName() {
267 return textNewTaxonName
;
270 public boolean isComplete() {