Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / classification / TaxonNodeDetailElement.java
index a7a8261a097d306b630599e9bce40dc0d58c7c4b..ce8e2f78c78eaddb2854a8004bc8f9159e0c814f 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
  * Copyright (C) 2007 EDIT
  * European Distributed Institute of Taxonomy
 
 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;
 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.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.parser.ParseHandler;
-import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
 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
@@ -38,6 +40,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;
@@ -47,44 +51,34 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
        private ITaxonTreeNode parentTreeNode;
 
        private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
+       private EntitySelectionElement<Reference> selection_SecRef;
 
        private Taxon taxon;
 
-       private TextWithLabelElement text_newTaxonName;
+       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;
        }
 
-       /*
-        * (non-Javadoc)
-        *
-        * @see
-        * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#createControls
-        * (eu.etaxonomy.taxeditor.forms.ICdmFormElement,
-        * eu.etaxonomy.cdm.model.common.IAnnotatableEntity, int)
-        */
        /** {@inheritDoc} */
        @Override
        protected void createControls(ICdmFormElement formElement,
@@ -96,34 +90,88 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                selection_parentTaxonNode = formFactory
                                .createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", null,
                                                EntitySelectionElement.DELETABLE, style);
+
                selection_reuseExistingTaxon = formFactory
                                .createSelectionElement(Taxon.class,
                                                getConversationHolder(), formElement,
-                                               "Reuse existing Taxon", null,
-                                               EntitySelectionElement.NOTHING, style);
+                                               "Reuse existing taxon", null,
+                                               EntitySelectionElement.DELETABLE, style);
 
                selection_reuseExistingName = formFactory
                                .createSelectionElement(TaxonNameBase.class,
                                                getConversationHolder(), formElement,
                                                "Reuse existing name", null,
-                                               EntitySelectionElement.ALL, style);
+                                               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);
 
-               text_newTaxonName = formFactory.createTextWithLabelElement(formElement,
-                               "New Taxon", "", style);
+               }
+
+               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();
+
+               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());
+               }
                checkbox_openInEditor = formFactory.createCheckbox(formElement,
-                               "Open in Editor", true, style);
+                       "Open in Editor", true, style);
                setOpenInEditor(true);
-               setParentTreeNode(entity);
        }
 
-       /*
-        * (non-Javadoc)
-        *
-        * @see
-        * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleEvent(java
-        * .lang.Object)
-        */
+       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());
+                               }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
        /** {@inheritDoc} */
        @Override
        public void handleEvent(Object eventSource) {
@@ -132,85 +180,63 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                } else if (eventSource == selection_parentTaxonNode) {
                        setParentTreeNode(selection_parentTaxonNode.getEntity());
                } else if (eventSource == selection_reuseExistingTaxon) {
-                       boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
-                       selection_reuseExistingName.setEnabled(enabled);
-                       text_newTaxonName.setEnabled(enabled);
+                               boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
+                               selection_reuseExistingName.setEnabled(enabled);
+                               if (!enabled){
+                    setTaxon(selection_reuseExistingTaxon.getEntity());
+                }else{
+                               textNewTaxonName.setText(null);
+                           }
+                               complete = !textNewTaxonName.getText().isEmpty();
+
 
-                       setTaxon(selection_reuseExistingTaxon.getEntity());
                } 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);
-                       text_newTaxonName.setEnabled(enabled);
+                       textNewTaxonName.setEnabled(enabled);
 
-                       setTaxon(selection_reuseExistingName.getEntity());
-               } else if (eventSource == text_newTaxonName) {
-                       boolean enabled = CdmUtils.isEmpty(text_newTaxonName.getText());
+                       complete = !textNewTaxonName.getText().isEmpty();
+               } else if (eventSource == textNewTaxonName) {
+                       boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
                        selection_reuseExistingTaxon.setEnabled(enabled);
                        selection_reuseExistingName.setEnabled(enabled);
 
-                       setTaxon(text_newTaxonName.getText());
+                       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;
        }
 
-       /**
-        * <p>
-        * isOpenInEditor
-        * </p>
-        *
-        * @return the openInEditor
-        */
        public boolean isOpenInEditor() {
                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;
-       }
-
-       /**
-        * @param classification
-        *            the classification to set
-        */
        private void setClassification(Classification classification) {
                this.classification = classification;
                setParentTreeNode(classification);
        }
 
-       /**
-        * @param parentTreeNode
-        *            the parentTreeNode to set
-        */
        private void setParentTreeNode(ITaxonTreeNode parentTreeNode) {
                this.parentTreeNode = parentTreeNode;
 
@@ -219,62 +245,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();
                }
        }
 
-       /**
-        * @param reuseExistingTaxon
-        *            the reuseExistingTaxon to set
-        */
-       private void setTaxon(Taxon taxon) {
-               this.taxon = taxon;
+       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(String taxonNameString) {
-               TaxonNameBase taxonName = ParseHandler.parseReferencedName(
-                               taxonNameString, null);
-               setTaxon(taxonName);
+       private void setTaxon(Taxon taxon) {
+               this.taxon = taxon;
+               if (isCreateNew()){
+                   textNewTaxonName.setText(taxon.getName().getTitleCache());
+               }
+               selection_SecRef.setEntity(taxon.getSec());
        }
 
        private void setTaxon(TaxonNameBase taxonName) {
                Reference secundum = null;
                if (getParentTreeNode() != null) {
-                       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);
+               }
        }
 
-       /**
-        * @param openInEditor
-        *            the openInEditor to set
-        */
        private void setOpenInEditor(boolean openInEditor) {
                this.openInEditor = openInEditor;
        }
 
-       /**
-        * <p>
-        * isComplete
-        * </p>
-        *
-        * @return the complete
-        */
+       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;
+    }
+
 }