[Zope3-checkins] CVS: Zope3/src/zope/app/browser/form - widget.py:1.29.4.2

Fred L. Drake, Jr. fred@zope.com
Fri, 2 May 2003 18:05:08 -0400


Update of /cvs-repository/Zope3/src/zope/app/browser/form
In directory cvs.zope.org:/tmp/cvs-serv28903

Modified Files:
      Tag: schema-vocabulary-branch
	widget.py 
Log Message:
- re-organize somewhat: the view constructor does call the constructed
  widget, since it may be needed outside simple rendering (for
  example, by the forms machinery)
- add a simple display widget


=== Zope3/src/zope/app/browser/form/widget.py 1.29.4.1 => 1.29.4.2 ===
--- Zope3/src/zope/app/browser/form/widget.py:1.29.4.1	Fri May  2 12:26:25 2003
+++ Zope3/src/zope/app/browser/form/widget.py	Fri May  2 18:05:07 2003
@@ -82,7 +82,6 @@
         return value
 
     def _showData(self):
-
         if (self._data is None):
             if self.haveData():
                 data = self.getData(1)
@@ -111,8 +110,6 @@
                              cssClass = self.getValue('cssClass'),
                              extra = self.getValue('extra'))
 
-
-
     def render(self, value):
         self.setData(value)
         return self()
@@ -698,22 +695,56 @@
 def VocabularyFieldDisplayWidget(field, request):
     """Return a display widget based on a vocabulary field."""
     view = getView(field.vocabulary, "field-display-widget", request)
-    return view(field)
+    view.setField(field)
+    return view
 
 def VocabularyFieldEditWidget(field, request):
     """Return a value-selection widget based on a vocabulary field."""
     view = getView(field.vocabulary, "field-edit-widget", request)
-    return view(field)
+    view.setField(field)
+    return view
 
 def VocabularyMultiFieldDisplayWidget(field, request):
     """Return a display widget based on a vocabulary field."""
     view = getView(field.vocabulary, "field-display-multi-widget", request)
-    return view(field)
+    view.setField(field)
+    return view
 
 def VocabularyMultiFieldEditWidget(field, request):
     """Return a value-selection widget based on a vocabulary field."""
     view = getView(field.vocabulary, "field-edit-multi-widget", request)
-    return view(field)
+    view.setField(field)
+    return view
+
+
+class VocabularyWidgetBase(BrowserWidget):
+    """Convenience base class for vocabulary-based widgets."""
+
+    def __init__(self, context, request):
+        self.context = context
+        self.request = request
+        self.field = None
+
+    def setField(self, field):
+        assert self.field is None
+        # only allow this to happen for a bound field
+        assert field.context is not None
+        self.field = field
+        self.name = self._prefix + field.__name__
+
+    def __call__(self):
+        return self.render()
+
+    def render(self):
+        raise NotImplementedError(
+            "render() must be implemented by a subclass")
+
+
+class VocabularyDisplayWidget(VocabularyWidgetBase):
+    """Simple single-selection display that can be used in many cases."""
+
+    def render(self):
+        return str(self.field.get(self.field.context))
 
 
 # XXX Note, some HTML quoting is needed in renderTag and renderElement.