blob: 1ab48da5cf48c8313bdb34101d37e8347e79cd8c [file] [log] [blame]
/*=============================================================================#
# 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.viewport.action;
import java.util.Collections;
import java.util.List;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.statet.ecommons.waltable.NatTable;
import org.eclipse.statet.ecommons.waltable.coordinate.LRange;
import org.eclipse.statet.ecommons.waltable.coordinate.Orientation;
import org.eclipse.statet.ecommons.waltable.layer.ILayerDim;
import org.eclipse.statet.ecommons.waltable.layer.cell.ILayerCell;
import org.eclipse.statet.ecommons.waltable.layer.cell.ILayerCellDim;
import org.eclipse.statet.ecommons.waltable.selection.SelectionFlags;
import org.eclipse.statet.ecommons.waltable.ui.action.IMouseAction;
import org.eclipse.statet.ecommons.waltable.viewport.ViewportSelectDimPositionsCommand;
/**
* Action to select the column/row at the mouse position.
*/
public class ViewportSelectDimPositionsAction implements IMouseAction {
private final Orientation orientation;
public ViewportSelectDimPositionsAction(final Orientation orientation) {
this.orientation= orientation;
}
@Override
public void run(final NatTable natTable, final MouseEvent event) {
final ILayerCell cell= natTable.getCellByPosition(
natTable.getColumnPositionByX(event.x),
natTable.getRowPositionByY(event.y) );
if (cell == null) {
return;
}
final ILayerDim layerDim= natTable.getDim(this.orientation);
final ILayerCellDim cellDim= cell.getDim(this.orientation);
if (cellDim.getPositionSpan() > 1) {
final List<LRange> positions= Collections.singletonList(
new LRange(cellDim.getOriginPosition(), cellDim.getOriginPosition() + cellDim.getPositionSpan()) );
natTable.doCommand(new ViewportSelectDimPositionsCommand(layerDim,
cellDim.getPosition(), positions, cellDim.getPosition(),
SelectionFlags.swt2Flags(event.stateMask) ));
}
else {
natTable.doCommand(new ViewportSelectDimPositionsCommand(layerDim,
cellDim.getPosition(),
SelectionFlags.swt2Flags(event.stateMask) ));
}
}
}