ref #10410: create new default description of not exist
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / checklist / e4 / DistributionColumnAccessor.java
index 29afbaaf3b8e083b6849f258e0622158b359d884..e67d890b9c35a144a65fda337985bf097616a2d5 100755 (executable)
@@ -11,13 +11,11 @@ package eu.etaxonomy.taxeditor.editor.view.checklist.e4;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import javax.inject.Inject;
-
-import org.eclipse.e4.core.services.events.IEventBroker;
 import org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor;
 
 import eu.etaxonomy.cdm.api.service.dto.TaxonDistributionDTO;
@@ -26,31 +24,28 @@ import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
 import eu.etaxonomy.cdm.model.description.Distribution;
 import eu.etaxonomy.cdm.model.description.TaxonDescription;
 import eu.etaxonomy.cdm.model.location.NamedArea;
+import eu.etaxonomy.cdm.model.metadata.DistributionDescription;
 import eu.etaxonomy.taxeditor.event.EventUtility;
 import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
-
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 
 /**
  * @author k.luther
  * @since 28.11.2018
- *
  */
 public class DistributionColumnAccessor implements IColumnPropertyAccessor<TaxonDistributionDTO> {
-    private DistributionEditor editor;
+
     public static final String DEFAULT_ENTRY = "";
-    @Inject
-    private IEventBroker eventBroker;
+
+    private DistributionEditor editor;
 
     public DistributionColumnAccessor(DistributionEditor editor) {
         this.editor = editor;
     }
 
-
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public Object getDataValue(TaxonDistributionDTO rowObject, int columnIndex) {
+//     editor.setActualNameCache(rowObject.getNameCache());
      switch (columnIndex) {
             case 0:
                 return rowObject.getNameCache();
@@ -91,35 +86,21 @@ public class DistributionColumnAccessor implements IColumnPropertyAccessor<Taxon
 
     }
 
-
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public int getColumnCount() {
         return editor.getPropertyToLabelMap().size();
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public String getColumnProperty(int columnIndex) {
         return editor.getPropertyToLabelMap().get(columnIndex);
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public int getColumnIndex(String propertyName){
         return editor.getPropertyToLabelMap().indexOf(propertyName);
     }
 
-
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public void setDataValue(TaxonDistributionDTO taxonWrapper, int columnIndex, Object newValue) {
         if (newValue instanceof StatusHelper){
@@ -151,28 +132,48 @@ public class DistributionColumnAccessor implements IColumnPropertyAccessor<Taxon
                     }
                     dist = Distribution.NewInstance(area, ((StatusHelper)newValue).term);
                     Set<TaxonDescription> descs = taxonWrapper.getDescriptionsWrapper().getDescriptions();
-                    TaxonDescription desc;
+                    TaxonDescription desc = null;
                     if (descs.size() >= 1){
-                        desc = descs.iterator().next();
+                        Iterator<TaxonDescription> itDesc = descs.iterator();
+                        while(itDesc.hasNext()) {
+                            TaxonDescription next = itDesc.next();
+                            if (next.isDefault()) {
+                                desc = next;
+                                break;
+                            }
+                        }
+
+                        if (desc == null) {
+                            if (PreferencesUtil.getDescriptionForChecklistEditor().equals(DistributionDescription.AlwaysDefault)){
+                                desc = TaxonDescription.NewInstance();
+                                desc.setDefault(true);
+                                taxonWrapper.getDescriptionsWrapper().getDescriptions().add(desc);
+                            }else {
+                                desc = descs.iterator().next();
+                            }
+                        }
                     }else {
                         desc = TaxonDescription.NewInstance();
+                        desc.setDefault(true);
+
                         taxonWrapper.getDescriptionsWrapper().getDescriptions().add(desc);
                     }
                     if (editor.getDefaultSource() != null){
                       dist.addSource(DescriptionElementSource.NewPrimarySourceInstance(editor.getDefaultSource(), null));
                     }
                     desc.addElement(dist);
-                    if(desc.isPersited()){
+                    if(desc.isPersisted()){
                         editor.part.getCdmEntitySession().load(desc, true);
                     }
                     distributions.add(dist);
-
+//                    EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS_DISTRIBUTION, taxonWrapper.getNameCache());
 
                 }
+                editor.setActualNameCache(taxonWrapper.getNameCache());
 
                 editor.descriptionsToSave.add(taxonWrapper);
             }
+
             EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS, true);
     }
-
-}
+}
\ No newline at end of file