# HG changeset patch # User souliane # Date 1388782129 -3600 # Node ID a0ded7df30f73a43ac2fe52b43010305805a642c # Parent 462a0a8894e3f4e9a547c0ed2fa1b778d6ec9e47 browser_side: based on LiberviaWidget.getWidgetsPanel, add a generic method to retrieve the first ancestor of any class diff -r 462a0a8894e3 -r a0ded7df30f7 browser_side/base_widget.py --- 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):