    FieldColumn
    ===========

    The FieldColumn is intended to be a replacement for the FieldEditColumn.  It
    has the following goals.

    - Support the standard column pattern of instantiating a single module global
      column set, and using formatters to store and access information about a
      single given rendering.  The formatter has annotations, prefix, context, and
      request, all of which are desired or even essential for various derived
      columns.  Generally, the formatter has state that the columns need.

    - Support display widgets, in addition to input widgets.

    - Support formlib form fields, to support custom widgets and other formlib
      field features.

    It also has an important design difference.  Rather than relying on functions
    passed at initialization for column customization, the field column returns to
    a standard subclass design.  The column expects that any of the following
    methods may be overridden:

    - getId(item, formatter): given an item whose value is to be displayed in the
      column, return a form-friendly string identifier, unique to the item.  We
      define 'form-friendly' as containing characters that are alphanumeric or a
      space, a plus, a slash, or an equals sign (that is, the standard characters
      used in MIME-based base64, excluding new lines).

    - get(item, formatter): return the current value for the item.

    - set(item, value, formatter): set the given value for the item.

    - getFieldContext(item, formatter): return a context to which a field should
      be bound, or None.

    - renderCell(item, formatter): the standard rendering of a cell

    - renderHeader(formatter): the standard rendering of a column header.

    Without subclassing, the column uses field.get and field.set to get and set
    values, returns None for bound context, and just renders the field's widget for
    the cell, and the title for the header.  Let's look at the default behavior.
    We'll use a modified version of the examples given for the FieldEditColumn in
    column.txt.

    To create a FieldColumn, you must minimally provide a schema field or form
    field.  If title is not provided, it is the field's title, or empty if the
    field has no title.  A explicit title of an empty string is acceptable and will
    be honored.  If name is not provided, it is the field's __name__.

    Let's look at a simple example.  We have a collection of contacts,
    with names and email addresses:

        >>> import re
        >>> from zope import schema, interface
        >>> class IContact(interface.Interface):
        ...     name = schema.TextLine(title=u'Name')
        ...     email = schema.TextLine(
        ...             title=u'Email Address',
        ...             constraint=re.compile(r'\w+@\w+([.]\w+)+$').match)
        ...     salutation = schema.Choice(
        ...             title=u'Salutation',
        ...             values = ['Mr','Ms'],
        ...             )

        >>> @interface.implementer(IContact)
        ... class Contact:
        ...     def __init__(self, id, name, email, salutation):
        ...         self.id = id
        ...         self.name = name
        ...         self.email = email
        ...         self.salutation = salutation

        >>> contacts = (
        ...     Contact('1', 'Bob Smith', 'bob@zope.com', 'Mr'),
        ...     Contact('2', 'Sally Baker', 'sally@zope.com', 'Ms'),
        ...     Contact('3', 'Jethro Tul', 'jethro@zope.com', 'Mr'),
        ...     Contact('4', 'Joe Walsh', 'joe@zope.com', 'Mr'),
        ...     )

    We'll define columns that allow us to display and edit name and
    email addresses.

        >>> from zc.table import fieldcolumn
        >>> class ContactColumn(fieldcolumn.FieldColumn):
        ...     def getId(self, item, formatter):
        ...         return fieldcolumn.toSafe(item.id)
        ...
        >>> class BindingContactColumn(ContactColumn):
        ...     def getFieldContext(self, item, formatter):
        ...         return item
        ...
        >>> columns = (ContactColumn(IContact["name"]),
        ...            ContactColumn(IContact["email"]),
        ...            BindingContactColumn(IContact["salutation"])
        ...            )

    Now, with this, we can create a table with input widgets.  The columns don't
    need a context other than the items themselves, so we ignore that part of the
    table formatter instantiation:

        >>> from zc import table
        >>> import zope.publisher.browser
        >>> request = zope.publisher.browser.TestRequest()
        >>> context = None
        >>> formatter = table.Formatter(
        ...     context, request, contacts, columns=columns, prefix='test')
        >>> print(formatter())

Name | Email Address | Salutation
--- | --- | ---

Note that the input names do not include base64 encodings of the item ids because they already match the necessary constraints. If the request has input for a value, then this will override item data: >>> request.form["test.4.email"] = u'walsh@zope.com' >>> print(formatter())

Name | Email Address | Salutation
--- | --- | ---

    and the contact data is unchanged:

        >>> contacts[3].email
        'joe@zope.com'

    Field edit columns provide methods for getting and validating input
    data, and for updating the undelying data:

        >>> data = columns[1].input(contacts, formatter)
        >>> data
        {'4': u'walsh@zope.com'}

    The data returned is a mapping from item id to input value.  Items
    that don't have input are ignored.  The data can be used with the
    update function to update the underlying data:

        >>> columns[1].update(contacts, data, formatter)
        True

        >>> contacts[3].email
        u'walsh@zope.com'

    Note that the update function returns a boolean value indicating
    whether any changes were made:

        >>> columns[1].update(contacts, data, formatter)
        False

    The input function also validates input.  If there are any errors, a
    WidgetsError will be raised:

        >>> request.form["test.4.email"] = u'walsh'
        >>> try:
        ...     data = columns[1].input(contacts, formatter)
        ... except zope.formlib.interfaces.WidgetsError as e:
        ...     e
        WidgetInputError:
            ('email', u'Email Address', ConstraintNotSatisfied(u'walsh', 'email'))

    Custom getters and setters
    --------------------------

    Normally, the given fields getter and setter is used, however, custom
    getters and setters can be provided.  Let's look at an example of
    a bit table:

        >>> data = [0, 0], [1, 1], [2, 2], [3, 3]

        >>> class BitColumn(fieldcolumn.FieldColumn):
        ...     def __init__(self, field, bit, title=None, name=''):
        ...         super(BitColumn, self).__init__(field, title, name)
        ...         self.bit = bit
        ...     def getId(self, item, formatter):
        ...         return str(item[0])
        ...     def get(self, item, formatter):
        ...         return (1 << self.bit)&(item[1])
        ...     def set(self, item, value, formatter):
        ...         value = bool(value) << self.bit
        ...         mask = 1 << self.bit
        ...         item[1] = ((item[1] | mask) ^ mask) | value

        >>> columns = (
        ...     BitColumn(schema.Bool(__name__='0', title=u'Bit 0'), 0),
        ...     BitColumn(schema.Bool(__name__='1', title=u'Bit 1'), 1))

        >>> context = None # not needed
        >>> request = zope.publisher.browser.TestRequest()
        >>> formatter = table.Formatter(
        ...     context, request, data, columns=columns, prefix='test')
        >>> print(formatter())

Bit 0 | Bit 1
--- | ---

        >>> request.form["test.3.1.used"] = ""
        >>> request.form["test.0.1.used"] = ""
        >>> request.form["test.0.1"] = "on"

        >>> input = columns[1].input(data, formatter)
        >>> from pprint import pprint
        >>> pprint(input)
        {'0': True,
         '3': False}

        >>> columns[1].update(data, input, formatter)
        True

        >>> data
        ([0, 2], [1, 1], [2, 2], [3, 1])