From 7d1b06cbc4dd07b8b40fbc63d148027fd4c9cf4b Mon Sep 17 00:00:00 2001 From: Aleksi Hietanen Date: Wed, 19 Apr 2017 13:54:16 +0300 Subject: Fix client-side memory leak caused by Grid events (#9103) Refactors AbstractGridKeyEvent, AbstractGridMouseEvent and their descendants to follow the pattern used in other GWT DomEvents. Fixes #7633 --- .../client/widget/grid/events/GridClickEvent.java | 20 +++- .../widget/grid/events/GridDoubleClickEvent.java | 21 +++- .../widget/grid/events/GridKeyDownEvent.java | 20 +++- .../widget/grid/events/GridKeyPressEvent.java | 20 +++- .../client/widget/grid/events/GridKeyUpEvent.java | 20 +++- .../java/com/vaadin/v7/client/widgets/Grid.java | 132 ++++++++++++--------- 6 files changed, 169 insertions(+), 64 deletions(-) (limited to 'compatibility-client/src') diff --git a/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridClickEvent.java b/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridClickEvent.java index 8f2350727f..ec1559b9da 100644 --- a/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridClickEvent.java +++ b/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridClickEvent.java @@ -30,8 +30,26 @@ import com.vaadin.v7.shared.ui.grid.GridConstants.Section; */ public class GridClickEvent extends AbstractGridMouseEvent { + public static final Type TYPE = new Type( + BrowserEvents.CLICK, new GridClickEvent()); + + /** + * @since 7.7.9 + */ + public GridClickEvent() { + } + + /** + * @deprecated This constructor's arguments are no longer used. Use the + * no-args constructor instead. + */ + @Deprecated public GridClickEvent(Grid grid, CellReference targetCell) { - super(grid, targetCell); + } + + @Override + public Type getAssociatedType() { + return TYPE; } @Override diff --git a/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridDoubleClickEvent.java b/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridDoubleClickEvent.java index 711e70f3b4..17231ee3a8 100644 --- a/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridDoubleClickEvent.java +++ b/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridDoubleClickEvent.java @@ -31,8 +31,26 @@ import com.vaadin.v7.shared.ui.grid.GridConstants.Section; public class GridDoubleClickEvent extends AbstractGridMouseEvent { + public static final Type TYPE = new Type( + BrowserEvents.DBLCLICK, new GridDoubleClickEvent()); + + /** + * @since 7.7.9 + */ + public GridDoubleClickEvent() { + } + + /** + * @deprecated This constructor's arguments are no longer used. Use the + * no-args constructor instead. + */ + @Deprecated public GridDoubleClickEvent(Grid grid, CellReference targetCell) { - super(grid, targetCell); + } + + @Override + public Type getAssociatedType() { + return TYPE; } @Override @@ -51,5 +69,4 @@ public class GridDoubleClickEvent handler.onDoubleClick(this); } } - } diff --git a/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridKeyDownEvent.java b/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridKeyDownEvent.java index e4405b69a5..617798cb3c 100644 --- a/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridKeyDownEvent.java +++ b/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridKeyDownEvent.java @@ -31,8 +31,26 @@ import com.vaadin.v7.shared.ui.grid.GridConstants.Section; */ public class GridKeyDownEvent extends AbstractGridKeyEvent { + public static final Type TYPE = new Type( + BrowserEvents.KEYDOWN, new GridKeyDownEvent()); + + /** + * @since 7.7.9 + */ + public GridKeyDownEvent() { + } + + /** + * @deprecated This constructor's arguments are no longer used. Use the + * no-args constructor instead. + */ + @Deprecated public GridKeyDownEvent(Grid grid, CellReference targetCell) { - super(grid, targetCell); + } + + @Override + public Type getAssociatedType() { + return TYPE; } @Override diff --git a/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridKeyPressEvent.java b/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridKeyPressEvent.java index 86a4664df0..1ada27a1f6 100644 --- a/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridKeyPressEvent.java +++ b/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridKeyPressEvent.java @@ -31,8 +31,26 @@ import com.vaadin.v7.shared.ui.grid.GridConstants.Section; public class GridKeyPressEvent extends AbstractGridKeyEvent { + public static final Type TYPE = new Type( + BrowserEvents.KEYPRESS, new GridKeyPressEvent()); + + /** + * @since 7.7.9 + */ + public GridKeyPressEvent() { + } + + /** + * @deprecated This constructor's arguments are no longer used. Use the + * no-args constructor instead. + */ + @Deprecated public GridKeyPressEvent(Grid grid, CellReference targetCell) { - super(grid, targetCell); + } + + @Override + public Type getAssociatedType() { + return TYPE; } @Override diff --git a/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridKeyUpEvent.java b/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridKeyUpEvent.java index 770f0214f9..ec81e65ea8 100644 --- a/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridKeyUpEvent.java +++ b/compatibility-client/src/main/java/com/vaadin/v7/client/widget/grid/events/GridKeyUpEvent.java @@ -31,8 +31,21 @@ import com.vaadin.v7.shared.ui.grid.GridConstants.Section; */ public class GridKeyUpEvent extends AbstractGridKeyEvent { + public static final Type TYPE = new Type( + BrowserEvents.KEYUP, new GridKeyUpEvent()); + + /** + * @since 7.7.9 + */ + public GridKeyUpEvent() { + } + + /** + * @deprecated This constructor's arguments are no longer used. Use the + * no-args constructor instead. + */ + @Deprecated public GridKeyUpEvent(Grid grid, CellReference targetCell) { - super(grid, targetCell); } @Override @@ -46,6 +59,11 @@ public class GridKeyUpEvent extends AbstractGridKeyEvent { } } + @Override + public Type getAssociatedType() { + return TYPE; + } + @Override protected String getBrowserEventType() { return BrowserEvents.KEYUP; diff --git a/compatibility-client/src/main/java/com/vaadin/v7/client/widgets/Grid.java b/compatibility-client/src/main/java/com/vaadin/v7/client/widgets/Grid.java index 7ba5c9cc82..3efb253e3a 100644 --- a/compatibility-client/src/main/java/com/vaadin/v7/client/widgets/Grid.java +++ b/compatibility-client/src/main/java/com/vaadin/v7/client/widgets/Grid.java @@ -2298,94 +2298,115 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, public static abstract class AbstractGridKeyEvent extends KeyEvent { - private Grid grid; - private final Type associatedType = new Type( - getBrowserEventType(), this); - private final CellReference targetCell; + /** + * @since 7.7.9 + */ + public AbstractGridKeyEvent() { + } + /** + * @deprecated This constructor's arguments are no longer used. Use the + * no-args constructor instead. + */ + @Deprecated public AbstractGridKeyEvent(Grid grid, CellReference targetCell) { - this.grid = grid; - this.targetCell = targetCell; } protected abstract String getBrowserEventType(); /** - * Gets the Grid instance for this event. + * Gets the Grid instance for this event, if it originated from a Grid. * - * @return grid + * @return the grid this event originated from, or {@code null} if this + * event did not originate from a grid */ public Grid getGrid() { - return grid; + EventTarget target = getNativeEvent().getEventTarget(); + if (!Element.is(target)) { + return null; + } + return WidgetUtil.findWidget(Element.as(target), Grid.class); } /** - * Gets the focused cell for this event. + * Gets the reference of target cell for this event, if this event + * originated from a Grid. * - * @return focused cell + * @return target cell, or {@code null} if this event did not originate + * from a grid */ public CellReference getFocusedCell() { - return targetCell; + return getGrid().getEventCell(); } @Override protected void dispatch(HANDLER handler) { EventTarget target = getNativeEvent().getEventTarget(); - if (Element.is(target) + Grid grid = getGrid(); + if (Element.is(target) && grid != null && !grid.isElementInChildWidget(Element.as(target))) { Section section = Section.FOOTER; final RowContainer container = grid.cellFocusHandler.containerWithFocus; if (container == grid.escalator.getHeader()) { section = Section.HEADER; - } else if (container == grid.escalator.getBody()) { + } else if (container == getGrid().escalator.getBody()) { section = Section.BODY; } - doDispatch(handler, section); } } protected abstract void doDispatch(HANDLER handler, Section section); - - @Override - public Type getAssociatedType() { - return associatedType; - } } public static abstract class AbstractGridMouseEvent extends MouseEvent { - private Grid grid; - private final CellReference targetCell; - private final Type associatedType = new Type( - getBrowserEventType(), this); + /** + * @since 7.7.9 + */ + public AbstractGridMouseEvent() { + } + /** + * @deprecated This constructor's arguments are no longer used. Use the + * no-args constructor instead. + */ + @Deprecated public AbstractGridMouseEvent(Grid grid, CellReference targetCell) { - this.grid = grid; - this.targetCell = targetCell; } protected abstract String getBrowserEventType(); /** - * Gets the Grid instance for this event. + * Gets the Grid instance for this event, if it originated from a Grid. * - * @return grid + * @return the grid this event originated from, or {@code null} if this + * event did not originate from a grid */ public Grid getGrid() { - return grid; + EventTarget target = getNativeEvent().getEventTarget(); + if (!Element.is(target)) { + return null; + } + return WidgetUtil.findWidget(Element.as(target), Grid.class); } /** - * Gets the reference of target cell for this event. + * Gets the reference of target cell for this event, if this event + * originated from a Grid. * - * @return target cell + * @return target cell, or {@code null} if this event did not originate + * from a grid */ public CellReference getTargetCell() { - return targetCell; + Grid grid = getGrid(); + if (grid == null) { + return null; + } + return grid.getEventCell(); } @Override @@ -2396,6 +2417,12 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, return; } + Grid grid = getGrid(); + if (grid == null) { + // Target is not an element of a grid + return; + } + Element targetElement = Element.as(target); if (grid.isElementInChildWidget(targetElement)) { // Target is some widget inside of Grid @@ -2420,11 +2447,6 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, } protected abstract void doDispatch(HANDLER handler, Section section); - - @Override - public Type getAssociatedType() { - return associatedType; - } } private static final String CUSTOM_STYLE_PROPERTY_NAME = "customStyle"; @@ -2438,12 +2460,6 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, private static final double DETAILS_ROW_INITIAL_HEIGHT = 50; private EventCellReference eventCell = new EventCellReference(this); - private GridKeyDownEvent keyDown = new GridKeyDownEvent(this, eventCell); - private GridKeyUpEvent keyUp = new GridKeyUpEvent(this, eventCell); - private GridKeyPressEvent keyPress = new GridKeyPressEvent(this, eventCell); - private GridClickEvent clickEvent = new GridClickEvent(this, eventCell); - private GridDoubleClickEvent doubleClickEvent = new GridDoubleClickEvent( - this, eventCell); private class CellFocusHandler { @@ -8176,7 +8192,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, */ public HandlerRegistration addBodyKeyDownHandler( BodyKeyDownHandler handler) { - return addHandler(handler, keyDown.getAssociatedType()); + return addHandler(handler, GridKeyDownEvent.TYPE); } /** @@ -8189,7 +8205,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, * @return the registration for the event */ public HandlerRegistration addBodyKeyUpHandler(BodyKeyUpHandler handler) { - return addHandler(handler, keyUp.getAssociatedType()); + return addHandler(handler, GridKeyUpEvent.TYPE); } /** @@ -8203,7 +8219,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, */ public HandlerRegistration addBodyKeyPressHandler( BodyKeyPressHandler handler) { - return addHandler(handler, keyPress.getAssociatedType()); + return addHandler(handler, GridKeyPressEvent.TYPE); } /** @@ -8217,7 +8233,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, */ public HandlerRegistration addHeaderKeyDownHandler( HeaderKeyDownHandler handler) { - return addHandler(handler, keyDown.getAssociatedType()); + return addHandler(handler, GridKeyDownEvent.TYPE); } /** @@ -8231,7 +8247,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, */ public HandlerRegistration addHeaderKeyUpHandler( HeaderKeyUpHandler handler) { - return addHandler(handler, keyUp.getAssociatedType()); + return addHandler(handler, GridKeyUpEvent.TYPE); } /** @@ -8245,7 +8261,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, */ public HandlerRegistration addHeaderKeyPressHandler( HeaderKeyPressHandler handler) { - return addHandler(handler, keyPress.getAssociatedType()); + return addHandler(handler, GridKeyPressEvent.TYPE); } /** @@ -8259,7 +8275,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, */ public HandlerRegistration addFooterKeyDownHandler( FooterKeyDownHandler handler) { - return addHandler(handler, keyDown.getAssociatedType()); + return addHandler(handler, GridKeyDownEvent.TYPE); } /** @@ -8273,7 +8289,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, */ public HandlerRegistration addFooterKeyUpHandler( FooterKeyUpHandler handler) { - return addHandler(handler, keyUp.getAssociatedType()); + return addHandler(handler, GridKeyUpEvent.TYPE); } /** @@ -8287,7 +8303,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, */ public HandlerRegistration addFooterKeyPressHandler( FooterKeyPressHandler handler) { - return addHandler(handler, keyPress.getAssociatedType()); + return addHandler(handler, GridKeyPressEvent.TYPE); } /** @@ -8299,7 +8315,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, * @return the registration for the event */ public HandlerRegistration addBodyClickHandler(BodyClickHandler handler) { - return addHandler(handler, clickEvent.getAssociatedType()); + return addHandler(handler, GridClickEvent.TYPE); } /** @@ -8312,7 +8328,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, */ public HandlerRegistration addHeaderClickHandler( HeaderClickHandler handler) { - return addHandler(handler, clickEvent.getAssociatedType()); + return addHandler(handler, GridClickEvent.TYPE); } /** @@ -8325,7 +8341,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, */ public HandlerRegistration addFooterClickHandler( FooterClickHandler handler) { - return addHandler(handler, clickEvent.getAssociatedType()); + return addHandler(handler, GridClickEvent.TYPE); } /** @@ -8339,7 +8355,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, */ public HandlerRegistration addBodyDoubleClickHandler( BodyDoubleClickHandler handler) { - return addHandler(handler, doubleClickEvent.getAssociatedType()); + return addHandler(handler, GridDoubleClickEvent.TYPE); } /** @@ -8353,7 +8369,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, */ public HandlerRegistration addHeaderDoubleClickHandler( HeaderDoubleClickHandler handler) { - return addHandler(handler, doubleClickEvent.getAssociatedType()); + return addHandler(handler, GridDoubleClickEvent.TYPE); } /** @@ -8367,7 +8383,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, */ public HandlerRegistration addFooterDoubleClickHandler( FooterDoubleClickHandler handler) { - return addHandler(handler, doubleClickEvent.getAssociatedType()); + return addHandler(handler, GridDoubleClickEvent.TYPE); } /** -- cgit v1.2.3