fix #9013: fix NPE when creating root taxon and NPE when changing taxon in taxonnode...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / classification / TaxonNodeDetailElement.java
index 7da137dc9f612283ebd870576359dd36b61fafd1..245d3440dc7777bf5515f015e47a3b4d2dc0773c 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
  * Copyright (C) 2007 EDIT
  * European Distributed Institute of Taxonomy
 
 package eu.etaxonomy.taxeditor.ui.section.classification;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Map;
+
 import org.apache.commons.lang.StringUtils;
+import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Text;
 
 import eu.etaxonomy.cdm.common.CdmUtils;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
-import eu.etaxonomy.cdm.model.name.NonViralName;
-import eu.etaxonomy.cdm.model.name.TaxonNameBase;
+import eu.etaxonomy.cdm.model.common.Language;
+import eu.etaxonomy.cdm.model.common.LanguageString;
+import eu.etaxonomy.cdm.model.metadata.CdmPreference;
+import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
+import eu.etaxonomy.cdm.model.metadata.PublishEnum;
+import eu.etaxonomy.cdm.model.name.TaxonName;
 import eu.etaxonomy.cdm.model.reference.Reference;
 import eu.etaxonomy.cdm.model.taxon.Classification;
-import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+import eu.etaxonomy.taxeditor.preference.CdmPreferenceCache;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.store.CdmStore;
+import eu.etaxonomy.taxeditor.store.StoreUtil;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
+import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
+import eu.etaxonomy.taxeditor.ui.element.MultilanguageTextElement;
 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
@@ -38,35 +51,45 @@ import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
  * @created Sep 27, 2010
  * @version 1.0
  */
