Search in sources :

Example 1 with EnableFormulaEvaluationCommand

use of org.eclipse.nebula.widgets.nattable.formula.command.EnableFormulaEvaluationCommand in project nebula.widgets.nattable by eclipse.

the class _305_FormulaDataExample method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    Composite panel = new Composite(parent, SWT.NONE);
    panel.setLayout(new GridLayout());
    GridDataFactory.fillDefaults().grab(true, true).applyTo(panel);
    // TODO add combo box and text field for editing formulas
    Composite gridPanel = new Composite(panel, SWT.NONE);
    gridPanel.setLayout(new GridLayout());
    GridDataFactory.fillDefaults().grab(true, true).applyTo(gridPanel);
    Composite buttonPanel = new Composite(panel, SWT.NONE);
    buttonPanel.setLayout(new GridLayout());
    GridDataFactory.fillDefaults().grab(true, false).applyTo(buttonPanel);
    ConfigRegistry configRegistry = new ConfigRegistry();
    final FormulaGridLayer gridLayer = new FormulaGridLayer();
    final NatTable natTable = new NatTable(gridPanel, gridLayer, false);
    natTable.setConfigRegistry(configRegistry);
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    final FormulaBodyLayerStack bodyLayer = gridLayer.getBodyLayer();
    natTable.addConfiguration(new FillHandleConfiguration(bodyLayer.getSelectionLayer()));
    // This is the formula specific configuration
    natTable.addConfiguration(new DefaultFormulaConfiguration(bodyLayer.getFormulaDataProvider(), bodyLayer.getSelectionLayer(), natTable.getInternalCellClipboard()));
    bodyLayer.getFormulaDataProvider().setErrorReporter(new FormulaTooltipErrorReporter(natTable, bodyLayer.getDataLayer()));
    natTable.addConfiguration(new AbstractRegistryConfiguration() {

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            PoiExcelExporter exporter = new HSSFExcelExporter();
            exporter.setApplyBackgroundColor(false);
            exporter.setFormulaParser(bodyLayer.getFormulaDataProvider().getFormulaParser());
            configRegistry.registerConfigAttribute(ExportConfigAttributes.EXPORTER, exporter);
        }
    });
    natTable.configure();
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
    final Button toggleFormulaButton = new Button(panel, SWT.PUSH);
    toggleFormulaButton.setText("Disable Formula Evaluation");
    toggleFormulaButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            _305_FormulaDataExample.this.evaluationEnabled = !_305_FormulaDataExample.this.evaluationEnabled;
            if (_305_FormulaDataExample.this.evaluationEnabled) {
                natTable.doCommand(new EnableFormulaEvaluationCommand());
                toggleFormulaButton.setText("Disable Formula Evaluation");
            } else {
                natTable.doCommand(new DisableFormulaEvaluationCommand());
                toggleFormulaButton.setText("Enable Formula Evaluation");
            }
        }
    });
    return panel;
}
Also used : Composite(org.eclipse.swt.widgets.Composite) AbstractRegistryConfiguration(org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration) EnableFormulaEvaluationCommand(org.eclipse.nebula.widgets.nattable.formula.command.EnableFormulaEvaluationCommand) DisableFormulaEvaluationCommand(org.eclipse.nebula.widgets.nattable.formula.command.DisableFormulaEvaluationCommand) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) FillHandleConfiguration(org.eclipse.nebula.widgets.nattable.fillhandle.config.FillHandleConfiguration) ConfigRegistry(org.eclipse.nebula.widgets.nattable.config.ConfigRegistry) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) FormulaTooltipErrorReporter(org.eclipse.nebula.widgets.nattable.formula.FormulaTooltipErrorReporter) GridLayout(org.eclipse.swt.layout.GridLayout) DefaultFormulaConfiguration(org.eclipse.nebula.widgets.nattable.formula.config.DefaultFormulaConfiguration) PoiExcelExporter(org.eclipse.nebula.widgets.nattable.extension.poi.PoiExcelExporter) Button(org.eclipse.swt.widgets.Button) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) SelectionEvent(org.eclipse.swt.events.SelectionEvent) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) HSSFExcelExporter(org.eclipse.nebula.widgets.nattable.extension.poi.HSSFExcelExporter)

Example 2 with EnableFormulaEvaluationCommand

use of org.eclipse.nebula.widgets.nattable.formula.command.EnableFormulaEvaluationCommand in project nebula.widgets.nattable by eclipse.

the class FormulaIntegrationTest method testDisableFormulaResolution.

@Test
public void testDisableFormulaResolution() throws InterruptedException {
    this.natTable.doCommand(new DisableFormulaEvaluationCommand());
    assertNotNull(this.natTable.getDataValueByPosition(3, 1));
    assertEquals("=A1*B1", this.natTable.getDataValueByPosition(3, 1));
    this.natTable.doCommand(new EnableFormulaEvaluationCommand());
    assertNull(this.natTable.getDataValueByPosition(3, 1));
    // wait until calculation is processed
    Thread.sleep(50);
    assertEquals(new BigDecimal("15"), this.natTable.getDataValueByPosition(3, 1));
}
Also used : DisableFormulaEvaluationCommand(org.eclipse.nebula.widgets.nattable.formula.command.DisableFormulaEvaluationCommand) EnableFormulaEvaluationCommand(org.eclipse.nebula.widgets.nattable.formula.command.EnableFormulaEvaluationCommand) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 3 with EnableFormulaEvaluationCommand

