diff options
author | Henri Sara <hesara@vaadin.com> | 2017-05-09 09:14:47 +0300 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-05-10 12:37:58 +0300 |
commit | 027b14f484f9e072ebba9b8be7d9c0f3d87d39de (patch) | |
tree | d0a7840779b88b8fbef2c28ed708a1bb42ab8b2c /client | |
parent | b5da856bdbe9961151a265db1058c7ee1393bcff (diff) | |
download | vaadin-framework-027b14f484f9e072ebba9b8be7d9c0f3d87d39de.tar.gz vaadin-framework-027b14f484f9e072ebba9b8be7d9c0f3d87d39de.zip |
Refactor WidgetUtil.findWidget()
Adds two variants:
- find widget of any type
- accept all or only exact matches
Updates uses of findWidget(e, null) in non-compatibility packages.
Diffstat (limited to 'client')
7 files changed, 85 insertions, 28 deletions
diff --git a/client/src/main/java/com/vaadin/client/WidgetUtil.java b/client/src/main/java/com/vaadin/client/WidgetUtil.java index b9ea8b81b7..176f123d18 100644 --- a/client/src/main/java/com/vaadin/client/WidgetUtil.java +++ b/client/src/main/java/com/vaadin/client/WidgetUtil.java @@ -828,6 +828,25 @@ public class WidgetUtil { }-*/; /** + * Helper method to find first instance of any Widget found by traversing + * DOM upwards from given element. + * <p> + * <strong>Note:</strong> If {@code element} is inside some widget {@code W} + * , <em>and</em> {@code W} in turn is wrapped in a {@link Composite} + * {@code C}, this method will not find {@code W} but returns {@code C}. + * This may also be the case with other Composite-like classes that hijack + * the event handling of their child widget(s). + * + * @param element + * the element where to start seeking of Widget + * @since 8.1 + */ + @SuppressWarnings("unchecked") + public static <T> T findWidget(Element element) { + return findWidget(element, null); + } + + /** * Helper method to find first instance of given Widget type found by * traversing DOM upwards from given element. * <p> @@ -837,15 +856,43 @@ public class WidgetUtil { * {@code C} or null, depending on whether the class parameter matches. This * may also be the case with other Composite-like classes that hijack the * event handling of their child widget(s). + * <p> + * Only accepts the exact class {@code class1} if not null. * * @param element * the element where to start seeking of Widget * @param class1 - * the Widget type to seek for + * the Widget type to seek for, null for any */ @SuppressWarnings("unchecked") public static <T> T findWidget(Element element, Class<? extends Widget> class1) { + return findWidget(element, class1, true); + } + + /** + * Helper method to find first instance of given Widget type found by + * traversing DOM upwards from given element. + * <p> + * <strong>Note:</strong> If {@code element} is inside some widget {@code W} + * , <em>and</em> {@code W} in turn is wrapped in a {@link Composite} + * {@code C}, this method will not find {@code W}. It returns either + * {@code C} or null, depending on whether the class parameter matches. This + * may also be the case with other Composite-like classes that hijack the + * event handling of their child widget(s). + * + * @param element + * the element where to start seeking of Widget + * @param class1 + * the Widget type to seek for + * @param exactMatch + * true to only accept class1, false to also accept its + * superclasses + * @since 8.1 + */ + @SuppressWarnings("unchecked") + public static <T> T findWidget(Element element, + Class<? extends Widget> class1, boolean exactMatch) { if (element != null) { /* First seek for the first EventListener (~Widget) from dom */ EventListener eventListener = null; @@ -861,9 +908,19 @@ public class WidgetUtil { * hierarchy */ Widget w = (Widget) eventListener; + if (class1 == null && w != null) { + return (T) w; + } while (w != null) { - if (class1 == null || w.getClass() == class1) { - return (T) w; + Class<?> widgetClass = w.getClass(); + while (widgetClass != null) { + if (widgetClass == class1) { + return (T) w; + } + // terminate after first check if looking for exact + // match + widgetClass = exactMatch ? null + : widgetClass.getSuperclass(); } w = w.getParent(); } @@ -1086,7 +1143,7 @@ public class WidgetUtil { * Fixes infocusable form fields in Safari of iOS 5.x and some Android * browsers. */ - Widget targetWidget = findWidget(target, null); + Widget targetWidget = findWidget(target); if (targetWidget instanceof com.google.gwt.user.client.ui.Focusable) { final com.google.gwt.user.client.ui.Focusable toBeFocusedWidget = (com.google.gwt.user.client.ui.Focusable) targetWidget; toBeFocusedWidget.setFocus(true); diff --git a/client/src/main/java/com/vaadin/client/componentlocator/LegacyLocatorStrategy.java b/client/src/main/java/com/vaadin/client/componentlocator/LegacyLocatorStrategy.java index 2e56918ab3..ad5a33c02e 100644 --- a/client/src/main/java/com/vaadin/client/componentlocator/LegacyLocatorStrategy.java +++ b/client/src/main/java/com/vaadin/client/componentlocator/LegacyLocatorStrategy.java @@ -213,7 +213,7 @@ public class LegacyLocatorStrategy implements LocatorStrategy { // widget to which the path is relative. Otherwise, the current // implementation simply interprets the path as if baseElement was // null. - Widget baseWidget = WidgetUtil.findWidget(baseElement, null); + Widget baseWidget = WidgetUtil.findWidget(baseElement); Widget w = getWidgetFromPath(widgetPath, baseWidget); if (w == null || !WidgetUtil.isAttachedAndDisplayed(w)) { @@ -337,8 +337,8 @@ public class LegacyLocatorStrategy implements LocatorStrategy { String childIndexString = part.substring("domChild[".length(), part.length() - 1); - if (WidgetUtil.findWidget(baseElement, - null) instanceof VAbstractOrderedLayout) { + if (WidgetUtil.findWidget( + baseElement) instanceof VAbstractOrderedLayout) { if (element.hasChildNodes()) { Element e = element.getFirstChildElement().cast(); String cn = e.getClassName(); diff --git a/client/src/main/java/com/vaadin/client/renderers/ClickableRenderer.java b/client/src/main/java/com/vaadin/client/renderers/ClickableRenderer.java index 91c81dece0..34a2df4f18 100644 --- a/client/src/main/java/com/vaadin/client/renderers/ClickableRenderer.java +++ b/client/src/main/java/com/vaadin/client/renderers/ClickableRenderer.java @@ -171,16 +171,15 @@ public abstract class ClickableRenderer<T, W extends Widget> * <strong>Note:</strong> This method may not work reliably if the grid * in question is wrapped in a {@link Composite} <em>unless</em> the * element is inside another widget that is a child of the wrapped grid; - * please refer to the note in - * {@link WidgetUtil#findWidget(Element, Class) Util.findWidget} for - * details. + * please refer to the note in {@link WidgetUtil#findWidget(Element) + * Util.findWidget} for details. * * @param e * the element whose parent grid to find * @return the parent grid or null if none found. */ private static Grid<?> findClosestParentGrid(Element e) { - Widget w = WidgetUtil.findWidget(e, null); + Widget w = WidgetUtil.findWidget(e); while (w != null && !(w instanceof Grid)) { w = w.getParent(); diff --git a/client/src/main/java/com/vaadin/client/renderers/HierarchyRenderer.java b/client/src/main/java/com/vaadin/client/renderers/HierarchyRenderer.java index 823961d316..5194f77dfb 100644 --- a/client/src/main/java/com/vaadin/client/renderers/HierarchyRenderer.java +++ b/client/src/main/java/com/vaadin/client/renderers/HierarchyRenderer.java @@ -119,7 +119,8 @@ public class HierarchyRenderer extends ClickableRenderer<Object, Widget> { leaf = isLeaf(rowDescription); if (!leaf) { collapsed = isCollapsed(rowDescription); - collapseAllowed = TreeGridConnector.isCollapseAllowed(rowDescription); + collapseAllowed = TreeGridConnector + .isCollapseAllowed(rowDescription); } } @@ -198,14 +199,14 @@ public class HierarchyRenderer extends ClickableRenderer<Object, Widget> { * @return Wrapped renderer. */ public Renderer getInnerRenderer() { - return this.innerRenderer; + return innerRenderer; } /** * Decides whether the element was rendered by {@link HierarchyRenderer} */ public static boolean isElementInHierarchyWidget(Element element) { - Widget w = WidgetUtil.findWidget(element, null); + Widget w = WidgetUtil.findWidget(element); while (w != null) { if (w instanceof HierarchyItem) { diff --git a/client/src/main/java/com/vaadin/client/ui/VWindow.java b/client/src/main/java/com/vaadin/client/ui/VWindow.java index caf41785a7..8b907f9a4a 100644 --- a/client/src/main/java/com/vaadin/client/ui/VWindow.java +++ b/client/src/main/java/com/vaadin/client/ui/VWindow.java @@ -1314,7 +1314,7 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, if (!DOM.isOrHasChild(getTopmostWindow().getElement(), target)) { // not within the modal window, but let's see if it's in the // debug window - Widget w = WidgetUtil.findWidget(target, null); + Widget w = WidgetUtil.findWidget(target); while (w != null) { if (w instanceof VDebugWindow) { return true; // allow debug-window clicks diff --git a/client/src/main/java/com/vaadin/client/ui/dd/VDragAndDropManager.java b/client/src/main/java/com/vaadin/client/ui/dd/VDragAndDropManager.java index 824bcd25f9..906165a499 100644 --- a/client/src/main/java/com/vaadin/client/ui/dd/VDragAndDropManager.java +++ b/client/src/main/java/com/vaadin/client/ui/dd/VDragAndDropManager.java @@ -56,7 +56,7 @@ import com.vaadin.shared.ui.dd.DragEventType; * {@link #get()} to get instance. * * TODO cancel drag and drop if more than one touches !? - * + * * @author Vaadin Ltd * @deprecated Replaced in 8.1 with {@link DropTargetExtensionConnector} and * {@link DragSourceExtensionConnector} @@ -419,7 +419,7 @@ public class VDragAndDropManager { */ protected VDropHandler findDragTarget(Element element) { try { - Widget w = WidgetUtil.findWidget(element, null); + Widget w = WidgetUtil.findWidget(element); if (w == null) { return null; } diff --git a/client/src/main/java/com/vaadin/client/widgets/Escalator.java b/client/src/main/java/com/vaadin/client/widgets/Escalator.java index 7d2ca986a7..903b98e000 100644 --- a/client/src/main/java/com/vaadin/client/widgets/Escalator.java +++ b/client/src/main/java/com/vaadin/client/widgets/Escalator.java @@ -702,13 +702,13 @@ public class Escalator extends Widget /*-{ var vScroll = esc.@com.vaadin.client.widgets.Escalator::verticalScrollbar; var vScrollElem = vScroll.@com.vaadin.client.widget.escalator.ScrollbarBundle::getElement()(); - + var hScroll = esc.@com.vaadin.client.widgets.Escalator::horizontalScrollbar; var hScrollElem = hScroll.@com.vaadin.client.widget.escalator.ScrollbarBundle::getElement()(); - + return $entry(function(e) { var target = e.target; - + // in case the scroll event was native (i.e. scrollbars were dragged, or // the scrollTop/Left was manually modified), the bundles have old cache // values. We need to make sure that the caches are kept up to date. @@ -729,29 +729,29 @@ public class Escalator extends Widget return $entry(function(e) { var deltaX = e.deltaX ? e.deltaX : -0.5*e.wheelDeltaX; var deltaY = e.deltaY ? e.deltaY : -0.5*e.wheelDeltaY; - + // Delta mode 0 is in pixels; we don't need to do anything... - + // A delta mode of 1 means we're scrolling by lines instead of pixels // We need to scale the number of lines by the default line height if(e.deltaMode === 1) { var brc = esc.@com.vaadin.client.widgets.Escalator::body; deltaY *= brc.@com.vaadin.client.widgets.Escalator.AbstractRowContainer::getDefaultRowHeight()(); } - + // Other delta modes aren't supported if((e.deltaMode !== undefined) && (e.deltaMode >= 2 || e.deltaMode < 0)) { var msg = "Unsupported wheel delta mode \"" + e.deltaMode + "\""; - + // Print warning message esc.@com.vaadin.client.widgets.Escalator::logWarning(*)(msg); } - + // IE8 has only delta y if (isNaN(deltaY)) { deltaY = -0.5*e.wheelDelta; } - + @com.vaadin.client.widgets.Escalator.JsniUtil::moveScrollFromEvent(*)(esc, deltaX, deltaY, e); }); }-*/; @@ -4012,7 +4012,7 @@ public class Escalator extends Widget @Override public void setNewEscalatorRowCallback( Consumer<List<TableRowElement>> callback) { - this.newEscalatorRowCallback = callback; + newEscalatorRowCallback = callback; } } @@ -6415,7 +6415,7 @@ public class Escalator extends Widget @SuppressWarnings("deprecation") com.google.gwt.user.client.Element castElement = (com.google.gwt.user.client.Element) possibleWidgetNode .cast(); - Widget w = WidgetUtil.findWidget(castElement, null); + Widget w = WidgetUtil.findWidget(castElement); // Ensure findWidget did not traverse past the cell element in the // DOM hierarchy |