-public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeNode> {
+public class TaxonNodeDetailElement extends AbstractCdmDetailElement<TaxonNode> {
 
     private boolean createNew = false;
-
-       private EntitySelectionElement<Classification> selection_classification;
-
        private Classification classification;
+       private TaxonNode parentNode;
+       private Taxon taxon;
 
        private TaxonNodeSelectionElement selection_parentTaxonNode;
+       private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
+       private EntitySelectionElement<TaxonName> selection_reuseExistingName;
 
-       private ITaxonTreeNode parentTreeNode;
+    private EntitySelectionElement<Reference> selection_SecRef;
+       private EntitySelectionElement<Reference> selection_Ref;
 
-       private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
-       private EntitySelectionElement<Reference> selection_SecRef;
 
-       private Taxon taxon;
+    TextWithLabelElement textTaxonSec;
+
+       private TextWithLabelElement microReference;
+
 
-       private TextWithLabelElement textNewTaxonName;
+    private TextWithLabelElement textNewTaxonName;
 
        private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
 
+       private CheckboxElement checkbox_unplaced;
+       private CheckboxElement checkbox_excluded;
+       private CheckboxElement checkbox_publish;
+       private CheckboxElement checkbox_placementDoubtful;
 
-       private CheckboxElement checkbox_openInEditor;
+    private MultilanguageTextElement multiLanguageTextExcludedNotes;
 
-       private boolean openInEditor;
 
        private boolean complete;
 
-       private EntitySelectionElement<TaxonNameBase> selection_reuseExistingName;
+       private boolean excluded = false;
+       private boolean unplaced = false;
+       private boolean placementDoubtful = false;
+
+
 
        public TaxonNodeDetailElement(CdmFormFactory formFactory,
                        ICdmFormElement formElement, boolean isCreateNew) {
@@ -74,134 +97,304 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                createNew = isCreateNew;
        }
 
+
+       public Map<Language, LanguageString> getMultiLanguageTextExcludedNotes() {
+        return multiLanguageTextExcludedNotes.getMultilanguageText();
+    }
+
+
+
+    public boolean isExcluded() {
+        return excluded;
+    }
+
+
+
+    public boolean isUnplaced() {
+        return unplaced;
+    }
+
+
+
+    public boolean isPlacementDoubtful() {
+        return placementDoubtful;
+    }
+
+    public Reference getRef() {
+        return selection_Ref.getEntity();
+    }
+    public TaxonName getExistingName() {
+        return selection_reuseExistingName.getEntity();
+    }
+    public String getMicroReference() {
+        return microReference.getText();
+    }
+
        /** {@inheritDoc} */
        @Override
        protected void createControls(ICdmFormElement formElement,
-               ITaxonTreeNode entity, int style) {
-               selection_classification = formFactory
-                               .createSelectionElement(Classification.class,
-                                               getConversationHolder(), formElement, "Classification",
-                                               null, EntitySelectionElement.DELETABLE, style);
-               selection_parentTaxonNode = formFactory
-                               .createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", null,
-                                               EntitySelectionElement.DELETABLE, style);
-
-               selection_reuseExistingTaxon = formFactory
-                               .createSelectionElement(Taxon.class,
-                                               getConversationHolder(), formElement,
-                                               "Reuse existing taxon", null,
-                                               EntitySelectionElement.DELETABLE, style);
-
-               selection_reuseExistingName = formFactory
-                               .createSelectionElement(TaxonNameBase.class,
-                                               getConversationHolder(), formElement,
-                                               "Reuse existing name", null,
-                                               EntitySelectionElement.DELETABLE, style);
-               selection_SecRef = formFactory
-                .createSelectionElement(Reference.class,getConversationHolder(), formElement, "Secundum Reference", null,
-                        EntitySelectionElement.DELETABLE, style);
-               if (isCreateNew()){
-                   textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"New Taxon", "", style);
-                   textNewTaxonName.setFocus();
-               } else{
-                   textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"Taxon", "", style);
+               TaxonNode entity, int style) {
+//         taxon = entity.getTaxon();
+
+           if (isCreateNew()){
+//             setParentTreeNode(entity);
+               selection_parentTaxonNode = formFactory
+                               .createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", parentNode,
+                                               EntitySelectionElement.DELETABLE, style, 100);
+
+               selection_Ref = formFactory
+                    .createSelectionElement(Reference.class,
+                            formElement, "Parent Child Relation Source", null,
+                            EntitySelectionElement.DELETABLE, style, 100);
+
+            microReference = formFactory.createTextWithLabelElement(formElement,"Detail", "", style);
+            microReference.setText(parentNode.getMicroReference());
+
+               selection_reuseExistingTaxon = formFactory
+                               .createSelectionElement(Taxon.class,//getConversationHolder(),
+                                       formElement,
+                                               "Reuse existing taxon", null,
+                                               EntitySelectionElement.DELETABLE, style);
+
+               selection_reuseExistingName = formFactory
+                               .createSelectionElement(TaxonName.class,//getConversationHolder(),
+                                       formElement,
+                                               "Reuse existing name", taxon != null? taxon.getName(): null,
+                                               EntitySelectionElement.DELETABLE, style);
+               selection_SecRef = formFactory
+                    .createSelectionElement(Reference.class,//getConversationHolder(),
+                            formElement, "Secundum Reference", taxon != null? taxon.getSec(): null,
+                            EntitySelectionElement.DELETABLE, style, 100);
+
+               if (isCreateNew()){
+                   textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"New Taxon", "", style);
+                   textNewTaxonName.setFocus();
+               } else{
+                   textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"Taxon", "", style);
+
+               }
+           }else{
+               taxon = getEntity().getTaxon();
+               if (entity.getParent().getTaxon() == null){
+                   TextWithLabelElement textParent = formFactory.createTextWithLabelElement(formElement,"Classification", entity.getClassification().getTitleCache(), style);
+                textParent.setEnabled(false);
+               }else{
+                   TextWithLabelElement textParent = formFactory.createTextWithLabelElement(formElement,"Parent", entity.getParent().getTaxon().getTitleCache(), style);
+                   textParent.setEnabled(false);
+               }
 
-               }
+               selection_Ref = formFactory
+                    .createSelectionElement(Reference.class,
+                            formElement, "Parent Child Relation Source", null,
+                            EntitySelectionElement.DELETABLE, style);
 
