Author: jmorliaguet
Date: Mon Oct 24 23:28:58 2005
New Revision: 28586

Modified:
   z3lab/cpsskins/branches/jmo-perspectives/browser/rendering/engine.py
   z3lab/cpsskins/branches/jmo-perspectives/elements/__init__.py
   z3lab/cpsskins/branches/jmo-perspectives/elements/configure.zcml
   z3lab/cpsskins/branches/jmo-perspectives/elements/interfaces.py
   z3lab/cpsskins/branches/jmo-perspectives/elements/slot.py
   z3lab/cpsskins/branches/jmo-perspectives/elements/theme.py
Log:

- moved subclassed getChildNodes() method to some INodeTraverser adapter

- removed unused getTreeIndex()



Modified: z3lab/cpsskins/branches/jmo-perspectives/browser/rendering/engine.py
==============================================================================
--- z3lab/cpsskins/branches/jmo-perspectives/browser/rendering/engine.py        
(original)
+++ z3lab/cpsskins/branches/jmo-perspectives/browser/rendering/engine.py        
Mon Oct 24 23:28:58 2005
@@ -33,6 +33,7 @@
 from cpsskins.relations.interfaces import IPredicate
 from cpsskins.storage.interfaces import IRelationStorage
 from interfaces import IViewer, IRenderer, IFilter, IContextInfo
+from cpsskins.elements.interfaces import INodeTraverser
 
 import logging
 
@@ -170,7 +171,8 @@
         if ISlot.providedBy(parent):
             display = IDisplayable(parent).getEffectiveDisplay(perspective)
 
-    children = node.getChildNodes(display=display, perspective=perspective)
+    children = INodeTraverser(node).getChildNodes(display=display,
+                                                  perspective=perspective)
 
     node_index['display'] = display
     node_index['children'] = children

Modified: z3lab/cpsskins/branches/jmo-perspectives/elements/__init__.py
==============================================================================
--- z3lab/cpsskins/branches/jmo-perspectives/elements/__init__.py       
(original)
+++ z3lab/cpsskins/branches/jmo-perspectives/elements/__init__.py       Mon Oct 
24 23:28:58 2005
@@ -24,10 +24,13 @@
 from zope.app.container.interfaces import INameChooser
 from zope.app.content.interfaces import IContentType
 from zope.app.interface import queryType
+from zope.component import adapts
 from zope.interface import implements
 from zope.app.content.interfaces import IContentType
 
-from interfaces import IElement, IElementType, INode, IInnerNode, ILeaf
+from interfaces import (
+    IElement, IElementType, INode, IInnerNode, ILeaf, INodeTraverser
+    )
 
 class Element(Contained):
     """An element
@@ -66,17 +69,6 @@
     def __init__(self):
         Element.__init__(self)
 
-    def getTreeIndex(self, first=True):
-        """Return the ordered tree data structure starting from this element.
-        Elements are identified by their unique identifier.
-        """
-        index = [self.identifier]
-        if isinstance(self, Leaf):
-            return index
-        for node in self.getChildNodes():
-            index.extend(node.getTreeIndex(first=False))
-        return index
-
 class InnerNode(OrderedContainer, Node):
     """An inner node is a node that has children.
     """
@@ -86,30 +78,31 @@
         Node.__init__(self)
         OrderedContainer.__init__(self)
 
-    def getChildNodes(self, iface=None, display=None, perspective=None):
-        """Return the list of child nodes implementing the specified interface
-           Override this method if the element is a virtual container.
-        """
-        if iface is None:
-            nodes = self.values()
-        else:
-            iface_providedBy = iface.providedBy
-            nodes = [v for v in self.values()
-                       if iface_providedBy(v)]
-        return nodes
-
 class Leaf(Persistent, Node):
-    """A leaf is a node that has no children."""
-
+    """A leaf is a node that has no children.
+    """
     implements(ILeaf)
 
     def __init__(self):
         Node.__init__(self)
 
-    def getChildNodes(self, iface=None, display=None, perspective=None):
+class NodeTraverser(object):
+    """An adapter for traversing the tree.
+    """
+    adapts(INode)
+    implements(INodeTraverser)
+
+    def __init__(self, node):
+        self.node = node
+
+    def getChildNodes(self, display=None, perspective=None):
         """Return the list of child nodes implementing the specified interface
