avoid NPE when reusing taxonname for taxon creation
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / classification / TaxonNodeDetailElement.java
index bd12ab644dc4fadd2c425fedd044a91e3b18725f..3f6849c959d573655b8c136e16561ff3b6a270f9 100644 (file)
@@ -11,6 +11,7 @@
 package eu.etaxonomy.taxeditor.ui.section.classification;
 
 import org.apache.commons.lang.StringUtils;
+import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Text;
 
 import eu.etaxonomy.cdm.common.CdmUtils;
@@ -22,19 +23,17 @@ 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.parser.ParseHandler;
 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.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;
 import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
 
 /**
- * <p>
- * TaxonNodeDetailElement class.
- * </p>
  *
  * @author n.hoffmann
  * @created Sep 27, 2010
@@ -42,6 +41,8 @@ import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
  */
 public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeNode> {
 
+    private boolean createNew = false;
+
        private EntitySelectionElement<Classification> selection_classification;
 
        private Classification classification;
@@ -55,33 +56,28 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
 
        private Taxon taxon;
 
-       private Reference secReference;
-
        private TextWithLabelElement textNewTaxonName;
 
+       private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
+
        private CheckboxElement checkbox_openInEditor;
 
+       private CheckboxElement checkbox_unplaced;
+
+       private CheckboxElement checkbox_excluded;
+
+       private MultilanguageTextElement multiLanguageTextExcludedNotes;
+
        private boolean openInEditor;
 
        private boolean complete;
 
        private EntitySelectionElement<TaxonNameBase> selection_reuseExistingName;
 
-       /**
-        * <p>
-        * Constructor for TaxonNodeDetailElement.
-        * </p>
-        *
-        * @param formFactory
-        *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
-        *            object.
-        * @param formElement
-        *            a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
-        *            object.
-        */
        public TaxonNodeDetailElement(CdmFormFactory formFactory,
-                       ICdmFormElement formElement) {
+                       ICdmFormElement formElement, boolean isCreateNew) {
                super(formFactory, formElement);
+               createNew = isCreateNew;
        }
 
        /** {@inheritDoc} */
@@ -95,9 +91,7 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                selection_parentTaxonNode = formFactory
                                .createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", null,
                                                EntitySelectionElement.DELETABLE, style);
-               selection_SecRef = formFactory
-                               .createSelectionElement(Reference.class,getConversationHolder(), formElement, "Secundum Reference", null,
-                                               EntitySelectionElement.DELETABLE, style);
+
                selection_reuseExistingTaxon = formFactory
                                .createSelectionElement(Taxon.class,
                                                getConversationHolder(), formElement,
@@ -109,26 +103,56 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                                                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);
+
+               }
 
-               textNewTaxonName = formFactory.createTextWithLabelElement(formElement,
-                               "New Taxon", "", style);
-               textNewTaxonName.setFocus();
-               preFillParentTaxonName();
+               if (isCreateNew()){
+            setParentTreeNode(entity);
+        } else{
+            setTreeNode(entity);
+            complete = true;
+        }
+               if (entity instanceof TaxonNode) {
+                   checkbox_unplaced = formFactory.createCheckbox(formElement,
+                           "Taxon is unplaced", ((TaxonNode) entity).isUnplaced(), style);
+               }
 
+               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);
-               setParentTreeNode(entity);
+
+               if (entity instanceof TaxonNode) {
+                   checkbox_excluded = formFactory.createCheckbox(formElement,
+                           "Taxon is excluded", ((TaxonNode) entity).isExcluded(), style);
+                   multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", ((TaxonNode)entity).getExcludedNote(), 50, style);
+                   multiLanguageTextExcludedNotes.setEnabled(((TaxonNode) entity).isExcluded());
+               }
        }
 
-       private void preFillParentTaxonName() {
-        if(getEntity() instanceof TaxonNode){
+       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( ! name.isSupraGeneric() && name.getRank() != null){
+                    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();
@@ -156,44 +180,47 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                        setClassification(selection_classification.getEntity());
                } else if (eventSource == selection_parentTaxonNode) {
                        setParentTreeNode(selection_parentTaxonNode.getEntity());
-               } else if (eventSource == selection_SecRef) {
-                       setSecReference(selection_SecRef.getEntity());
                } else if (eventSource == selection_reuseExistingTaxon) {
                                boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
                                selection_reuseExistingName.setEnabled(enabled);
-                               textNewTaxonName.setEnabled(enabled);
                                if (!enabled){
-                                       setTaxon(selection_reuseExistingTaxon.getEntity());
-                               }
+                    setTaxon(selection_reuseExistingTaxon.getEntity());
+                }else{
+                               textNewTaxonName.setText(null);
+                           }
                                complete = !textNewTaxonName.getText().isEmpty();
 
 
                } else if (eventSource == selection_reuseExistingName) {
-                       boolean enabled = selection_reuseExistingName.getEntity() == null;
+                   boolean enabled = selection_reuseExistingName.getEntity() == null;
+                       setTaxon(selection_reuseExistingName.getEntity());
                        selection_reuseExistingTaxon.setEnabled(enabled);
                        textNewTaxonName.setEnabled(enabled);
 
-                       setTaxon(selection_reuseExistingName.getEntity());
                        complete = !textNewTaxonName.getText().isEmpty();
                } else if (eventSource == textNewTaxonName) {
                        boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
                        selection_reuseExistingTaxon.setEnabled(enabled);
                        selection_reuseExistingName.setEnabled(enabled);
 
-                       setTaxon(textNewTaxonName.getText());
-                       complete = !textNewTaxonName.getText().isEmpty();
+                       complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
+               } else if (eventSource == checkbox_excluded) {
+                   boolean isExcluded = checkbox_excluded.getSelection();
+            ((TaxonNode) getEntity()).setExcluded(isExcluded);
+            multiLanguageTextExcludedNotes.setEnabled(isExcluded);
+               } else if (eventSource == checkbox_unplaced) {
+                   ((TaxonNode) getEntity()).setUnplaced(checkbox_unplaced.getSelection());
                } else if (eventSource == checkbox_openInEditor) {
                        setOpenInEditor(checkbox_openInEditor.getSelection());
+               } else if (eventSource == selectionNodeAgentRelation){
+                  boolean allComplete = true;
+                  for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
+                      allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
+                  }
+                   complete = !isCreateNew() && allComplete;
                }
        }
 
