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
.Text
;
16 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
17 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
18 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
19 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
20 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
21 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
22 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
23 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
24 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
25 import eu
.etaxonomy
.taxeditor
.parser
.ParseHandler
;
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 Reference secReference
;
60 private TextWithLabelElement textNewTaxonName
;
62 private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation
;
65 private CheckboxElement checkbox_openInEditor
;
67 private boolean openInEditor
;
69 private boolean complete
;
71 private EntitySelectionElement
<TaxonNameBase
> selection_reuseExistingName
;
73 public TaxonNodeDetailElement(CdmFormFactory formFactory
,
74 ICdmFormElement formElement
, boolean isCreateNew
) {
75 super(formFactory
, formElement
);
76 createNew
= isCreateNew
;
81 protected void createControls(ICdmFormElement formElement
,
82 ITaxonTreeNode entity
, int style
) {
83 selection_classification
= formFactory
84 .createSelectionElement(Classification
.class,
85 getConversationHolder(), formElement
, "Classification",
86 null, EntitySelectionElement
.DELETABLE
, style
);
87 selection_parentTaxonNode
= formFactory
88 .createTaxonNodeSelectionElement(getConversationHolder(), formElement
, "Parent", null,
89 EntitySelectionElement
.DELETABLE
, style
);
91 selection_reuseExistingTaxon
= formFactory
92 .createSelectionElement(Taxon
.class,
93 getConversationHolder(), formElement
,
94 "Reuse existing taxon", null,
95 EntitySelectionElement
.DELETABLE
, style
);
97 selection_reuseExistingName
= formFactory
98 .createSelectionElement(TaxonNameBase
.class,
99 getConversationHolder(), formElement
,
100 "Reuse existing name", null,
101 EntitySelectionElement
.DELETABLE
, style
);
102 selection_SecRef
= formFactory
103 .createSelectionElement(Reference
.class,getConversationHolder(), formElement
, "Secundum Reference", null,
104 EntitySelectionElement
.DELETABLE
, style
);
106 textNewTaxonName
= formFactory
.createTextWithLabelElement(formElement
,"New Taxon", "", style
);
107 textNewTaxonName
.setFocus();
109 textNewTaxonName
= formFactory
.createTextWithLabelElement(formElement
,"Taxon", "", style
);
115 setParentTreeNode(entity
);
122 selectionNodeAgentRelation
= formFactory
.createTaxonNodeAgentRelationCollectionSection(formElement
, getConversationHolder(), style
);
123 selectionNodeAgentRelation
.setEntity((TaxonNode
)entity
);
124 selectionNodeAgentRelation
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
127 checkbox_openInEditor
= formFactory
.createCheckbox(formElement
,
128 "Open in Editor", true, style
);
129 setOpenInEditor(true);
133 private void preFillTaxonName() {
134 if(getEntity() instanceof TaxonNode
){
135 TaxonNode node
= (TaxonNode
)getEntity();
136 if(node
.getTaxon()!=null){
137 Taxon taxon
= HibernateProxyHelper
.deproxy(node
.getTaxon(), Taxon
.class);
138 if(taxon
.getName()!=null && taxon
.getName().isInstanceOf(NonViralName
.class)){
139 NonViralName
<?
> name
= HibernateProxyHelper
.deproxy(node
.getTaxon().getName(), NonViralName
.class);
141 textNewTaxonName
.setText(name
.getNameCache());
142 } else if( ! name
.isSupraGeneric() && name
.getRank() != null){
143 String taxonName
= "";
144 if(name
.isGenus() || name
.isInfraGeneric()|| name
.isSpeciesAggregate() ){
145 taxonName
= name
.getGenusOrUninomial();
147 else if(name
.isSpecies() || name
.isInfraSpecific() ){
148 taxonName
= CdmUtils
.concat(" ", name
.getGenusOrUninomial(),name
.getSpecificEpithet());
150 if (StringUtils
.isNotBlank(taxonName
)){
151 textNewTaxonName
.setText(taxonName
+ " ");
152 if(textNewTaxonName
.getMainControl() instanceof Text
){
153 Text text
= (Text
)textNewTaxonName
.getMainControl();
154 text
.setSelection(textNewTaxonName
.getText().length());
168 public void handleEvent(Object eventSource
) {
169 if (eventSource
== selection_classification
) {
170 setClassification(selection_classification
.getEntity());
171 } else if (eventSource
== selection_parentTaxonNode
) {
172 setParentTreeNode(selection_parentTaxonNode
.getEntity());
173 } else if (eventSource
== selection_SecRef
) {
174 setSecReference(selection_SecRef
.getEntity());
175 } else if (eventSource
== selection_reuseExistingTaxon
) {
176 boolean enabled
= selection_reuseExistingTaxon
.getEntity() == null;
177 selection_reuseExistingName
.setEnabled(enabled
);
179 setTaxon(selection_reuseExistingTaxon
.getEntity());
182 textNewTaxonName
.setEnabled(enabled
);
184 setTaxon(selection_reuseExistingTaxon
.getEntity());
186 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 setTaxon(textNewTaxonName
.getText());
207 complete
= !textNewTaxonName
.getText().isEmpty();
208 } else if (eventSource
== checkbox_openInEditor
) {
209 setOpenInEditor(checkbox_openInEditor
.getSelection());
210 } else if (eventSource
== selectionNodeAgentRelation
){
211 boolean allComplete
= true;
212 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
213 allComplete
&=((TaxonNodeAgentRelationCollectionElement
)element
).isComplete();
216 complete
= !isCreateNew() && allComplete
;
220 public Classification
getClassification() {
221 return classification
;
224 public boolean isOpenInEditor() {
228 public ITaxonTreeNode
getParentTreeNode() {
229 return parentTreeNode
;
232 public Taxon
getTaxon() {
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 setSecReference(Reference secRef
){
272 this.secReference
= secRef
;
273 setTaxon(textNewTaxonName
.getText());
276 private void setTaxon(Taxon taxon
) {
279 textNewTaxonName
.setText(taxon
.getName().getTitleCache());
281 selection_SecRef
.setEntity(taxon
.getSec());
284 private void setTaxon(String taxonNameString
) {
285 TaxonNameBase taxonName
= ParseHandler
.parseReferencedName(
286 taxonNameString
, null);
290 private void setTaxon(TaxonNameBase taxonName
) {
291 Reference secundum
= null;
292 if (getParentTreeNode() != null) {
293 if (this.selection_SecRef
.getEntity() != null){
294 secundum
= this.selection_SecRef
.getEntity();
295 } /*else if (getParentTreeNode() instanceof Classification) {
296 secundum = ((Classification) getParentTreeNode())
298 } else if (getParentTreeNode() instanceof TaxonNode) {
299 secundum = ((TaxonNode) getParentTreeNode()).getTaxon()
303 if (taxonName
!= null){
304 taxon
= Taxon
.NewInstance(taxonName
, secundum
);
305 if (!taxonName
.getTitleCache().trim().equals(textNewTaxonName
.getText().trim())){
306 textNewTaxonName
.setText(taxonName
.getTitleCache());
309 textNewTaxonName
.setText(null);
313 private void setOpenInEditor(boolean openInEditor
) {
314 this.openInEditor
= openInEditor
;
317 public TextWithLabelElement
getTextNewTaxonName() {
318 return textNewTaxonName
;
321 public boolean isComplete() {
322 boolean allComplete
= true;
324 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
325 allComplete
&=((TaxonNodeAgentRelationCollectionElement
)element
).isComplete();
327 complete
= allComplete
;
335 * @return the createNew
337 public boolean isCreateNew() {