Refactor categorical combo box creation
authorPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 24 Jul 2018 13:44:12 +0000 (15:44 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 24 Jul 2018 13:44:12 +0000 (15:44 +0200)
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/CellEditorDataConversionConfiguration.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/categorical/CategoricalComboBoxDataProvider.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/categorical/CategoricalDataCellEditor.java

index 500f5768d3c0ce8e8af7523e36b3d1ebcb927228..94943d1a008e733a45254b1d5e99ab4a9904479e 100644 (file)
@@ -21,7 +21,7 @@ import org.eclipse.nebula.widgets.nattable.edit.gui.ICellEditDialog;
 import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
 import org.eclipse.swt.graphics.Point;
 
-import eu.etaxonomy.cdm.model.description.Feature;
+import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.categorical.CategoricalComboBoxDataProvider;
 import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.categorical.CategoricalDataCellEditor;
 import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.categorical.CategoricalDataDisplayConverter;
 import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.quantitative.QuantitativeDataDialogEditor;
@@ -87,6 +87,12 @@ final class CellEditorDataConversionConfiguration extends AbstractRegistryConfig
                 DisplayMode.NORMAL,
                 CharacterMatrixConfigLabelAccumulator.CATEGORICAL
                 );
+        //register categorical editor
+        configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR,
+                new CategoricalDataCellEditor(matrix, new CategoricalComboBoxDataProvider(matrix)),
+                DisplayMode.EDIT,
+                CharacterMatrixConfigLabelAccumulator.CATEGORICAL
+                );
 
 
         /**
@@ -141,21 +147,6 @@ final class CellEditorDataConversionConfiguration extends AbstractRegistryConfig
                 editDialogSettings,
                 DisplayMode.EDIT,
                 CharacterMatrixConfigLabelAccumulator.QUANTITATIVE);
-
-
-        //TODO: this for loop can maybe be avoided
-        matrix.getFeatures().forEach(feature->registerColumnConfiguration(feature, configRegistry));
     }
 
-    private void registerColumnConfiguration(Feature feature, IConfigRegistry configRegistry) {
-        if(feature.isSupportsCategoricalData()){
-            //add combo box cell editor
-            //register editor
-            configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR,
-                    new CategoricalDataCellEditor(matrix.getSupportedStatesForCategoricalFeature(feature), matrix, feature),
-                    DisplayMode.EDIT,
-                    MatrixUtility.getProperty(feature));
-
-        }
-    }
 }
diff --git a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/categorical/CategoricalComboBoxDataProvider.java b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/categorical/CategoricalComboBoxDataProvider.java
new file mode 100644 (file)
index 0000000..645158c
--- /dev/null
@@ -0,0 +1,46 @@
+// $Id$
+/**
+* Copyright (C) 2018 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.categorical;
+
+import java.util.List;
+
+import org.eclipse.nebula.widgets.nattable.edit.editor.IComboBoxDataProvider;
+
+import eu.etaxonomy.cdm.model.description.Feature;
+import eu.etaxonomy.cdm.model.description.State;
+import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.CharacterMatrix;
+
+/**
+ * @author pplitzner
+ * @date 24.07.2018
+ *
+ */
+public class CategoricalComboBoxDataProvider implements IComboBoxDataProvider {
+
+    private CharacterMatrix matrix;
+    private int maxVisibleItems;
+
+    public CategoricalComboBoxDataProvider(CharacterMatrix matrix) {
+        super();
+        this.matrix = matrix;
+    }
+
+    @Override
+    public List<?> getValues(int columnIndex, int rowIndex) {
+        Feature feature = matrix.getIndexToFeatureMap().get(columnIndex);
+        List<State> supportedStatesForCategoricalFeature = matrix.getSupportedStatesForCategoricalFeature(feature);
+        maxVisibleItems = Math.max(3, Math.min(supportedStatesForCategoricalFeature.size()-1, 10));
+        return supportedStatesForCategoricalFeature;
+    }
+
+    int getMaxVisibleItems(){
+        return maxVisibleItems;
+    }
+}
index 396a3caa97d186fa1e08e83ae8bbcae1a708b9ec..d9492203e7382974f6fc7746707af2be0a03b868 100644 (file)
@@ -8,8 +8,6 @@
 */
 package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.categorical;
 
-import java.util.List;
-
 import org.eclipse.nebula.widgets.nattable.edit.editor.ComboBoxCellEditor;
 import org.eclipse.nebula.widgets.nattable.widget.NatCombo;
 import org.eclipse.swt.events.SelectionEvent;
@@ -19,7 +17,6 @@ import org.eclipse.swt.widgets.Control;
 
 import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
 import eu.etaxonomy.cdm.model.description.Feature;
-import eu.etaxonomy.cdm.model.description.State;
 import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.CharacterMatrix;
 
 /**
@@ -31,12 +28,10 @@ import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.CharacterMatrix;
 public class CategoricalDataCellEditor extends ComboBoxCellEditor{
 
     private CharacterMatrix matrix;
-    private Feature feature;
 
-    public CategoricalDataCellEditor(List<State> list, CharacterMatrix matrix, Feature feature) {
-        super(list, Math.max(3, Math.min(list.size()-1, 10)));
+    public CategoricalDataCellEditor(CharacterMatrix matrix, CategoricalComboBoxDataProvider categoricalComboBoxDataProvider) {
+        super(categoricalComboBoxDataProvider, categoricalComboBoxDataProvider.getMaxVisibleItems());
         this.matrix = matrix;
-        this.feature = feature;
         setUseCheckbox(true);
         setMultiselect(true);
     }
@@ -46,15 +41,13 @@ public class CategoricalDataCellEditor extends ComboBoxCellEditor{
         if(canonicalValue==null){
             Object rowWrapper = matrix.getBodyDataProvider().getRowObject(this.getRowIndex());
             if(rowWrapper instanceof RowWrapperDTO){
+                Feature feature = matrix.getIndexToFeatureMap().get(getColumnIndex());
                 ((RowWrapperDTO) rowWrapper).addCategoricalData(feature);
             }
         }
         super.setCanonicalValue(canonicalValue);
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     protected Control activateCell(Composite parent, Object originalCanonicalValue) {
         if(matrix.getBodyDataProvider().getRowObject(this.getRowIndex()) instanceof RowWrapperDTO){