Skip to content

Commit

Permalink
Migrate ClickableRenderer and subclasses
Browse files Browse the repository at this point in the history
Change-Id: I233250d9b68052825b73bed0288fffeaf5bdd04b
  • Loading branch information
ahie committed Sep 9, 2016
1 parent dfe23f7 commit 33809f4
Show file tree
Hide file tree
Showing 18 changed files with 730 additions and 39 deletions.
@@ -0,0 +1,45 @@
/*
* Copyright 2000-2016 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.connectors;

import com.google.web.bindery.event.shared.HandlerRegistration;
import com.vaadin.client.renderers.ButtonRenderer;
import com.vaadin.client.renderers.ClickableRenderer.RendererClickHandler;
import com.vaadin.shared.ui.Connect;

import elemental.json.JsonObject;

/**
* A connector for {@link ButtonRenderer}.
*
* @since 7.4
* @author Vaadin Ltd
*/
@Connect(com.vaadin.ui.renderers.ButtonRenderer.class)
public class ButtonRendererConnector
extends ClickableRendererConnector<String> {

@Override
public ButtonRenderer getRenderer() {
return (ButtonRenderer) super.getRenderer();
}

@Override
protected HandlerRegistration addClickHandler(
RendererClickHandler<JsonObject> handler) {
return getRenderer().addClickHandler(handler);
}
}
@@ -0,0 +1,63 @@
/*
* Copyright 2000-2016 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.connectors;

import com.google.web.bindery.event.shared.HandlerRegistration;
import com.vaadin.client.MouseEventDetailsBuilder;
import com.vaadin.client.connectors.grid.AbstractGridRendererConnector;
import com.vaadin.client.renderers.ClickableRenderer.RendererClickEvent;
import com.vaadin.client.renderers.ClickableRenderer.RendererClickHandler;
import com.vaadin.shared.ui.grid.renderers.RendererClickRpc;

import elemental.json.JsonObject;

/**
* An abstract base class for {@link ClickableRenderer} connectors.
*
* @param <T>
* the presentation type of the renderer
*
* @since 7.4
* @author Vaadin Ltd
*/
public abstract class ClickableRendererConnector<T>
extends AbstractGridRendererConnector<T> {

private HandlerRegistration clickRegistration;

@Override
protected void init() {
clickRegistration = addClickHandler(
new RendererClickHandler<JsonObject>() {
@Override
public void onClick(RendererClickEvent<JsonObject> event) {
getRpcProxy(RendererClickRpc.class).click(
getRowKey(event.getCell().getRow()),
getColumnId(event.getCell().getColumn()),
MouseEventDetailsBuilder.buildMouseEventDetails(
event.getNativeEvent()));
}
});
}

@Override
public void onUnregister() {
clickRegistration.removeHandler();
}

protected abstract HandlerRegistration addClickHandler(
RendererClickHandler<JsonObject> handler);
}
@@ -0,0 +1,57 @@
/*
* Copyright 2000-2016 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.connectors;

import com.google.web.bindery.event.shared.HandlerRegistration;
import com.vaadin.client.communication.JsonDecoder;
import com.vaadin.client.metadata.TypeDataStore;
import com.vaadin.client.renderers.ClickableRenderer.RendererClickHandler;
import com.vaadin.client.renderers.ImageRenderer;
import com.vaadin.shared.communication.URLReference;
import com.vaadin.shared.ui.Connect;

import elemental.json.JsonObject;
import elemental.json.JsonValue;

/**
* A connector for {@link ImageRenderer}.
*
* @since 7.4
* @author Vaadin Ltd
*/
@Connect(com.vaadin.ui.renderers.ImageRenderer.class)
public class ImageRendererConnector extends ClickableRendererConnector<String> {

@Override
public ImageRenderer getRenderer() {
return (ImageRenderer) super.getRenderer();
}

@Override
public String decode(JsonValue value) {
URLReference reference = (URLReference) JsonDecoder.decodeValue(
TypeDataStore.getType(URLReference.class), value, null,
getConnection());

return reference != null ? reference.getURL() : null;
}

@Override
protected HandlerRegistration addClickHandler(
RendererClickHandler<JsonObject> handler) {
return getRenderer().addClickHandler(handler);
}
}
Expand Up @@ -16,7 +16,12 @@

package com.vaadin.client.connectors.grid;

import com.vaadin.client.ServerConnector;
import com.vaadin.client.connectors.AbstractRendererConnector;
import com.vaadin.client.widgets.Grid.Column;
import com.vaadin.shared.data.DataCommunicatorConstants;

