]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add convenience methods to EditorRowHandler (#13334)
authorJohannes Dahlström <johannesd@vaadin.com>
Thu, 9 Oct 2014 11:55:19 +0000 (14:55 +0300)
committerJohannes Dahlström <johannesd@vaadin.com>
Thu, 9 Oct 2014 12:26:21 +0000 (15:26 +0300)
Change-Id: I046715f33113e94e3239e3df04bdd96b89c23aee

client/src/com/vaadin/client/ui/grid/EditorRow.java
client/src/com/vaadin/client/ui/grid/EditorRowHandler.java
client/src/com/vaadin/client/ui/grid/GridConnector.java
uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java

index 43e42adf177e779be31a98e02aa59135b390be5f..e7585b78a3c970b56d80b7ec5ab95b209877d13c 100644 (file)
@@ -15,8 +15,8 @@
  */
 package com.vaadin.client.ui.grid;
 
-import java.util.ArrayList;
-import java.util.List;
+import java.util.HashMap;
+import java.util.Map;
 
 import com.google.gwt.dom.client.DivElement;
 import com.google.gwt.dom.client.Element;
@@ -62,7 +62,7 @@ public class EditorRow<T> {
 
     private DivElement editorOverlay = DivElement.as(DOM.createDiv());
 
-    private List<Widget> editorWidgets = new ArrayList<Widget>();
+    private Map<GridColumn<?, T>, Widget> columnToWidget = new HashMap<GridColumn<?, T>, Widget>();
 
     private boolean enabled = false;
     private State state = State.INACTIVE;
@@ -127,7 +127,7 @@ public class EditorRow<T> {
         }
         hideOverlay();
         grid.getEscalator().setScrollLocked(Direction.VERTICAL, false);
-        handler.cancel(new EditorRowRequest(rowIndex, null));
+        handler.cancel(new EditorRowRequest<T>(grid, rowIndex, null));
         state = State.INACTIVE;
     }
 
@@ -151,14 +151,15 @@ public class EditorRow<T> {
 
         state = State.COMMITTING;
 
-        handler.commit(new EditorRowRequest(rowIndex, new RequestCallback() {
-            @Override
-            public void onResponse(EditorRowRequest request) {
-                if (state == State.COMMITTING) {
-                    state = State.ACTIVE;
-                }
-            }
-        }));
+        handler.commit(new EditorRowRequest<T>(grid, rowIndex,
+                new RequestCallback<T>() {
+                    @Override
+                    public void onResponse(EditorRowRequest<T> request) {
+                        if (state == State.COMMITTING) {
+                            state = State.ACTIVE;
+                        }
+                    }
+                }));
     }
 
     /**
@@ -178,7 +179,7 @@ public class EditorRow<T> {
             throw new IllegalStateException(
                     "Cannot discard: EditorRow is not in edit mode");
         }
-        handler.discard(new EditorRowRequest(rowIndex, null));
+        handler.discard(new EditorRowRequest<T>(grid, rowIndex, null));
     }
 
     /**
@@ -237,16 +238,17 @@ public class EditorRow<T> {
 
     protected void show() {
         if (state == State.ACTIVATING) {
-            handler.bind(new EditorRowRequest(rowIndex, new RequestCallback() {
-                @Override
-                public void onResponse(EditorRowRequest request) {
-                    if (state == State.ACTIVATING) {
-                        state = State.ACTIVE;
-                        showOverlay(grid.getEscalator().getBody()
-                                .getRowElement(request.getRowIndex()));
-                    }
-                }
-            }));
+            handler.bind(new EditorRowRequest<T>(grid, rowIndex,
+                    new RequestCallback<T>() {
+                        @Override
+                        public void onResponse(EditorRowRequest<T> request) {
+                            if (state == State.ACTIVATING) {
+                                state = State.ACTIVE;
+                                showOverlay(grid.getEscalator().getBody()
+                                        .getRowElement(request.getRowIndex()));
+                            }
+                        }
+                    }));
             grid.getEscalator().setScrollLocked(Direction.VERTICAL, true);
         }
     }
@@ -275,6 +277,18 @@ public class EditorRow<T> {
         this.state = state;
     }
 
+    /**
+     * Returns the editor widget associated with the given column. If the editor
+     * row is not active, returns null.
+     *
+     * @param column
+     *            the column
+     * @return the widget if the editor row is open, null otherwise
+     */
+    protected Widget getWidget(GridColumn<?, T> column) {
+        return columnToWidget.get(column);
+    }
+
     /**
      * Opens the editor overlay over the given table row.
      * 
@@ -319,7 +333,7 @@ public class EditorRow<T> {
 
             Widget editor = getHandler().getWidget(column);
             if (editor != null) {
-                editorWidgets.add(editor);
+                columnToWidget.put(column, editor);
                 attachWidget(editor, cell);
             }
         }
@@ -352,10 +366,10 @@ public class EditorRow<T> {
     }
 
     protected void hideOverlay() {
-        for (Widget w : editorWidgets) {
+        for (Widget w : columnToWidget.values()) {
             GridUtil.setParent(w, null);
         }
-        editorWidgets.clear();
+        columnToWidget.clear();
 
         editorOverlay.removeAllChildren();
         editorOverlay.removeFromParent();
index d2d1e61efb150503f45a96a6ee8e29caee80695f..1e19e93353bf621165267c5154e79fa925cd4083 100644 (file)
@@ -37,18 +37,19 @@ public interface EditorRowHandler<T> {
      * <p>
      * TODO Should have a mechanism for signaling a failed request to the caller
      */
-    public static class EditorRowRequest {
+    public static class EditorRowRequest<T> {
 
         /**
          * A callback interface used to notify the caller about completed
          * requests.
          */
-        public interface RequestCallback {
-            public void onResponse(EditorRowRequest request);
+        public interface RequestCallback<T> {
+            public void onResponse(EditorRowRequest<T> request);
         }
 
+        private Grid<T> grid;
         private int rowIndex;
-        private RequestCallback callback;
+        private RequestCallback<T> callback;
 
         /**
          * Creates a new editor row request.
@@ -59,7 +60,9 @@ public interface EditorRowHandler<T> {
          *            the callback invoked when the request is ready, or null if
          *            no need to call back
          */
-        public EditorRowRequest(int rowIndex, RequestCallback callback) {
+        public EditorRowRequest(Grid<T> grid, int rowIndex,
+                RequestCallback<T> callback) {
+            this.grid = grid;
             this.rowIndex = rowIndex;
             this.callback = callback;
         }
@@ -73,6 +76,38 @@ public interface EditorRowHandler<T> {
             return rowIndex;
         }
 
+        /**
+         * Returns the row data related to the row being requested.
+         *
+         * @return the row data
+         */
+        public T getRow() {
+            return grid.getDataSource().getRow(rowIndex);
+        }
+
+        /**
+         * Returns the grid instance related to this editor row request.
+         * 
+         * @return the grid instance
+         */
+        public Grid<T> getGrid() {
+            return grid;
+        }
+
+        /**
+         * Returns the editor row widget used to edit the values of the given
+         * column.
+         * 
+         * @param column
+         *            the column whose widget to get
+         * @return the widget related to the column
+         */
+        public Widget getWidget(GridColumn<?, T> column) {
+            Widget w = grid.getEditorRow().getWidget(column);
+            assert w != null;
+            return w;
+        }
+
         /**
          * Invokes the stored callback if it is not null.
          */
@@ -96,7 +131,7 @@ public interface EditorRowHandler<T> {
      * 
      * @see EditorRow#editRow(int)
      */
-    public void bind(EditorRowRequest request);
+    public void bind(EditorRowRequest<T> request);
 
     /**
      * Cancels a currently active edit if any. Called by the editor row when
@@ -111,7 +146,7 @@ public interface EditorRowHandler<T> {
      * 
      * @see EditorRow#cancel()
      */
-    public void cancel(EditorRowRequest request);
+    public void cancel(EditorRowRequest<T> request);
 
     /**
      * Commits changes in the currently active edit to the data source. Called
@@ -120,7 +155,7 @@ public interface EditorRowHandler<T> {
      * @param request
      *            the commit request
      */
-    public void commit(EditorRowRequest request);
+    public void commit(EditorRowRequest<T> request);
 
     /**
      * Discards any unsaved changes and reloads editor content from the data
@@ -133,7 +168,7 @@ public interface EditorRowHandler<T> {
      * @param request
      *            the discard request
      */
-    public void discard(EditorRowRequest request);
+    public void discard(EditorRowRequest<T> request);
 
     /**
      * Returns a widget instance that is used to edit the values in the given
index 505b2870f5fe368324b5c64ac412b7bf99ec25b5..39c7e2cde46685c64f0849b871f8d0529a254f8a 100644 (file)
@@ -136,7 +136,7 @@ public class GridConnector extends AbstractHasComponentsConnector implements
 
         private EditorRowServerRpc rpc = getRpcProxy(EditorRowServerRpc.class);
 
-        private EditorRowRequest currentRequest = null;
+        private EditorRowRequest<?> currentRequest = null;
         private boolean serverInitiated = false;
 
         public CustomEditorRowHandler() {
@@ -174,7 +174,7 @@ public class GridConnector extends AbstractHasComponentsConnector implements
         }
 
         @Override
-        public void bind(EditorRowRequest request) {
+        public void bind(EditorRowRequest<JSONObject> request) {
             if (!handleServerInitiated(request)) {
                 startRequest(request);
                 rpc.bind(request.getRowIndex());
@@ -182,7 +182,7 @@ public class GridConnector extends AbstractHasComponentsConnector implements
         }
 
         @Override
-        public void commit(EditorRowRequest request) {
+        public void commit(EditorRowRequest<JSONObject> request) {
             if (!handleServerInitiated(request)) {
                 startRequest(request);
                 rpc.commit(request.getRowIndex());
@@ -190,7 +190,7 @@ public class GridConnector extends AbstractHasComponentsConnector implements
         }
 
         @Override
-        public void discard(EditorRowRequest request) {
+        public void discard(EditorRowRequest<JSONObject> request) {
             if (!handleServerInitiated(request)) {
                 startRequest(request);
                 rpc.discard(request.getRowIndex());
@@ -198,7 +198,7 @@ public class GridConnector extends AbstractHasComponentsConnector implements
         }
 
         @Override
-        public void cancel(EditorRowRequest request) {
+        public void cancel(EditorRowRequest<JSONObject> request) {
             if (!handleServerInitiated(request)) {
                 // No startRequest as we don't get (or need)
                 // a confirmation from the server
@@ -230,7 +230,7 @@ public class GridConnector extends AbstractHasComponentsConnector implements
          * @return true if the request was originally triggered by the server,
          *         false otherwise
          */
-        private boolean handleServerInitiated(EditorRowRequest request) {
+        private boolean handleServerInitiated(EditorRowRequest<?> request) {
             assert request != null;
             assert currentRequest == null;
 
@@ -243,7 +243,7 @@ public class GridConnector extends AbstractHasComponentsConnector implements
             }
         }
 
-        private void startRequest(EditorRowRequest request) {
+        private void startRequest(EditorRowRequest<?> request) {
             currentRequest = request;
         }
 
index b67acf31c63348210c1109d4d86b1b7e8e81197d..d6d0437a68fe3e59ee980d2a90820569a952acf9 100644 (file)
@@ -94,7 +94,7 @@ public class GridBasicClientFeaturesWidget extends
         }
 
         @Override
-        public void bind(EditorRowRequest request) {
+        public void bind(EditorRowRequest<List<Data>> request) {
             List<Data> rowData = ds.getRow(request.getRowIndex());
 
             boolean hasSelectionColumn = !(grid.getSelectionModel() instanceof None);
@@ -107,13 +107,13 @@ public class GridBasicClientFeaturesWidget extends
         }
 
         @Override
-        public void cancel(EditorRowRequest request) {
+        public void cancel(EditorRowRequest<List<Data>> request) {
             log.setText("Row " + request.getRowIndex() + " edit cancelled");
             request.invokeCallback();
         }
 
         @Override
-        public void commit(EditorRowRequest request) {
+        public void commit(EditorRowRequest<List<Data>> request) {
             log.setText("Row " + request.getRowIndex() + " edit committed");
             List<Data> rowData = ds.getRow(request.getRowIndex());
 
@@ -135,7 +135,7 @@ public class GridBasicClientFeaturesWidget extends
         }
 
         @Override
-        public void discard(EditorRowRequest request) {
+        public void discard(EditorRowRequest<List<Data>> request) {
             bind(request);
         }