+               selection_reuseExistingTaxon = formFactory
+                  .createSelectionElement(Taxon.class,//getConversationHolder(),
+                          formElement,
+                          "Taxon", taxon,
+                          EntitySelectionElement.DELETABLE, style);
+
+               textTaxonSec = formFactory.createTextWithLabelElement(formElement,"Secundum Reference", "", style);
+            if (entity.getTaxon().getSec() != null){
+                textTaxonSec.setText(taxon.getSec().getTitleCache());
+            }
+            textTaxonSec.setEnabled(false);
+
+
+
+           }
+
+
+        if (!isCreateNew()){
+            if (entity.getReference() != null){
+                selection_Ref.setEntity(entity.getReference());
+            }
+
+        }
+
+
+        Taxon tax = null;
+        if (entity.getTaxon() != null){
+            tax = entity.getTaxon();
+        }
+        checkbox_publish = formFactory.createCheckbox(formElement,
+                "Taxon is published", tax != null? tax.isPublish(): true, style);
+
+
+        checkbox_publish.setEnabled(isCreateNew());
+
+        checkbox_placementDoubtful = formFactory.createCheckbox(formElement,
+                "Placement is doubtful", tax!= null? entity.isDoubtful(): false, style);
 
-               if (isCreateNew()){
-            setParentTreeNode(entity);
 
-        } else{
+
+               if(isCreateNew()){
+
+
+                  String defaultPublish = PreferencesUtil.getStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey());
+               if (defaultPublish != null && defaultPublish.equals(PublishEnum.Publish.getKey())){
+                   checkbox_publish.setSelection(true);
+               } else if (defaultPublish != null && defaultPublish.equals(PublishEnum.NotPublish.getKey())){
+                   checkbox_publish.setSelection(false);
+               }else{
+                   if(this.getParentTreeNode() != null &&  this.getParentTreeNode().isInstanceOf(TaxonNode.class) && this.getParentTreeNode().getTaxon() != null){
+                       checkbox_publish.setSelection(this.getParentTreeNode().getTaxon().isPublish());
+                   }else{
+                       checkbox_publish.setSelection(true);
+                   }
+               }
+
+           } else{
             setTreeNode(entity);
             complete = true;
         }
