blob: 9eb3e13590410f4ff8ff3f7ce14c26aa4b20fac3 [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.layer;
import static org.eclipse.statet.ecommons.waltable.coordinate.Orientation.HORIZONTAL;
import static org.eclipse.statet.ecommons.waltable.coordinate.Orientation.VERTICAL;
import org.eclipse.statet.ecommons.waltable.coordinate.PositionOutOfBoundsException;
public class LayerUtil {
/**
* Convert column/row position from the source layer to the target layer
* @param sourceLayer source layer
* @param sourceColumnPosition column position in the source layer
* @param targetLayer layer to convert the from position to
* @return converted position, or ILayerDim.POSITION_NA if conversion not possible
*/
public static final long convertPosition(final ILayerDim sourceDim,
final long sourceRefPosition, final long sourcePosition,
final ILayerDim targetDim) {
if (targetDim == sourceDim) {
return sourcePosition;
}
try {
final long id= sourceDim.getPositionId(sourceRefPosition, sourcePosition);
return targetDim.getPositionById(id);
}
catch (final PositionOutOfBoundsException e) {
return ILayerDim.POSITION_NA;
}
}
/**
* Convert column position from the source layer to the target layer
* @param sourceLayer source layer
* @param sourceColumnPosition column position in the source layer
* @param targetLayer layer to convert the from position to
* @return converted column position, or -1 if conversion not possible
*/
public static final long convertColumnPosition(final ILayer sourceLayer,
final long sourceColumnPosition, final ILayer targetLayer) {
return convertPosition(sourceLayer.getDim(HORIZONTAL),
sourceColumnPosition, sourceColumnPosition,
targetLayer.getDim(HORIZONTAL) );
}
/**
* Convert row position from the source layer to the target layer
* @param sourceLayer source layer
* @param sourceRowPosition position in the source layer
* @param targetLayer layer to convert the from position to
* @return converted row position, or -1 if conversion not possible
*/
public static final long convertRowPosition(final ILayer sourceLayer,
final long sourceRowPosition, final ILayer targetLayer) {
return convertPosition(sourceLayer.getDim(VERTICAL),
sourceRowPosition, sourceRowPosition,
targetLayer.getDim(VERTICAL) );
}
}