From: Artur Signell Date: Mon, 28 Sep 2009 13:49:25 +0000 (+0000) Subject: Fix for #3417 - ComponentLocator should not assume all parent Widgets implements... X-Git-Tag: 6.7.0.beta1~2462^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2a4fd0d30ff1c7c175cd58ccb63395ff710c80b5;p=vaadin-framework.git Fix for #3417 - ComponentLocator should not assume all parent Widgets implements HasWidgets svn changeset:8959/svn branch:6.1 --- diff --git a/src/com/vaadin/terminal/gwt/client/ComponentLocator.java b/src/com/vaadin/terminal/gwt/client/ComponentLocator.java index 82ec1a4735..a436e08b28 100644 --- a/src/com/vaadin/terminal/gwt/client/ComponentLocator.java +++ b/src/com/vaadin/terminal/gwt/client/ComponentLocator.java @@ -5,11 +5,10 @@ import java.util.Iterator; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Element; -import com.google.gwt.user.client.ui.HasWidgets; import com.google.gwt.user.client.ui.Widget; +import com.vaadin.terminal.gwt.client.ui.SubPartAware; import com.vaadin.terminal.gwt.client.ui.VView; import com.vaadin.terminal.gwt.client.ui.VWindow; -import com.vaadin.terminal.gwt.client.ui.SubPartAware; /** * ComponentLocator provides methods for uniquely identifying DOM elements using @@ -39,8 +38,9 @@ public class ComponentLocator { * EXPERIMENTAL. * * Generates a string expression (path) which uniquely identifies the target - * element . The getElementByPath method can be used for the inverse - * operation, i.e. locating an element based on the string expression. + * element. The getElementByPath method can be used for the inverse + * operation, i.e. locating an element based on the string expression: + * getElementByPath(getPathForElement(element)) == element. * * @since 5.4 * @param targetElement @@ -86,6 +86,11 @@ public class ComponentLocator { // "First parent widget: " + Util.getSimpleName(w)); String path = getPathForWidget(w); + if (path == null) { + // No path could be determined for the widget. Cannot create a + // locator string. + return null; + } // ApplicationConnection.getConsole().log( // "getPathFromWidget returned " + path); if (w.getElement() == targetElement) { @@ -216,9 +221,21 @@ public class ComponentLocator { return null; } + /** + * Creates a locator path for the given widget. The path can be used to + * uniquely identify the widget in the application. The path is in a form + * compatible with getWidgetFromPath so that + * getWidgetFromPath(getPathForWidget(widget)).equals(widget). + * + * Returns null if no path can be determined for the widget or if the widget + * is null. + * + * @param w + * @return + */ private String getPathForWidget(Widget w) { if (w == null) { - return ""; + return null; } String pid = client.getPid(w.getElement()); @@ -239,10 +256,18 @@ public class ComponentLocator { Widget parent = w.getParent(); String basePath = getPathForWidget(parent); - + if (basePath == null) { + return null; + } String simpleName = Util.getSimpleName(w); - Iterator i = ((HasWidgets) parent).iterator(); + if (!(parent instanceof Iterable)) { + // Parent does not implement Iterable so we cannot find out which + // child this is + return null; + } + + Iterator i = ((Iterable) parent).iterator(); int pos = 0; while (i.hasNext()) { Object child = i.next(); @@ -256,7 +281,7 @@ public class ComponentLocator { } } - return "NOTFOUND"; + return null; } private Widget getWidgetFromPath(String path) { @@ -276,8 +301,8 @@ public class ComponentLocator { w = (Widget) client.getPaintable(part); } else if (part.startsWith("domChild[")) { break; - } else if (w instanceof HasWidgets) { - HasWidgets parent = (HasWidgets) w; + } else if (w instanceof Iterable) { + Iterable parent = (Iterable) w; String[] split = part.split("\\[");