+           Override this method if the element is a virtual container.
         """
-        return []
+        node = self.node
+        if ILeaf.providedBy(node):
+            return []
+        return node.values()
 
 class ElementNameChooser:
     """An adapter to choose names for elements.

Modified: z3lab/cpsskins/branches/jmo-perspectives/elements/configure.zcml
==============================================================================
--- z3lab/cpsskins/branches/jmo-perspectives/elements/configure.zcml    
(original)
+++ z3lab/cpsskins/branches/jmo-perspectives/elements/configure.zcml    Mon Oct 
24 23:28:58 2005
@@ -13,6 +13,23 @@
       provides="zope.app.container.interfaces.INameChooser"
   />
 
+  <!-- these adapters make nodes traversable -->
+
+  <adapter
+      for=".interfaces.INode"
+      factory=".NodeTraverser"
+  />
+
+  <adapter
+      for=".interfaces.ITheme"
+      factory=".theme.ThemeTraverser"
+  />
+
+  <adapter
+      for=".interfaces.ISlot"
+      factory=".slot.SlotTraverser"
+  />
+
   <!-- this adapter makes elements presentable -->
 
   <adapter

Modified: z3lab/cpsskins/branches/jmo-perspectives/elements/interfaces.py
==============================================================================
--- z3lab/cpsskins/branches/jmo-perspectives/elements/interfaces.py     
(original)
+++ z3lab/cpsskins/branches/jmo-perspectives/elements/interfaces.py     Mon Oct 
24 23:28:58 2005
@@ -52,16 +52,7 @@
         """ """
 
 class INode(IElement):
-
-    def getTreeIndex(index=[]):
-        """Return a structure representing the elements under this element.
-        Elements are identified by their id.
-        """
-
-    def getChildNodes(iface, display, perspective):
-        """Return the list of child nodes implementing the specified interface
-        Override this method if the element is a virtual container.
-        """
+    """A generic tree node"""
 
 class IInnerNode(INode, IOrderedContainer):
     """An inner node has children"""
@@ -69,6 +60,11 @@
 class ILeaf(INode):
     """A leaf node has no children"""
 
+class INodeTraverser(Interface):
+
+    def getChildNodes(display, perspective):
+        """Return the list of child nodes
+        """
 
 # The properties of elements
 

Modified: z3lab/cpsskins/branches/jmo-perspectives/elements/slot.py
==============================================================================
--- z3lab/cpsskins/branches/jmo-perspectives/elements/slot.py   (original)
+++ z3lab/cpsskins/branches/jmo-perspectives/elements/slot.py   Mon Oct 24 
23:28:58 2005
@@ -18,9 +18,12 @@
 __docformat__ = "reStructuredText"
 
 from zope.interface import implements
+from zope.component import adapts
 
 from cpsskins.elements import InnerNode
-from cpsskins.elements.interfaces import ILeaf, ISlot, IPortlet
+from cpsskins.elements.interfaces import (
+    ILeaf, ISlot, INode, INodeTraverser, IPortlet,
+    )
 from cpsskins.ontology import hasPortlet, hasPortletFromPerspective
 from cpsskins.relations.tool import RelationTool
 from cpsskins.storage.interfaces import IPortletStorage
@@ -52,39 +55,6 @@
     def __repr__(self):
         return "Slot('%s', '%s')" % (self.title, str(self))
 
-    def getChildNodes(self, iface=None, display=None, perspective=None):
-        """Return the list of child nodes implementing the specified interface
-           Override this method if the element is a virtual container.
-        """
-        if iface and not iface is IPortlet:
-            return []
-
-        reltool = RelationTool(self)
-
-        if perspective is None:
-            portlets = reltool.getSeconds(
-                predicate=hasPortlet,
-                first=self,
-                )
-        else:
-            portlets = reltool.getSeconds(
-                predicate=hasPortletFromPerspective,
-                first=self,
-                third=perspective,
-                )
-
-        # the portlet order is obtained from the display, if the display is
-        # iterable otherwise the portlets are sorted by their identifier.
-        def cmp_by_order(a, b):
-           if a in display and b in display:
-               index = display.index
-               return cmp(index(a), index(b))
-           return cmp(a.identifier, b.identifier)
-
-        # sort the portlets
-        portlets.sort(cmp_by_order)
-        return portlets
-
     def __getitem__(self, name):
         """Get a portlet by name"""
         tmutil = getThemeManager()
