*/
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;
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;
}
hideOverlay();
grid.getEscalator().setScrollLocked(Direction.VERTICAL, false);
- handler.cancel(new EditorRowRequest(rowIndex, null));
+ handler.cancel(new EditorRowRequest<T>(grid, rowIndex, null));
state = State.INACTIVE;
}
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;
+ }
+ }
+ }));
}
/**
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));
}
/**
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);
}
}
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.
*
Widget editor = getHandler().getWidget(column);
if (editor != null) {
- editorWidgets.add(editor);
+ columnToWidget.put(column, editor);
attachWidget(editor, cell);
}
}
}
protected void hideOverlay() {
- for (Widget w : editorWidgets) {
+ for (Widget w : columnToWidget.values()) {
GridUtil.setParent(w, null);
}
- editorWidgets.clear();
+ columnToWidget.clear();
editorOverlay.removeAllChildren();
editorOverlay.removeFromParent();
* <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.
* 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;
}
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.
*/
*
* @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
*
* @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
* @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
* @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
private EditorRowServerRpc rpc = getRpcProxy(EditorRowServerRpc.class);
- private EditorRowRequest currentRequest = null;
+ private EditorRowRequest<?> currentRequest = null;
private boolean serverInitiated = false;
public CustomEditorRowHandler() {
}
@Override
- public void bind(EditorRowRequest request) {
+ public void bind(EditorRowRequest<JSONObject> request) {
if (!handleServerInitiated(request)) {
startRequest(request);
rpc.bind(request.getRowIndex());
}
@Override
- public void commit(EditorRowRequest request) {
+ public void commit(EditorRowRequest<JSONObject> request) {
if (!handleServerInitiated(request)) {
startRequest(request);
rpc.commit(request.getRowIndex());
}
@Override
- public void discard(EditorRowRequest request) {
+ public void discard(EditorRowRequest<JSONObject> request) {
if (!handleServerInitiated(request)) {
startRequest(request);
rpc.discard(request.getRowIndex());
}
@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
* @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;
}
}
- private void startRequest(EditorRowRequest request) {
+ private void startRequest(EditorRowRequest<?> request) {
currentRequest = request;
}
}
@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);
}
@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());
}
@Override
- public void discard(EditorRowRequest request) {
+ public void discard(EditorRowRequest<List<Data>> request) {
bind(request);
}