| /*=============================================================================# |
| # Copyright (c) 2012, 2021 Original NatTable authors and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 |
| # |
| # Contributors: |
| # Original NatTable authors and others - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.ecommons.waltable.selection; |
| |
| import java.util.Collection; |
| import java.util.List; |
| |
| import org.eclipse.statet.ecommons.waltable.coordinate.LRange; |
| import org.eclipse.statet.ecommons.waltable.coordinate.LRectangle; |
| import org.eclipse.statet.ecommons.waltable.layer.event.ILayerEventHandler; |
| import org.eclipse.statet.ecommons.waltable.layer.event.IStructuralChangeEvent; |
| import org.eclipse.statet.ecommons.waltable.layer.event.StructuralDiff; |
| import org.eclipse.statet.ecommons.waltable.layer.event.StructuralDiff.DiffTypeEnum; |
| |
| |
| public class SelectionLayerStructuralChangeEventHandler implements ILayerEventHandler<IStructuralChangeEvent> { |
| |
| private final ISelectionModel selectionModel; |
| private final SelectionLayer selectionLayer; |
| |
| public SelectionLayerStructuralChangeEventHandler(final SelectionLayer selectionLayer, final ISelectionModel selectionModel) { |
| this.selectionLayer= selectionLayer; |
| this.selectionModel= selectionModel; |
| } |
| |
| @Override |
| public Class<IStructuralChangeEvent> getLayerEventClass() { |
| return IStructuralChangeEvent.class; |
| } |
| |
| @Override |
| public void handleLayerEvent(final IStructuralChangeEvent event) { |
| if (event.isHorizontalStructureChanged()) { |
| // TODO handle column deletion |
| this.selectionLayer.clear(); |
| return; |
| } |
| |
| if (event.isVerticalStructureChanged()) { |
| //if there are no row diffs, it seems to be a complete refresh |
| if (event.getRowDiffs() == null) { |
| final Collection<LRectangle> lRectangles= event.getChangedPositionRectangles(); |
| for (final LRectangle lRectangle : lRectangles) { |
| final LRange changedRange= new LRange(lRectangle.y, lRectangle.y + lRectangle.height); |
| if (selectedRowModified(changedRange)) { |
| if (this.selectionLayer.getRowCount() > 0) { |
| long columnPosition= this.selectionLayer.getSelectionAnchor().columnPosition; |
| if (columnPosition == SelectionLayer.NO_SELECTION) { |
| columnPosition= 0; |
| } |
| this.selectionLayer.setSelectionToCell(columnPosition, 0, false); |
| } |
| else { |
| this.selectionLayer.clear(); |
| } |
| return; |
| } |
| } |
| } |
| else { |
| //there are row diffs so we try to determine the diffs to process |
| for (final StructuralDiff diff : event.getRowDiffs()) { |
| //DiffTypeEnum.CHANGE is used for resizing and shouldn't result in clearing the selection |
| if (diff.getDiffType() != DiffTypeEnum.CHANGE) { |
| if (selectedRowModified(diff.getBeforePositionRange())) { |
| this.selectionLayer.clear(); |
| return; |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| private boolean selectedRowModified(final LRange changedRange){ |
| final List<LRange> selectedRows= this.selectionModel.getSelectedRowPositions(); |
| for (final LRange rowRange : selectedRows) { |
| if (rowRange.overlap(changedRange)){ |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| } |