-       /**
-        * <p>
-        * Getter for the field <code>classification</code>.
-        * </p>
-        *
-        * @return the classification
-        */
        public Classification getClassification() {
                return classification;
        }
@@ -202,28 +229,10 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                return openInEditor;
        }
 
-       /**
-        * <p>
-        * Getter for the field <code>parentTreeNode</code>.
-        * </p>
-        *
-        * @return a {@link eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode} object.
-        */
        public ITaxonTreeNode getParentTreeNode() {
                return parentTreeNode;
        }
 
-       /**
-        * <p>
-        * Getter for the field <code>taxon</code>.
-        * </p>
-        *
-        * @return the taxon
-        */
-       public Taxon getTaxon() {
-               return taxon;
-       }
-
        private void setClassification(Classification classification) {
                this.classification = classification;
                setParentTreeNode(classification);
@@ -237,60 +246,88 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                        selection_classification.setEntity(classification);
                        selection_parentTaxonNode.setEntity(null);
                        selection_parentTaxonNode.setClassification(classification);
+                       selection_SecRef.setEntity(classification.getReference());
                } else if (parentTreeNode instanceof TaxonNode) {
-                       classification = (Classification) HibernateProxyHelper
+                       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();
                }
        }
 
-       private void setSecReference(Reference secRef){
-               this.secReference = secRef;
-               setTaxon(textNewTaxonName.getText());
+       private void setTreeNode(ITaxonTreeNode 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());
        }
 
        private void setTaxon(Taxon taxon) {
                this.taxon = taxon;
-               textNewTaxonName.setText(taxon.getName().getTitleCache());
-
-       }
-
-       private void setTaxon(String taxonNameString) {
-               TaxonNameBase taxonName = ParseHandler.parseReferencedName(
-                               taxonNameString, null);
-               setTaxon(taxonName);
+               if (isCreateNew()){
+                   textNewTaxonName.setText(taxon.getName().getTitleCache());
+               }
+               selection_SecRef.setEntity(taxon.getSec());
        }
 
        private void setTaxon(TaxonNameBase taxonName) {
                Reference secundum = null;
                if (getParentTreeNode() != null) {
-                       if (this.secReference != null){
-                               secundum = this.secReference;
-                       } else if (getParentTreeNode() instanceof Classification) {
-                               secundum = ((Classification) getParentTreeNode())
-                                               .getReference();
-                       } else if (getParentTreeNode() instanceof TaxonNode) {
-                               secundum = ((TaxonNode) getParentTreeNode()).getTaxon()
-                                               .getSec();
+                       if (this.selection_SecRef.getEntity() != null){
+                               secundum = this.selection_SecRef.getEntity();
                        }
                }
-               taxon = Taxon.NewInstance(taxonName, secundum);
+               if (taxonName != null){
+                   taxon = Taxon.NewInstance(taxonName, secundum);
+                   if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
+                        textNewTaxonName.setText(taxonName.getTitleCache());
+                   }
+//                 if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
+//                     textNewTaxonName.setText(taxonName.getTitleCache());
+//                 }
+               } else {
+                   textNewTaxonName.setText(null);
+               }
        }
 
        private void setOpenInEditor(boolean openInEditor) {
                this.openInEditor = openInEditor;
        }
 
-       public TextWithLabelElement getTextNewTaxonName() {
-        return textNewTaxonName;
+       public String getTaxonName() {
+        return textNewTaxonName.getText();
+    }
+       public Taxon getTaxon() {
+        return taxon;
     }
 
+       public Reference getReference(){
+           return selection_SecRef.getSelection();
+       }
+
+       public Control getMainControl(){
+           return textNewTaxonName.getMainControl();
+       }
+
        public boolean isComplete() {
+           boolean allComplete = true;
+           if (!isCreateNew()){
+               for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
+                   allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
+               }
+               complete = allComplete;
+           }
                return complete;
        }
 
+    public boolean isCreateNew() {
+        return createNew;
+    }
+
 }