ref #7095 Overhauled categorical data cell editor
authorPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 30 Jan 2018 12:41:31 +0000 (13:41 +0100)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 30 Jan 2018 12:41:49 +0000 (13:41 +0100)
 - encapsulated data fetching and storing in RowWrapper
 - simplified data accesion by caching States in RowWrapper

eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/CharacterMatrix.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/RowWrapper.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/SpecimenColumnPropertyAccessor.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/categorical/CategoricalDataCellEditor.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/categorical/CategoricalDataDisplayConverter.java

index c305f1bb5a747e92b9952599ae5c265f6d769d20..f07223b878f8db24c3209d2891fa310911f32606 100644 (file)
@@ -51,7 +51,6 @@ import org.eclipse.nebula.widgets.nattable.config.IEditableRule;
 import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
 import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
 import org.eclipse.nebula.widgets.nattable.edit.EditConfigAttributes;
-import org.eclipse.nebula.widgets.nattable.edit.editor.IComboBoxDataProvider;
 import org.eclipse.nebula.widgets.nattable.export.command.ExportCommand;
 import org.eclipse.nebula.widgets.nattable.export.command.ExportCommandHandler;
 import org.eclipse.nebula.widgets.nattable.extension.glazedlists.GlazedListsEventLayer;
@@ -915,17 +914,9 @@ ICdmEntitySessionEnabled{
                     MatrixUtility.getProperty(feature));
 
             //add combo box cell editor
-            CategoricalDataCellEditor comboBoxCellEditor = new CategoricalDataCellEditor(new IComboBoxDataProvider() {
-
-                @Override
-                public List<?> getValues(int columnIndex, int rowIndex) {
-                    Feature feature = indexToFeatureMap.get(columnIndex);
-                    return getSupportedStatesForCategoricalFeature(feature);
-                }
-            }, 10, this, feature);
             //register editor
             configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR,
-                    comboBoxCellEditor,
+                    new CategoricalDataCellEditor(getSupportedStatesForCategoricalFeature(feature), this, feature),
                     DisplayMode.EDIT,
                     MatrixUtility.getProperty(feature));
 
index f195b34b3831bbb52cd39c2c26b57c86d2c81adb..50ce2233cb7455e5959fb9e42a1deb52e2e0ba20 100644 (file)
@@ -8,8 +8,10 @@
  */
 package eu.etaxonomy.taxeditor.editor.workingSet.matrix;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -20,6 +22,8 @@ import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
 import eu.etaxonomy.cdm.model.description.Feature;
 import eu.etaxonomy.cdm.model.description.QuantitativeData;
 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
+import eu.etaxonomy.cdm.model.description.State;
+import eu.etaxonomy.cdm.model.description.StateData;
 import eu.etaxonomy.cdm.model.location.NamedArea;
 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
@@ -111,8 +115,29 @@ public class RowWrapper {
         return country;
     }
 
-    public DescriptionElementBase getDescriptionElementForFeature(Feature feature){
-        return featureToElementMap.get(feature);
+    public Object getDataValueForFeature(Feature feature){
+        DescriptionElementBase descriptionElementBase = featureToElementMap.get(feature);
+        if(descriptionElementBase!=null && descriptionElementBase.isInstanceOf(CategoricalData.class)){
+            CategoricalData categoricalData = HibernateProxyHelper.deproxy(descriptionElementBase, CategoricalData.class);
+            return categoricalData.getStatesOnly();
+        }
+        return descriptionElementBase;
+    }
+
+    public void setDataValueForFeature(Feature feature, Object newValue){
+        DescriptionElementBase descriptionElementBase = featureToElementMap.get(feature);
+        //FIXME move this to cdmlib service layer
+        if(descriptionElementBase!=null && descriptionElementBase.isInstanceOf(CategoricalData.class) && newValue instanceof Collection){
+            CategoricalData categoricalData = HibernateProxyHelper.deproxy(descriptionElementBase, CategoricalData.class);
+            List<StateData> stateData = new ArrayList<>(categoricalData.getStateData());
+            for (StateData stateData2 : stateData) {
+                categoricalData.removeStateData(stateData2);
+            }
+            Collection<State> states = (Collection<State>) newValue;
+            for (State state : states) {
+                categoricalData.addStateData(state);
+            }
+        }
     }
 
 }
