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
.MultilanguageTextElement
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.TextWithLabelElement
;
32 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractCdmDetailElement
;
33 import eu
.etaxonomy
.taxeditor
.ui
.selection
.EntitySelectionElement
;
34 import eu
.etaxonomy
.taxeditor
.ui
.selection
.TaxonNodeSelectionElement
;
39 * @created Sep 27, 2010
42 public class TaxonNodeDetailElement
extends AbstractCdmDetailElement
<ITaxonTreeNode
> {
44 private boolean createNew
= false;
46 private EntitySelectionElement
<Classification
> selection_classification
;
48 private Classification classification
;
50 private TaxonNodeSelectionElement selection_parentTaxonNode
;
52 private ITaxonTreeNode parentTreeNode
;
54 private EntitySelectionElement
<Taxon
> selection_reuseExistingTaxon
;
55 private EntitySelectionElement
<Reference
> selection_SecRef
;
59 private TextWithLabelElement textNewTaxonName
;
61 private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation
;
63 private CheckboxElement checkbox_openInEditor
;
65 private CheckboxElement checkbox_unplaced
;
67 private CheckboxElement checkbox_excluded
;
69 private MultilanguageTextElement multiLanguageTextExcludedNotes
;
71 private boolean openInEditor
;
73 private boolean complete
;
75 private EntitySelectionElement
<TaxonNameBase
> selection_reuseExistingName
;
77 public TaxonNodeDetailElement(CdmFormFactory formFactory
,
78 ICdmFormElement formElement
, boolean isCreateNew
) {
79 super(formFactory
, formElement
);
80 createNew
= isCreateNew
;
85 protected void createControls(ICdmFormElement formElement
,
86 ITaxonTreeNode entity
, int style
) {
87 selection_classification
= formFactory
88 .createSelectionElement(Classification
.class,
89 getConversationHolder(), formElement
, "Classification",
90 null, EntitySelectionElement
.DELETABLE
, style
);
91 selection_parentTaxonNode
= formFactory
92 .createTaxonNodeSelectionElement(getConversationHolder(), formElement
, "Parent", null,
93 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
);
106 selection_SecRef
= formFactory
107 .createSelectionElement(Reference
.class,getConversationHolder(), formElement
, "Secundum Reference", null,
108 EntitySelectionElement
.DELETABLE
, style
);
110 textNewTaxonName
= formFactory
.createTextWithLabelElement(formElement
,"New Taxon", "", style
);
111 textNewTaxonName
.setFocus();
113 textNewTaxonName
= formFactory
.createTextWithLabelElement(formElement
,"Taxon", "", style
);
118 setParentTreeNode(entity
);
123 if (entity
instanceof TaxonNode
) {
124 checkbox_unplaced
= formFactory
.createCheckbox(formElement
,
125 "Taxon is unplaced", ((TaxonNode
) entity
).isUnplaced(), style
);
126 checkbox_excluded
= formFactory
.createCheckbox(formElement
,
127 "Taxon is excluded", ((TaxonNode
) entity
).isExcluded(), style
);
128 multiLanguageTextExcludedNotes
= formFactory
.createMultiLanguageTextElement(formElement
, "Excluded Notes", ((TaxonNode
)entity
).getExcludedNote(), 50, style
);
129 multiLanguageTextExcludedNotes
.setEnabled(((TaxonNode
) entity
).isExcluded());
133 selectionNodeAgentRelation
= formFactory
.createTaxonNodeAgentRelationCollectionSection(formElement
, getConversationHolder(), style
);
134 selectionNodeAgentRelation
.setEntity((TaxonNode
)entity
);
135 selectionNodeAgentRelation
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
138 checkbox_openInEditor
= formFactory
.createCheckbox(formElement
,
139 "Open in Editor", true, style
);
140 setOpenInEditor(true);
143 private void preFillTaxonName() {
144 if(getEntity() instanceof TaxonNode
){
145 TaxonNode node
= (TaxonNode
)getEntity();
146 if(node
.getTaxon()!=null){
147 Taxon taxon
= HibernateProxyHelper
.deproxy(node
.getTaxon(), Taxon
.class);
148 if(taxon
.getName()!=null && taxon
.getName().isInstanceOf(NonViralName
.class)){
149 NonViralName
<?
> name
= HibernateProxyHelper
.deproxy(node
.getTaxon().getName(), NonViralName
.class);
151 textNewTaxonName
.setText(name
.getNameCache());
152 } else if( ! name
.isSupraGeneric() && name
.getRank() != null){
153 String taxonName
= "";
154 if(name
.isGenus() || name
.isInfraGeneric()|| name
.isSpeciesAggregate() ){
155 taxonName
= name
.getGenusOrUninomial();
157 else if(name
.isSpecies() || name
.isInfraSpecific() ){
158 taxonName
= CdmUtils
.concat(" ", name
.getGenusOrUninomial(),name
.getSpecificEpithet());
160 if (StringUtils
.isNotBlank(taxonName
)){
161 textNewTaxonName
.setText(taxonName
+ " ");
162 if(textNewTaxonName
.getMainControl() instanceof Text
){
163 Text text
= (Text
)textNewTaxonName
.getMainControl();
164 text
.setSelection(textNewTaxonName
.getText().length());
175 public void handleEvent(Object eventSource
) {
176 if (eventSource
== selection_classification
) {
177 setClassification(selection_classification
.getEntity());
178 } else if (eventSource
== selection_parentTaxonNode
) {
179 setParentTreeNode(selection_parentTaxonNode
.getEntity());
180 } else if (eventSource
== selection_reuseExistingTaxon
) {
181 boolean enabled
= selection_reuseExistingTaxon
.getEntity() == null;
182 selection_reuseExistingName
.setEnabled(enabled
);
184 setTaxon(selection_reuseExistingTaxon
.getEntity());
186 textNewTaxonName
.setText(null);
188 complete
= !textNewTaxonName
.getText().isEmpty();
191 } else if (eventSource
== selection_reuseExistingName
) {
192 boolean enabled
= selection_reuseExistingName
.getEntity() == null;
193 setTaxon(selection_reuseExistingName
.getEntity());
194 selection_reuseExistingTaxon
.setEnabled(enabled
);
195 textNewTaxonName
.setEnabled(enabled
);
197 complete
= !textNewTaxonName
.getText().isEmpty();
198 } else if (eventSource
== textNewTaxonName
) {
199 boolean enabled
= CdmUtils
.isBlank(textNewTaxonName
.getText());
200 selection_reuseExistingTaxon
.setEnabled(enabled
);
201 selection_reuseExistingName
.setEnabled(enabled
);
203 complete
= CdmUtils
.isNotBlank(textNewTaxonName
.getText());
204 } else if (eventSource
== checkbox_excluded
) {
205 boolean isExcluded
= checkbox_excluded
.getSelection();
206 ((TaxonNode
) getEntity()).setExcluded(isExcluded
);
207 multiLanguageTextExcludedNotes
.setEnabled(isExcluded
);
208 } else if (eventSource
== checkbox_unplaced
) {
209 ((TaxonNode
) getEntity()).setUnplaced(checkbox_unplaced
.getSelection());
210 } else if (eventSource
== checkbox_openInEditor
) {
211 setOpenInEditor(checkbox_openInEditor
.getSelection());
212 } else if (eventSource
== selectionNodeAgentRelation
){
213 boolean allComplete
= true;
214 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
215 allComplete
&=((TaxonNodeAgentRelationCollectionElement
)element
).isComplete();
217 complete
= !isCreateNew() && allComplete
;
221 public Classification
getClassification() {
222 return classification
;
225 public boolean isOpenInEditor() {
229 public ITaxonTreeNode
getParentTreeNode() {
230 return parentTreeNode
;
233 private void setClassification(Classification classification
) {
234 this.classification
= classification
;
235 setParentTreeNode(classification
);
238 private void setParentTreeNode(ITaxonTreeNode parentTreeNode
) {
239 this.parentTreeNode
= parentTreeNode
;
241 if (parentTreeNode
instanceof Classification
) {
242 classification
= (Classification
) parentTreeNode
;
243 selection_classification
.setEntity(classification
);
244 selection_parentTaxonNode
.setEntity(null);
245 selection_parentTaxonNode
.setClassification(classification
);
246 selection_SecRef
.setEntity(classification
.getReference());
247 } else if (parentTreeNode
instanceof TaxonNode
) {
248 classification
= HibernateProxyHelper
249 .deproxy(((TaxonNode
) parentTreeNode
).getClassification());
250 selection_classification
.setEntity(classification
);
251 selection_parentTaxonNode
.setEntity((TaxonNode
) parentTreeNode
);
252 selection_parentTaxonNode
.setClassification(classification
);
253 selection_SecRef
.setEntity(((TaxonNode
)parentTreeNode
).getTaxon().getSec());
254 } else if(parentTreeNode
== null){
255 this.parentTreeNode
= selection_classification
.getEntity();
259 private void setTreeNode(ITaxonTreeNode treeNode
) {
260 classification
= HibernateProxyHelper
261 .deproxy(((TaxonNode
) treeNode
).getClassification());
262 selection_classification
.setEntity(classification
);
263 selection_parentTaxonNode
.setEntity(((TaxonNode
) treeNode
).getParent());
264 selection_parentTaxonNode
.setClassification(((TaxonNode
) treeNode
).getParent().getClassification());
265 selection_SecRef
.setEntity(((TaxonNode
)treeNode
).getTaxon().getSec());
268 private void setTaxon(Taxon taxon
) {
271 textNewTaxonName
.setText(taxon
.getName().getTitleCache());
273 selection_SecRef
.setEntity(taxon
.getSec());
276 private void setTaxon(TaxonNameBase taxonName
) {
277 Reference secundum
= null;
278 if (getParentTreeNode() != null) {
279 if (this.selection_SecRef
.getEntity() != null){
280 secundum
= this.selection_SecRef
.getEntity();
283 if (taxonName
!= null){
284 taxon
= Taxon
.NewInstance(taxonName
, secundum
);
285 if (!taxonName
.getTitleCache().trim().equals(textNewTaxonName
.getText().trim())){
286 textNewTaxonName
.setText(taxonName
.getTitleCache());
289 textNewTaxonName
.setText(null);
293 private void setOpenInEditor(boolean openInEditor
) {
294 this.openInEditor
= openInEditor
;
297 public String
getTaxonName() {
298 return textNewTaxonName
.getText();
300 public Taxon
getTaxon() {
304 public Reference
getReference(){
305 return selection_SecRef
.getSelection();
308 public Control
getMainControl(){
309 return textNewTaxonName
.getMainControl();
312 public boolean isComplete() {
313 boolean allComplete
= true;
315 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
316 allComplete
&=((TaxonNodeAgentRelationCollectionElement
)element
).isComplete();
318 complete
= allComplete
;
323 public boolean isCreateNew() {