summaryrefslogtreecommitdiffstats
path: root/client/src
diff options
context:
space:
mode:
authorJohannes Dahlström <johannesd@vaadin.com>2014-09-25 17:18:08 +0300
committerHenrik Paul <henrik@vaadin.com>2014-10-07 10:41:15 +0000
commit5b3c9bc4ebfcb2837b07327b81831e81db2deccc (patch)
treef6999a78e7eaef8df806bc2d616f635a14582774 /client/src
parent3d4d607e18923f12ac1eb0a7934d76f6acba1bbf (diff)
downloadvaadin-framework-5b3c9bc4ebfcb2837b07327b81831e81db2deccc.tar.gz
vaadin-framework-5b3c9bc4ebfcb2837b07327b81831e81db2deccc.zip
Implement ButtonRenderer (#13334)
Change-Id: Id7c6f3cf85f8e75905e86b55edbc1b8782780996
Diffstat (limited to 'client/src')
-rw-r--r--client/src/com/vaadin/client/ui/grid/Grid.java13
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/ButtonRenderer.java150
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/ButtonRendererConnector.java61
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/WidgetRenderer.java30
4 files changed, 253 insertions, 1 deletions
diff --git a/client/src/com/vaadin/client/ui/grid/Grid.java b/client/src/com/vaadin/client/ui/grid/Grid.java
index 7be14cb068..074d795946 100644
--- a/client/src/com/vaadin/client/ui/grid/Grid.java
+++ b/client/src/com/vaadin/client/ui/grid/Grid.java
@@ -2197,6 +2197,19 @@ public class Grid<T> extends ResizeComposite implements
}
}
+ /**
+ * Returns the cell the given element belongs to. For internal use only.
+ *
+ * @param e
+ * a cell element or the descendant of one
+ * @return the cell or null if no such cell
+ */
+ public Cell findCell(Element e) {
+ RowContainer container = escalator.findRowContainer(e);
+ return container != null ? container.getCell(e) : null;
+
+ }
+
private boolean handleEditorRowEvent(Event event, RowContainer container,
Cell cell) {
if (editorRow.getState() != State.INACTIVE) {
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/ButtonRenderer.java b/client/src/com/vaadin/client/ui/grid/renderers/ButtonRenderer.java
new file mode 100644
index 0000000000..90812bfba1
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/ButtonRenderer.java
@@ -0,0 +1,150 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.ui.grid.renderers;
+
+import com.google.gwt.core.shared.GWT;
+import com.google.gwt.dom.client.BrowserEvents;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.event.dom.client.DomEvent;
+import com.google.gwt.event.dom.client.MouseEvent;
+import com.google.gwt.event.shared.EventHandler;
+import com.google.gwt.event.shared.HandlerManager;
+import com.google.gwt.user.client.ui.Button;
+import com.google.web.bindery.event.shared.HandlerRegistration;
+import com.vaadin.client.Util;
+import com.vaadin.client.ui.grid.Cell;
+import com.vaadin.client.ui.grid.FlyweightCell;
+import com.vaadin.client.ui.grid.Grid;
+
+/**
+ * A Renderer that displays buttons with textual captions. The values of the
+ * corresponding column are used as the captions. Click handlers can be added to
+ * the renderer, invoked when any of the rendered buttons is clicked.
+ *
+ * @param <T>
+ * the row type
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class ButtonRenderer<T> extends WidgetRenderer<String, Button> implements
+ ClickHandler {
+
+ /**
+ * A handler for {@link RendererClickEvent renderer click events}.
+ *
+ * @see {@link ButtonRenderer#addClickHandler(RendererClickHandler)}
+ */
+ public interface RendererClickHandler<T> extends EventHandler {
+
+ /**
+ * Called when a rendered button is clicked.
+ *
+ * @param event
+ * the event representing the click
+ */
+ void onClick(RendererClickEvent<T> event);
+ }
+
+ /**
+ * An event fired when a button rendered by a ButtonRenderer is clicked.
+ */
+ @SuppressWarnings("rawtypes")
+ public static class RendererClickEvent<T> extends
+ MouseEvent<RendererClickHandler> {
+
+ @SuppressWarnings("unchecked")
+ private static final Type<RendererClickHandler> TYPE = new Type<RendererClickHandler>(
+ BrowserEvents.CLICK, new RendererClickEvent());
+
+ private Cell cell;
+
+ private T row;
+
+ private RendererClickEvent() {
+ }
+
+ /**
+ * Returns the cell of the clicked button.
+ *
+ * @return the cell
+ */
+ public Cell getCell() {
+ return cell;
+ }
+
+ /**
+ * Returns the data object corresponding to the row of the clicked
+ * button.
+ *
+ * @return the row data object
+ */
+ public T getRow() {
+ return row;
+ }
+
+ @Override
+ public Type<RendererClickHandler> getAssociatedType() {
+ return TYPE;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ protected void dispatch(RendererClickHandler handler) {
+ cell = WidgetRenderer.getCell(getNativeEvent());
+ assert cell != null;
+ Grid<T> grid = Util.findWidget(cell.getElement(), Grid.class);
+ row = grid.getDataSource().getRow(cell.getRow());
+ handler.onClick(this);
+ }
+ }
+
+ private HandlerManager handlerManager;
+
+ @Override
+ public Button createWidget() {
+ Button b = GWT.create(Button.class);
+ b.addClickHandler(this);
+ return b;
+ }
+
+ @Override
+ public void render(FlyweightCell cell, String text, Button button) {
+ button.setText(text);
+ }
+
+ /**
+ * Adds a click handler to this button renderer. The handler is invoked
+ * every time one of the buttons rendered by this renderer is clicked.
+ *
+ * @param handler
+ * the click handler to be added
+ */
+ public HandlerRegistration addClickHandler(RendererClickHandler<T> handler) {
+ if (handlerManager == null) {
+ handlerManager = new HandlerManager(this);
+ }
+ return handlerManager.addHandler(RendererClickEvent.TYPE, handler);
+ }
+
+ @Override
+ public void onClick(ClickEvent event) {
+ if (handlerManager != null) {
+ DomEvent.fireNativeEvent(event.getNativeEvent(), handlerManager);
+ }
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/ButtonRendererConnector.java b/client/src/com/vaadin/client/ui/grid/renderers/ButtonRendererConnector.java
new file mode 100644
index 0000000000..899975b0eb
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/ButtonRendererConnector.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.ui.grid.renderers;
+
+import com.google.gwt.json.client.JSONObject;
+import com.google.web.bindery.event.shared.HandlerRegistration;
+import com.vaadin.client.MouseEventDetailsBuilder;
+import com.vaadin.client.ui.grid.renderers.ButtonRenderer.RendererClickEvent;
+import com.vaadin.client.ui.grid.renderers.ButtonRenderer.RendererClickHandler;
+import com.vaadin.shared.ui.Connect;
+import com.vaadin.shared.ui.grid.renderers.RendererClickRpc;
+
+/**
+ * A connector for {@link ButtonRenderer}.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+@Connect(com.vaadin.ui.components.grid.renderers.ButtonRenderer.class)
+public class ButtonRendererConnector extends AbstractRendererConnector<String>
+ implements RendererClickHandler<JSONObject> {
+
+ HandlerRegistration clickRegistration;
+
+ @Override
+ protected void init() {
+ clickRegistration = getRenderer().addClickHandler(this);
+ }
+
+ @Override
+ public void onUnregister() {
+ clickRegistration.removeHandler();
+ }
+
+ @Override
+ public ButtonRenderer<JSONObject> getRenderer() {
+ return (ButtonRenderer<JSONObject>) super.getRenderer();
+ }
+
+ @Override
+ public void onClick(RendererClickEvent<JSONObject> event) {
+ getRpcProxy(RendererClickRpc.class).click(
+ event.getCell().getRow(),
+ event.getCell().getColumn(),
+ MouseEventDetailsBuilder.buildMouseEventDetails(event
+ .getNativeEvent()));
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/WidgetRenderer.java b/client/src/com/vaadin/client/ui/grid/renderers/WidgetRenderer.java
index 9fcd9c906e..5d07e0929b 100644
--- a/client/src/com/vaadin/client/ui/grid/renderers/WidgetRenderer.java
+++ b/client/src/com/vaadin/client/ui/grid/renderers/WidgetRenderer.java
@@ -15,9 +15,14 @@
*/
package com.vaadin.client.ui.grid.renderers;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.EventTarget;
+import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.Util;
+import com.vaadin.client.ui.grid.Cell;
import com.vaadin.client.ui.grid.FlyweightCell;
+import com.vaadin.client.ui.grid.Grid;
/**
* A renderer for rendering widgets into cells.
@@ -48,7 +53,7 @@ public abstract class WidgetRenderer<T, W extends Widget> extends
@Override
public void render(FlyweightCell cell, T data) {
- W w = Util.findWidget(cell.getElement().getFirstChildElement(), null);
+ W w = getWidget(cell.getElement());
assert w != null : "Widget not found in cell (" + cell.getColumn()
+ "," + cell.getRow() + ")";
render(cell, data, w);
@@ -69,4 +74,27 @@ public abstract class WidgetRenderer<T, W extends Widget> extends
*/
public abstract void render(FlyweightCell cell, T data, W widget);
+ protected W getWidget(Element e) {
+ return Util.findWidget(e.getFirstChildElement(), null);
+ }
+
+ /**
+ * Returns the cell instance corresponding to the element that the given
+ * event originates from. If the event does not originate from a grid cell,
+ * returns null.
+ *
+ * @param event
+ * the event
+ * @return the cell or null if no such cell
+ */
+ protected static Cell getCell(NativeEvent event) {
+ EventTarget target = event.getEventTarget();
+ if (!Element.is(target)) {
+ return null;
+ }
+
+ Element elem = Element.as(target);
+ Grid<?> grid = Util.findWidget(elem, Grid.class);
+ return grid.findCell(elem);
+ }
}