import elemental.json.JsonObject;

/**
* An abstract base class for renderer connectors. A renderer connector is used
Expand All @@ -35,5 +40,42 @@
*/
public abstract class AbstractGridRendererConnector<T>
extends AbstractRendererConnector<T> {
// getRowKey and getColumnId will be needed here later on

/**
* Gets the row key for a row object.
* <p>
* In case this renderer wants be able to identify a row in such a way that
* the server also understands it, the row key is used for that. Rows are
* identified by unified keys between the client and the server.
*
* @param row
* the row object
* @return the row key for the given row
*/
protected String getRowKey(JsonObject row) {
return row.getString(DataCommunicatorConstants.KEY);
}

/**
* Gets the column id for a column.
* <p>
* In case this renderer wants be able to identify a column in such a way
* that the server also understands it, the column id is used for that.
* Columns are identified by unified ids between the client and the server.
*
* @param column
* the column object
* @return the column id for the given column
*/
protected String getColumnId(Column<?, JsonObject> column) {
final ServerConnector parent = getParent();
if (parent instanceof ColumnConnector) {
final ServerConnector parentGrid = parent.getParent();
if (parentGrid instanceof GridConnector) {
return ((GridConnector) parentGrid).getColumnId(column);
}
}
throw new IllegalStateException(
"Renderers can only be used with a Grid.");
}
}
Expand Up @@ -66,6 +66,17 @@ public class GridConnector
private SpaceSelectHandler<JsonObject> spaceSelectHandler;
private ClickSelectHandler<JsonObject> clickSelectHandler;

/**
* Gets the string identifier of a {@link Column} in this grid.
*
* @param column
* the column for which the identifier is to be retrieved for
* @return the string identifying the given column in this grid
*/
public String getColumnId(Grid.Column<?, ?> column) {
return columnToIdMap.get(column);
}

@Override
@SuppressWarnings("unchecked")
public Grid<JsonObject> getWidget() {
Expand Down Expand Up @@ -216,7 +227,7 @@ public List<ComponentConnector> getChildComponents() {

@Override
public void setChildComponents(List<ComponentConnector> children) {
this.childComponents = children;
childComponents = children;

}

Expand Down
33 changes: 25 additions & 8 deletions server/src/main/java/com/vaadin/ui/Grid.java
Expand Up @@ -44,6 +44,7 @@
import com.vaadin.shared.ui.grid.GridServerRpc;
import com.vaadin.shared.ui.grid.GridState;
import com.vaadin.shared.ui.grid.HeightMode;
import com.vaadin.ui.renderers.AbstractRenderer;
import com.vaadin.ui.renderers.Renderer;
import com.vaadin.ui.renderers.TextRenderer;

Expand Down Expand Up @@ -711,7 +712,7 @@ public Grid() {
}

/**
* Adds a new column to this {@link Grid} with given header caption,
* Adds a new column to this {@link Grid} with given header caption, typed
* renderer and value provider.
*
* @param caption
Expand All @@ -720,21 +721,26 @@ public Grid() {
* the value provider
* @param renderer
* the column value class
* @param <T>
* the type of this grid
* @param <V>
* the column value type
*
* @return the new column
*
* @see {@link AbstractRenderer}
*/
public <V> Column<T, V> addColumn(String caption,
Function<T, ? extends V> valueProvider, Renderer<V> renderer) {
Column<T, V> c = new Column<>(caption, valueProvider, renderer);
Function<T, ? extends V> valueProvider,
AbstractRenderer<? super T, V> renderer) {
Column<T, V> column = new Column<>(caption, valueProvider, renderer);

c.extend(this);
c.setId(columnKeys.key(c));
columnSet.add(c);
addDataGenerator(c);
column.extend(this);
column.setId(columnKeys.key(column));
columnSet.add(column);
addDataGenerator(column);

return c;
return column;
}

/**
Expand Down Expand Up @@ -813,6 +819,17 @@ public boolean isDetailsVisible(T data) {
return Collections.unmodifiableSet(columnSet);
}

/**
* Gets a {@link Column} of this grid by its identifying string.
*
* @param columnId
* the identifier of the column to get
* @return the column corresponding to the given column id
*/
public Column<T, ?> getColumn(String columnId) {
return columnKeys.get(columnId);
}

@Override
public Iterator<Component> iterator() {
return Collections.unmodifiableSet(extensionComponents).iterator();
Expand Down

0 comments on commit 33809f4

Please sign in to comment.