fix #6197 Move excluded and unplaced from Taxon to TaxonNode in
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / classification / TaxonNodeDetailElement.java
index d82f7fe444204affe3e9a0ea78794442cec94cfc..9087bac7ad5ebfb15ade18d4f39000f3e1ffdb81 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,16 @@ 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.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;
@@ -54,34 +54,27 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
        private EntitySelectionElement<Reference> selection_SecRef;
 
        private Taxon taxon;
-       
-       private Reference secReference;
 
        private TextWithLabelElement textNewTaxonName;
 
+       private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
+
        private CheckboxElement checkbox_openInEditor;
 
+    private CheckboxElement checkbox_excluded;
+
+    private CheckboxElement checkbox_unplaced;
+
        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 +88,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 +100,54 @@ 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);
 
-               textNewTaxonName = formFactory.createTextWithLabelElement(formElement,
-                               "New Taxon", "", style);
-               textNewTaxonName.setFocus();
-               preFillParentTaxonName();
+        } else{
+            setTreeNode(entity);
+            complete = true;
+        }
+               if (entity instanceof TaxonNode) {
+                   checkbox_excluded = formFactory.createCheckbox(formElement,
+                           "Taxon is excluded", ((TaxonNode) entity).isExcluded(), style);
+                   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);
+
        }
 
-       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();
@@ -144,8 +163,11 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                                }
                         }
                     }
+
                 }
+
             }
+
         }
     }
 
@@ -156,41 +178,48 @@ 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);
+                               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());
-                       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());
+                       //setTaxon(selection_reuseExistingName.getEntity());
+
+                       complete = !textNewTaxonName.getText().isEmpty();
                } else if (eventSource == textNewTaxonName) {
-                       boolean enabled = CdmUtils.isEmpty(textNewTaxonName.getText());
+                       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) {
+                   ((TaxonNode) getEntity()).setExcluded(checkbox_excluded.getSelection());
+               } 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;
        }
@@ -199,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);
@@ -234,60 +245,90 @@ 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 (!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;
        }
 
+    /**
+     * @return the createNew
+     */
+    public boolean isCreateNew() {
+        return createNew;
+    }
+
 }