changeset 320:a0ded7df30f7

browser_side: based on LiberviaWidget.getWidgetsPanel, add a generic method to retrieve the first ancestor of any class
author souliane <souliane@mailoo.org>
date Fri, 03 Jan 2014 21:48:49 +0100
parents 462a0a8894e3
children bfbd9d6eb901
files browser_side/base_widget.py
diffstat 1 files changed, 17 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/browser_side/base_widget.py	Fri Jan 03 16:06:00 2014 +0100
+++ b/browser_side/base_widget.py	Fri Jan 03 21:48:49 2014 +0100
@@ -31,11 +31,13 @@
 from pyjamas.ui.Label import Label
 from pyjamas.ui.Button import Button
 from pyjamas.ui.Image import Image
+from pyjamas.ui.Widget import Widget
 from pyjamas.ui.DropWidget import DropWidget
 from pyjamas.ui.ClickListener import ClickHandler
 from pyjamas.ui import HasAlignment
 from pyjamas import DOM
 import dialog
+import logging
 from tools import LiberviaDragWidget
 from pyjamas import Window
 from __pyjamas__ import doc
@@ -197,12 +199,22 @@
         return "%s (%s)" % (self, self.__title.getText())
 
     def getWidgetsPanel(self, verbose=True):
-        # current = self was not reassuring for the code analyzer
-        current = self.getParent()
-        while current is not None and not isinstance(current, WidgetsPanel):
-            current = current.getParent()
+        return self.getParent(WidgetsPanel, verbose)
+
+    def getParent(self, class_=None, verbose=True):
+        """
+        Note: this method overrides pyjamas.ui.Widget.getParent
+        @param class_: class of the ancestor to look for or None to return the first parent
+        @param verbose: set to True to log error messages
+        @return: the parent/ancestor or None if it has not been found
+        """
+        current = Widget.getParent(self)
+        if class_ is None:
+            return current  # this is the default behavior
+        while current is not None and not isinstance(current, class_):
+            current = Widget.getParent(current)
         if current is None and verbose:
-            print "Error: can't find WidgetsPanel"
+            logging.error("Can't find parent %s for %s" % (class_, self))
         return current
 
     def onClick(self, sender):