]> source.dussan.org Git - vaadin-framework.git/commitdiff
Added column resize event handling. #2807
authorJohn Alhroos <john.ahlroos@itmill.com>
Wed, 26 May 2010 09:21:23 +0000 (09:21 +0000)
committerJohn Alhroos <john.ahlroos@itmill.com>
Wed, 26 May 2010 09:21:23 +0000 (09:21 +0000)
svn changeset:13367/svn branch:6.4

src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java
src/com/vaadin/ui/Table.java

index 4d4a38c2db2d1d5852c1590ff3637716ed700686..69f0e0aae3d2d75366f22479fc16ec2113b2677e 100644 (file)
@@ -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) {
index 50ae72f7a7db24a1f77e2ab30792533dd4050e9c..03f2bd27e23900ef10e46bdeba41aabc1f0b0722 100644 (file)
@@ -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<String, Object> 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);
+    }
 }