diff options
author | Henri Sara <hesara@vaadin.com> | 2013-10-01 14:35:17 +0300 |
---|---|---|
committer | Henri Sara <hesara@vaadin.com> | 2013-10-01 14:35:17 +0300 |
commit | 5cc968b378ccf99ed511deaadd8c47073c300365 (patch) | |
tree | adc0a0de87c15ec382cd3717398c41d31714f18e | |
parent | a9f332fddb5ff70338527c1ddad3ca40dca70d6d (diff) | |
download | vaadin-framework-5cc968b378ccf99ed511deaadd8c47073c300365.tar.gz vaadin-framework-5cc968b378ccf99ed511deaadd8c47073c300365.zip |
Support mixed legacy and VaadinFinder locators (#12641, #12642, #12643)
ComponentLocator tries to use all available locator strategies until a
match is found (#12641) and VaadinFinderLocatorStrategy also accepts
simple indexed paths (#12642) with legacy locators as a backup. To
enable mixed use of locators, LegacyLocatorStrategy now implements
getElementByPathStartingAt() (#12643).
Change-Id: I6b763565adef0e294b353ef6e2dfdf70ae0d77a9
4 files changed, 36 insertions, 55 deletions
diff --git a/client/src/com/vaadin/client/componentlocator/ComponentLocator.java b/client/src/com/vaadin/client/componentlocator/ComponentLocator.java index c1f117d992..e319650d23 100644 --- a/client/src/com/vaadin/client/componentlocator/ComponentLocator.java +++ b/client/src/com/vaadin/client/componentlocator/ComponentLocator.java @@ -131,8 +131,9 @@ public class ComponentLocator { // always used as a last resort if no other strategies claim // responsibility for the path syntax. for (LocatorStrategy strategy : locatorStrategies) { - if (strategy.handlesPathSyntax(path)) { - return strategy.getElementByPath(path); + Element element = strategy.getElementByPath(path); + if (null != element) { + return element; } } return null; @@ -156,17 +157,22 @@ public class ComponentLocator { // always used as a last resort if no other strategies claim // responsibility for the path syntax. for (LocatorStrategy strategy : locatorStrategies) { - if (strategy.handlesPathSyntax(path)) { - return strategy.getElementByPathStartingAt(path, root); + Element element = strategy.getElementByPathStartingAt(path, root); + if (null != element) { + return element; } } return null; } /** + * Returns the {@link ApplicationConnection} used by this locator. + * <p> + * This method is primarily for internal use by the framework. + * * @return the application connection */ - ApplicationConnection getClient() { + public ApplicationConnection getClient() { return client; } } diff --git a/client/src/com/vaadin/client/componentlocator/LegacyLocatorStrategy.java b/client/src/com/vaadin/client/componentlocator/LegacyLocatorStrategy.java index 34f5967092..ed70486ff5 100644 --- a/client/src/com/vaadin/client/componentlocator/LegacyLocatorStrategy.java +++ b/client/src/com/vaadin/client/componentlocator/LegacyLocatorStrategy.java @@ -177,17 +177,30 @@ public class LegacyLocatorStrategy implements LocatorStrategy { @Override public Element getElementByPath(String path) { + return getElementByPathStartingAt(path, null); + } + + @Override + public Element getElementByPathStartingAt(String path, Element baseElement) { /* * Path is of type "targetWidgetPath#componentPart" or * "targetWidgetPath". */ String parts[] = path.split(LegacyLocatorStrategy.SUBPART_SEPARATOR, 2); String widgetPath = parts[0]; - Widget w = getWidgetFromPath(widgetPath); + + Widget baseWidget = null; + if (null != baseElement) { + // Note that this only works if baseElement can be mapped to a + // widget to which the path is relative. Otherwise, the current + // implementation simply interprets the path as if baseElement was + // null. + baseWidget = Util.findWidget(baseElement, null); + } + Widget w = getWidgetFromPath(widgetPath, baseWidget); if (w == null || !Util.isAttachedAndDisplayed(w)) { return null; } - if (parts.length == 1) { int pos = widgetPath.indexOf("domChild"); if (pos == -1) { @@ -196,7 +209,7 @@ public class LegacyLocatorStrategy implements LocatorStrategy { // Contains dom reference to a sub element of the widget String subPath = widgetPath.substring(pos); - return getElementByDOMPath(w.getElement(), subPath); + return getElementByDOMPath(baseElement, subPath); } else if (parts.length == 2) { if (w instanceof SubPartAware) { return ((SubPartAware) w).getSubPartElement(parts[1]); @@ -205,19 +218,6 @@ public class LegacyLocatorStrategy implements LocatorStrategy { return null; } - @Override - public Element getElementByPathStartingAt(String path, Element root) { - // Not supported by the legacy format - return null; - } - - @Override - public boolean handlesPathSyntax(String path) { - // The legacy strategy is always used if all else fails, so just return - // true here. - return true; - } - /** * Finds the first widget in the hierarchy (moving upwards) that implements * SubPartAware. Returns the SubPartAware implementor or null if none is @@ -364,7 +364,7 @@ public class LegacyLocatorStrategy implements LocatorStrategy { /** * Creates a locator String for the given widget. The path can be used to - * locate the widget using {@link #getWidgetFromPath(String)}. + * locate the widget using {@link #getWidgetFromPath(String, Widget)}. * <p/> * Returns null if no path can be determined for the widget or if the widget * is null. @@ -436,11 +436,14 @@ public class LegacyLocatorStrategy implements LocatorStrategy { * * @param path * The String locator that identifies the widget. + * @param baseWidget + * the widget to which the path is relative, null if relative to + * root * @return The Widget identified by the String locator or null if the widget * could not be identified. */ - private Widget getWidgetFromPath(String path) { - Widget w = null; + private Widget getWidgetFromPath(String path, Widget baseWidget) { + Widget w = baseWidget; String parts[] = path.split(PARENTCHILD_SEPARATOR); for (int i = 0; i < parts.length; i++) { @@ -517,7 +520,7 @@ public class LegacyLocatorStrategy implements LocatorStrategy { .next(); } /* - * The new grid and ordered layotus do not contain + * The new grid and ordered layouts do not contain * ChildComponentContainer widgets. This is instead simulated by * constructing a path step that would find the desired widget * from the layout and injecting it as the next search step diff --git a/client/src/com/vaadin/client/componentlocator/LocatorStrategy.java b/client/src/com/vaadin/client/componentlocator/LocatorStrategy.java index 835b81bdae..56ed396609 100644 --- a/client/src/com/vaadin/client/componentlocator/LocatorStrategy.java +++ b/client/src/com/vaadin/client/componentlocator/LocatorStrategy.java @@ -74,17 +74,4 @@ public interface LocatorStrategy { * could not be located. */ Element getElementByPathStartingAt(String path, Element root); - - /** - * Allows the component locator orchestrator to determine whether this - * strategy should be used to locate an element using the provided path. - * Paths can have (slightly) different syntax, and each locator strategy - * should inspect the path string to see if it can be used to locate the - * element by the path in question. - * - * @param path - * The path whose syntax to check whether handled or not - * @return true if this strategy handles the path syntax in question - */ - boolean handlesPathSyntax(String path); } diff --git a/client/src/com/vaadin/client/componentlocator/VaadinFinderLocatorStrategy.java b/client/src/com/vaadin/client/componentlocator/VaadinFinderLocatorStrategy.java index 3516aa3e93..a95b2013ed 100644 --- a/client/src/com/vaadin/client/componentlocator/VaadinFinderLocatorStrategy.java +++ b/client/src/com/vaadin/client/componentlocator/VaadinFinderLocatorStrategy.java @@ -18,7 +18,6 @@ package com.vaadin.client.componentlocator; import java.util.ArrayList; import java.util.List; -import com.google.gwt.regexp.shared.RegExp; import com.google.gwt.user.client.Element; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; @@ -51,7 +50,8 @@ import com.vaadin.shared.AbstractComponentState; */ public class VaadinFinderLocatorStrategy implements LocatorStrategy { - private static final String SUBPART_SEPARATOR = "#"; + public static final String SUBPART_SEPARATOR = "#"; + private ComponentLocator componentLocator; public VaadinFinderLocatorStrategy(ComponentLocator componentLocator) { @@ -372,19 +372,4 @@ public class VaadinFinderLocatorStrategy implements LocatorStrategy { return new String[] { path }; } - /** - * Matches a string that contains either double slashes ({@code //}) or a - * complex predicate ({@code [caption = "foo"]}) - */ - private static RegExp syntaxMatcher = RegExp - .compile("^.*//.*$|^.*\\[\\w+.*=.*\\].*$"); - - /** - * {@inheritDoc} - */ - @Override - public boolean handlesPathSyntax(String path) { - // If the path contains a double-slash at any point, it's probably ours. - return syntaxMatcher.test(path); - } } |