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
.Control
;
15 import org
.eclipse
.swt
.widgets
.Text
;
17 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
18 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
19 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
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
.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
.LayoutConstants
;
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
;
38 * @created Sep 27, 2010
41 public class TaxonNodeDetailElement
extends AbstractCdmDetailElement
<ITaxonTreeNode
> {
43 private boolean createNew
= false;
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 TextWithLabelElement textNewTaxonName
;
60 private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation
;
63 private CheckboxElement checkbox_openInEditor
;
65 private boolean openInEditor
;
67 private boolean complete
;
69 private EntitySelectionElement
<TaxonNameBase
> selection_reuseExistingName
;
71 public TaxonNodeDetailElement(CdmFormFactory formFactory
,
72 ICdmFormElement formElement
, boolean isCreateNew
) {
73 super(formFactory
, formElement
);
74 createNew
= isCreateNew
;
79 protected void createControls(ICdmFormElement formElement
,
80 ITaxonTreeNode entity
, int style
) {
81 selection_classification
= formFactory
82 .createSelectionElement(Classification
.class,
83 getConversationHolder(), formElement
, "Classification",
84 null, EntitySelectionElement
.DELETABLE
, style
);
85 selection_parentTaxonNode
= formFactory
86 .createTaxonNodeSelectionElement(getConversationHolder(), formElement
, "Parent", null,
87 EntitySelectionElement
.DELETABLE
, style
);
89 selection_reuseExistingTaxon
= formFactory
90 .createSelectionElement(Taxon
.class,
91 getConversationHolder(), formElement
,
92 "Reuse existing taxon", null,
93 EntitySelectionElement
.DELETABLE
, style
);
95 selection_reuseExistingName
= formFactory
96 .createSelectionElement(TaxonNameBase
.class,
97 getConversationHolder(), formElement
,
98 "Reuse existing name", null,
99 EntitySelectionElement
.DELETABLE
, style
);
100 selection_SecRef
= formFactory
101 .createSelectionElement(Reference
.class,getConversationHolder(), formElement
, "Secundum Reference", null,
102 EntitySelectionElement
.DELETABLE
, style
);
104 textNewTaxonName
= formFactory
.createTextWithLabelElement(formElement
,"New Taxon", "", style
);
105 textNewTaxonName
.setFocus();
107 textNewTaxonName
= formFactory
.createTextWithLabelElement(formElement
,"Taxon", "", style
);
113 setParentTreeNode(entity
);
120 selectionNodeAgentRelation
= formFactory
.createTaxonNodeAgentRelationCollectionSection(formElement
, getConversationHolder(), style
);
121 selectionNodeAgentRelation
.setEntity((TaxonNode
)entity
);
122 selectionNodeAgentRelation
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
125 checkbox_openInEditor
= formFactory
.createCheckbox(formElement
,
126 "Open in Editor", true, style
);
127 setOpenInEditor(true);
131 private void preFillTaxonName() {
132 if(getEntity() instanceof TaxonNode
){
133 TaxonNode node
= (TaxonNode
)getEntity();
134 if(node
.getTaxon()!=null){
135 Taxon taxon
= HibernateProxyHelper
.deproxy(node
.getTaxon(), Taxon
.class);
136 if(taxon
.getName()!=null && taxon
.getName().isInstanceOf(NonViralName
.class)){
137 NonViralName
<?
> name
= HibernateProxyHelper
.deproxy(node
.getTaxon().getName(), NonViralName
.class);
139 textNewTaxonName
.setText(name
.getNameCache());
140 } else if( ! name
.isSupraGeneric() && name
.getRank() != null){
141 String taxonName
= "";
142 if(name
.isGenus() || name
.isInfraGeneric()|| name
.isSpeciesAggregate() ){
143 taxonName
= name
.getGenusOrUninomial();
145 else if(name
.isSpecies() || name
.isInfraSpecific() ){
146 taxonName
= CdmUtils
.concat(" ", name
.getGenusOrUninomial(),name
.getSpecificEpithet());
148 if (StringUtils
.isNotBlank(taxonName
)){
149 textNewTaxonName
.setText(taxonName
+ " ");
150 if(textNewTaxonName
.getMainControl() instanceof Text
){
151 Text text
= (Text
)textNewTaxonName
.getMainControl();
152 text
.setSelection(textNewTaxonName
.getText().length());
166 public void handleEvent(Object eventSource
) {
167 if (eventSource
== selection_classification
) {
168 setClassification(selection_classification
.getEntity());
169 } else if (eventSource
== selection_parentTaxonNode
) {
170 setParentTreeNode(selection_parentTaxonNode
.getEntity());
171 } else if (eventSource
== selection_reuseExistingTaxon
) {
172 boolean enabled
= selection_reuseExistingTaxon
.getEntity() == null;
173 selection_reuseExistingName
.setEnabled(enabled
);
175 setTaxon(selection_reuseExistingTaxon
.getEntity());
178 textNewTaxonName
.setEnabled(enabled
);
180 setTaxon(selection_reuseExistingTaxon
.getEntity());
182 textNewTaxonName
.setText(null);
185 complete
= !textNewTaxonName
.getText().isEmpty();
188 } else if (eventSource
== selection_reuseExistingName
) {
189 boolean enabled
= selection_reuseExistingName
.getEntity() == null;
190 setTaxon(selection_reuseExistingName
.getEntity());
191 selection_reuseExistingTaxon
.setEnabled(enabled
);
192 textNewTaxonName
.setEnabled(enabled
);
194 setTaxon(selection_reuseExistingName
.getEntity());
196 complete
= !textNewTaxonName
.getText().isEmpty();
197 } else if (eventSource
== textNewTaxonName
) {
198 boolean enabled
= CdmUtils
.isBlank(textNewTaxonName
.getText());
199 selection_reuseExistingTaxon
.setEnabled(enabled
);
200 selection_reuseExistingName
.setEnabled(enabled
);
202 complete
= !textNewTaxonName
.getText().isEmpty();
203 } else if (eventSource
== checkbox_openInEditor
) {
204 setOpenInEditor(checkbox_openInEditor
.getSelection());
205 } else if (eventSource
== selectionNodeAgentRelation
){
206 boolean allComplete
= true;
207 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
208 allComplete
&=((TaxonNodeAgentRelationCollectionElement
)element
).isComplete();
211 complete
= !isCreateNew() && allComplete
;
215 public Classification
getClassification() {
216 return classification
;
219 public boolean isOpenInEditor() {
223 public ITaxonTreeNode
getParentTreeNode() {
224 return parentTreeNode
;
227 private void setClassification(Classification classification
) {
228 this.classification
= classification
;
229 setParentTreeNode(classification
);
232 private void setParentTreeNode(ITaxonTreeNode parentTreeNode
) {
233 this.parentTreeNode
= parentTreeNode
;
235 if (parentTreeNode
instanceof Classification
) {
236 classification
= (Classification
) parentTreeNode
;
237 selection_classification
.setEntity(classification
);
238 selection_parentTaxonNode
.setEntity(null);
239 selection_parentTaxonNode
.setClassification(classification
);
240 selection_SecRef
.setEntity(classification
.getReference());
241 } else if (parentTreeNode
instanceof TaxonNode
) {
242 classification
= HibernateProxyHelper
243 .deproxy(((TaxonNode
) parentTreeNode
).getClassification());
244 selection_classification
.setEntity(classification
);
245 selection_parentTaxonNode
.setEntity((TaxonNode
) parentTreeNode
);
246 selection_parentTaxonNode
.setClassification(classification
);
247 selection_SecRef
.setEntity(((TaxonNode
)parentTreeNode
).getTaxon().getSec());
248 } else if(parentTreeNode
== null){
249 this.parentTreeNode
= selection_classification
.getEntity();
253 private void setTreeNode(ITaxonTreeNode treeNode
) {
254 classification
= HibernateProxyHelper
255 .deproxy(((TaxonNode
) treeNode
).getClassification());
256 selection_classification
.setEntity(classification
);
257 selection_parentTaxonNode
.setEntity(((TaxonNode
) treeNode
).getParent());
258 selection_parentTaxonNode
.setClassification(((TaxonNode
) treeNode
).getParent().getClassification());
259 selection_SecRef
.setEntity(((TaxonNode
)treeNode
).getTaxon().getSec());
262 private void setTaxon(Taxon taxon
) {
265 textNewTaxonName
.setText(taxon
.getName().getTitleCache());
267 selection_SecRef
.setEntity(taxon
.getSec());
270 private void setTaxon(TaxonNameBase taxonName
) {
271 Reference secundum
= null;
272 if (getParentTreeNode() != null) {
273 if (this.selection_SecRef
.getEntity() != null){
274 secundum
= this.selection_SecRef
.getEntity();
277 if (taxonName
!= null){
278 taxon
= Taxon
.NewInstance(taxonName
, secundum
);
279 if (!taxonName
.getTitleCache().trim().equals(textNewTaxonName
.getText().trim())){
280 textNewTaxonName
.setText(taxonName
.getTitleCache());
283 textNewTaxonName
.setText(null);
287 private void setOpenInEditor(boolean openInEditor
) {
288 this.openInEditor
= openInEditor
;
291 public String
getTaxonName() {
292 return textNewTaxonName
.getText();
295 public Reference
getReference(){
296 return selection_SecRef
.getSelection();
299 public Control
getMainControl(){
300 return textNewTaxonName
.getMainControl();
303 public boolean isComplete() {
304 boolean allComplete
= true;
306 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
307 allComplete
&=((TaxonNodeAgentRelationCollectionElement
)element
).isComplete();
309 complete
= allComplete
;
317 * @return the createNew
319 public boolean isCreateNew() {