aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohannes Dahlström <johannesd@vaadin.com>2014-11-05 15:56:58 +0200
committerVaadin Code Review <review@vaadin.com>2014-11-13 14:21:09 +0000
commit1ac8b6bff007d997bf2cfc69954e69d8fa65e119 (patch)
tree58485a25a59988bbe54638bdf6c3944b3b0c46d1
parenteffe4927119eed370bb7061300123877588c5432 (diff)
downloadvaadin-framework-1ac8b6bff007d997bf2cfc69954e69d8fa65e119.tar.gz
vaadin-framework-1ac8b6bff007d997bf2cfc69954e69d8fa65e119.zip
Make ImageRenderers emit click events (#13334)
Change-Id: I1f98409e9787e1057ffaeebbe37427042a1e3233
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/AbstractRendererConnector.java3
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/ButtonRenderer.java109
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/ButtonRendererConnector.java33
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/ClickableRenderer.java164
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/ClickableRendererConnector.java60
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/ImageRenderer.java10
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/ImageRendererConnector.java11
-rw-r--r--server/src/com/vaadin/ui/components/grid/renderers/ButtonRenderer.java92
-rw-r--r--server/src/com/vaadin/ui/components/grid/renderers/ClickableRenderer.java121
-rw-r--r--server/src/com/vaadin/ui/components/grid/renderers/ImageRenderer.java14
-rw-r--r--uitest/src/com/vaadin/tests/components/grid/WidgetRenderers.java16
-rw-r--r--uitest/src/com/vaadin/tests/components/grid/WidgetRenderersTest.java6
12 files changed, 406 insertions, 233 deletions
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/AbstractRendererConnector.java b/client/src/com/vaadin/client/ui/grid/renderers/AbstractRendererConnector.java
index cad5af97df..e528fcfa8b 100644
--- a/client/src/com/vaadin/client/ui/grid/renderers/AbstractRendererConnector.java
+++ b/client/src/com/vaadin/client/ui/grid/renderers/AbstractRendererConnector.java
@@ -37,6 +37,9 @@ import com.vaadin.client.ui.grid.Renderer;
* {@link com.vaadin.shared.communication.SharedState SharedState} and no RPC
* interfaces.
*
+ * @param <T>
+ * the presentation type of the renderer
+ *
* @since
* @author Vaadin Ltd
*/
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/ButtonRenderer.java b/client/src/com/vaadin/client/ui/grid/renderers/ButtonRenderer.java
index 90812bfba1..e054c5d1bd 100644
--- a/client/src/com/vaadin/client/ui/grid/renderers/ButtonRenderer.java
+++ b/client/src/com/vaadin/client/ui/grid/renderers/ButtonRenderer.java
@@ -16,104 +16,18 @@
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;
+public class ButtonRenderer extends ClickableRenderer<String, Button> {
@Override
public Button createWidget() {
@@ -126,25 +40,4 @@ public class ButtonRenderer<T> extends WidgetRenderer<String, Button> implements
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
index 899975b0eb..405ec79a64 100644
--- a/client/src/com/vaadin/client/ui/grid/renderers/ButtonRendererConnector.java
+++ b/client/src/com/vaadin/client/ui/grid/renderers/ButtonRendererConnector.java
@@ -17,11 +17,8 @@ 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.client.ui.grid.renderers.ClickableRenderer.RendererClickHandler;
import com.vaadin.shared.ui.Connect;
-import com.vaadin.shared.ui.grid.renderers.RendererClickRpc;
/**
* A connector for {@link ButtonRenderer}.
@@ -30,32 +27,16 @@ import com.vaadin.shared.ui.grid.renderers.RendererClickRpc;
* @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();
- }
+public class ButtonRendererConnector extends ClickableRendererConnector<String> {
@Override
- public ButtonRenderer<JSONObject> getRenderer() {
- return (ButtonRenderer<JSONObject>) super.getRenderer();
+ public ButtonRenderer getRenderer() {
+ return (ButtonRenderer) super.getRenderer();
}
@Override
- public void onClick(RendererClickEvent<JSONObject> event) {
- getRpcProxy(RendererClickRpc.class).click(
- event.getCell().getRow(),
- event.getCell().getColumn(),
- MouseEventDetailsBuilder.buildMouseEventDetails(event
- .getNativeEvent()));
+ protected HandlerRegistration addClickHandler(
+ RendererClickHandler<JSONObject> handler) {
+ return getRenderer().addClickHandler(handler);
}
}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/ClickableRenderer.java b/client/src/com/vaadin/client/ui/grid/renderers/ClickableRenderer.java
new file mode 100644
index 0000000000..1cc79f1199
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/ClickableRenderer.java
@@ -0,0 +1,164 @@
+/*
+ * 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.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.Widget;
+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.Grid;
+
+/**
+ * An abstract superclass for renderers that render clickable widgets. Click
+ * handlers can be added to a renderer to listen to click events emitted by all
+ * widgets rendered by the renderer.
+ *
+ * @param <T>
+ * the presentation (column) type
+ * @param <W>
+ * the widget type
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public abstract class ClickableRenderer<T, W extends Widget> extends
+ WidgetRenderer<T, W> implements ClickHandler {
+
+ /**
+ * A handler for {@link RendererClickEvent renderer click events}.
+ *
+ * @param <R>
+ * the row type of the containing Grid
+ *
+ * @see {@link ButtonRenderer#addClickHandler(RendererClickHandler)}
+ */
+ public interface RendererClickHandler<R> extends EventHandler {
+
+ /**
+ * Called when a rendered button is clicked.
+ *
+ * @param event
+ * the event representing the click
+ */
+ void onClick(RendererClickEvent<R> event);
+ }
+
+ /**
+ * An event fired when a widget rendered by a ClickableWidgetRenderer
+ * subclass is clicked.
+ *
+ * @param <R>
+ * the row type of the containing Grid
+ */
+ @SuppressWarnings("rawtypes")
+ public static class RendererClickEvent<R> extends
+ MouseEvent<RendererClickHandler> {
+
+ @SuppressWarnings("unchecked")
+ static final Type<RendererClickHandler> TYPE = new Type<RendererClickHandler>(
+ BrowserEvents.CLICK, new RendererClickEvent());
+
+ private Cell cell;
+
+ private R 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 R 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<R> grid = Util.findWidget(cell.getElement(), Grid.class);
+ row = grid.getDataSource().getRow(cell.getRow());
+ handler.onClick(this);
+ }
+ }
+
+ private HandlerManager handlerManager;
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * <em>Implementation note:</em> It is the implementing method's
+ * responsibility to add {@code this} as a click handler of the returned
+ * widget, or a widget nested therein, in order to make click events
+ * propagate properly to handlers registered via
+ * {@link #addClickHandler(RendererClickHandler) addClickHandler}.
+ */
+ @Override
+ public abstract W createWidget();
+
+ /**
+ * Adds a click handler to this button renderer. The handler is invoked
+ * every time one of the widgets rendered by this renderer is clicked.
+ * <p>
+ * Note that the row type of the click handler must match the row type of
+ * the containing Grid.
+ *
+ * @param handler
+ * the click handler to be added
+ */
+ public HandlerRegistration addClickHandler(RendererClickHandler<?> handler) {
+ if (handlerManager == null) {
+ handlerManager = new HandlerManager(this);
+ }
+ return handlerManager.addHandler(RendererClickEvent.TYPE, handler);
+ }
+
+ @Override
+ public void onClick(ClickEvent event) {
+ /*
+ * The handler manager is lazily instantiated so it's null iff
+ * addClickHandler is never called.
+ */
+ if (handlerManager != null) {
+ DomEvent.fireNativeEvent(event.getNativeEvent(), handlerManager);
+ }
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/ClickableRendererConnector.java b/client/src/com/vaadin/client/ui/grid/renderers/ClickableRendererConnector.java
new file mode 100644
index 0000000000..f0d98c6ec0
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/ClickableRendererConnector.java
@@ -0,0 +1,60 @@
+/*
+ * 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.ClickableRenderer.RendererClickEvent;
+import com.vaadin.client.ui.grid.renderers.ClickableRenderer.RendererClickHandler;
+import com.vaadin.shared.ui.grid.renderers.RendererClickRpc;
+
+/**
+ * An abstract base class for {@link ClickableRenderer} connectors.
+ *
+ * @param <T>
+ * the presentation type of the renderer
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public abstract class ClickableRendererConnector<T> extends
+ AbstractRendererConnector<T> {
+
+ HandlerRegistration clickRegistration;
+
+ @Override
+ protected void init() {
+ clickRegistration = addClickHandler(new RendererClickHandler<JSONObject>() {
+ @Override
+ public void onClick(RendererClickEvent<JSONObject> event) {
+ getRpcProxy(RendererClickRpc.class).click(
+ event.getCell().getRow(),
+ event.getCell().getColumn(),
+ MouseEventDetailsBuilder.buildMouseEventDetails(event
+ .getNativeEvent()));
+ }
+ });
+ }
+
+ @Override
+ public void onUnregister() {
+ clickRegistration.removeHandler();
+ }
+
+ protected abstract HandlerRegistration addClickHandler(
+ RendererClickHandler<JSONObject> handler);
+}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/ImageRenderer.java b/client/src/com/vaadin/client/ui/grid/renderers/ImageRenderer.java
index 3d8faf44d8..591012f9e1 100644
--- a/client/src/com/vaadin/client/ui/grid/renderers/ImageRenderer.java
+++ b/client/src/com/vaadin/client/ui/grid/renderers/ImageRenderer.java
@@ -20,16 +20,20 @@ import com.google.gwt.user.client.ui.Image;
import com.vaadin.client.ui.grid.FlyweightCell;
/**
- * A renderer that renders an image into a cell.
+ * A renderer that renders an image into a cell. Click handlers can be added to
+ * the renderer, invoked every time any of the images rendered by that rendered
+ * is clicked.
*
* @since
* @author Vaadin Ltd
*/
-public class ImageRenderer extends WidgetRenderer<String, Image> {
+public class ImageRenderer extends ClickableRenderer<String, Image> {
@Override
public Image createWidget() {
- return GWT.create(Image.class);
+ Image image = GWT.create(Image.class);
+ image.addClickHandler(this);
+ return image;
}
@Override
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/ImageRendererConnector.java b/client/src/com/vaadin/client/ui/grid/renderers/ImageRendererConnector.java
index 1e33ccfa52..c425b0a40d 100644
--- a/client/src/com/vaadin/client/ui/grid/renderers/ImageRendererConnector.java
+++ b/client/src/com/vaadin/client/ui/grid/renderers/ImageRendererConnector.java
@@ -15,9 +15,12 @@
*/
package com.vaadin.client.ui.grid.renderers;
+import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;
+import com.google.web.bindery.event.shared.HandlerRegistration;
import com.vaadin.client.communication.JsonDecoder;
import com.vaadin.client.metadata.TypeDataStore;
+import com.vaadin.client.ui.grid.renderers.ClickableRenderer.RendererClickHandler;
import com.vaadin.shared.communication.URLReference;
import com.vaadin.shared.ui.Connect;
@@ -28,7 +31,7 @@ import com.vaadin.shared.ui.Connect;
* @author Vaadin Ltd
*/
@Connect(com.vaadin.ui.components.grid.renderers.ImageRenderer.class)
-public class ImageRendererConnector extends AbstractRendererConnector<String> {
+public class ImageRendererConnector extends ClickableRendererConnector<String> {
@Override
public ImageRenderer getRenderer() {
@@ -41,4 +44,10 @@ public class ImageRendererConnector extends AbstractRendererConnector<String> {
TypeDataStore.getType(URLReference.class), value, null,
getConnection())).getURL();
}
+
+ @Override
+ protected HandlerRegistration addClickHandler(
+ RendererClickHandler<JSONObject> handler) {
+ return getRenderer().addClickHandler(handler);
+ }
}
diff --git a/server/src/com/vaadin/ui/components/grid/renderers/ButtonRenderer.java b/server/src/com/vaadin/ui/components/grid/renderers/ButtonRenderer.java
index de63449f81..dece41c7ab 100644
--- a/server/src/com/vaadin/ui/components/grid/renderers/ButtonRenderer.java
+++ b/server/src/com/vaadin/ui/components/grid/renderers/ButtonRenderer.java
@@ -15,16 +15,6 @@
*/
package com.vaadin.ui.components.grid.renderers;
-import java.lang.reflect.Method;
-
-import com.vaadin.event.ConnectorEventListener;
-import com.vaadin.event.MouseEvents.ClickEvent;
-import com.vaadin.shared.MouseEventDetails;
-import com.vaadin.shared.ui.grid.renderers.RendererClickRpc;
-import com.vaadin.ui.components.grid.AbstractRenderer;
-import com.vaadin.ui.components.grid.Grid;
-import com.vaadin.util.ReflectTools;
-
/**
* A Renderer that displays a button with a textual caption. The value of the
* corresponding property is used as the caption. Click listeners can be added
@@ -33,97 +23,23 @@ import com.vaadin.util.ReflectTools;
* @since
* @author Vaadin Ltd
*/
-public class ButtonRenderer extends AbstractRenderer<String> {
-
- /**
- * An interface for listening to {@link RendererClickEvent renderer click
- * events}.
- *
- * @see {@link ButtonRenderer#addClickListener(RendererClickListener)}
- */
- public interface RendererClickListener extends ConnectorEventListener {
-
- static final Method CLICK_METHOD = ReflectTools.findMethod(
- RendererClickListener.class, "click", RendererClickEvent.class);
-
- /**
- * Called when a rendered button is clicked.
- *
- * @param event
- * the event representing the click
- */
- void click(RendererClickEvent event);
- }
-
- /**
- * An event fired when a button rendered by a ButtonRenderer is clicked.
- */
- public static class RendererClickEvent extends ClickEvent {
-
- private Object itemId;
-
- protected RendererClickEvent(Grid source, Object itemId,
- MouseEventDetails mouseEventDetails) {
- super(source, mouseEventDetails);
- this.itemId = itemId;
- }
-
- /**
- * Returns the item ID of the row where the click event originated.
- *
- * @return the item ID of the clicked row
- */
- public Object getItemId() {
- return itemId;
- }
- }
+public class ButtonRenderer extends ClickableRenderer<String> {
/**
* Creates a new button renderer.
*/
public ButtonRenderer() {
super(String.class);
- registerRpc(new RendererClickRpc() {
- @Override
- public void click(int row, int column,
- MouseEventDetails mouseDetails) {
-
- Grid grid = (Grid) getParent();
-
- Object itemId = grid.getContainerDatasource().getIdByIndex(row);
-
- fireEvent(new RendererClickEvent(grid, itemId, mouseDetails));
- }
- });
}
/**
* Creates a new button renderer and adds the given click listener to it.
+ *
+ * @param listener
+ * the click listener to register
*/
public ButtonRenderer(RendererClickListener listener) {
this();
addClickListener(listener);
}
-
- /**
- * Adds a click listener to this button renderer. The listener is invoked
- * every time one of the buttons rendered by this renderer is clicked.
- *
- * @param listener
- * the click listener to be added
- */
- public void addClickListener(RendererClickListener listener) {
- addListener(RendererClickEvent.class, listener,
- RendererClickListener.CLICK_METHOD);
- }
-
- /**
- * Removes the given click listener from this renderer.
- *
- * @param listener
- * the click listener to be removed
- */
- public void removeClickListener(RendererClickListener listener) {
- removeListener(RendererClickEvent.class, listener);
- }
}
diff --git a/server/src/com/vaadin/ui/components/grid/renderers/ClickableRenderer.java b/server/src/com/vaadin/ui/components/grid/renderers/ClickableRenderer.java
new file mode 100644
index 0000000000..ec2ae7bf67
--- /dev/null
+++ b/server/src/com/vaadin/ui/components/grid/renderers/ClickableRenderer.java
@@ -0,0 +1,121 @@
+/*
+ * 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.ui.components.grid.renderers;
+
+import java.lang.reflect.Method;
+
+import com.vaadin.event.ConnectorEventListener;
+import com.vaadin.event.MouseEvents.ClickEvent;
+import com.vaadin.shared.MouseEventDetails;
+import com.vaadin.shared.ui.grid.renderers.RendererClickRpc;
+import com.vaadin.ui.components.grid.AbstractRenderer;
+import com.vaadin.ui.components.grid.Grid;
+import com.vaadin.util.ReflectTools;
+
+/**
+ * An abstract superclass for Renderers that render clickable items. Click
+ * listeners can be added to a renderer to be notified when any of the rendered
+ * items is clicked.
+ *
+ * @param <T>
+ * the type presented by the renderer
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class ClickableRenderer<T> extends AbstractRenderer<T> {
+
+ /**
+ * An interface for listening to {@link RendererClickEvent renderer click
+ * events}.
+ *
+ * @see {@link ButtonRenderer#addClickListener(RendererClickListener)}
+ */
+ public interface RendererClickListener extends ConnectorEventListener {
+
+ static final Method CLICK_METHOD = ReflectTools.findMethod(
+ RendererClickListener.class, "click", RendererClickEvent.class);
+
+ /**
+ * Called when a rendered button is clicked.
+ *
+ * @param event
+ * the event representing the click
+ */
+ void click(RendererClickEvent event);
+ }
+
+ /**
+ * An event fired when a button rendered by a ButtonRenderer is clicked.
+ */
+ public static class RendererClickEvent extends ClickEvent {
+
+ private Object itemId;
+
+ protected RendererClickEvent(Grid source, Object itemId,
+ MouseEventDetails mouseEventDetails) {
+ super(source, mouseEventDetails);
+ this.itemId = itemId;
+ }
+
+ /**
+ * Returns the item ID of the row where the click event originated.
+ *
+ * @return the item ID of the clicked row
+ */
+ public Object getItemId() {
+ return itemId;
+ }
+ }
+
+ protected ClickableRenderer(Class<T> presentationType) {
+ super(presentationType);
+ registerRpc(new RendererClickRpc() {
+ @Override
+ public void click(int row, int column,
+ MouseEventDetails mouseDetails) {
+
+ Grid grid = (Grid) getParent();
+ Object itemId = grid.getContainerDatasource().getIdByIndex(row);
+ // TODO map column index to property ID or send column ID
+ // instead of index from the client
+ fireEvent(new RendererClickEvent(grid, itemId, mouseDetails));
+ }
+ });
+ }
+
+ /**
+ * Adds a click listener to this button renderer. The listener is invoked
+ * every time one of the buttons rendered by this renderer is clicked.
+ *
+ * @param listener
+ * the click listener to be added
+ */
+ public void addClickListener(RendererClickListener listener) {
+ addListener(RendererClickEvent.class, listener,
+ RendererClickListener.CLICK_METHOD);
+ }
+
+ /**
+ * Removes the given click listener from this renderer.
+ *
+ * @param listener
+ * the click listener to be removed
+ */
+ public void removeClickListener(RendererClickListener listener) {
+ removeListener(RendererClickEvent.class, listener);
+ }
+}
diff --git a/server/src/com/vaadin/ui/components/grid/renderers/ImageRenderer.java b/server/src/com/vaadin/ui/components/grid/renderers/ImageRenderer.java
index b7134aab01..be57a5ec19 100644
--- a/server/src/com/vaadin/ui/components/grid/renderers/ImageRenderer.java
+++ b/server/src/com/vaadin/ui/components/grid/renderers/ImageRenderer.java
@@ -20,7 +20,6 @@ import com.vaadin.server.Resource;
import com.vaadin.server.ResourceReference;
import com.vaadin.server.ThemeResource;
import com.vaadin.shared.communication.URLReference;
-import com.vaadin.ui.components.grid.AbstractRenderer;
import elemental.json.JsonValue;
@@ -34,7 +33,7 @@ import elemental.json.JsonValue;
* @since
* @author Vaadin Ltd
*/
-public class ImageRenderer extends AbstractRenderer<Resource> {
+public class ImageRenderer extends ClickableRenderer<Resource> {
/**
* Creates a new image renderer.
@@ -43,6 +42,17 @@ public class ImageRenderer extends AbstractRenderer<Resource> {
super(Resource.class);
}
+ /**
+ * Creates a new image renderer and adds the given click listener to it.
+ *
+ * @param listener
+ * the click listener to register
+ */
+ public ImageRenderer(RendererClickListener listener) {
+ this();
+ addClickListener(listener);
+ }
+
@Override
public JsonValue encode(Resource resource) {
if (!(resource instanceof ExternalResource || resource instanceof ThemeResource)) {
diff --git a/uitest/src/com/vaadin/tests/components/grid/WidgetRenderers.java b/uitest/src/com/vaadin/tests/components/grid/WidgetRenderers.java
index 534b8a9e4d..ba2fc4455b 100644
--- a/uitest/src/com/vaadin/tests/components/grid/WidgetRenderers.java
+++ b/uitest/src/com/vaadin/tests/components/grid/WidgetRenderers.java
@@ -24,8 +24,8 @@ import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.components.grid.Grid;
import com.vaadin.ui.components.grid.Grid.SelectionMode;
import com.vaadin.ui.components.grid.renderers.ButtonRenderer;
-import com.vaadin.ui.components.grid.renderers.ButtonRenderer.RendererClickEvent;
-import com.vaadin.ui.components.grid.renderers.ButtonRenderer.RendererClickListener;
+import com.vaadin.ui.components.grid.renderers.ClickableRenderer.RendererClickEvent;
+import com.vaadin.ui.components.grid.renderers.ClickableRenderer.RendererClickListener;
import com.vaadin.ui.components.grid.renderers.ImageRenderer;
import com.vaadin.ui.components.grid.renderers.ProgressBarRenderer;
@@ -47,7 +47,7 @@ public class WidgetRenderers extends AbstractTestUI {
item.getItemProperty(ProgressBarRenderer.class).setValue(0.3);
item.getItemProperty(ButtonRenderer.class).setValue("Click");
item.getItemProperty(ImageRenderer.class).setValue(
- new ThemeResource("window/img/resize.png"));
+ new ThemeResource("window/img/close.png"));
Grid grid = new Grid(container);
@@ -66,7 +66,15 @@ public class WidgetRenderers extends AbstractTestUI {
}
}));
- grid.getColumn(ImageRenderer.class).setRenderer(new ImageRenderer());
+ grid.getColumn(ImageRenderer.class).setRenderer(
+ new ImageRenderer(new RendererClickListener() {
+
+ @Override
+ public void click(RendererClickEvent event) {
+ item.getItemProperty(ImageRenderer.class).setValue(
+ new ThemeResource("window/img/maximize.png"));
+ }
+ }));
addComponent(grid);
}
diff --git a/uitest/src/com/vaadin/tests/components/grid/WidgetRenderersTest.java b/uitest/src/com/vaadin/tests/components/grid/WidgetRenderersTest.java
index d18494d277..c6649326c8 100644
--- a/uitest/src/com/vaadin/tests/components/grid/WidgetRenderersTest.java
+++ b/uitest/src/com/vaadin/tests/components/grid/WidgetRenderersTest.java
@@ -60,8 +60,12 @@ public class WidgetRenderersTest extends MultiBrowserTest {
WebElement image = getGridCell(0, 2).findElement(
By.className("gwt-Image"));
- assertTrue(image.getAttribute("src").endsWith("window/img/resize.png"));
+ assertTrue(image.getAttribute("src").endsWith("window/img/close.png"));
+ image.click();
+
+ assertTrue(image.getAttribute("src")
+ .endsWith("window/img/maximize.png"));
}
GridCellElement getGridCell(int row, int col) {