use of org.eclipse.nebula.widgets.nattable.formula.command.EnableFormulaEvaluationCommand in project nebula.widgets.nattable by eclipse.

the class NatExporter method exportLayer.

/**
 * Exports the given {@link ILayer} to the given {@link OutputStream} using
 * the provided {@link ITableExporter}.
 *
 * @param exporter
 *            The {@link ITableExporter} that should be used for exporting.
 * @param outputStream
 *            The {@link OutputStream} that should be used to write the
 *            export to.
 * @param layer
 *            The {@link ILayer} that should be exported.
 * @param configRegistry
 *            The {@link IConfigRegistry} needed to retrieve the export
 *            configurations.
 *
 * @since 1.5
 */
protected void exportLayer(final ITableExporter exporter, final OutputStream outputStream, final ILayer layer, final IConfigRegistry configRegistry) {
    if (this.preRender) {
        AutoResizeHelper.autoResize(layer, configRegistry);
    }
    IClientAreaProvider originalClientAreaProvider = layer.getClientAreaProvider();
    // This needs to be done so that the layer can return all the cells
    // not just the ones visible in the viewport
    layer.doCommand(new TurnViewportOffCommand());
    setClientAreaToMaximum(layer);
    // if a SummaryRowLayer is in the layer stack, we need to ensure that
    // the values are calculated
    layer.doCommand(new CalculateSummaryRowValuesCommand());
    // if a FormulaDataProvider is involved, we need to ensure that the
    // formula evaluation is disabled so the formula itself is exported
    // instead of the calculated value
    layer.doCommand(new DisableFormulaEvaluationCommand());
    ProgressBar progressBar = null;
    if (this.shell != null) {
        Shell childShell = new Shell(this.shell.getDisplay(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        // $NON-NLS-1$
        childShell.setText(Messages.getString("NatExporter.exporting"));
        int endRow = layer.getRowCount() - 1;
        progressBar = new ProgressBar(childShell, SWT.SMOOTH);
        progressBar.setMinimum(0);
        progressBar.setMaximum(endRow);
        progressBar.setBounds(0, 0, 400, 25);
        progressBar.setFocus();
        childShell.pack();
        childShell.open();
    }
    try {
        exporter.exportTable(this.shell, progressBar, outputStream, layer, configRegistry);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        // These must be fired at the end of the thread execution
        layer.setClientAreaProvider(originalClientAreaProvider);
        layer.doCommand(new TurnViewportOnCommand());
        layer.doCommand(new EnableFormulaEvaluationCommand());
        if (progressBar != null) {
            Shell childShell = progressBar.getShell();
            progressBar.dispose();
            childShell.dispose();
        }
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) CalculateSummaryRowValuesCommand(org.eclipse.nebula.widgets.nattable.summaryrow.command.CalculateSummaryRowValuesCommand) TurnViewportOnCommand(org.eclipse.nebula.widgets.nattable.print.command.TurnViewportOnCommand) IClientAreaProvider(org.eclipse.nebula.widgets.nattable.util.IClientAreaProvider) DisableFormulaEvaluationCommand(org.eclipse.nebula.widgets.nattable.formula.command.DisableFormulaEvaluationCommand) EnableFormulaEvaluationCommand(org.eclipse.nebula.widgets.nattable.formula.command.EnableFormulaEvaluationCommand) TurnViewportOffCommand(org.eclipse.nebula.widgets.nattable.print.command.TurnViewportOffCommand) ProgressBar(org.eclipse.swt.widgets.ProgressBar) IOException(java.io.IOException)

Aggregations

DisableFormulaEvaluationCommand (org.eclipse.nebula.widgets.nattable.formula.command.DisableFormulaEvaluationCommand)3 EnableFormulaEvaluationCommand (org.eclipse.nebula.widgets.nattable.formula.command.EnableFormulaEvaluationCommand)3 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)1 AbstractRegistryConfiguration (org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration)1 ConfigRegistry (org.eclipse.nebula.widgets.nattable.config.ConfigRegistry)1 DefaultNatTableStyleConfiguration (org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration)1 IConfigRegistry (org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)1 HSSFExcelExporter (org.eclipse.nebula.widgets.nattable.extension.poi.HSSFExcelExporter)1 PoiExcelExporter (org.eclipse.nebula.widgets.nattable.extension.poi.PoiExcelExporter)1 FillHandleConfiguration (org.eclipse.nebula.widgets.nattable.fillhandle.config.FillHandleConfiguration)1 FormulaTooltipErrorReporter (org.eclipse.nebula.widgets.nattable.formula.FormulaTooltipErrorReporter)1 DefaultFormulaConfiguration (org.eclipse.nebula.widgets.nattable.formula.config.DefaultFormulaConfiguration)1 TurnViewportOffCommand (org.eclipse.nebula.widgets.nattable.print.command.TurnViewportOffCommand)1 TurnViewportOnCommand (org.eclipse.nebula.widgets.nattable.print.command.TurnViewportOnCommand)1 CalculateSummaryRowValuesCommand (org.eclipse.nebula.widgets.nattable.summaryrow.command.CalculateSummaryRowValuesCommand)1 IClientAreaProvider (org.eclipse.nebula.widgets.nattable.util.IClientAreaProvider)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1