From 060e77d5ddedc701b68ea1986ea5654616457220 Mon Sep 17 00:00:00 2001 From: John Alhroos Date: Mon, 19 Apr 2010 12:40:22 +0000 Subject: [PATCH] Added footer click handler #4516 svn changeset:12641/svn branch:6.4 --- .../terminal/gwt/client/ui/VScrollTable.java | 63 +++++++- src/com/vaadin/ui/Table.java | 150 +++++++++++++++++- 2 files changed, 209 insertions(+), 4 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java b/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java index 841e210b63..22e3d85886 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java @@ -86,6 +86,7 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, public static final String CLASSNAME = "v-table"; public static final String ITEM_CLICK_EVENT_ID = "itemClick"; public static final String HEADER_CLICK_EVENT_ID = "handleHeaderClick"; + public static final String FOOTER_CLICK_EVENT_ID = "handleFooterClick"; private static final double CACHE_RATE_DEFAULT = 2; @@ -1238,6 +1239,12 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, floatingCopyOfHeaderCell = null; } + /** + * Fires a header click event after the user has clicked a column header cell + * + * @param event + * The click event + */ private void fireHeaderClickedEvent(Event event) { if (client.hasEventListeners(VScrollTable.this, HEADER_CLICK_EVENT_ID)) { @@ -1888,8 +1895,11 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, private char align = ALIGN_LEFT; private int width = -1; private float expandRatio = 0; + private String cid; public FooterCell(String colId, String headerText) { + cid = colId; + setText(headerText); DOM.setElementProperty(captionContainer, "className", CLASSNAME @@ -1898,8 +1908,12 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, // ensure no clipping initially (problem on column additions) DOM.setStyleAttribute(captionContainer, "overflow", "visible"); + DOM.sinkEvents(captionContainer, Event.MOUSEEVENTS); + DOM.appendChild(td, captionContainer); + DOM.sinkEvents(td, Event.MOUSEEVENTS); + setElement(td); } @@ -2035,6 +2049,54 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, return getParent() != null; } + /** + * Handle column clicking + */ + + @Override + public void onBrowserEvent(Event event) { + if (enabled && event != null) { + handleCaptionEvent(event); + } + } + + /** + * Handles a event on the captions + * + * @param event + * The event to handle + */ + protected void handleCaptionEvent(Event event) { + if (DOM.eventGetType(event) == Event.ONMOUSEUP) { + fireFooterClickedEvent(event); + } + } + + /** + * Fires a footer click event after the user has clicked a column footer + * cell + * + * @param event + * The click event + */ + private void fireFooterClickedEvent(Event event) { + if (client.hasEventListeners(VScrollTable.this, + FOOTER_CLICK_EVENT_ID)) { + MouseEventDetails details = new MouseEventDetails(event); + client.updateVariable(paintableId, "footerClickEvent", details + .toString(), false); + client.updateVariable(paintableId, "footerClickCID", cid, true); + } + } + + /** + * Returns the column key of the column + * + * @return The column key + */ + public String getColKey() { + return cid; + } } /** @@ -2093,7 +2155,6 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, * * @see com.google.gwt.user.client.ui.HasWidgets#iterator() */ - @Override public Iterator iterator() { return visibleCells.iterator(); } diff --git a/src/com/vaadin/ui/Table.java b/src/com/vaadin/ui/Table.java index 19ffaae2b9..94e26810fa 100644 --- a/src/com/vaadin/ui/Table.java +++ b/src/com/vaadin/ui/Table.java @@ -398,6 +398,8 @@ public class Table extends AbstractSelect implements Action.Container, private MultiSelectMode multiSelectMode = MultiSelectMode.DEFAULT; private HeaderClickHandler headerClickHandler; + + private FooterClickHandler footerClickHandler; /* Table constructors */ @@ -2031,6 +2033,8 @@ public class Table extends AbstractSelect implements Action.Container, * @param variables */ private void handleClickEvent(Map variables) { + + // Item click event if (variables.containsKey("clickEvent")) { String key = (String) variables.get("clickedKey"); Object itemId = itemIdMapper.get(key); @@ -2049,6 +2053,7 @@ public class Table extends AbstractSelect implements Action.Container, } } + // Header click event else if (variables.containsKey("headerClickEvent")) { MouseEventDetails details = MouseEventDetails @@ -2061,6 +2066,19 @@ public class Table extends AbstractSelect implements Action.Container, } fireEvent(new HeaderClickEvent(this, propertyId, details)); } + + // Footer click event + else if (variables.containsKey("footerClickEvent")) { + MouseEventDetails details = MouseEventDetails + .deSerialize((String) variables.get("footerClickEvent")); + + Object cid = variables.get("footerClickCID"); + Object propertyId = null; + if (cid != null) { + propertyId = columnIdMap.get(cid.toString()); + } + fireEvent(new FooterClickEvent(this, propertyId, details)); + } } /** @@ -3653,11 +3671,74 @@ public class Table extends AbstractSelect implements Action.Container, return details; } } + + /** + * Click event fired when clicking on the Table footers. The event includes + * a reference the the Table the event originated from, the property id of + * the column which header was pressed and details about the mouse event + * itself. + */ + public static class FooterClickEvent extends Component.Event { + public static final Method FOOTER_CLICK_METHOD; + + static { + try { + // Set the header click method + FOOTER_CLICK_METHOD = FooterClickHandler.class + .getDeclaredMethod("handleFooterClick", + new Class[] { FooterClickEvent.class }); + } catch (final java.lang.NoSuchMethodException e) { + // This should never happen + throw new java.lang.RuntimeException(); + } + } + + // The property id of the column which header was pressed + private Object columnPropertyId; + + // The mouse details + private MouseEventDetails details; + + /** + * Constructor + * @param source + * The source of the component + * @param propertyId + * The propertyId of the column + * @param details + * The mouse details of the click + */ + public FooterClickEvent(Component source, Object propertyId, + MouseEventDetails details) { + super(source); + columnPropertyId = propertyId; + this.details = details; + } + + /** + * Gets the property id of the column which header was pressed + * + * @return The column propety id + */ + public Object getPropertyId() { + return columnPropertyId; + } + + /** + * Returns the details of the mouse event like the mouse coordinates, + * button pressed etc. + * + * @return The mouse details + */ + public MouseEventDetails getEventDetails() { + return details; + } + } /** - * Interface for the handler listening to column change events. The - * handleHeaderClick method is called when the user presses a header column - * cell. + * Interface for the handler listening to column header mouse click events. + * The handleHeaderClick method is called when the user presses a header + * column cell. */ public interface HeaderClickHandler { @@ -3671,6 +3752,23 @@ public class Table extends AbstractSelect implements Action.Container, public void handleHeaderClick(HeaderClickEvent event); } + /** + * Interface for the handler listening to column footer mouse click events. + * The handleHeaderClick method is called when the user presses a footer + * column cell. + */ + public interface FooterClickHandler { + + /** + * Called when a user clicks a footer column cell + * + * @param event + * The event which contains information about the column and + * the mouse click event + */ + public void handleFooterClick(FooterClickEvent event); + } + /** * Sets the header click handler which handles the click events when the * user clicks on a column header cell in the Table. @@ -3710,6 +3808,42 @@ public class Table extends AbstractSelect implements Action.Container, } } + /** + * Sets the footer click handler which handles the click events when the + * user clicks on a column footer cell in the Table. + *

