From 5921cbcdbe0a545093f54aedb45278c44b2af506 Mon Sep 17 00:00:00 2001 From: John Alhroos Date: Wed, 26 May 2010 09:21:23 +0000 Subject: [PATCH] Added column resize event handling. #2807 svn changeset:13367/svn branch:6.4 --- .../terminal/gwt/client/ui/VScrollTable.java | 24 +++ src/com/vaadin/ui/Table.java | 150 ++++++++++++++++++ 2 files changed, 174 insertions(+) diff --git a/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java b/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java index 4d4a38c2db..69f0e0aae3 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java @@ -98,6 +98,7 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, 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"; + public static final String COLUMN_RESIZE_EVENT_ID = "columnResize"; private static final double CACHE_RATE_DEFAULT = 2; @@ -339,6 +340,28 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, } } + /** + * Fires a column resize event which sends the resize information to the + * server. + * + * @param columnId + * The columnId of the column which was resized + * @param originalWidth + * The width in pixels of the column before the resize event + * @param newWidth + * The width in pixels of the column after the resize event + */ + private void fireColumnResizeEvent(String columnId, int originalWidth, + int newWidth) { + client.updateVariable(paintableId, "columnResizeEventColumn", + columnId, false); + client.updateVariable(paintableId, "columnResizeEventPrev", + originalWidth, false); + client.updateVariable(paintableId, "columnResizeEventCurr", + newWidth, immediate); + + } + /** * Moves the focus one step down * @@ -1894,6 +1917,7 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, // readjust undefined width columns lazyAdjustColumnWidths.cancel(); lazyAdjustColumnWidths.schedule(1); + fireColumnResizeEvent(cid, originalWidth, getColWidth(cid)); break; case Event.ONMOUSEMOVE: if (isResizing) { diff --git a/src/com/vaadin/ui/Table.java b/src/com/vaadin/ui/Table.java index 50ae72f7a7..03f2bd27e2 100644 --- a/src/com/vaadin/ui/Table.java +++ b/src/com/vaadin/ui/Table.java @@ -1937,6 +1937,8 @@ public class Table extends AbstractSelect implements Action.Container, handleClickEvent(variables); + handleColumnResizeEvent(variables); + disableContentRefreshing(); if (!isSelectable() && variables.containsKey("selected")) { @@ -2149,6 +2151,43 @@ public class Table extends AbstractSelect implements Action.Container, } } + /** + * Handles the column resize event sent by the client. + * + * @param variables + */ + private void handleColumnResizeEvent(Map variables) { + if (variables.containsKey("columnResizeEventColumn")) { + Object cid = variables.get("columnResizeEventColumn"); + Object propertyId = null; + if (cid != null) { + propertyId = columnIdMap.get(cid.toString()); + } + + Object prev = variables.get("columnResizeEventPrev"); + int previousWidth = -1; + if (prev != null) { + previousWidth = Integer.valueOf(prev.toString()); + } + + Object curr = variables.get("columnResizeEventCurr"); + int currentWidth = -1; + if (curr != null) { + currentWidth = Integer.valueOf(curr.toString()); + } + + /* + * Update the sizes on the server side. If a column previously had a + * expand ratio and the user resized the column then the expand + * ratio will be turned into a static pixel size. + */ + setColumnWidth(propertyId, currentWidth); + + fireEvent(new ColumnResizeEvent(this, propertyId, previousWidth, + currentWidth)); + } + } + /** * Go to mode where content updates are not done. This is due we want to * bypass expensive content for some reason (like when we know we may have @@ -4026,4 +4065,115 @@ public class Table extends AbstractSelect implements Action.Container, public boolean isFooterVisible() { return columnFootersVisible; } + + /** + * This event is fired when a column is resized. The event contains the + * columns property id which was fired, the previous width of the column and + * the width of the column after the resize. + */ + public static class ColumnResizeEvent extends Component.Event{ + public static final Method COLUMN_RESIZE_METHOD; + + static { + try { + COLUMN_RESIZE_METHOD = ColumnResizeListener.class + .getDeclaredMethod("columnResize", + new Class[] { ColumnResizeEvent.class }); + } catch (final java.lang.NoSuchMethodException e) { + // This should never happen + throw new java.lang.RuntimeException(); + } + } + + private final int previousWidth; + private final int currentWidth; + private final Object columnPropertyId; + + /** + * Constructor + * + * @param source + * The source of the event + * @param propertyId + * The columns property id + * @param previous + * The width in pixels of the column before the resize event + * @param current + * The width in pixels of the column after the resize event + */ + public ColumnResizeEvent(Component source, Object propertyId, + int previous, int current) { + super(source); + previousWidth = previous; + currentWidth = current; + columnPropertyId = propertyId; + } + + /** + * Get the column property id of the column that was resized. + * + * @return The column property id + */ + public Object getPropertyId() { + return columnPropertyId; + } + + /** + * Get the width in pixels of the column before the resize event + * + * @return Width in pixels + */ + public int getPreviousWidth() { + return previousWidth; + } + + /** + * Get the width in pixels of the column after the resize event + * + * @return Width in pixels + */ + public int getCurrentWidth() { + return currentWidth; + } + } + + /** + * Interface for listening to column resize events. + */ + public interface ColumnResizeListener{ + + /** + * This method is triggered when the column has been resized + * + * @param event + * The event which contains the column property id, the + * previous width of the column and the current width of the + * column + */ + public void columnResize(ColumnResizeEvent event); + } + + /** + * Adds a column resize listener to the Table. A column resize listener is + * called when a user resizes a columns width. + * + * @param listener + * The listener to attach to the Table + */ + public void addListener(ColumnResizeListener listener) { + addListener(VScrollTable.COLUMN_RESIZE_EVENT_ID, + ColumnResizeEvent.class, listener, + ColumnResizeEvent.COLUMN_RESIZE_METHOD); + } + + /** + * Removes a column resize listener from the Table. + * + * @param listener + * The listener to remove + */ + public void removeListener(ColumnResizeListener listener) { + removeListener(VScrollTable.COLUMN_RESIZE_EVENT_ID, + ColumnResizeEvent.class, listener); + } } -- 2.39.5