Merge branch 'develop' of https://dev.e-taxonomy.eu/git/taxeditor into actualBranch
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / classification / TaxonNodeDetailElement.java
index f0472fffa3562d821ff478d512d37f7bbc5ef081..bd12ab644dc4fadd2c425fedd044a91e3b18725f 100644 (file)
@@ -1,60 +1,63 @@
 // $Id$
 /**
  * Copyright (C) 2007 EDIT
- * European Distributed Institute of Taxonomy 
+ * European Distributed Institute of Taxonomy
  * http://www.e-taxonomy.eu
- * 
+ *
  * The contents of this file are subject to the Mozilla Public License Version 1.1
  * See LICENSE.TXT at the top of this package for the full license terms.
  */
 
 package eu.etaxonomy.taxeditor.ui.section.classification;
 
+import org.apache.commons.lang.StringUtils;
+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.ITreeNode;
+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.CdmPropertyChangeEvent;
 import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
-import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory.SelectionType;
 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
-import eu.etaxonomy.taxeditor.ui.selection.ClassificationSelectionElement;
-import eu.etaxonomy.taxeditor.ui.selection.NameSelectionElement;
+import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
 import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
-import eu.etaxonomy.taxeditor.ui.selection.TaxonSelectionElement;
 
 /**
  * <p>
  * TaxonNodeDetailElement class.
  * </p>
- * 
+ *
  * @author n.hoffmann
  * @created Sep 27, 2010
  * @version 1.0
  */
-public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITreeNode> {
+public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeNode> {
 
-       private ClassificationSelectionElement selection_classification;
+       private EntitySelectionElement<Classification> selection_classification;
 
        private Classification classification;
 
        private TaxonNodeSelectionElement selection_parentTaxonNode;
 
-       private ITreeNode parentTreeNode;
+       private ITaxonTreeNode parentTreeNode;
 
-       private TaxonSelectionElement selection_reuseExistingTaxon;
+       private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
+       private EntitySelectionElement<Reference> selection_SecRef;
 
        private Taxon taxon;
 
-       private TextWithLabelElement text_newTaxonName;
+       private Reference secReference;
+
+       private TextWithLabelElement textNewTaxonName;
 
        private CheckboxElement checkbox_openInEditor;
 
@@ -62,13 +65,13 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITreeNode>
 
        private boolean complete;
 
-       private NameSelectionElement selection_reuseExistingName;
+       private EntitySelectionElement<TaxonNameBase> selection_reuseExistingName;
 
        /**
         * <p>
         * Constructor for TaxonNodeDetailElement.
         * </p>
-        * 
+        *
         * @param formFactory
         *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
         *            object.
@@ -81,40 +84,36 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITreeNode>
                super(formFactory, formElement);
        }
 
-       /*
-        * (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,
-                       ITreeNode entity, int style) {
-               selection_classification = (ClassificationSelectionElement) formFactory
-                               .createSelectionElement(SelectionType.CLASSIFICATION,
+               ITaxonTreeNode entity, int style) {
+               selection_classification = formFactory
+                               .createSelectionElement(Classification.class,
                                                getConversationHolder(), formElement, "Classification",
-                                               null, ClassificationSelectionElement.DEFAULT, style);
-               selection_parentTaxonNode = (TaxonNodeSelectionElement) formFactory
-                               .createSelectionElement(SelectionType.TAXON_NODE,
-                                               getConversationHolder(), formElement, "Parent", null,
-                                               TaxonNodeSelectionElement.DEFAULT, style);
-               selection_reuseExistingTaxon = (TaxonSelectionElement) formFactory
-                               .createSelectionElement(SelectionType.TAXON,
+                                               null, EntitySelectionElement.DELETABLE, style);
+               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,
-                                               "Reuse existing Taxon", null,
-                                               TaxonSelectionElement.DELETABLE, style);
+                                               "Reuse existing taxon", null,
+                                               EntitySelectionElement.DELETABLE, style);
 
-               selection_reuseExistingName = (NameSelectionElement) formFactory
-                               .createSelectionElement(SelectionType.NAME,
+               selection_reuseExistingName = formFactory
+                               .createSelectionElement(TaxonNameBase.class,
                                                getConversationHolder(), formElement,
                                                "Reuse existing name", null,
-                                               NameSelectionElement.DEFAULT, style);
+                                               EntitySelectionElement.DELETABLE, style);
 
-               text_newTaxonName = formFactory.createTextWithLabelElement(formElement,
+               textNewTaxonName = formFactory.createTextWithLabelElement(formElement,
                                "New Taxon", "", style);
+               textNewTaxonName.setFocus();
+               preFillParentTaxonName();
 
                checkbox_openInEditor = formFactory.createCheckbox(formElement,
                                "Open in Editor", true, style);
@@ -122,13 +121,34 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITreeNode>
                setParentTreeNode(entity);
        }
 
-       /*
-        * (non-Javadoc)
-        * 
-        * @see
-        * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleEvent(java
-        * .lang.Object)
-        */
+       private void preFillParentTaxonName() {
+        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){
+                        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) {
@@ -136,24 +156,32 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITreeNode>
                        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);
-                       text_newTaxonName.setEnabled(enabled);
+                               boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
+                               selection_reuseExistingName.setEnabled(enabled);
+                               textNewTaxonName.setEnabled(enabled);
+                               if (!enabled){
+                                       setTaxon(selection_reuseExistingTaxon.getEntity());
+                               }
+                               complete = !textNewTaxonName.getText().isEmpty();
+
 
-                       setTaxon(selection_reuseExistingTaxon.getEntity());
                } else if (eventSource == selection_reuseExistingName) {
                        boolean enabled = selection_reuseExistingName.getEntity() == null;
                        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());
+                       setTaxon(textNewTaxonName.getText());
+                       complete = !textNewTaxonName.getText().isEmpty();
                } else if (eventSource == checkbox_openInEditor) {
                        setOpenInEditor(checkbox_openInEditor.getSelection());
                }
