Extreme Component Documentation

JDataGrid formula works with the Beans Binding

May 6,2008

This article introduce the Beans Binding can work with the JDataGrid formula.

The JDataGrid formula works in the CellFormulaContext, every function in the formula can get other cell value from CellFormulaContext.

The Beans Binding framework can bind the different property together, if the cell value in JDataGrid bind a JavaBean, the cell should have the binding expression, this can be implements by a customize function, it should get the JavaBean from the formula context.

The following is the example demonstrate a function works with Beans Binding:

static class ELPropertyFunction extends AbstractFunction {
        private static final String NAME = "ELPROPERTY";
        public ELPropertyFunction() {
            super(NAME);
        }

        @Override
        public Object evaluate(FormulaContext ctx, List args) {
            try {
                PropertyFormulaContext context = (PropertyFormulaContext) ctx;
                Object arg = args.iterator().next();
                String el = CellUtils.stringValue(arg);
                ELProperty property = ELProperty.create(el);
                if (property.isReadable(context)) {
                    Object value = property.getValue(context);
                    return value;
                } else {
                    throw new CellException(CellException.Type.ERROR_VALUE);
                }
            } catch (RuntimeException e) {
                e.printStackTrace();
                throw e;
            }
           
        }
}

This function use the ELProperty to get the binding value.

The following is the PropertyFormulaContext, it hold the JavaBean that the ELProperty needed:
public class PropertyFormulaContext extends CellFormulaContext {
              private Bean bean;
        public Bean getBean() {
                     return bean;
    }
    }

The customize FormulaFactory can return the PropertyFormulaContext:
class PropertyFormulaFactory extends CellFormulaFactory {
        public FormulaContext createFormulaContext() { 
            return new PropertyFormulaContext();
        }
    }

The following code make the the ELPropertyFunction works:

FormulaFactory.setDefault(new PropertyFormulaFactory());
FormulaFactory.getDefault().addFunction(new ELPropertyFunction());

  The method “createFormulaContext()” in FormulaFactory is a new feature in JDataGrid 2.5.0

Subscribe RSS >>download

Send Us Feedback