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.
9 package eu
.etaxonomy
.taxeditor
.ui
.section
.classification
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collection
;
15 import org
.apache
.commons
.lang3
.StringUtils
;
16 import org
.eclipse
.jface
.resource
.JFaceResources
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.graphics
.Font
;
19 import org
.eclipse
.swt
.widgets
.Control
;
20 import org
.eclipse
.swt
.widgets
.Label
;
21 import org
.eclipse
.swt
.widgets
.Text
;
23 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
24 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
25 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
26 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
27 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmPreference
;
28 import eu
.etaxonomy
.cdm
.model
.metadata
.PreferencePredicate
;
29 import eu
.etaxonomy
.cdm
.model
.metadata
.PublishEnum
;
30 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.NamedSource
;
32 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNodeStatus
;
37 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
38 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
39 import eu
.etaxonomy
.taxeditor
.preference
.CdmPreferenceCache
;
40 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
41 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
42 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
43 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
44 import eu
.etaxonomy
.taxeditor
.ui
.combo
.EnumComboElement
;
45 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
46 import eu
.etaxonomy
.taxeditor
.ui
.element
.CheckboxElement
;
47 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
48 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEnableableFormElement
;
49 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
50 import eu
.etaxonomy
.taxeditor
.ui
.element
.MultilanguageTextElement
;
51 import eu
.etaxonomy
.taxeditor
.ui
.element
.SelectionArbitrator
;
52 import eu
.etaxonomy
.taxeditor
.ui
.element
.TextWithLabelElement
;
53 import eu
.etaxonomy
.taxeditor
.ui
.section
.supplemental
.AbstractSingleSourceElement
;
54 import eu
.etaxonomy
.taxeditor
.ui
.selection
.EntitySelectionElement
;
55 import eu
.etaxonomy
.taxeditor
.ui
.selection
.TaxonNodeSelectionElement
;
59 * @created Sep 27, 2010
61 public class TaxonNodeDetailElement
extends AbstractSingleSourceElement
<TaxonNode
> {
63 private static final String STATUS_PLACEMENT
= Messages
.TaxonNodeWizardPage_PLACEMENT_NOTES
;
64 private static final String CLASSIFICATION_STR
= Messages
.TaxonNodeWizardPage_CLASSIFICATION
;
65 private static final String REUSE_EXISTING_TAXON
= Messages
.TaxonNodeWizardPage_REUSE_EXISTING_TAXON
;
66 private static final String REUSE_EXISTING_NAME
= Messages
.TaxonNodeWizardPage_REUSE_EXISTING_NAME
;
67 private static final String SECUNDUM_REFERENCE
= Messages
.TaxonNodeWizardPage_SECUNDUM_REFERENCE
;
69 private static final Font FONT_BOLD
= JFaceResources
.getFontRegistry().getBold(JFaceResources
.DEFAULT_FONT
);
71 private boolean createNew
= false;
72 private Classification classification
;
73 private TaxonNode parentNode
;
75 private boolean complete
;
76 private TaxonNodeStatus status
= null;
77 // private Map<Language, LanguageString> multilanguageTextCache;
79 private TaxonNodeSelectionElement selection_parentTaxonNode
;
80 private EntitySelectionElement
<Taxon
> selection_reuseExistingTaxon
;
81 private EntitySelectionElement
<TaxonName
> selection_reuseExistingName
;
82 private EntitySelectionElement
<Reference
> selection_SecRef
;
84 private TextWithLabelElement textTaxonSec
;
85 private TextWithLabelElement microReference
;
86 private TextWithLabelElement textNewTaxonName
;
87 private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation
;
88 private EnumComboElement
<TaxonNodeStatus
> combo_status
;
89 private CheckboxElement checkbox_publish
;
90 private MultilanguageTextElement multiLanguageTextPlacementNotes
;
91 private String defaultPublish
;
93 public TaxonNodeDetailElement(CdmFormFactory formFactory
,
94 ICdmFormElement formElement
, boolean isCreateNew
) {
95 super(formFactory
, formElement
);
96 createNew
= isCreateNew
;
99 public Map
<Language
, LanguageString
> getMultiLanguageTextExcludedNotes() {
100 return multiLanguageTextPlacementNotes
.getMultilanguageText();
103 public TaxonNodeStatus
getTaxonNodeStatus(){
107 public Reference
getRef() {
108 return getEntity().getCitation();
110 public TaxonName
getExistingName() {
111 return selection_reuseExistingName
.getEntity();
113 public String
getMicroReference() {
114 return microReference
.getText();
118 public void createControls(ICdmFormElement formElement
, TaxonNode entity
, int style
) {
119 // taxon = entity.getTaxon();
121 Label taxonTitle
= new Label(getLayoutComposite(), SWT
.NULL
);
122 taxonTitle
.setText(Messages
.TaxonNodeWizardPage_TAXON_INFORMATION
);
123 taxonTitle
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
124 taxonTitle
.setFont(FONT_BOLD
);
127 textNewTaxonName
= formFactory
.createTextWithLabelElement(formElement
,
128 Messages
.TaxonNodeWizardPage_NEW_TAXON
, "", style
);
129 textNewTaxonName
.setFocus();
130 textNewTaxonName
.setIndent(10);
132 selection_reuseExistingTaxon
= formFactory
133 .createSelectionElement(Taxon
.class,
135 REUSE_EXISTING_TAXON
, null,
136 EntitySelectionElement
.SELECTABLE
, style
);
137 selection_reuseExistingTaxon
.setIndent(10);
138 selection_reuseExistingName
= formFactory
139 .createSelectionElement(TaxonName
.class,
141 REUSE_EXISTING_NAME
, taxon
!= null? taxon
.getName(): null,
142 EntitySelectionElement
.DELETABLE
, style
);
143 selection_reuseExistingName
.setIndent(10);
144 selection_SecRef
= formFactory
145 .createSelectionElement(Reference
.class,
146 formElement
, SECUNDUM_REFERENCE
, taxon
!= null? taxon
.getSec(): null,
147 EntitySelectionElement
.DELETABLE
, style
, 100);
148 selection_SecRef
.setIndent(10);
149 microReference
= formFactory
.createTextWithLabelElement(formElement
, "Secundum Details", "", style
);
151 microReference
.setIndent(10);
152 checkbox_publish
= formFactory
.createCheckbox(formElement
,
153 Messages
.TaxonNodeWizardPage_TAXON_IS_PUBLISH
, true, style
);
155 checkbox_publish
.setEnabled(isCreateNew());
156 checkbox_publish
.setIndent(10);
157 Label spacer
= new Label(getLayoutComposite(), SWT
.NULL
);
158 spacer
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 2));
159 Label nodeTitle
= new Label(getLayoutComposite(), SWT
.NULL
);
160 nodeTitle
.setText("Taxon Node");
161 nodeTitle
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
162 nodeTitle
.setFont(FONT_BOLD
);
163 // LabelElement nodeTitle = formFactory.createLabel(formElement, Messages.TaxonNodeWizardPage_TAXON_INFORMATION);
164 // nodeTitle.setLayout(LayoutConstants.FILL_HORIZONTALLY(2, 1));
165 // nodeTitle.setBackground(getPersistentBackground());
166 // nodeTitle.setBold();
168 selection_parentTaxonNode
= formFactory
169 .createTaxonNodeSelectionElement(formElement
, Messages
.TaxonNodeWizardPage_PARENT
, parentNode
,
170 EntitySelectionElement
.SELECTABLE
, style
, 100);
171 selection_parentTaxonNode
.setIndent(10);
172 selection_parentTaxonNode
.setEnabled(false);
177 taxon
= getEntity().getTaxon();
178 selection_reuseExistingTaxon
= formFactory
179 .createSelectionElement(Taxon
.class, formElement
,
180 Messages
.TaxonNodeWizardPage_TAXON
, taxon
,
181 EntitySelectionElement
.SELECTABLE
, SWT
.NULL
);
183 selection_reuseExistingTaxon
.setIndent(10);
184 textTaxonSec
= formFactory
.createTextWithLabelElement(formElement
,SECUNDUM_REFERENCE
, "", SWT
.NULL
);
185 textTaxonSec
.setIndent(10);
186 microReference
= formFactory
.createTextWithLabelElement(formElement
, "Secundum Details", "", SWT
.NULL
);
187 microReference
.setIndent(10);
189 if (entity
.getTaxon().getSec() != null){
190 textTaxonSec
.setText(taxon
.getSec().getTitleCache());
192 textTaxonSec
.setEnabled(false);
193 microReference
.setEnabled(false);
194 Label spacer
= new Label(getLayoutComposite(), SWT
.NULL
);
195 spacer
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 2));
197 Label nodeTitle
= new Label(getLayoutComposite(), SWT
.NULL
);
198 nodeTitle
.setText("Taxon Node");
199 nodeTitle
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
200 nodeTitle
.setFont(FONT_BOLD
);
202 // LabelElement nodeTitle = formFactory.createLabel(formElement, "Taxon Node");
203 // nodeTitle.setLayout(LayoutConstants.FILL_HORIZONTALLY(2, 1));
204 // nodeTitle.setBackground(getPersistentBackground());
205 // nodeTitle.setBold();
208 if (entity
.getParent().getTaxon() == null){
209 TextWithLabelElement textParent
= formFactory
.createTextWithLabelElement(
210 formElement
,CLASSIFICATION_STR
, entity
.getClassification().getTitleCache(), SWT
.NULL
);
211 textParent
.setEnabled(false);
212 textParent
.setIndent(10);
214 TextWithLabelElement textParent
= formFactory
.createTextWithLabelElement(
215 formElement
, Messages
.TaxonNodeWizardPage_PARENT
, entity
.getParent().getTaxon().getTitleCache(), SWT
.NULL
);
216 textParent
.setEnabled(false);
217 textParent
.setIndent(10);
222 combo_status
= formFactory
.createEnumComboElement(TaxonNodeStatus
.class, formElement
, SWT
.NULL
, true);
224 combo_status
.setSelection(entity
.getStatus());
226 combo_status
.setIndent(10);
229 defaultPublish
= PreferencesUtil
.getStringValue(PreferencePredicate
.DefaultBehaviourForPublishFlag
.getKey());
240 multiLanguageTextPlacementNotes
= formFactory
.createMultiLanguageTextElement(formElement
, STATUS_PLACEMENT
, null, 50, style
);
242 multiLanguageTextPlacementNotes
.setMultilanguageText(entity
.getStatusNote());
243 // multiLanguageTextStatusNotes.setEnabled(entity.getStatus() != null);
245 multiLanguageTextPlacementNotes
.setIndent(10);
246 singleSourceSection
= formFactory
.createOriginalSourceElement(formElement
, entity
, "");
247 if (isCreateNew() || entity
.getSource() == null){
248 NamedSource source
= NamedSource
.NewPrimarySourceInstance(null, null);
249 entity
.setSource(source
);
250 singleSourceSection
.setEntity(source
);
252 singleSourceSection
.setEntity(entity
.getSource());
255 singleSourceSection
.setIndent(10);
257 setSourceLabel(Messages
.TaxonNodeWizardPage_PLACEMENT_SOURCE
);
259 selectionNodeAgentRelation
= formFactory
.createTaxonNodeAgentRelationCollectionSection(formElement
, StoreUtil
.getSectionStyle(TaxonNodeAgentRelationCollectionSection
.class, entity
.getClass().getCanonicalName()));
260 selectionNodeAgentRelation
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
261 selectionNodeAgentRelation
.setEntity(entity
);
262 selectionNodeAgentRelation
.setIndent(10);
263 selectionNodeAgentRelation
.setFont(AbstractUtility
.getFont(Resources
.COLOR_FONT_DEFAULT
));
264 setBackground(getPersistentBackground());
267 private void setDefaultPublish() {
268 if (defaultPublish
!= null && defaultPublish
.equals(PublishEnum
.Publish
.getKey())){
269 checkbox_publish
.setSelection(true);
270 } else if (defaultPublish
!= null && defaultPublish
.equals(PublishEnum
.NotPublish
.getKey())){
271 checkbox_publish
.setSelection(false);
273 if(this.getParentTreeNode() != null && this.getParentTreeNode().isInstanceOf(TaxonNode
.class) && this.getParentTreeNode().getTaxon() != null){
274 checkbox_publish
.setSelection(this.getParentTreeNode().getTaxon().isPublish());
276 checkbox_publish
.setSelection(true);
281 private void preFillTaxonName() {
282 TaxonNode node
= parentNode
;
283 if(node
!= null && node
.getTaxon()!=null){
284 Taxon taxon
= HibernateProxyHelper
.deproxy(node
.getTaxon(), Taxon
.class);
285 if(taxon
.getName()!=null){
286 TaxonName name
= HibernateProxyHelper
.deproxy(node
.getTaxon().getName());
288 textNewTaxonName
.setText(name
.getNameCache());
289 } else if( ! name
.isSupraGeneric() && name
.getRank() != null){
290 String taxonName
= "";
291 if(name
.isGenus() || name
.isInfraGeneric()|| name
.isSpeciesAggregate() ){
292 taxonName
= name
.getGenusOrUninomial();
294 else if(name
.isSpecies() || name
.isInfraSpecific() ){
295 taxonName
= CdmUtils
.concat(" ", name
.getGenusOrUninomial(),name
.getSpecificEpithet());
297 if (StringUtils
.isNotBlank(taxonName
)){
298 textNewTaxonName
.setText(taxonName
+ " ");
299 if(textNewTaxonName
.getMainControl() instanceof Text
){
300 Text text
= (Text
)textNewTaxonName
.getMainControl();
301 text
.setSelection(textNewTaxonName
.getText().length());
310 public void handleEvent(Object eventSource
) {
311 if (eventSource
== selection_parentTaxonNode
) {
312 setParentTreeNode(selection_parentTaxonNode
.getEntity());
313 } else if (eventSource
== selection_reuseExistingTaxon
) {
314 if (selection_reuseExistingTaxon
.getEntity() != null){
315 setTaxon(selection_reuseExistingTaxon
.getEntity());
316 if (getTaxon().getSec()!= null){
317 microReference
.setText(getTaxon().getSecMicroReference());
319 selection_SecRef
.setEntity(getTaxon().getSec());
322 textTaxonSec
.setText((getTaxon().getSec().getTitleCache()));
325 microReference
.setText("");
327 selection_SecRef
.setEntity(null);
330 textTaxonSec
.setText("");
333 if (checkbox_publish
!= null){
334 checkbox_publish
.setSelected(getTaxon().isPublish());
338 selection_SecRef
.setEntity(null);
340 textTaxonSec
.setText(null);
344 boolean enabled
= selection_reuseExistingTaxon
.getEntity() == null;
345 if (selection_reuseExistingName
!= null){
346 selection_reuseExistingName
.setEnabled(enabled
);
348 if (selection_SecRef
!= null){
349 selection_SecRef
.setEnabled(enabled
);
351 if (textNewTaxonName
!= null){
352 textNewTaxonName
.setEnabled(enabled
);
355 } else if (eventSource
== selection_reuseExistingName
) {
356 boolean enabled
= selection_reuseExistingName
.getEntity() == null;
357 setTaxon(HibernateProxyHelper
.deproxy(selection_reuseExistingName
.getEntity()));
358 if (selection_reuseExistingTaxon
!= null){
359 selection_reuseExistingTaxon
.setEnabled(enabled
);
361 textNewTaxonName
.setEnabled(enabled
);
362 if (textNewTaxonName
.getText() != null){
363 complete
= !textNewTaxonName
.getText().isEmpty();
369 if (eventSource
== combo_status
) {
370 status
= combo_status
.getSelection();
371 // if (status == null){
372 // if (multiLanguageTextStatusNotes.getMultilanguageText() != null && !multiLanguageTextStatusNotes.getMultilanguageText().isEmpty()){
373 // multilanguageTextCache = new HashMap<>();
374 // for (LanguageString langString: multiLanguageTextStatusNotes.getMultilanguageText().values()){
375 // multilanguageTextCache.put(langString.getLanguage(), langString);
377 //// multilanguageTextCache = multiLanguageTextStatusNotes.getMultilanguageText();
379 // multiLanguageTextStatusNotes.setMultilanguageText(new HashMap<>());
380 //// multiLanguageTextStatusNotes.refresh();
382 // }else if (multilanguageTextCache != null ){
384 // multiLanguageTextStatusNotes.setMultilanguageText(multilanguageTextCache);
385 // multilanguageTextCache = null;
388 getEntity().setStatus(status
);
389 // if (status == null){
390 // getEntity().getStatusNote().clear();
391 // multiLanguageTextStatusNotes.setMultilanguageText(new HashMap<>());
392 // multiLanguageTextStatusNotes.refresh();
394 // if (multiLanguageTextStatusNotes.getMultilanguageText() != null){
395 // for (LanguageString lang:multiLanguageTextStatusNotes.getMultilanguageText().values()){
396 // getEntity().putStatusNote(lang);
402 // multiLanguageTextStatusNotes.setEnabled(status != null);
407 if (eventSource
== textNewTaxonName
) {
408 boolean enabled
= CdmUtils
.isBlank(textNewTaxonName
.getText());
409 if (selection_reuseExistingTaxon
!= null){
410 selection_reuseExistingTaxon
.setEnabled(enabled
);
412 selection_reuseExistingName
.setEnabled(enabled
);
414 complete
= CdmUtils
.isNotBlank(textNewTaxonName
.getText());
417 if (eventSource
== selectionNodeAgentRelation
){
418 boolean allComplete
= true;
419 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
420 allComplete
&=((TaxonNodeAgentRelationCollectionElement
)element
).isComplete();
422 complete
= !isCreateNew() && allComplete
;
426 public Classification
getClassification() {
427 return classification
;
430 public TaxonNode
getParentTreeNode() {
434 public void setParentTreeNode(TaxonNode parentTreeNode
) {
435 this.parentNode
= parentTreeNode
;
437 if (parentTreeNode
.getTaxon() == null) {
438 classification
= parentTreeNode
.getClassification();
439 if (selection_parentTaxonNode
!= null){
440 selection_parentTaxonNode
.setEntity(classification
.getRootNode());
441 selection_parentTaxonNode
.setClassification(classification
);
442 selection_SecRef
.setEntity(classification
.getReference());
445 classification
= HibernateProxyHelper
446 .deproxy(parentTreeNode
.getClassification());
447 if (selection_parentTaxonNode
!= null){
448 selection_parentTaxonNode
.setEntity(HibernateProxyHelper
.deproxy(parentTreeNode
));
449 selection_parentTaxonNode
.setClassification(classification
);
450 selection_SecRef
.setEntity(parentTreeNode
.getTaxon().getSec());
456 protected void updateControlStates(){
457 Collection
<Object
> except
= new ArrayList
<Object
>();
458 for(ICdmFormElement formElement
:getElements()){
459 if(formElement
instanceof IEnableableFormElement
&& !((IEnableableFormElement
) formElement
).isEnabled()){
460 except
.add(formElement
);
466 enabled
= getEntity() != null && CdmStore
.currentAuthentiationHasPermission(StoreUtil
.getCdmEntity(getEntity()), requiredCrud
);
468 setEnabled(enabled
, except
);
471 private void setTreeNode(TaxonNode treeNode
) {
472 classification
= HibernateProxyHelper
473 .deproxy(treeNode
.getClassification());
475 selection_parentTaxonNode
.setEntity(treeNode
.getParent());
476 selection_parentTaxonNode
.setClassification(treeNode
.getParent().getClassification());
477 selection_SecRef
.setEntity(treeNode
.getTaxon().getSec());
481 private void setTaxon(Taxon taxon
) {
484 getEntity().setTaxon(taxon
);
487 this.set_publish(taxon
.isPublish());
489 textNewTaxonName
.setText(taxon
.getName().getTitleCache());
490 if (taxon
.getSec() != null){
491 selection_SecRef
.setEntity(taxon
.getSec());
494 if (taxon
.getSec() != null) {
495 this.textTaxonSec
.setText(taxon
.getSec().getTitleCache());
500 private void setTaxon(TaxonName taxonName
) {
501 Reference secundum
= null;
502 if (getParentTreeNode() != null) {
503 if (this.selection_SecRef
.getEntity() != null){
504 secundum
= this.selection_SecRef
.getEntity();
507 if (taxonName
!= null){
508 textNewTaxonName
.setText(taxonName
.getTitleCache());
510 textNewTaxonName
.setText("");
511 if (selection_reuseExistingTaxon
!= null){
512 selection_reuseExistingTaxon
.setEnabled(true);
514 textNewTaxonName
.setEnabled(true);
520 public String
getTaxonName() {
521 return textNewTaxonName
.getText();
523 public Taxon
getTaxon() {
527 public Reference
getSecReference(){
528 return selection_SecRef
.getSelection();
531 public Control
getMainControl(){
532 return textNewTaxonName
.getMainControl();
535 public boolean isComplete() {
536 boolean allComplete
= true;
538 for (ICdmFormElement element
: selectionNodeAgentRelation
.getElements()){
539 allComplete
&=((TaxonNodeAgentRelationCollectionElement
)element
).isComplete();
541 complete
= allComplete
;
546 public boolean isCreateNew() {
550 public boolean is_publish() {
551 boolean isPublishPreference
= true;
552 CdmPreference defaultPublish
= CdmPreferenceCache
.instance().get(PreferencePredicate
.DefaultBehaviourForPublishFlag
.getKey());
553 if (defaultPublish
!= null && defaultPublish
.getValue()!= null && defaultPublish
.getValue().equals(PublishEnum
.NotPublish
.getKey())){
554 isPublishPreference
= false;
556 return checkbox_publish
!=null?checkbox_publish
.getSelection():isPublishPreference
;
559 public void set_publish(boolean publish
) {
560 if (checkbox_publish
!= null){
561 this.checkbox_publish
.setSelection(publish
);
566 * Updates all widgets to display the latest data
569 protected void updateContent() {
572 createControls(this, parentNode
, SWT
.NONE
);
574 createControls(this, getEntity(), SWT
.NONE
);
576 updateControlStates();
580 public SelectionArbitrator
getSelectionArbitrator() {
581 // TODO Auto-generated method stub