@@ -163,20 +191,13 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITreeNode>
         * <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;
        }
@@ -185,10 +206,10 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITreeNode>
         * <p>
         * Getter for the field <code>parentTreeNode</code>.
         * </p>
-        * 
-        * @return a {@link eu.etaxonomy.cdm.model.taxon.ITreeNode} object.
+        *
+        * @return a {@link eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode} object.
         */
-       public ITreeNode getParentTreeNode() {
+       public ITaxonTreeNode getParentTreeNode() {
                return parentTreeNode;
        }
 
@@ -196,27 +217,19 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITreeNode>
         * <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(ITreeNode parentTreeNode) {
+       private void setParentTreeNode(ITaxonTreeNode parentTreeNode) {
                this.parentTreeNode = parentTreeNode;
 
                if (parentTreeNode instanceof Classification) {
@@ -231,16 +244,19 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITreeNode>
                        selection_parentTaxonNode.setEntity((TaxonNode) parentTreeNode);
                        selection_parentTaxonNode.setClassification(classification);
                } else if(parentTreeNode == null){
-                       this.parentTreeNode = selection_classification.getEntity();                     
+                       this.parentTreeNode = selection_classification.getEntity();
                }
        }
 
-       /**
-        * @param reuseExistingTaxon
-        *            the reuseExistingTaxon to set
-        */
+       private void setSecReference(Reference secRef){
+               this.secReference = secRef;
+               setTaxon(textNewTaxonName.getText());
+       }
+
        private void setTaxon(Taxon taxon) {
                this.taxon = taxon;
+               textNewTaxonName.setText(taxon.getName().getTitleCache());
+
        }
 
        private void setTaxon(String taxonNameString) {
@@ -252,7 +268,9 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITreeNode>
        private void setTaxon(TaxonNameBase taxonName) {
                Reference secundum = null;
                if (getParentTreeNode() != null) {
-                       if (getParentTreeNode() instanceof Classification) {
+                       if (this.secReference != null){
+                               secundum = this.secReference;
+                       } else if (getParentTreeNode() instanceof Classification) {
                                secundum = ((Classification) getParentTreeNode())
                                                .getReference();
                        } else if (getParentTreeNode() instanceof TaxonNode) {
@@ -263,21 +281,14 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITreeNode>
                taxon = Taxon.NewInstance(taxonName, secundum);
        }
 
-       /**
-        * @param openInEditor
-        *            the openInEditor to set
-        */
        private void setOpenInEditor(boolean openInEditor) {
                this.openInEditor = openInEditor;
        }
 
-       /**
-        * <p>
-        * isComplete
-        * </p>
-        * 
-        * @return the complete
-        */
+       public TextWithLabelElement getTextNewTaxonName() {
+        return textNewTaxonName;
+    }
+
        public boolean isComplete() {
                return complete;
        }