2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.ui
.section
.classification
;
12 import org
.apache
.commons
.lang
.StringUtils
;
13 import org
.eclipse
.swt
.widgets
.Control
;
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
.ui
.element
.CdmFormFactory
;
26 import eu
.etaxonomy
.taxeditor
.ui
.element
.CheckboxElement
;
27 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
28 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
29 import eu
.etaxonomy
.taxeditor
.ui
.element
.MultilanguageTextElement
;
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_unplaced
;
66 private CheckboxElement checkbox_excluded
;
68 private MultilanguageTextElement multiLanguageTextExcludedNotes
;
70 private boolean openInEditor
;
72 private boolean complete
;
74 private EntitySelectionElement
<TaxonNameBase
> selection_reuseExistingName
;
76 public TaxonNodeDetailElement(CdmFormFactory formFactory
,
77 ICdmFormElement formElement
, boolean isCreateNew
) {
78 super(formFactory
, formElement
);
79 createNew
= isCreateNew
;
84 protected void createControls(ICdmFormElement formElement
,
85 ITaxonTreeNode entity
, int style
) {
86 selection_classification
= formFactory
87 .createSelectionElement(Classification
.class,
88 getConversationHolder(), formElement
, "Classification",
89 null, EntitySelectionElement
.DELETABLE
, style
);
90 selection_parentTaxonNode
= formFactory
91 .createTaxonNodeSelectionElement(getConversationHolder(), formElement
, "Parent", null,
92 EntitySelectionElement
.DELETABLE
, style
);
94 selection_reuseExistingTaxon
= formFactory
95 .createSelectionElement(Taxon
.class,
96 getConversationHolder(), formElement
,
97 "Reuse existing taxon", null,
98 EntitySelectionElement
.DELETABLE
, style
);
100 selection_reuseExistingName
= formFactory
101 .createSelectionElement(TaxonNameBase
.class,
102 getConversationHolder(), formElement
,
103 "Reuse existing name", null,
104 EntitySelectionElement
.DELETABLE
, style
);
105 selection_SecRef
= formFactory
106 .createSelectionElement(Reference
.class,getConversationHolder(), formElement
, "Secundum Reference", null,
107 EntitySelectionElement
.DELETABLE
, style
);
109 textNewTaxonName
= formFactory
.createTextWithLabelElement(formElement
,"New Taxon", "", style
);
110 textNewTaxonName
.setFocus();
112 textNewTaxonName
= formFactory
.createTextWithLabelElement(formElement
,"Taxon", "", style
);
117 setParentTreeNode(entity
);
122 if (entity
instanceof TaxonNode
) {
123 checkbox_unplaced
= formFactory
.createCheckbox(formElement
,
124 "Taxon is unplaced", ((TaxonNode
) entity
).isUnplaced(), style
);
128 selectionNodeAgentRelation
= formFactory
.createTaxonNodeAgentRelationCollectionSection(formElement
, getConversationHolder(), style
);
129 selectionNodeAgentRelation
.setEntity((TaxonNode
)entity
);
130 selectionNodeAgentRelation
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
134 if (entity
instanceof TaxonNode
) {
135 checkbox_excluded
= formFactory
.createCheckbox(formElement
,
136 "Taxon is excluded", ((TaxonNode
) entity
).isExcluded(), style
);
137 multiLanguageTextExcludedNotes
= formFactory
.createMultiLanguageTextElement(formElement
, "Excluded Notes", ((TaxonNode
)entity
).getExcludedNote(), 50, style
);
138 multiLanguageTextExcludedNotes
.setEnabled(((TaxonNode
) entity
).isExcluded());
140 checkbox_openInEditor
= formFactory
.createCheckbox(formElement
,
141 "Open in Editor", true, style
);
142 setOpenInEditor(true);
145 private void preFillTaxonName() {
146 if(getEntity() instanceof TaxonNode
){
147 TaxonNode node
= (TaxonNode
)getEntity();
148 if(node
.getTaxon()!=null){
149 Taxon taxon
= HibernateProxyHelper
.deproxy(node
.getTaxon(), Taxon
.class);
150 if(taxon
.getName()!=null && taxon
.getName().isInstanceOf(NonViralName
.class)){
151 NonViralName
<?
> name
= HibernateProxyHelper
.deproxy(node
.getTaxon().getName(), NonViralName
.class);
153 textNewTaxonName
.setText(name
.getNameCache());
154 } else if( ! name
.isSupraGeneric() && name
.getRank() != null){
155 String taxonName
= "";
156 if(name
.isGenus() || name
.isInfraGeneric()|| name
.isSpeciesAggregate() ){
157 taxonName
= name
.getGenusOrUninomial();
159 else if(name
.isSpecies() || name
.isInfraSpecific() ){
160 taxonName
= CdmUtils
.concat(" ", name
.getGenusOrUninomial(),name
.getSpecificEpithet());
162 if (StringUtils
.isNotBlank(taxonName
)){
163 textNewTaxonName
.setText(taxonName
+ " ");
164 if(textNewTaxonName
.getMainControl() instanceof Text
){
165 Text text
= (Text
)textNewTaxonName
.getMainControl();
166 text
.setSelection(textNewTaxonName
.getText().length());
177 public void handleEvent(Object eventSource
) {
178 if (eventSource
== selection_classification
) {
179 setClassification(selection_classification
.getEntity());
180 } else if (eventSource
== selection_parentTaxonNode
) {
181 setParentTreeNode(selection_parentTaxonNode
.getEntity());
182 } else if (eventSource
== selection_reuseExistingTaxon
) {
183 boolean enabled
= selection_reuseExistingTaxon
.getEntity() == null;
184 selection_reuseExistingName
.setEnabled(enabled
);
186 setTaxon(selection_reuseExistingTaxon
.getEntity());
188 textNewTaxonName
.setText(null);
190 complete
= !textNewTaxonName
.getText().isEmpty();
193 } else if (eventSource
== selection_reuseExistingName
) {
194 boolean enabled
= selection_reuseExistingName
.getEntity() == null;
195 setTaxon(selection_reuseExistingName
.getEntity());
196 selection_reuseExistingTaxon
.setEnabled(enabled
);
197 textNewTaxonName
.setEnabled(enabled
);
199 complete
= !textNewTaxonName
.getText().isEmpty();
200 } else if (eventSource
== textNewTaxonName
) {
201 boolean enabled
= CdmUtils
.isBlank(textNewTaxonName
.getText());
202 selection_reuseExistingTaxon
.setEnabled(enabled
);
203 selection_reuseExistingName
.setEnabled(enabled
);
205 complete
= CdmUtils
.isNotBlank(textNewTaxonName
.getText());
206 } else if (eventSource
== checkbox_excluded
) {
207 boolean isExcluded
= checkbox_excluded
.getSelection();
208 ((TaxonNode
) getEntity()).setExcluded(isExcluded
);
209 multiLanguageTextExcludedNotes
.setEnabled(isExcluded
);
210 } else if (eventSource
== checkbox_unplaced
) {
211 ((TaxonNode
) getEntity()).setUnplaced(checkbox_unplaced
.getSelection());
212 } else if (eventSource
== checkbox_openInEditor
) {
213 setOpenInEditor(checkbox_openInEditor
.getSelection());
214 } else if (eventSource
== selectionNodeAgentRelation
){
215 boolean allComplete
= true;
216 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
217 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 (textNewTaxonName
.getText() == null || !taxonName
.getTitleCache().trim().equals(textNewTaxonName
.getText().trim())){
288 textNewTaxonName
.setText(taxonName
.getTitleCache());
290 // if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
291 // textNewTaxonName.setText(taxonName.getTitleCache());
294 textNewTaxonName
.setText(null);
298 private void setOpenInEditor(boolean openInEditor
) {
299 this.openInEditor
= openInEditor
;
302 public String
getTaxonName() {
303 return textNewTaxonName
.getText();
305 public Taxon
getTaxon() {
309 public Reference
getReference(){
310 return selection_SecRef
.getSelection();
313 public Control
getMainControl(){
314 return textNewTaxonName
.getMainControl();
317 public boolean isComplete() {
318 boolean allComplete
= true;
320 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
321 allComplete
&=((TaxonNodeAgentRelationCollectionElement
)element
).isComplete();
323 complete
= allComplete
;
328 public boolean isCreateNew() {