Search in sources :

Example 1 with ICalculator

use of org.eclipse.nebula.widgets.nattable.util.ICalculator in project nebula.widgets.nattable by eclipse.

the class GroupByDataLayer method getDataValueByPosition.

/**
 * This method is used to retrieve a data value of an {@link ILayerCell}. It
 * is intended to be used for conditional formatting. It allows to specify
 * the {@link LabelStack} and to disable background calculation processing,
 * since the conditional formatting needs the summary value without a delay.
 *
 * @param columnPosition
 *            The column position of the cell whose data value is requested.
 * @param rowPosition
 *            The row position of the cell whose data value is requested.
 * @param labelStack
 *            The {@link LabelStack} of the cell whose data value is
 *            requested. Needed to retrieve a possible existing
 *            {@link IGroupBySummaryProvider}.
 * @param calculateInBackground
 *            <code>true</code> to calculate the summary value in the
 *            background, <code>false</code> if the calculation should be
 *            processed in the UI thread.
 * @return The data value for the {@link ILayerCell} at the given
 *         coordinates.
 */
public Object getDataValueByPosition(final int columnPosition, final int rowPosition, LabelStack labelStack, boolean calculateInBackground) {
    if (labelStack.hasLabel(GROUP_BY_OBJECT)) {
        GroupByObject groupByObject = (GroupByObject) this.treeData.getDataAtIndex(rowPosition);
        final IGroupBySummaryProvider<T> summaryProvider = getGroupBySummaryProvider(labelStack);
        if (summaryProvider != null) {
            final List<T> children = getItemsInGroup(groupByObject);
            return this.valueCache.getCalculatedValue(columnPosition, rowPosition, new GroupByValueCacheKey(columnPosition, rowPosition, groupByObject), calculateInBackground, new ICalculator() {

                @Override
                public Object executeCalculation() {
                    return summaryProvider.summarize(columnPosition, children);
                }
            });
        }
    }
    return super.getDataValueByPosition(columnPosition, rowPosition);
}
Also used : ICalculator(org.eclipse.nebula.widgets.nattable.util.ICalculator)

Example 2 with ICalculator

use of org.eclipse.nebula.widgets.nattable.util.ICalculator in project nebula.widgets.nattable by eclipse.

the class GroupByDataLayer method doCommand.

@Override
public boolean doCommand(ILayerCommand command) {
    if (command instanceof CalculateSummaryRowValuesCommand) {
        // summary values
        for (int i = 0; i < getRowCount(); i++) {
            if (this.treeData.getDataAtIndex(i) instanceof GroupByObject) {
                for (int j = 0; j < getColumnCount(); j++) {
                    LabelStack labelStack = getConfigLabelsByPosition(j, i);
                    final IGroupBySummaryProvider<T> summaryProvider = getGroupBySummaryProvider(labelStack);
                    if (summaryProvider != null) {
                        GroupByObject groupByObject = (GroupByObject) this.treeData.getDataAtIndex(i);
                        final List<T> children = getItemsInGroup(groupByObject);
                        final int col = j;
                        this.valueCache.getCalculatedValue(j, i, new GroupByValueCacheKey(j, i, groupByObject), false, new ICalculator() {

                            @Override
                            public Object executeCalculation() {
                                return summaryProvider.summarize(col, children);
                            }
                        });
                    }
                }
            }
        }
    // we do not return true here, as there might be other layers
    // involved in the composition that also need to calculate the
    // summary values immediately
    } else if (command instanceof DisposeResourcesCommand) {
        // ensure to clear the caches to avoid memory leaks
        this.treeFormat.clearComparatorCache();
        this.valueCache.killCache();
        this.valueCache.dispose();
    }
    return super.doCommand(command);
}
Also used : DisposeResourcesCommand(org.eclipse.nebula.widgets.nattable.command.DisposeResourcesCommand) CalculateSummaryRowValuesCommand(org.eclipse.nebula.widgets.nattable.summaryrow.command.CalculateSummaryRowValuesCommand) LabelStack(org.eclipse.nebula.widgets.nattable.layer.LabelStack) ICalculator(org.eclipse.nebula.widgets.nattable.util.ICalculator)

Aggregations

ICalculator (org.eclipse.nebula.widgets.nattable.util.ICalculator)2 DisposeResourcesCommand (org.eclipse.nebula.widgets.nattable.command.DisposeResourcesCommand)1 LabelStack (org.eclipse.nebula.widgets.nattable.layer.LabelStack)1 CalculateSummaryRowValuesCommand (org.eclipse.nebula.widgets.nattable.summaryrow.command.CalculateSummaryRowValuesCommand)1