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());
177 textNewTaxonName
.setText(null);
179 complete
= !textNewTaxonName
.getText().isEmpty();
182 } else if (eventSource
== selection_reuseExistingName
) {
183 boolean enabled
= selection_reuseExistingName
.getEntity() == null;
184 setTaxon(selection_reuseExistingName
.getEntity());
185 selection_reuseExistingTaxon
.setEnabled(enabled
);
186 textNewTaxonName
.setEnabled(enabled
);
188 setTaxon(selection_reuseExistingName
.getEntity());
190 complete
= !textNewTaxonName
.getText().isEmpty();
191 } else if (eventSource
== textNewTaxonName
) {
192 boolean enabled
= CdmUtils
.isBlank(textNewTaxonName
.getText());
193 selection_reuseExistingTaxon
.setEnabled(enabled
);
194 selection_reuseExistingName
.setEnabled(enabled
);
196 complete
= !textNewTaxonName
.getText().isEmpty();
197 } else if (eventSource
== checkbox_openInEditor
) {
198 setOpenInEditor(checkbox_openInEditor
.getSelection());
199 } else if (eventSource
== selectionNodeAgentRelation
){
200 boolean allComplete
= true;
201 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
202 allComplete
&=((TaxonNodeAgentRelationCollectionElement
)element
).isComplete();
205 complete
= !isCreateNew() && allComplete
;
209 public Classification
getClassification() {
210 return classification
;
213 public boolean isOpenInEditor() {
217 public ITaxonTreeNode
getParentTreeNode() {
218 return parentTreeNode
;
221 private void setClassification(Classification classification
) {
222 this.classification
= classification
;
223 setParentTreeNode(classification
);
226 private void setParentTreeNode(ITaxonTreeNode parentTreeNode
) {
227 this.parentTreeNode
= parentTreeNode
;
229 if (parentTreeNode
instanceof Classification
) {
230 classification
= (Classification
) parentTreeNode
;
231 selection_classification
.setEntity(classification
);
232 selection_parentTaxonNode
.setEntity(null);
233 selection_parentTaxonNode
.setClassification(classification
);
234 selection_SecRef
.setEntity(classification
.getReference());
235 } else if (parentTreeNode
instanceof TaxonNode
) {
236 classification
= HibernateProxyHelper
237 .deproxy(((TaxonNode
) parentTreeNode
).getClassification());
238 selection_classification
.setEntity(classification
);
239 selection_parentTaxonNode
.setEntity((TaxonNode
) parentTreeNode
);
240 selection_parentTaxonNode
.setClassification(classification
);
241 selection_SecRef
.setEntity(((TaxonNode
)parentTreeNode
).getTaxon().getSec());
242 } else if(parentTreeNode
== null){
243 this.parentTreeNode
= selection_classification
.getEntity();
247 private void setTreeNode(ITaxonTreeNode treeNode
) {
248 classification
= HibernateProxyHelper
249 .deproxy(((TaxonNode
) treeNode
).getClassification());
250 selection_classification
.setEntity(classification
);
251 selection_parentTaxonNode
.setEntity(((TaxonNode
) treeNode
).getParent());
252 selection_parentTaxonNode
.setClassification(((TaxonNode
) treeNode
).getParent().getClassification());
253 selection_SecRef
.setEntity(((TaxonNode
)treeNode
).getTaxon().getSec());
256 private void setTaxon(Taxon taxon
) {
259 textNewTaxonName
.setText(taxon
.getName().getTitleCache());
261 selection_SecRef
.setEntity(taxon
.getSec());
264 private void setTaxon(TaxonNameBase taxonName
) {
265 Reference secundum
= null;
266 if (getParentTreeNode() != null) {
267 if (this.selection_SecRef
.getEntity() != null){
268 secundum
= this.selection_SecRef
.getEntity();
271 if (taxonName
!= null){
272 taxon
= Taxon
.NewInstance(taxonName
, secundum
);
273 if (!taxonName
.getTitleCache().trim().equals(textNewTaxonName
.getText().trim())){
274 textNewTaxonName
.setText(taxonName
.getTitleCache());
277 textNewTaxonName
.setText(null);
281 private void setOpenInEditor(boolean openInEditor
) {
282 this.openInEditor
= openInEditor
;
285 public String
getTaxonName() {
286 return textNewTaxonName
.getText();
288 public Taxon
getTaxon() {
289 return selection_reuseExistingTaxon
.getEntity();
292 public Reference
getReference(){
293 return selection_SecRef
.getSelection();
296 public Control
getMainControl(){
297 return textNewTaxonName
.getMainControl();
300 public boolean isComplete() {
301 boolean allComplete
= true;
303 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
304 allComplete
&=((TaxonNodeAgentRelationCollectionElement
)element
).isComplete();
306 complete
= allComplete
;
314 * @return the createNew
316 public boolean isCreateNew() {