"""

    Dropdown column definition for DataGridField

    Copyright 2006 Mikko Ohtamaa


"""

from __future__ import nested_scopes
__author__ = "Mikko Ohtamaa <mikko@redinnovation.com>"
__docformat__ = 'epytext'

# Python imports
import StringIO


# Zope imports
from AccessControl import ClassSecurityInfo
from AccessControl import getSecurityManager
from Globals import InitializeClass

from ZODB.POSException import ConflictError

# Plone imports
from Products.CMFCore.utils import getToolByName
from Products.Archetypes.public import *
from Products.Archetypes.Field import ObjectField, encode, decode, registerField

# Local imports
from Products.DataGridField import DataGridWidget
from Products.DataGridField.interfaces import IDataGridField
from Products.DataGridField.Column import Column


class SelectColumn(Column):
    """ Defines column with dropdown menu cells in DataGridField """

    security = ClassSecurityInfo()

    def __init__(self, title, vocabulary, default=None):
        """ Create a SelectColumn

        @param vocabulary Vocabulary method name. This method is called
               from Archetypes instance to get values for dropdown list.
        """
        Column.__init__(self, title, default=default)
        self.vocabulary = vocabulary


    security.declarePublic('getVocabulary')
    def getVocabulary(self, instance):
        """ Gets this column vocabulary for specific Archetypes instance
        """
        try:
            func = getattr(instance, self.vocabulary)
        except AttributeError:
            raise AttributeError, "Class %s is missing vocabulary function %s" % (str(instance), self.vocabulary)

        return func()


    security.declarePublic('getMacro')
    def getMacro(self):
        """ Return macro used to render this column in view/edit """
        return "datagrid_select_cell"


# Initializes class security
InitializeClass(SelectColumn)
