Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / classification / TaxonNodeDetailElement.java
index 869801d2abca7765b2823fd6906cf1b045b77616..ce8e2f78c78eaddb2854a8004bc8f9159e0c814f 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
  * Copyright (C) 2007 EDIT
  * European Distributed Institute of Taxonomy
@@ -11,6 +10,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 +22,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 +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;
@@ -55,33 +55,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 +90,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 +102,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);
+
+               }
+
+               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);
+               }
 
-               textNewTaxonName = formFactory.createTextWithLabelElement(formElement,
-                               "New Taxon", "", style);
-               textNewTaxonName.setFocus();
-               preFillParentTaxonName();
+               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);
        }
 
-       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,15 +179,14 @@ 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();
 
 
@@ -174,28 +196,30 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                        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;
        }
@@ -204,28 +228,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);
@@ -239,61 +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();
                }
        }
 
-       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());
-               this.secReference = taxon.getSec();
-       }
-
-       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);
-               //textNewTaxonName.setText(taxonName.getTitleCache());
+               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;
+    }
+
 }