Search in sources :

Example 1 with IDataValidator

use of org.eclipse.nebula.widgets.nattable.data.validate.IDataValidator in project nebula.widgets.nattable by eclipse.

the class MultiCellEditDialogRunner method shouldOpenDefaultDialogWithoutIncrementDecrementBox.

public void shouldOpenDefaultDialogWithoutIncrementDecrementBox() {
    CellFixture cell = new CellFixture();
    cell.setBounds(new Rectangle(100, 100, 100, 20));
    cell.setConfigLabels(new LabelStack("Cell_Edit"));
    cell.setDataValue("123");
    cell.setDisplayMode(DisplayMode.NORMAL);
    TextCellEditor cellEditor = new TextCellEditor();
    IDisplayConverter dataTypeConverter = new DisplayConverter() {

        @Override
        public Object canonicalToDisplayValue(Object canonicalValue) {
            return canonicalValue;
        }

        @Override
        public Object displayToCanonicalValue(Object displayValue) {
            return displayValue;
        }
    };
    final Character newValue = Character.valueOf('4');
    IDataValidator dataValidator = new DataValidator() {

        @Override
        public boolean validate(int columnIndex, int rowIndex, Object newValue) {
            Assert.assertEquals(newValue, newValue);
            return false;
        }
    };
    Shell shell = new Shell(Display.getDefault(), SWT.H_SCROLL | SWT.V_SCROLL | SWT.RESIZE);
    ConfigRegistry configRegistry = new ConfigRegistry();
    configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, dataTypeConverter);
    configRegistry.registerConfigAttribute(EditConfigAttributes.DATA_VALIDATOR, dataValidator);
    final CellEditDialog dialog = new CellEditDialog(shell, newValue, cell, cellEditor, configRegistry);
    if (!this.interactive) {
        Display.getDefault().asyncExec(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    dialog.close();
                }
            }
        });
    }
    dialog.open();
}
Also used : CellFixture(org.eclipse.nebula.widgets.nattable.test.fixture.layer.CellFixture) LabelStack(org.eclipse.nebula.widgets.nattable.layer.LabelStack) IDataValidator(org.eclipse.nebula.widgets.nattable.data.validate.IDataValidator) DisplayConverter(org.eclipse.nebula.widgets.nattable.data.convert.DisplayConverter) IDisplayConverter(org.eclipse.nebula.widgets.nattable.data.convert.IDisplayConverter) Rectangle(org.eclipse.swt.graphics.Rectangle) IDisplayConverter(org.eclipse.nebula.widgets.nattable.data.convert.IDisplayConverter) ConfigRegistry(org.eclipse.nebula.widgets.nattable.config.ConfigRegistry) Shell(org.eclipse.swt.widgets.Shell) IDataValidator(org.eclipse.nebula.widgets.nattable.data.validate.IDataValidator) DataValidator(org.eclipse.nebula.widgets.nattable.data.validate.DataValidator) TextCellEditor(org.eclipse.nebula.widgets.nattable.edit.editor.TextCellEditor)

Example 2 with IDataValidator

use of org.eclipse.nebula.widgets.nattable.data.validate.IDataValidator in project nebula.widgets.nattable by eclipse.

the class TickUpdateCommandHandler method updateSingleCell.

/**
 * Will calculate the new value after tick update processing for the cell at
 * the given coordinates, trying to update the value represented by that
 * cell. The update will only be processed if the new value is valid.
 *
 * @param command
 *            The command to process
 * @param selectedPosition
 *            The coordinates of the cell on which the tick update should be
 *            executed
 */
private void updateSingleCell(TickUpdateCommand command, PositionCoordinate selectedPosition) {
    if (selectedPosition != null) {
        ILayerCell cell = this.selectionLayer.getCellByPosition(selectedPosition.columnPosition, selectedPosition.rowPosition);
        IConfigRegistry configRegistry = command.getConfigRegistry();
        IEditableRule editableRule = configRegistry.getConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, DisplayMode.EDIT, cell.getConfigLabels().getLabels());
        IDataValidator validator = configRegistry.getConfigAttribute(EditConfigAttributes.DATA_VALIDATOR, DisplayMode.EDIT, cell.getConfigLabels().getLabels());
        if (editableRule.isEditable(cell, configRegistry)) {
            // process the tick update
            Object newValue = getNewCellValue(command, cell);
            // validate the value
            try {
                if (validator == null || validator.validate(cell, configRegistry, newValue)) {
                    this.selectionLayer.doCommand(new UpdateDataCommand(this.selectionLayer, selectedPosition.columnPosition, selectedPosition.rowPosition, newValue));
                } else {
                    LOG.warn(// $NON-NLS-1$ //$NON-NLS-2$
                    "Tick update failed for cell at " + selectedPosition + " and value " + newValue + // $NON-NLS-1$
                    ". New value is not valid!");
                }
            } catch (Exception e) {
                LOG.warn(// $NON-NLS-1$ //$NON-NLS-2$
                "Tick update failed for cell at " + selectedPosition + " and value " + newValue + ". " + // $NON-NLS-1$
                e.getLocalizedMessage());
            }
        }
    }
}
Also used : IDataValidator(org.eclipse.nebula.widgets.nattable.data.validate.IDataValidator) IEditableRule(org.eclipse.nebula.widgets.nattable.config.IEditableRule) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) UpdateDataCommand(org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)

Aggregations

IDataValidator (org.eclipse.nebula.widgets.nattable.data.validate.IDataValidator)2 ConfigRegistry (org.eclipse.nebula.widgets.nattable.config.ConfigRegistry)1 IConfigRegistry (org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)1 IEditableRule (org.eclipse.nebula.widgets.nattable.config.IEditableRule)1 DisplayConverter (org.eclipse.nebula.widgets.nattable.data.convert.DisplayConverter)1 IDisplayConverter (org.eclipse.nebula.widgets.nattable.data.convert.IDisplayConverter)1 DataValidator (org.eclipse.nebula.widgets.nattable.data.validate.DataValidator)1 UpdateDataCommand (org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommand)1 TextCellEditor (org.eclipse.nebula.widgets.nattable.edit.editor.TextCellEditor)1 LabelStack (org.eclipse.nebula.widgets.nattable.layer.LabelStack)1 ILayerCell (org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)1 CellFixture (org.eclipse.nebula.widgets.nattable.test.fixture.layer.CellFixture)1 Rectangle (org.eclipse.swt.graphics.Rectangle)1 Shell (org.eclipse.swt.widgets.Shell)1