@@ -110,6 +80,44 @@
         theme = tmutil.getThemeInContext(self)
         return k in theme.getStorage(IPortletStorage)
 
+class SlotTraverser(object):
+    """This adapter makes slots traversable (as nodes).
+    """
+    adapts(INode)
+    implements(INodeTraverser)
+
+    def __init__(self, node):
+        self.node = node
+
+    def getChildNodes(self, display=None, perspective=None):
+        """Return the list of child nodes implementing the specified interface
+           Override this method if the element is a virtual container.
+        """
+        node = self.node
+        reltool = RelationTool(node)
+
+        if perspective is None:
+            portlets = reltool.getSeconds(
+                predicate=hasPortlet,
+                first=node)
+        else:
+            portlets = reltool.getSeconds(
+                predicate=hasPortletFromPerspective,
+                first=node,
+                third=perspective)
+
+        # the portlet order is obtained from the display, if the display is
+        # iterable otherwise the portlets are sorted by their identifier.
+        def cmp_by_order(a, b):
+           if a in display and b in display:
+               index = display.index
+               return cmp(index(a), index(b))
+           return cmp(a.identifier, b.identifier)
+
+        # sort the portlets
+        #portlets.sort(cmp_by_order)
+        return portlets
+
 class Sublocations(object):
     """Get the sublocations of the slot
 

Modified: z3lab/cpsskins/branches/jmo-perspectives/elements/theme.py
==============================================================================
--- z3lab/cpsskins/branches/jmo-perspectives/elements/theme.py  (original)
+++ z3lab/cpsskins/branches/jmo-perspectives/elements/theme.py  Mon Oct 24 
23:28:58 2005
@@ -21,8 +21,8 @@
 from zope.component import adapts, getUtility, getUtilitiesFor
 from zope.interface import implements
 
-from cpsskins.configuration.interfaces import IRegistry
 from cpsskins.elements import InnerNode
+from cpsskins.elements.interfaces import INode, INodeTraverser
 from cpsskins.perspectives.interfaces import IPerspective
 from cpsskins.storage.interfaces import IPerspectiveStorage
 from cpsskins.thememanager import getThemeManager
@@ -39,8 +39,6 @@
     """
     implements(ITheme)
 
-    _getChildNodes = InnerNode.getChildNodes
-
     def __init__(self, title=''):
         InnerNode.__init__(self)
         self.title = title
@@ -56,15 +54,6 @@
                 return page
         return None
 
-    def getChildNodes(self, iface=None, display=None, perspective=None):
-        """Get the child elements in the rendering tree.
-        We override InnerNode.getChildNodes() to return only the page that
-        will be displayed.
-
-        TODO: Return the effective page instead
-        """
-        return [self.getDefaultPage()]
-
     def getPages(self):
         """Return the list of pages
 
@@ -77,15 +66,14 @@
         >>> theme.getPages() == [page]
         True
         """
-        return self._getChildNodes(iface=IThemePage)
+        return [v for v in self.values()
+               if IThemePage.providedBy(v)]
 
     def setAsDefault(self):
-        tmutil = getThemeManager()
-        return tmutil.setAsDefault(self)
+        return getThemeManager().setAsDefault(self)
 
     def isDefault(self):
-        tmutil = getThemeManager()
-        return tmutil.isDefault(self)
+        return getThemeManager().isDefault(self)
 
     def getStorage(self, iface):
         """Return a storage implementing a given interface
@@ -115,3 +103,18 @@
         perspectives = self.getStorage(IPerspectiveStorage)
         return [str(p) for p in perspectives.values()] \
              + [p[0] for p in list(getUtilitiesFor(IPerspective))]
+
+class ThemeTraverser(object):
+    """This adapter makes themes traversable.
+    """
+    adapts(INode)
+    implements(INodeTraverser)
+
+    def __init__(self, node):
+        self.node = node
+
+    def getChildNodes(self, iface=None, display=None, perspective=None):
+        """Get the child elements in the rendering tree.
+        TODO: Return the effective page instead
+        """
+        return [self.node.getDefaultPage()]
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to