index 25b7ba12b8ebf4acd6099b7fd27d66142abadbe7..b278c723b7a89ddb13b52e54c89ca65fc8cf7d02 100644 (file)
@@ -8,18 +8,9 @@
 */
 package eu.etaxonomy.taxeditor.editor.workingSet.matrix;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.Set;
-
 import org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor;
 
-import eu.etaxonomy.cdm.model.description.CategoricalData;
-import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
 import eu.etaxonomy.cdm.model.description.Feature;
-import eu.etaxonomy.cdm.model.description.State;
-import eu.etaxonomy.cdm.model.description.StateData;
 
 /**
  * @author pplitzner
@@ -56,7 +47,7 @@ public class SpecimenColumnPropertyAccessor implements IColumnPropertyAccessor<O
                 break;
             }
             Feature feature = matrix.getIndexToFeatureMap().get(columnIndex);
-            return rowWrapper.getDescriptionElementForFeature(feature);
+            return rowWrapper.getDataValueForFeature(feature);
         } else if (columnIndex == 0) {
             return rowObject;
         }
@@ -71,29 +62,7 @@ public class SpecimenColumnPropertyAccessor implements IColumnPropertyAccessor<O
         if(rowObject instanceof RowWrapper){
             RowWrapper rowWrapper = (RowWrapper)rowObject;
             Feature feature = matrix.getIndexToFeatureMap().get(columnIndex);
-            Set<DescriptionElementBase> elements = rowWrapper.getSpecimenDescription().getElements();
-            for (DescriptionElementBase descriptionElementBase : elements) {
-                if(descriptionElementBase.getFeature().equals(feature)){
-                    setDescriptionElement(descriptionElementBase, feature, newValue);
-                    return;
-                }
-            }
-        }
-    }
-
-
-    private void setDescriptionElement(DescriptionElementBase descriptionElementBase, Feature feature, Object newValue) {
-        //FIXME move this to cdmlib service layer
-        if(feature.isSupportsCategoricalData() && descriptionElementBase instanceof CategoricalData && newValue instanceof Collection){
-            CategoricalData categoricalData = (CategoricalData)descriptionElementBase;
-            List<StateData> stateData = new ArrayList<>(categoricalData.getStateData());
-            for (StateData stateData2 : stateData) {
-                categoricalData.removeStateData(stateData2);
-            }
-            Collection<State> states = (Collection<State>) newValue;
-            for (State state : states) {
-                categoricalData.addStateData(state);
-            }
+            rowWrapper.setDataValueForFeature(feature, newValue);
         }
     }
 
index 6d7d6fc8b1e0b4c6763b7202588221360ce4586c..8fec45edc7a95de54b56c2e1594f04582c6f5faf 100644 (file)
@@ -10,15 +10,13 @@ package eu.etaxonomy.taxeditor.editor.workingSet.matrix.categorical;
 
 import java.util.List;
 
-import org.eclipse.nebula.widgets.nattable.edit.editor.IComboBoxDataProvider;
-import org.eclipse.nebula.widgets.nattable.filterrow.combobox.FilterRowComboBoxCellEditor;
+import org.eclipse.nebula.widgets.nattable.edit.editor.ComboBoxCellEditor;
 import org.eclipse.nebula.widgets.nattable.widget.NatCombo;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 
-import eu.etaxonomy.cdm.model.description.CategoricalData;
 import eu.etaxonomy.cdm.model.description.Feature;
 import eu.etaxonomy.cdm.model.description.State;
 import eu.etaxonomy.taxeditor.editor.workingSet.matrix.CharacterMatrix;
@@ -29,15 +27,17 @@ import eu.etaxonomy.taxeditor.editor.workingSet.matrix.RowWrapper;
  * @since Dec 7, 2017
  *
  */