+ * The handler will recieve events which contains information about which + * column was clicked and some details about the mouse event. + *

+ * + * @param handler + * The handler which should handle the footer click events + */ + public void setFooterClickHandler(FooterClickHandler handler) { + if (footerClickHandler != handler) { + if (handler == null && footerClickHandler != null) { + // Remove header click handler + removeListener(VScrollTable.FOOTER_CLICK_EVENT_ID, + FooterClickEvent.class, footerClickHandler); + footerClickHandler = handler; + } else if (footerClickHandler != null) { + // Replace footer click handler + removeListener(VScrollTable.FOOTER_CLICK_EVENT_ID, + FooterClickEvent.class, footerClickHandler); + footerClickHandler = handler; + addListener(VScrollTable.FOOTER_CLICK_EVENT_ID, + FooterClickEvent.class, footerClickHandler, + FooterClickEvent.FOOTER_CLICK_METHOD); + } else if (handler != null) { + // Set a new footer click handler + footerClickHandler = handler; + addListener(VScrollTable.FOOTER_CLICK_EVENT_ID, + FooterClickEvent.class, footerClickHandler, + FooterClickEvent.FOOTER_CLICK_METHOD); + } + } + } + /** * Returns the header click handler which receives click events from the * columns header cells when they are clicked on. @@ -3720,6 +3854,16 @@ public class Table extends AbstractSelect implements Action.Container, return headerClickHandler; } + /** + * Returns the footer click handler which recieves click events from the + * columns footer cells when they are clicked on. + * + * @return + */ + public FooterClickHandler getFooterClickHandler() { + return footerClickHandler; + } + /** * Gets the footer caption beneath the rows * -- 2.39.5