'Returns the grid/matrix of checkboxes as a web2py TABLE object and
adds references to required Javascript files.
@type field: Field
@param field:
This gets passed in when the widget is rendered or used.
@type value: list
@param value:
A list of the values matching those of the checkboxes.
@param attributes:
HTML attributes to assign to the table.'

def __call__(self, field, value, **attributes):

    if isinstance(value, (list, tuple)):
        values = [str(v) for v in value]
    else:
        values = [str(value)]
    header_cells = []
    for col in self.cols:
        header_cells.append(TH(col, _scope='col'))
    header = THEAD(TR(header_cells))
    grid_rows = []
    for row in self.rows:
        row_cells = [TH(row[0], _scope='row')]
        for option in row[1:]:
            if (option in values):
                checked = True
            else:
                checked = False
            row_cells.append(TD(INPUT(_type='checkbox', _name=field.name, _value=option, value=checked)))
        grid_rows.append(TR(row_cells))
    s3 = current.response.s3
    s3.scripts.append(('/%s/static/scripts/S3/s3.optionsmatrix.js' % current.request.application))
    if ('_id' in attributes):
        s3.jquery_ready.append("$('#{0}').s3optionsmatrix()".format(attributes.get('_id')))
    return TABLE(header, TBODY(grid_rows), **attributes)