+
+               checkbox_unplaced = formFactory.createCheckbox(formElement,
+                           "Taxon is unplaced", entity.isUnplaced(), style);
+
+               if (isCreateNew()){
+                   preFillTaxonName();
+               }
+
+           checkbox_excluded = formFactory.createCheckbox(formElement,
+                   "Taxon is excluded", entity.isExcluded(), style);
+           multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", entity.getExcludedNote(), 50, style);
+           multiLanguageTextExcludedNotes.setEnabled(entity.isExcluded());
+
                if (!isCreateNew()){
-            selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), style);
-            selectionNodeAgentRelation.setEntity((TaxonNode)entity);
-            selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
-        }
-               preFillTaxonName();
-               checkbox_openInEditor = formFactory.createCheckbox(formElement,
-                               "Open in Editor", true, style);
-               setOpenInEditor(true);
+                   selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), StoreUtil.getSectionStyle(TaxonNodeAgentRelationCollectionSection.class, entity.getClass().getCanonicalName()));
+                   selectionNodeAgentRelation.setEntity(entity);
+                   selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
+               }
 
        }
 
        private void preFillTaxonName() {
-        if(getEntity() instanceof TaxonNode ){
-            TaxonNode node = (TaxonNode)getEntity();
-            if(node.getTaxon()!=null){
-                Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
-                if(taxon.getName()!=null && taxon.getName().isInstanceOf(NonViralName.class)){
-                    NonViralName<?> name = HibernateProxyHelper.deproxy(node.getTaxon().getName(), NonViralName.class);
-                    if (!isCreateNew()){
-                        textNewTaxonName.setText(name.getNameCache());
-                    } else if( ! name.isSupraGeneric() && name.getRank() != null){
-                        String taxonName = "";
-                        if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
-                            taxonName = name.getGenusOrUninomial();
-                        }
-                        else if(name.isSpecies() || name.isInfraSpecific() ){
-                            taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
-                        }
-                        if (StringUtils.isNotBlank(taxonName)){
-                               textNewTaxonName.setText(taxonName + " ");
-                               if(textNewTaxonName.getMainControl() instanceof Text){
-                                       Text text = (Text)textNewTaxonName.getMainControl();
-                                       text.setSelection(textNewTaxonName.getText().length());
-                               }
-                        }
+        TaxonNode node = parentNode;
+        if(node.getTaxon()!=null){
+            Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
+            if(taxon.getName()!=null){
+                TaxonName name = HibernateProxyHelper.deproxy(node.getTaxon().getName());
+                if (!isCreateNew()){
+                    textNewTaxonName.setText(name.getNameCache());
+                } else if( ! name.isSupraGeneric() && name.getRank() != null){
+                    String taxonName = "";
+                    if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
+                        taxonName = name.getGenusOrUninomial();
+                    }
+                    else if(name.isSpecies() || name.isInfraSpecific() ){
+                        taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
+                    }
+                    if (StringUtils.isNotBlank(taxonName)){
+                       textNewTaxonName.setText(taxonName + " ");
+                       if(textNewTaxonName.getMainControl() instanceof Text){
+                               Text text = (Text)textNewTaxonName.getMainControl();
+                               text.setSelection(textNewTaxonName.getText().length());
+                       }
                     }
-
                 }
-
             }
-
         }
+
     }
 
        /** {@inheritDoc} */
        @Override
        public void handleEvent(Object eventSource) {
-               if (eventSource == selection_classification) {
-                       setClassification(selection_classification.getEntity());
-               } else if (eventSource == selection_parentTaxonNode) {
-                       setParentTreeNode(selection_parentTaxonNode.getEntity());
+               if (eventSource == selection_parentTaxonNode) {
+                   setParentTreeNode(selection_parentTaxonNode.getEntity());
                } else if (eventSource == selection_reuseExistingTaxon) {
-                               boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
-                               selection_reuseExistingName.setEnabled(enabled);
-                               if (!enabled){
-                    setTaxon(selection_reuseExistingTaxon.getEntity());
-                }else{
-                               textNewTaxonName.setText(null);
-                           }
-                               complete = !textNewTaxonName.getText().isEmpty();
-                               
-
+                   if (selection_reuseExistingTaxon.getEntity() != null){
+                       setTaxon(selection_reuseExistingTaxon.getEntity());
+                       if (getTaxon().getSec()!= null){
+                           if (isCreateNew()){
+                              selection_SecRef.setEntity(getTaxon().getSec());
+                           }else{
+                              textTaxonSec.setText((getTaxon().getSec().getTitleCache()));
+                           }
+                       }else{
+                           if (isCreateNew()){
+                              selection_SecRef.setEntity(null);
+                           }else{
+                              textTaxonSec.setText("");
+                           }
+                       }
+                       checkbox_publish.setSelected(getTaxon().isPublish());
+            }
+                   boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
+                   if (selection_reuseExistingName != null){
+                       selection_reuseExistingName.setEnabled(enabled);
+            }
+                   if (textNewTaxonName != null){
+                       textNewTaxonName.setEnabled(enabled);
+                   }
+                       complete = true;
                } else if (eventSource == selection_reuseExistingName) {
                    boolean enabled = selection_reuseExistingName.getEntity() == null;
-                       setTaxon(selection_reuseExistingName.getEntity());
-                       selection_reuseExistingTaxon.setEnabled(enabled);
+                       setTaxon(HibernateProxyHelper.deproxy(selection_reuseExistingName.getEntity()));
+                       if (selection_reuseExistingTaxon != null){
+                           selection_reuseExistingTaxon.setEnabled(enabled);
+                       }
                        textNewTaxonName.setEnabled(enabled);
+                       complete = !textNewTaxonName.getText().isEmpty();
+               }
 
-                       setTaxon(selection_reuseExistingName.getEntity());
+        if (eventSource == selection_Ref) {
+            if(!isCreateNew()){
+                getEntity().setReference(selection_Ref.getEntity());
+            }
+        }else if (eventSource == selection_SecRef) {
+            if (taxon != null){
+                taxon.setSec(selection_SecRef.getEntity());
+            }
+        }else if (eventSource == microReference) {
+            if(!isCreateNew()){
+                getEntity().setMicroReference(microReference.getText());
+            }
+        }  else if (eventSource == checkbox_publish) {
+            if (taxon != null){
+                taxon.setPublish(checkbox_publish.getSelection());
+            }
+        }
 
-                       complete = !textNewTaxonName.getText().isEmpty();
-               } else if (eventSource == textNewTaxonName) {
-                       boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
-                       selection_reuseExistingTaxon.setEnabled(enabled);
-                       selection_reuseExistingName.setEnabled(enabled);
+               if (eventSource == checkbox_excluded) {
+            excluded = checkbox_excluded.getSelection();
+            if (!isCreateNew()){
+                getEntity().setExcluded(excluded);
+            }
 
-                       complete = !textNewTaxonName.getText().isEmpty();
-               } else if (eventSource == checkbox_openInEditor) {
-                       setOpenInEditor(checkbox_openInEditor.getSelection());
-               } else if (eventSource == selectionNodeAgentRelation){
+            multiLanguageTextExcludedNotes.setEnabled(excluded);
+        }
+               if (eventSource == checkbox_unplaced) {
+            unplaced = checkbox_unplaced.getSelection();
+            if (!isCreateNew()){
+                getEntity().setUnplaced(unplaced);
+            }
+
+        }
+
+               if (eventSource == checkbox_placementDoubtful) {
+                   placementDoubtful = checkbox_placementDoubtful.getSelection();
+                   if (!isCreateNew()){
+                       getEntity().setDoubtful(placementDoubtful);
+                   }
+
+        }
+               if (eventSource == textNewTaxonName) {
+            boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
+            if (selection_reuseExistingTaxon != null){
+                selection_reuseExistingTaxon.setEnabled(enabled);
+            }
+            selection_reuseExistingName.setEnabled(enabled);
+
+            complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
+        }
+
+               if (eventSource == selectionNodeAgentRelation){
                   boolean allComplete = true;
                   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
                       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
                   }
-
                    complete = !isCreateNew() && allComplete;
                }
        }
@@ -210,58 +403,81 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                return classification;
        }
 
-       public boolean isOpenInEditor() {
-               return openInEditor;
-       }
 
-       public ITaxonTreeNode getParentTreeNode() {
-               return parentTreeNode;
-       }
 
-       private void setClassification(Classification classification) {
-               this.classification = classification;
-               setParentTreeNode(classification);
+       public TaxonNode getParentTreeNode() {
+               return parentNode;
        }
 
-       private void setParentTreeNode(ITaxonTreeNode parentTreeNode) {
-               this.parentTreeNode = parentTreeNode;
-
-               if (parentTreeNode instanceof Classification) {
-                       classification = (Classification) parentTreeNode;
-                       selection_classification.setEntity(classification);
-                       selection_parentTaxonNode.setEntity(null);
-                       selection_parentTaxonNode.setClassification(classification);
-                       selection_SecRef.setEntity(classification.getReference());
-               } else if (parentTreeNode instanceof TaxonNode) {
+       public void setParentTreeNode(TaxonNode parentTreeNode) {
+               this.parentNode = parentTreeNode;
+               updateContent();
+               if (parentTreeNode.getTaxon() == null) {
+                       classification = parentTreeNode.getClassification();
+                       if (selection_parentTaxonNode != null){
+                       selection_parentTaxonNode.setEntity(classification.getRootNode());
+                       selection_parentTaxonNode.setClassification(classification);
+                       selection_SecRef.setEntity(classification.getReference());
+                       }
+               } else  {
                        classification = HibernateProxyHelper
-                                       .deproxy(((TaxonNode) parentTreeNode).getClassification());
-                       selection_classification.setEntity(classification);
-                       selection_parentTaxonNode.setEntity((TaxonNode) parentTreeNode);
-                       selection_parentTaxonNode.setClassification(classification);
-                       selection_SecRef.setEntity(((TaxonNode)parentTreeNode).getTaxon().getSec());
-               } else if(parentTreeNode == null){
-                       this.parentTreeNode = selection_classification.getEntity();
+                                       .deproxy(parentTreeNode.getClassification());
+                       if (selection_parentTaxonNode != null){
+                           selection_parentTaxonNode.setEntity(HibernateProxyHelper.deproxy(parentTreeNode));
+                           selection_parentTaxonNode.setClassification(classification);
+                   selection_SecRef.setEntity(parentTreeNode.getTaxon().getSec());
+                       }
+
+
                }
        }
+       @Override
+       protected void updateControlStates(){
+        Collection<Object> except = new ArrayList<Object>();
+        for(ICdmFormElement formElement:getElements()){
+            if(formElement instanceof IEnableableFormElement && !((IEnableableFormElement) formElement).isEnabled()){
+                except.add(formElement);
+            }
+        }
+        if (isCreateNew() ){
+            enabled = true;
+        }else{
+            enabled = getEntity() != null && CdmStore.currentAuthentiationHasPermission(StoreUtil.getCdmEntity(getEntity()), requiredCrud);
+        }
+        setEnabled(enabled, except);
+    }
 
-       private void setTreeNode(ITaxonTreeNode treeNode) {
+       private void setTreeNode(TaxonNode treeNode) {
         classification = HibernateProxyHelper
-                    .deproxy(((TaxonNode) treeNode).getClassification());
-        selection_classification.setEntity(classification);
-        selection_parentTaxonNode.setEntity(((TaxonNode) treeNode).getParent());
-        selection_parentTaxonNode.setClassification(((TaxonNode) treeNode).getParent().getClassification());
-        selection_SecRef.setEntity(((TaxonNode)treeNode).getTaxon().getSec());
+                    .deproxy(treeNode.getClassification());
+        if (isCreateNew()){
+           selection_parentTaxonNode.setEntity(treeNode.getParent());
+           selection_parentTaxonNode.setClassification(treeNode.getParent().getClassification());
+           selection_SecRef.setEntity(treeNode.getTaxon().getSec());
+        }
        }
 
        private void setTaxon(Taxon taxon) {
                this.taxon = taxon;
+               if (!isCreateNew()){
+                   getEntity().setTaxon(taxon);
+               }
+               this.set_publish(taxon.isPublish());
                if (isCreateNew()){
                    textNewTaxonName.setText(taxon.getName().getTitleCache());
+                   if (taxon.getSec() != null){
+                       selection_SecRef.setEntity(taxon.getSec());
+                   }
+               }else{
+                   if (taxon.getSec() != null) {
+                this.textTaxonSec.setText(taxon.getSec().getTitleCache());
+            }
                }
-               selection_SecRef.setEntity(taxon.getSec());
+
+
        }
 
-       private void setTaxon(TaxonNameBase taxonName) {
+       private void setTaxon(TaxonName taxonName) {
                Reference secundum = null;
                if (getParentTreeNode() != null) {
                        if (this.selection_SecRef.getEntity() != null){
@@ -270,26 +486,26 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                }
                if (taxonName != null){
                    taxon = Taxon.NewInstance(taxonName, secundum);
-                   if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
-                       textNewTaxonName.setText(taxonName.getTitleCache());
+                   if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
+                        textNewTaxonName.setText(taxonName.getTitleCache());
                    }
+                   if (!isCreateNew()){
+                       getEntity().setTaxon(taxon);
+                   }
+
                } else {
                    textNewTaxonName.setText(null);
                }
        }
 
-       private void setOpenInEditor(boolean openInEditor) {
-               this.openInEditor = openInEditor;
-       }
-
        public String getTaxonName() {
         return textNewTaxonName.getText();
     }
        public Taxon getTaxon() {
-        return selection_reuseExistingTaxon.getEntity();
+        return taxon;
     }
 
-       public Reference getReference(){
+       public Reference getSecReference(){
            return selection_SecRef.getSelection();
        }
 
@@ -305,16 +521,39 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                }
                complete = allComplete;
            }
-
-
                return complete;
        }
 
-    /**
-     * @return the createNew
-     */
     public boolean isCreateNew() {
         return createNew;
     }
 
+    public boolean is_publish() {
+        boolean isPublishPreference = true;
+        CdmPreference defaultPublish = CdmPreferenceCache.instance().get(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey());
+        if (defaultPublish != null && defaultPublish.getValue()!= null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
+            isPublishPreference = false;
+        }
+        return checkbox_publish!=null?checkbox_publish.getSelection():isPublishPreference;
+    }
+
+    public void set_publish(boolean publish) {
+        this.checkbox_publish.setSelection(publish);
+    }
+
+    /**
+     * Updates all widgets to display the latest data
+     */
+    @Override
+    protected void updateContent() {
+        removeElements();
+        if (isCreateNew()){
+            createControls(this, parentNode, SWT.NONE);
+        }else{
+            createControls(this, getEntity(), SWT.NONE);
+        }
+        updateControlStates();
+    }
+
+
 }