Add RowHoverEvent for the CellTable. This can come in handy if the user want to have special handling logic when a row is hovered. For example, the style of only a few cells can be updated, or one cell can show/hide a child element.
Review at http://gwt-code-reviews.appspot.com/1534804
Review by: jlabanca@google.com
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10595 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/cellview/client/AbstractCellTable.java b/user/src/com/google/gwt/user/cellview/client/AbstractCellTable.java
index 8402222..f942f2d 100644
--- a/user/src/com/google/gwt/user/cellview/client/AbstractCellTable.java
+++ b/user/src/com/google/gwt/user/cellview/client/AbstractCellTable.java
@@ -1034,6 +1034,16 @@
* @param styleName the style name to add
*/
public abstract void addColumnStyleName(int index, String styleName);
+
+ /**
+ * Add a handler to handle {@link RowHoverEvent}s.
+ *
+ * @param handler the {@link RowHoverEvent.Handler} to add
+ * @return a {@link HandlerRegistration} to remove the handler
+ */
+ public HandlerRegistration addRowHoverHandler(RowHoverEvent.Handler handler) {
+ return addHandler(handler, RowHoverEvent.getType());
+ }
/**
* Clear the width of the specified {@link Column}.
@@ -1871,12 +1881,12 @@
if ("mouseover".equals(eventType)) {
// Unstyle the old row if it is still part of the table.
if (hoveringRow != null && getTableBodyElement().isOrHasChild(hoveringRow)) {
- setRowStyleName(hoveringRow, style.hoveredRow(), style.hoveredRowCell(), false);
+ setRowHover(hoveringRow, false);
}
hoveringRow = targetTableRow;
- setRowStyleName(hoveringRow, style.hoveredRow(), style.hoveredRowCell(), true);
+ setRowHover(hoveringRow, true);
} else if ("mouseout".equals(eventType) && hoveringRow != null) {
- setRowStyleName(hoveringRow, style.hoveredRow(), style.hoveredRowCell(), false);
+ setRowHover(hoveringRow, false);
hoveringRow = null;
}
@@ -1923,7 +1933,7 @@
}
}
}
-
+
@Override
protected void onFocus() {
TableCellElement td = getKeyboardSelectedTableCellElement();
@@ -2526,6 +2536,17 @@
}
/**
+ * Set a row's hovering style and fire a {@link RowHoverEvent}
+ *
+ * @param tr the row element
+ * @param isHovering false if this is an unhover event
+ */
+ private void setRowHover(TableRowElement tr, boolean isHovering) {
+ setRowStyleName(tr, style.hoveredRow(), style.hoveredRowCell(), isHovering);
+ RowHoverEvent.fire(this, tr, isHovering);
+ }
+
+ /**
* Apply a style to a row and all cells in the row.
*
* @param tr the row element
diff --git a/user/src/com/google/gwt/user/cellview/client/RowHoverEvent.java b/user/src/com/google/gwt/user/cellview/client/RowHoverEvent.java
new file mode 100644
index 0000000..77a8af9
--- /dev/null
+++ b/user/src/com/google/gwt/user/cellview/client/RowHoverEvent.java
@@ -0,0 +1,117 @@
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.user.cellview.client;
+
+import com.google.gwt.dom.client.TableRowElement;
+import com.google.gwt.event.shared.EventHandler;
+import com.google.gwt.event.shared.GwtEvent;
+import com.google.gwt.event.shared.HasHandlers;
+
+/**
+ * Represents a row hover event.
+ */
+public class RowHoverEvent extends GwtEvent<RowHoverEvent.Handler> {
+
+ /**
+ * Handler for {@link RowHoverEvent}.
+ */
+ public static interface Handler extends EventHandler {
+
+ /**
+ * Called when {@link RowHoverEvent} is fired.
+ *
+ * @param event the {@link ColumnSortEvent} that was fired
+ */
+ void onRowHover(RowHoverEvent event);
+ }
+
+ /**
+ * Handler type.
+ */
+ private static Type<Handler> TYPE;
+
+ /**
+ * Fires a row hover event on all registered handlers in the handler
+ * manager. If no such handlers exist, this implementation will do nothing.
+ *
+ * @param source the source of the event
+ * @param hoveringRow the currently hovering {@link TableRowElement}. If isUnHover is false, this
+ * should be the previouly hovering {@link TableRowElement}
+ * @param isUnHover false if this is an unhover event
+ * @return the {@link RowHoverEvent} that was fired
+ */
+ public static RowHoverEvent fire(HasHandlers source, TableRowElement hoveringRow,
+ boolean isUnHover) {
+ RowHoverEvent event = new RowHoverEvent(hoveringRow, isUnHover);
+ if (TYPE != null) {
+ source.fireEvent(event);
+ }
+ return event;
+ }
+
+ /**
+ * Gets the type associated with this event.
+ *
+ * @return returns the handler type
+ */
+ public static Type<Handler> getType() {
+ if (TYPE == null) {
+ TYPE = new Type<Handler>();
+ }
+ return TYPE;
+ }
+
+ private TableRowElement hoveringRow;
+
+ private boolean isUnHover;
+
+ /**
+ * Construct a new {@link RowHoverEvent}.
+ *
+ * @param hoveringRow the currently hovering {@link TableRowElement}. If isUnHover is false, this
+ * should be the previouly hovering {@link TableRowElement}
+ * @param isUnHover false if this is an unhover event
+ */
+ protected RowHoverEvent(TableRowElement hoveringRow, boolean isUnHover) {
+ this.hoveringRow = hoveringRow;
+ this.isUnHover = isUnHover;
+ }
+
+ @Override
+ public Type<Handler> getAssociatedType() {
+ return TYPE;
+ }
+
+ /**
+ * Return the {@link TableRowElement} that the user just hovered or unhovered.
+ */
+ public TableRowElement getHoveringRow() {
+ return hoveringRow;
+ }
+
+ /**
+ * Return whether this is an unhover event.
+ */
+ public boolean isUnHover() {
+ return isUnHover;
+ }
+
+ @Override
+ protected void dispatch(Handler handler) {
+ handler.onRowHover(this);
+ }
+}
+