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
);
129 selectionNodeAgentRelation
= formFactory
.createTaxonNodeAgentRelationCollectionSection(formElement
, getConversationHolder(), style
);
130 selectionNodeAgentRelation
.setEntity((TaxonNode
)entity
);
131 selectionNodeAgentRelation
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
135 if (entity
instanceof TaxonNode
) {
136 checkbox_excluded
= formFactory
.createCheckbox(formElement
,
137 "Taxon is excluded", ((TaxonNode
) entity
).isExcluded(), style
);
138 multiLanguageTextExcludedNotes
= formFactory
.createMultiLanguageTextElement(formElement
, "Excluded Notes", ((TaxonNode
)entity
).getExcludedNote(), 50, style
);
139 multiLanguageTextExcludedNotes
.setEnabled(((TaxonNode
) entity
).isExcluded());
141 checkbox_openInEditor
= formFactory
.createCheckbox(formElement
,
142 "Open in Editor", true, style
);
143 setOpenInEditor(true);
146 private void preFillTaxonName() {
147 if(getEntity() instanceof TaxonNode
){
148 TaxonNode node
= (TaxonNode
)getEntity();
149 if(node
.getTaxon()!=null){
150 Taxon taxon
= HibernateProxyHelper
.deproxy(node
.getTaxon(), Taxon
.class);
151 if(taxon
.getName()!=null && taxon
.getName().isInstanceOf(NonViralName
.class)){
152 NonViralName
<?
> name
= HibernateProxyHelper
.deproxy(node
.getTaxon().getName(), NonViralName
.class);
154 textNewTaxonName
.setText(name
.getNameCache());
155 } else if( ! name
.isSupraGeneric() && name
.getRank() != null){
156 String taxonName
= "";
157 if(name
.isGenus() || name
.isInfraGeneric()|| name
.isSpeciesAggregate() ){
158 taxonName
= name
.getGenusOrUninomial();
160 else if(name
.isSpecies() || name
.isInfraSpecific() ){
161 taxonName
= CdmUtils
.concat(" ", name
.getGenusOrUninomial(),name
.getSpecificEpithet());
163 if (StringUtils
.isNotBlank(taxonName
)){
164 textNewTaxonName
.setText(taxonName
+ " ");
165 if(textNewTaxonName
.getMainControl() instanceof Text
){
166 Text text
= (Text
)textNewTaxonName
.getMainControl();
167 text
.setSelection(textNewTaxonName
.getText().length());
178 public void handleEvent(Object eventSource
) {
179 if (eventSource
== selection_classification
) {
180 setClassification(selection_classification
.getEntity());
181 } else if (eventSource
== selection_parentTaxonNode
) {
182 setParentTreeNode(selection_parentTaxonNode
.getEntity());
183 } else if (eventSource
== selection_reuseExistingTaxon
) {
184 boolean enabled
= selection_reuseExistingTaxon
.getEntity() == null;
185 selection_reuseExistingName
.setEnabled(enabled
);
187 setTaxon(selection_reuseExistingTaxon
.getEntity());
189 textNewTaxonName
.setText(null);
191 complete
= !textNewTaxonName
.getText().isEmpty();
194 } else if (eventSource
== selection_reuseExistingName
) {
195 boolean enabled
= selection_reuseExistingName
.getEntity() == null;
196 setTaxon(selection_reuseExistingName
.getEntity());
197 selection_reuseExistingTaxon
.setEnabled(enabled
);
198 textNewTaxonName
.setEnabled(enabled
);
200 complete
= !textNewTaxonName
.getText().isEmpty();
201 } else if (eventSource
== textNewTaxonName
) {
202 boolean enabled
= CdmUtils
.isBlank(textNewTaxonName
.getText());
203 selection_reuseExistingTaxon
.setEnabled(enabled
);
204 selection_reuseExistingName
.setEnabled(enabled
);
206 complete
= CdmUtils
.isNotBlank(textNewTaxonName
.getText());
207 } else if (eventSource
== checkbox_excluded
) {
208 boolean isExcluded
= checkbox_excluded
.getSelection();
209 ((TaxonNode
) getEntity()).setExcluded(isExcluded
);
210 multiLanguageTextExcludedNotes
.setEnabled(isExcluded
);
211 } else if (eventSource
== checkbox_unplaced
) {
212 ((TaxonNode
) getEntity()).setUnplaced(checkbox_unplaced
.getSelection());
213 } else if (eventSource
== checkbox_openInEditor
) {
214 setOpenInEditor(checkbox_openInEditor
.getSelection());
215 } else if (eventSource
== selectionNodeAgentRelation
){
216 boolean allComplete
= true;
217 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
218 allComplete
&=((TaxonNodeAgentRelationCollectionElement
)element
).isComplete();
220 complete
= !isCreateNew() && allComplete
;
224 public Classification
getClassification() {
225 return classification
;
228 public boolean isOpenInEditor() {
232 public ITaxonTreeNode
getParentTreeNode() {
233 return parentTreeNode
;
236 private void setClassification(Classification classification
) {
237 this.classification
= classification
;
238 setParentTreeNode(classification
);
241 private void setParentTreeNode(ITaxonTreeNode parentTreeNode
) {
242 this.parentTreeNode
= parentTreeNode
;
244 if (parentTreeNode
instanceof Classification
) {
245 classification
= (Classification
) parentTreeNode
;
246 selection_classification
.setEntity(classification
);
247 selection_parentTaxonNode
.setEntity(null);
248 selection_parentTaxonNode
.setClassification(classification
);
249 selection_SecRef
.setEntity(classification
.getReference());
250 } else if (parentTreeNode
instanceof TaxonNode
) {
251 classification
= HibernateProxyHelper
252 .deproxy(((TaxonNode
) parentTreeNode
).getClassification());
253 selection_classification
.setEntity(classification
);
254 selection_parentTaxonNode
.setEntity((TaxonNode
) parentTreeNode
);
255 selection_parentTaxonNode
.setClassification(classification
);
256 selection_SecRef
.setEntity(((TaxonNode
)parentTreeNode
).getTaxon().getSec());
257 } else if(parentTreeNode
== null){
258 this.parentTreeNode
= selection_classification
.getEntity();
262 private void setTreeNode(ITaxonTreeNode treeNode
) {
263 classification
= HibernateProxyHelper
264 .deproxy(((TaxonNode
) treeNode
).getClassification());
265 selection_classification
.setEntity(classification
);
266 selection_parentTaxonNode
.setEntity(((TaxonNode
) treeNode
).getParent());
267 selection_parentTaxonNode
.setClassification(((TaxonNode
) treeNode
).getParent().getClassification());
268 selection_SecRef
.setEntity(((TaxonNode
)treeNode
).getTaxon().getSec());
271 private void setTaxon(Taxon taxon
) {
274 textNewTaxonName
.setText(taxon
.getName().getTitleCache());
276 selection_SecRef
.setEntity(taxon
.getSec());
279 private void setTaxon(TaxonNameBase taxonName
) {
280 Reference secundum
= null;
281 if (getParentTreeNode() != null) {
282 if (this.selection_SecRef
.getEntity() != null){
283 secundum
= this.selection_SecRef
.getEntity();
286 if (taxonName
!= null){
287 taxon
= Taxon
.NewInstance(taxonName
, secundum
);
288 if (textNewTaxonName
.getText() == null || !taxonName
.getTitleCache().trim().equals(textNewTaxonName
.getText().trim())){
289 textNewTaxonName
.setText(taxonName
.getTitleCache());
291 // if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
292 // textNewTaxonName.setText(taxonName.getTitleCache());
295 textNewTaxonName
.setText(null);
299 private void setOpenInEditor(boolean openInEditor
) {
300 this.openInEditor
= openInEditor
;
303 public String
getTaxonName() {
304 return textNewTaxonName
.getText();
306 public Taxon
getTaxon() {
310 public Reference
getReference(){
311 return selection_SecRef
.getSelection();
314 public Control
getMainControl(){
315 return textNewTaxonName
.getMainControl();
318 public boolean isComplete() {
319 boolean allComplete
= true;
321 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
322 allComplete
&=((TaxonNodeAgentRelationCollectionElement
)element
).isComplete();
324 complete
= allComplete
;
329 public boolean isCreateNew() {