-public class CategoricalDataCellEditor extends FilterRowComboBoxCellEditor{
+public class CategoricalDataCellEditor extends ComboBoxCellEditor{
 
     private CharacterMatrix matrix;
     private Feature feature;
 
-    public CategoricalDataCellEditor(IComboBoxDataProvider dataProvider, int maxVisibleItems, CharacterMatrix matrix, Feature feature) {
-        super(dataProvider, maxVisibleItems);
+    public CategoricalDataCellEditor(List<State> list, CharacterMatrix matrix, Feature feature) {
+        super(list, 10);
         this.matrix = matrix;
         this.feature = feature;
+        setUseCheckbox(true);
+        setMultiselect(true);
     }
 
     @Override
@@ -45,19 +45,10 @@ public class CategoricalDataCellEditor extends FilterRowComboBoxCellEditor{
         if(canonicalValue==null){
             Object rowWrapper = matrix.getBodyDataProvider().getRowObject(this.getRowIndex());
             if(rowWrapper instanceof RowWrapper){
-                canonicalValue = ((RowWrapper) rowWrapper).addCategoricalData(feature);
+                ((RowWrapper) rowWrapper).addCategoricalData(feature);
             }
         }
-        else if (canonicalValue instanceof CategoricalData) {
-            CategoricalData data = (CategoricalData)canonicalValue;
-            List<State> states = data.getStatesOnly();
-            String[] result = new String[states.size()];
-            for (int i = 0; i < states.size(); i++) {
-                result[i] = (String) this.displayConverter.canonicalToDisplayValue(
-                        this.layerCell, this.configRegistry, states.get(i));
-            }
-            setEditorValue(result);
-        }
+        super.setCanonicalValue(canonicalValue);
     }
 
     /**
@@ -66,8 +57,8 @@ public class CategoricalDataCellEditor extends FilterRowComboBoxCellEditor{
     @Override
     protected Control activateCell(Composite parent, Object originalCanonicalValue) {
         if(matrix.getBodyDataProvider().getRowObject(this.getRowIndex()) instanceof RowWrapper){
-            Control editorControl = super.activateCell(parent, originalCanonicalValue);
-            ((NatCombo)editorControl).addSelectionListener(new SelectionListener() {
+            NatCombo natCombo = (NatCombo) super.activateCell(parent, originalCanonicalValue);
+            natCombo.addSelectionListener(new SelectionListener() {
                 @Override
                 public void widgetSelected(SelectionEvent e) {
                     matrix.setDirty();
@@ -76,7 +67,7 @@ public class CategoricalDataCellEditor extends FilterRowComboBoxCellEditor{
                 public void widgetDefaultSelected(SelectionEvent e) {
                 }
             });
-            return editorControl;
+            return natCombo;
         }
         return null;
     }
index db33052acd021cf4291f41f0741a1bb2ea17ca8d..305da20322987d2ca4e97b02ab53a07ffcaba893 100644 (file)
@@ -8,6 +8,8 @@
 */
 package eu.etaxonomy.taxeditor.editor.workingSet.matrix.categorical;
 
+import java.util.Collection;
+
 import org.eclipse.nebula.widgets.nattable.data.convert.DisplayConverter;
 
 import eu.etaxonomy.cdm.model.description.CategoricalData;
@@ -32,6 +34,15 @@ public class CategoricalDataDisplayConverter extends DisplayConverter {
         else if(canonicalValue instanceof State){
             return ((State) canonicalValue).getLabel();
         }
+        else if (canonicalValue instanceof Collection) {
+            // Collection.toString() will add [ and ] around
+            // the values in the Collection
+            // So by removing the leading and ending
+            // character, we remove the brackets
+            String result = canonicalValue.toString();
+            result = result.substring(1, result.length() - 1);
+            return result;
+        }
         else if(canonicalValue!=null){
             return canonicalValue.toString();
         }