ref #8026 Changed selection to cell selection in bulk editor
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 30 Jan 2019 16:11:53 +0000 (17:11 +0100)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 30 Jan 2019 16:11:53 +0000 (17:11 +0100)
 - Fix empty details view in some cases

eu.etaxonomy.taxeditor.bulkeditor/META-INF/MANIFEST.MF
eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/e4/BulkEditorE4Composite.java
eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/e4/CellSelectionListener.java [new file with mode: 0644]

index 4797aa01672ef10a33d38e2c4246de032ea2c524..9cd3b49d86ca7653e8d43db799858c1e74239aed 100644 (file)
@@ -29,7 +29,8 @@ Require-Bundle: eu.etaxonomy.taxeditor.workbench,
  org.eclipse.nebula.widgets.nattable.core,
  ca.odell.glazedlists,
  org.eclipse.nebula.widgets.nattable.extension.glazedlists,
- org.eclipse.e4.core.di
+ org.eclipse.e4.core.di,
+ org.eclipse.nebula.widgets.nattable.extension.e4
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Import-Package: eu.etaxonomy.taxeditor.editor,
  org.eclipse.core.runtime,
index 9bf86adfb62f20be7a30ae7792a8cdaa66d8fa01..c7de66f80dc9f62c1c846cff54a37ced973eddca 100644 (file)
 package eu.etaxonomy.taxeditor.bulkeditor.e4;
 
 import java.io.File;
-import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.Set;
 
 import javax.inject.Inject;
 
@@ -35,13 +35,14 @@ import org.eclipse.nebula.widgets.nattable.config.ConfigRegistry;
 import org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration;
 import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
 import org.eclipse.nebula.widgets.nattable.config.IEditableRule;
-import org.eclipse.nebula.widgets.nattable.data.IRowIdAccessor;
+import org.eclipse.nebula.widgets.nattable.coordinate.Range;
 import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
 import org.eclipse.nebula.widgets.nattable.data.convert.DefaultBooleanDisplayConverter;
 import org.eclipse.nebula.widgets.nattable.edit.EditConfigAttributes;
 import org.eclipse.nebula.widgets.nattable.edit.action.MouseEditAction;
 import org.eclipse.nebula.widgets.nattable.edit.config.DefaultEditConfiguration;
 import org.eclipse.nebula.widgets.nattable.edit.editor.TextCellEditor;
+import org.eclipse.nebula.widgets.nattable.extension.e4.selection.E4SelectionListener;
 import org.eclipse.nebula.widgets.nattable.extension.glazedlists.GlazedListsEventLayer;
 import org.eclipse.nebula.widgets.nattable.extension.glazedlists.GlazedListsSortModel;
 import org.eclipse.nebula.widgets.nattable.grid.GridRegion;
@@ -57,8 +58,6 @@ import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
 import org.eclipse.nebula.widgets.nattable.layer.stack.DefaultBodyLayerStack;
 import org.eclipse.nebula.widgets.nattable.painter.cell.CheckBoxPainter;
 import org.eclipse.nebula.widgets.nattable.painter.cell.ImagePainter;
-import org.eclipse.nebula.widgets.nattable.selection.RowSelectionModel;
-import org.eclipse.nebula.widgets.nattable.selection.RowSelectionProvider;
 import org.eclipse.nebula.widgets.nattable.sort.SortHeaderLayer;
 import org.eclipse.nebula.widgets.nattable.sort.config.SingleClickSortConfiguration;
 import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
