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
;
62 private CheckboxElement checkbox_openInEditor
;
64 private CheckboxElement checkbox_excluded
;
66 private CheckboxElement checkbox_unplaced
;
68 private boolean openInEditor
;
70 private boolean complete
;
72 private EntitySelectionElement
<TaxonNameBase
> selection_reuseExistingName
;
74 public TaxonNodeDetailElement(CdmFormFactory formFactory
,
75 ICdmFormElement formElement
, boolean isCreateNew
) {
76 super(formFactory
, formElement
);
77 createNew
= isCreateNew
;
82 protected void createControls(ICdmFormElement formElement
,
83 ITaxonTreeNode entity
, int style
) {
84 selection_classification
= formFactory
85 .createSelectionElement(Classification
.class,
86 getConversationHolder(), formElement
, "Classification",
87 null, EntitySelectionElement
.DELETABLE
, style
);
88 selection_parentTaxonNode
= formFactory
89 .createTaxonNodeSelectionElement(getConversationHolder(), formElement
, "Parent", null,
90 EntitySelectionElement
.DELETABLE
, style
);
92 selection_reuseExistingTaxon
= formFactory
93 .createSelectionElement(Taxon
.class,
94 getConversationHolder(), formElement
,
95 "Reuse existing taxon", null,
96 EntitySelectionElement
.DELETABLE
, style
);
98 selection_reuseExistingName
= formFactory
99 .createSelectionElement(TaxonNameBase
.class,
100 getConversationHolder(), formElement
,
101 "Reuse existing name", null,
102 EntitySelectionElement
.DELETABLE
, style
);
103 selection_SecRef
= formFactory
104 .createSelectionElement(Reference
.class,getConversationHolder(), formElement
, "Secundum Reference", null,
105 EntitySelectionElement
.DELETABLE
, style
);
107 textNewTaxonName
= formFactory
.createTextWithLabelElement(formElement
,"New Taxon", "", style
);
108 textNewTaxonName
.setFocus();
110 textNewTaxonName
= formFactory
.createTextWithLabelElement(formElement
,"Taxon", "", style
);
116 setParentTreeNode(entity
);
122 if (entity
instanceof TaxonNode
) {
123 checkbox_excluded
= formFactory
.createCheckbox(formElement
,
124 "Taxon is excluded", ((TaxonNode
) entity
).isExcluded(), style
);
125 checkbox_unplaced
= formFactory
.createCheckbox(formElement
,
126 "Taxon is unplaced", ((TaxonNode
) entity
).isUnplaced(), style
);
130 selectionNodeAgentRelation
= formFactory
.createTaxonNodeAgentRelationCollectionSection(formElement
, getConversationHolder(), style
);
131 selectionNodeAgentRelation
.setEntity((TaxonNode
)entity
);
132 selectionNodeAgentRelation
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
135 checkbox_openInEditor
= formFactory
.createCheckbox(formElement
,
136 "Open in Editor", true, style
);
137 setOpenInEditor(true);
141 private void preFillTaxonName() {
142 if(getEntity() instanceof TaxonNode
){
143 TaxonNode node
= (TaxonNode
)getEntity();
144 if(node
.getTaxon()!=null){
145 Taxon taxon
= HibernateProxyHelper
.deproxy(node
.getTaxon(), Taxon
.class);
146 if(taxon
.getName()!=null && taxon
.getName().isInstanceOf(NonViralName
.class)){
147 NonViralName
<?
> name
= HibernateProxyHelper
.deproxy(node
.getTaxon().getName(), NonViralName
.class);
149 textNewTaxonName
.setText(name
.getNameCache());
150 } else if( ! name
.isSupraGeneric() && name
.getRank() != null){
151 String taxonName
= "";
152 if(name
.isGenus() || name
.isInfraGeneric()|| name
.isSpeciesAggregate() ){
153 taxonName
= name
.getGenusOrUninomial();
155 else if(name
.isSpecies() || name
.isInfraSpecific() ){
156 taxonName
= CdmUtils
.concat(" ", name
.getGenusOrUninomial(),name
.getSpecificEpithet());
158 if (StringUtils
.isNotBlank(taxonName
)){
159 textNewTaxonName
.setText(taxonName
+ " ");
160 if(textNewTaxonName
.getMainControl() instanceof Text
){
161 Text text
= (Text
)textNewTaxonName
.getMainControl();
162 text
.setSelection(textNewTaxonName
.getText().length());
176 public void handleEvent(Object eventSource
) {
177 if (eventSource
== selection_classification
) {
178 setClassification(selection_classification
.getEntity());
179 } else if (eventSource
== selection_parentTaxonNode
) {
180 setParentTreeNode(selection_parentTaxonNode
.getEntity());
181 } else if (eventSource
== selection_reuseExistingTaxon
) {
182 boolean enabled
= selection_reuseExistingTaxon
.getEntity() == null;
183 selection_reuseExistingName
.setEnabled(enabled
);
185 setTaxon(selection_reuseExistingTaxon
.getEntity());
187 textNewTaxonName
.setText(null);
189 complete
= !textNewTaxonName
.getText().isEmpty();
192 } else if (eventSource
== selection_reuseExistingName
) {
193 boolean enabled
= selection_reuseExistingName
.getEntity() == null;
194 setTaxon(selection_reuseExistingName
.getEntity());
195 selection_reuseExistingTaxon
.setEnabled(enabled
);
196 textNewTaxonName
.setEnabled(enabled
);
198 //setTaxon(selection_reuseExistingName.getEntity());
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 ((TaxonNode
) getEntity()).setExcluded(checkbox_excluded
.getSelection());
209 } else if (eventSource
== checkbox_unplaced
) {
210 ((TaxonNode
) getEntity()).setUnplaced(checkbox_unplaced
.getSelection());
211 } else if (eventSource
== checkbox_openInEditor
) {
212 setOpenInEditor(checkbox_openInEditor
.getSelection());
213 } else if (eventSource
== selectionNodeAgentRelation
){
214 boolean allComplete
= true;
215 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
216 allComplete
&=((TaxonNodeAgentRelationCollectionElement
)element
).isComplete();
219 complete
= !isCreateNew() && allComplete
;
223 public Classification
getClassification() {
224 return classification
;
227 public boolean isOpenInEditor() {
231 public ITaxonTreeNode
getParentTreeNode() {
232 return parentTreeNode
;
235 private void setClassification(Classification classification
) {
236 this.classification
= classification
;
237 setParentTreeNode(classification
);
240 private void setParentTreeNode(ITaxonTreeNode parentTreeNode
) {
241 this.parentTreeNode
= parentTreeNode
;
243 if (parentTreeNode
instanceof Classification
) {
244 classification
= (Classification
) parentTreeNode
;
245 selection_classification
.setEntity(classification
);
246 selection_parentTaxonNode
.setEntity(null);
247 selection_parentTaxonNode
.setClassification(classification
);
248 selection_SecRef
.setEntity(classification
.getReference());
249 } else if (parentTreeNode
instanceof TaxonNode
) {
250 classification
= HibernateProxyHelper
251 .deproxy(((TaxonNode
) parentTreeNode
).getClassification());
252 selection_classification
.setEntity(classification
);
253 selection_parentTaxonNode
.setEntity((TaxonNode
) parentTreeNode
);
254 selection_parentTaxonNode
.setClassification(classification
);
255 selection_SecRef
.setEntity(((TaxonNode
)parentTreeNode
).getTaxon().getSec());
256 } else if(parentTreeNode
== null){
257 this.parentTreeNode
= selection_classification
.getEntity();
261 private void setTreeNode(ITaxonTreeNode treeNode
) {
262 classification
= HibernateProxyHelper
263 .deproxy(((TaxonNode
) treeNode
).getClassification());
264 selection_classification
.setEntity(classification
);
265 selection_parentTaxonNode
.setEntity(((TaxonNode
) treeNode
).getParent());
266 selection_parentTaxonNode
.setClassification(((TaxonNode
) treeNode
).getParent().getClassification());
267 selection_SecRef
.setEntity(((TaxonNode
)treeNode
).getTaxon().getSec());
270 private void setTaxon(Taxon taxon
) {
273 textNewTaxonName
.setText(taxon
.getName().getTitleCache());
275 selection_SecRef
.setEntity(taxon
.getSec());
278 private void setTaxon(TaxonNameBase taxonName
) {
279 Reference secundum
= null;
280 if (getParentTreeNode() != null) {
281 if (this.selection_SecRef
.getEntity() != null){
282 secundum
= this.selection_SecRef
.getEntity();
285 if (taxonName
!= null){
286 taxon
= Taxon
.NewInstance(taxonName
, secundum
);
287 if (!taxonName
.getTitleCache().trim().equals(textNewTaxonName
.getText().trim())){
288 textNewTaxonName
.setText(taxonName
.getTitleCache());
291 textNewTaxonName
.setText(null);
295 private void setOpenInEditor(boolean openInEditor
) {
296 this.openInEditor
= openInEditor
;
299 public String
getTaxonName() {
300 return textNewTaxonName
.getText();
302 public Taxon
getTaxon() {
306 public Reference
getReference(){
307 return selection_SecRef
.getSelection();
310 public Control
getMainControl(){
311 return textNewTaxonName
.getMainControl();
314 public boolean isComplete() {
315 boolean allComplete
= true;
317 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
318 allComplete
&=((TaxonNodeAgentRelationCollectionElement
)element
).isComplete();
320 complete
= allComplete
;
328 * @return the createNew
330 public boolean isCreateNew() {