add the possibility to add taxonnode agent relations to a taxonnode and to edit a...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / classification / TaxonNodeDetailElement.java
index 90706eb8b265d80d239665053ae1fabafcef4739..5ac40f536071de654a8df5356bf562f4269081a4 100644 (file)
@@ -26,6 +26,7 @@ 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;
@@ -39,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;
@@ -56,6 +59,9 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
 
        private TextWithLabelElement textNewTaxonName;
 
+       private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
+
+
        private CheckboxElement checkbox_openInEditor;
 
        private boolean openInEditor;
@@ -65,8 +71,9 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
        private EntitySelectionElement<TaxonNameBase> selection_reuseExistingName;
 
        public TaxonNodeDetailElement(CdmFormFactory formFactory,
-                       ICdmFormElement formElement) {
+                       ICdmFormElement formElement, boolean isCreateNew) {
                super(formFactory, formElement);
+               createNew = isCreateNew;
        }
 
        /** {@inheritDoc} */
@@ -95,25 +102,44 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                selection_SecRef = formFactory
                 .createSelectionElement(Reference.class,getConversationHolder(), formElement, "Secundum Reference", null,
                         EntitySelectionElement.DELETABLE, style);
-               textNewTaxonName = formFactory.createTextWithLabelElement(formElement,
-                               "New Taxon", "", style);
-               textNewTaxonName.setFocus();
-               preFillParentTaxonName();
+               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 (!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();
@@ -129,9 +155,11 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                                }
                         }
                     }
+
                 }
-               
+
             }
+
         }
     }
 
@@ -147,14 +175,19 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                } else if (eventSource == selection_reuseExistingTaxon) {
                                boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
                                selection_reuseExistingName.setEnabled(enabled);
-                               textNewTaxonName.setEnabled(enabled);
                                if (!enabled){
-                                       setTaxon(selection_reuseExistingTaxon.getEntity());
-                               } else{
-                                   textNewTaxonName.setText(null);
+                    setTaxon(selection_reuseExistingTaxon.getEntity());
+                }
+                               if (isCreateNew()){
+                                   textNewTaxonName.setEnabled(enabled);
+                                   if (!enabled){
+                                       setTaxon(selection_reuseExistingTaxon.getEntity());
+                                   } else{
+                                       textNewTaxonName.setText(null);
+                                   }
+
+                                   complete = !textNewTaxonName.getText().isEmpty();
                                }
-                               complete = !textNewTaxonName.getText().isEmpty();
-
 
                } else if (eventSource == selection_reuseExistingName) {
                    boolean enabled = selection_reuseExistingName.getEntity() == null;
@@ -174,6 +207,13 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                        complete = !textNewTaxonName.getText().isEmpty();
                } 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;
                }
        }
 
@@ -208,7 +248,7 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                        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);
@@ -219,6 +259,15 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
                }
        }
 
+       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 setSecReference(Reference secRef){
                this.secReference = secRef;
                setTaxon(textNewTaxonName.getText());
@@ -226,7 +275,9 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
 
        private void setTaxon(Taxon taxon) {
                this.taxon = taxon;
-               textNewTaxonName.setText(taxon.getName().getTitleCache());
+               if (isCreateNew()){
+                   textNewTaxonName.setText(taxon.getName().getTitleCache());
+               }
                selection_SecRef.setEntity(taxon.getSec());
        }
 
@@ -268,7 +319,23 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
     }
 
        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;
+    }
+
 }