@@ -164,9 +163,10 @@ public class BulkEditorE4Composite extends Composite {
         GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
 
         //propagate selection
-        selectionChangedListener = (event -> selService.setSelection(getSelection()));
-        RowSelectionProvider<CdmBase> selectionProvider = new RowSelectionProvider<CdmBase>(bodyLayer.getSelectionLayer(), bodyDataProvider, true);
-        selectionProvider.addSelectionChangedListener(selectionChangedListener);
+        // selection listener
+        E4SelectionListener<CdmBase> selectionListener = new CellSelectionListener(selService, bodyLayer.getSelectionLayer(), bodyDataProvider, this);
+        bodyLayer.getSelectionLayer().addLayerListener(selectionListener);
+        selectionListener.setFullySelectedRowsOnly(false);
 
         bottomComposite.layout();
 
@@ -239,14 +239,6 @@ public class BulkEditorE4Composite extends Composite {
                 rowHeaderLayer, cornerLayer, false);//set autoconfigure to false to get rid of the single click editing
         natTable = new NatTable(bottomComposite, gridLayer, false);
 
-        //full row selection
-        bodyLayer.getSelectionLayer().setSelectionModel(new RowSelectionModel<CdmBase>(bodyLayer.getSelectionLayer(), bodyDataProvider, new IRowIdAccessor<CdmBase>() {
-            @Override
-            public Serializable getRowId(CdmBase rowObject) {
-                return bulkEditor.getEditorInput().getModel().indexOf(rowObject);
-            }
-        }));
-
         dataLayer.setConfigLabelAccumulator(new BulkEditorConfigLabelAccumulator(bodyDataProvider, colHeaderDataProvider,
                 bulkEditor.getEditorInput()));
 
@@ -391,18 +383,14 @@ public class BulkEditorE4Composite extends Composite {
     }
 
     public IStructuredSelection getSelection(){
-        List<CdmBase> selection = new ArrayList<>();
-        int[] fullySelectedRowPositions = bodyLayer.getSelectionLayer().getFullySelectedRowPositions();
-        for (int i : fullySelectedRowPositions) {
-            if(i<0){
-                continue;
-            }
-            Object rowObject = bodyDataProvider.getRowObject(i);
-            if(rowObject instanceof CdmBase){
-                selection.add((CdmBase) rowObject);
+        Set<Range> selectedRowPositions = bodyLayer.getSelectionLayer().getSelectedRowPositions();
+        List<Object> selectedObjects = new ArrayList<>();
+        for (Range range : selectedRowPositions) {
+            for(int i=range.start;i<range.end;i++){
+                selectedObjects.add(bodyDataProvider.getRowObject(i));
             }
         }
-        return new StructuredSelection(selection);
+        return new StructuredSelection(selectedObjects);
     }
 
     public void setSelection(IStructuredSelection selection){
@@ -434,6 +422,14 @@ public class BulkEditorE4Composite extends Composite {
         }
     }
 
+    ListDataProvider<CdmBase> getBodyDataProvider() {
+        return bodyDataProvider;
+    }
+
+    ESelectionService getSelService() {
+        return selService;
+    }
+
     NatTable getNatTable() {
         return natTable;
     }
diff --git a/eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/e4/CellSelectionListener.java b/eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/e4/CellSelectionListener.java
new file mode 100644 (file)
index 0000000..55dc814
--- /dev/null
@@ -0,0 +1,66 @@
+// $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.bulkeditor.e4;
+
+import java.util.Collection;
+
+import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.nebula.widgets.nattable.data.IRowDataProvider;
+import org.eclipse.nebula.widgets.nattable.extension.e4.selection.E4SelectionListener;
+import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
+import org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent;
+import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
+import org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent;
+import org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent;
+
+import eu.etaxonomy.cdm.model.common.CdmBase;
+
+/**
+ * @author pplitzner
+ * @date 09.07.2018
+ *
+ */
+final class CellSelectionListener extends E4SelectionListener<CdmBase> {
+
+    private BulkEditorE4Composite bulkEditor;
+
+    public CellSelectionListener(
+            ESelectionService service,
+            SelectionLayer selectionLayer,
+            IRowDataProvider<CdmBase> rowDataProvider,
+            BulkEditorE4Composite bulkEditor) {
+        super(service, selectionLayer, rowDataProvider);
+        this.bulkEditor = bulkEditor;
+    }
+
+    @Override
+    public void handleLayerEvent(ILayerEvent event) {
+        if(event instanceof CellSelectionEvent){
+            CellSelectionEvent cellSelectionEvent = (CellSelectionEvent)event;
+            Collection<ILayerCell> selectedCells = cellSelectionEvent.getSelectionLayer().getSelectedCells();
+            if(selectedCells.size()==1){
+                Object rowObject = bulkEditor.getBodyDataProvider().getRowObject(cellSelectionEvent.getRowPosition());
+                bulkEditor.getSelService().setSelection(new StructuredSelection(rowObject));
+                return;
+            }
+        }
+        else if(event instanceof RowSelectionEvent){
+            RowSelectionEvent rowSelectionEvent = (RowSelectionEvent) event;
+            int[] fullySelectedRowPositions = rowSelectionEvent.getSelectionLayer().getFullySelectedRowPositions();
+            if(fullySelectedRowPositions.length==1){
+                Object rowObject = bulkEditor.getBodyDataProvider().getRowObject(fullySelectedRowPositions[0]);
+                bulkEditor.getSelService().setSelection(new StructuredSelection(rowObject));
+                return;
+            }
+        }
+        bulkEditor.getSelService().setSelection(new StructuredSelection());
+    }
+}