* @since
* @author Vaadin Ltd
*/
-@Connect(com.vaadin.ui.components.grid.renderers.ButtonRenderer.class)
+@Connect(com.vaadin.ui.renderer.ButtonRenderer.class)
public class ButtonRendererConnector extends ClickableRendererConnector<String> {
@Override
* @since
* @author Vaadin Ltd
*/
-@Connect(com.vaadin.ui.components.grid.renderers.DateRenderer.class)
+@Connect(com.vaadin.ui.renderer.DateRenderer.class)
public class DateRendererConnector extends TextRendererConnector {
// No implementation needed
}
* @since
* @author Vaadin Ltd
*/
-@Connect(com.vaadin.ui.components.grid.renderers.ImageRenderer.class)
+@Connect(com.vaadin.ui.renderer.ImageRenderer.class)
public class ImageRendererConnector extends ClickableRendererConnector<String> {
@Override
* @since
* @author Vaadin Ltd
*/
-@Connect(com.vaadin.ui.components.grid.renderers.NumberRenderer.class)
+@Connect(com.vaadin.ui.renderer.NumberRenderer.class)
public class NumberRendererConnector extends TextRendererConnector {
// no implementation needed
}
* @since
* @author Vaadin Ltd
*/
-@Connect(com.vaadin.ui.components.grid.renderers.ProgressBarRenderer.class)
+@Connect(com.vaadin.ui.renderer.ProgressBarRenderer.class)
public class ProgressBarRendererConnector extends
AbstractRendererConnector<Double> {
* @since
* @author Vaadin Ltd
*/
-@Connect(com.vaadin.ui.components.grid.renderers.TextRenderer.class)
+@Connect(com.vaadin.ui.renderer.TextRenderer.class)
public class TextRendererConnector extends AbstractRendererConnector<String> {
@Override
* @since
* @author Vaadin Ltd
*/
-@Connect(com.vaadin.ui.components.grid.renderers.HtmlRenderer.class)
+@Connect(com.vaadin.ui.renderer.HtmlRenderer.class)
public class UnsafeHtmlRendererConnector extends
AbstractRendererConnector<String> {
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.CellStyleGenerator;
import com.vaadin.ui.Grid.Column;
-import com.vaadin.ui.components.grid.Renderer;
+import com.vaadin.ui.renderer.Renderer;
import elemental.json.Json;
import elemental.json.JsonArray;
* @param removedPropertyIds
* the property ids that have been removed from the container
*/
- public void propertiesRemoved(
- @SuppressWarnings("unused") Collection<Object> removedPropertyIds) {
+ public void propertiesRemoved(@SuppressWarnings("unused")
+ Collection<Object> removedPropertyIds) {
/*
* no-op, for now.
*
import com.vaadin.shared.ui.grid.SortDirection;
import com.vaadin.shared.ui.grid.SortEventOriginator;
import com.vaadin.shared.util.SharedUtil;
-import com.vaadin.ui.components.grid.Renderer;
import com.vaadin.ui.components.grid.SortOrderChangeEvent;
import com.vaadin.ui.components.grid.SortOrderChangeListener;
-import com.vaadin.ui.components.grid.renderers.TextRenderer;
import com.vaadin.ui.components.grid.selection.MultiSelectionModel;
import com.vaadin.ui.components.grid.selection.NoSelectionModel;
import com.vaadin.ui.components.grid.selection.SelectionChangeEvent;
import com.vaadin.ui.components.grid.selection.SingleSelectionModel;
import com.vaadin.ui.components.grid.sort.Sort;
import com.vaadin.ui.components.grid.sort.SortOrder;
+import com.vaadin.ui.renderer.Renderer;
+import com.vaadin.ui.renderer.TextRenderer;
import com.vaadin.util.ReflectTools;
import elemental.json.Json;
+++ /dev/null
-/*
- * 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;
-
-import com.vaadin.server.ClientConnector;
-import com.vaadin.server.Extension;
-
-import elemental.json.JsonValue;
-
-/**
- * A ClientConnector for controlling client-side
- * {@link com.vaadin.client.ui.grid.Renderer Grid renderers}. Renderers
- * currently extend the Extension interface, but this fact should be regarded as
- * an implementation detail and subject to change in a future major or minor
- * Vaadin revision.
- *
- * @param <T>
- * the type this renderer knows how to present
- *
- * @since
- * @author Vaadin Ltd
- */
-public interface Renderer<T> extends Extension {
-
- /**
- * Returns the class literal corresponding to the presentation type T.
- *
- * @return the class literal of T
- */
- Class<T> getPresentationType();
-
- /**
- * Encodes the given value into a {@link JsonValue}.
- *
- * @param value
- * the value to encode
- * @return a JSON representation of the given value
- */
- JsonValue encode(T value);
-
- /**
- * This method is inherited from Extension but should never be called
- * directly with a Renderer.
- */
- @Override
- @Deprecated
- void remove();
-
- /**
- * This method is inherited from Extension but should never be called
- * directly with a Renderer.
- */
- @Override
- @Deprecated
- void setParent(ClientConnector parent);
-}
+++ /dev/null
-/*
- * 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;
-
-/**
- * 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
- * to the renderer, invoked when any of the rendered buttons is clicked.
- *
- * @since
- * @author Vaadin Ltd
- */
-public class ButtonRenderer extends ClickableRenderer<String> {
-
- /**
- * Creates a new button renderer.
- */
- public ButtonRenderer() {
- super(String.class);
- }
-
- /**
- * 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);
- }
-}
+++ /dev/null
-/*
- * 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.Grid;
-import com.vaadin.ui.Grid.AbstractRenderer;
-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);
- }
-}
+++ /dev/null
-/*
- * 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.text.DateFormat;
-import java.util.Date;
-import java.util.Locale;
-
-import com.vaadin.ui.Grid.AbstractRenderer;
-
-import elemental.json.JsonValue;
-
-/**
- * A renderer for presenting date values.
- *
- * @since
- * @author Vaadin Ltd
- */
-public class DateRenderer extends AbstractRenderer<Date> {
- private final Locale locale;
- private final String formatString;
- private final DateFormat dateFormat;
-
- /**
- * Creates a new date renderer.
- * <p>
- * The renderer is configured to render with the {@link Date#toString()}
- * representation for the default locale.
- */
- public DateRenderer() {
- this(Locale.getDefault());
- }
-
- /**
- * Creates a new date renderer.
- * <p>
- * The renderer is configured to render with the {@link Date#toString()}
- * representation for the given locale.
- *
- * @param locale
- * the locale in which to present dates
- * @throws IllegalArgumentException
- * if {@code locale} is {@code null}
- */
- public DateRenderer(Locale locale) throws IllegalArgumentException {
- this("%s", locale);
- }
-
- /**
- * Creates a new date renderer.
- * <p>
- * The renderer is configured to render with the given string format, as
- * displayed in the default locale.
- *
- * @param formatString
- * the format string with which to format the date
- * @throws IllegalArgumentException
- * if {@code formatString} is {@code null}
- * @see <a
- * href="http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">Format
- * String Syntax</a>
- */
- public DateRenderer(String formatString) throws IllegalArgumentException {
- this(formatString, Locale.getDefault());
- }
-
- /**
- * Creates a new date renderer.
- * <p>
- * The renderer is configured to render with the given string format, as
- * displayed in the given locale.
- *
- * @param formatString
- * the format string to format the date with
- * @param locale
- * the locale to use
- * @throws IllegalArgumentException
- * if either argument is {@code null}
- * @see <a
- * href="http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">Format
- * String Syntax</a>
- */
- public DateRenderer(String formatString, Locale locale)
- throws IllegalArgumentException {
- super(Date.class);
-
- if (formatString == null) {
- throw new IllegalArgumentException("format string may not be null");
- }
-
- if (locale == null) {
- throw new IllegalArgumentException("locale may not be null");
- }
-
- this.locale = locale;
- this.formatString = formatString;
- dateFormat = null;
- }
-
- /**
- * Creates a new date renderer.
- * <p>
- * The renderer is configured to render with he given date format.
- *
- * @param dateFormat
- * the date format to use when rendering dates
- * @throws IllegalArgumentException
- * if {@code dateFormat} is {@code null}
- */
- public DateRenderer(DateFormat dateFormat) throws IllegalArgumentException {
- super(Date.class);
- if (dateFormat == null) {
- throw new IllegalArgumentException("date format may not be null");
- }
-
- locale = null;
- formatString = null;
- this.dateFormat = dateFormat;
- }
-
- @Override
- public JsonValue encode(Date value) {
- String dateString;
- if (dateFormat != null) {
- dateString = dateFormat.format(value);
- } else {
- dateString = String.format(locale, formatString, value);
- }
- return encode(dateString, String.class);
- }
-
- @Override
- public String toString() {
- final String fieldInfo;
- if (dateFormat != null) {
- fieldInfo = "dateFormat: " + dateFormat.toString();
- } else {
- fieldInfo = "locale: " + locale + ", formatString: " + formatString;
- }
-
- return String.format("%s [%s]", getClass().getSimpleName(), fieldInfo);
- }
-}
+++ /dev/null
-/*
- * 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 com.vaadin.ui.Grid.AbstractRenderer;
-
-/**
- * A renderer for presenting HTML content.
- *
- * @since
- * @author Vaadin Ltd
- */
-public class HtmlRenderer extends AbstractRenderer<String> {
- /**
- * Creates a new HTML renderer.
- */
- public HtmlRenderer() {
- super(String.class);
- }
-}
+++ /dev/null
-/*
- * 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 com.vaadin.server.ExternalResource;
-import com.vaadin.server.Resource;
-import com.vaadin.server.ResourceReference;
-import com.vaadin.server.ThemeResource;
-import com.vaadin.shared.communication.URLReference;
-
-import elemental.json.JsonValue;
-
-/**
- * A renderer for presenting images.
- * <p>
- * The image for each rendered cell is read from a Resource-typed property in
- * the data source. Only {@link ExternalResource}s and {@link ThemeResource}s
- * are currently supported.
- *
- * @since
- * @author Vaadin Ltd
- */
-public class ImageRenderer extends ClickableRenderer<Resource> {
-
- /**
- * Creates a new image renderer.
- */
- public ImageRenderer() {
- 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)) {
- throw new IllegalArgumentException(
- "ImageRenderer only supports ExternalResource and ThemeResource ("
- + resource.getClass().getSimpleName() + "given )");
- }
-
- return encode(ResourceReference.create(resource, this, null),
- URLReference.class);
- }
-}
+++ /dev/null
-/*
- * 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.text.NumberFormat;
-import java.util.Locale;
-
-import com.vaadin.ui.Grid.AbstractRenderer;
-
-import elemental.json.JsonValue;
-
-/**
- * A renderer for presenting number values.
- *
- * @since
- * @author Vaadin Ltd
- */
-public class NumberRenderer extends AbstractRenderer<Number> {
- private final Locale locale;
- private final NumberFormat numberFormat;
- private final String formatString;
-
- /**
- * Creates a new number renderer.
- * <p>
- * The renderer is configured to render with the number's natural string
- * representation in the default locale.
- */
- public NumberRenderer() {
- this(Locale.getDefault());
- }
-
- /**
- * Creates a new number renderer.
- * <p>
- * The renderer is configured to render the number as defined with the given
- * number format.
- *
- * @param numberFormat
- * the number format with which to display numbers
- * @throws IllegalArgumentException
- * if {@code numberFormat} is {@code null}
- */
- public NumberRenderer(NumberFormat numberFormat)
- throws IllegalArgumentException {
- super(Number.class);
-
- if (numberFormat == null) {
- throw new IllegalArgumentException("Number format may not be null");
- }
-
- locale = null;
- this.numberFormat = numberFormat;
- formatString = null;
- }
-
- /**
- * Creates a new number renderer.
- * <p>
- * The renderer is configured to render with the number's natural string
- * representation in the given locale.
- *
- * @param locale
- * the locale in which to display numbers
- * @throws IllegalArgumentException
- * if {@code locale} is {@code null}
- */
- public NumberRenderer(Locale locale) throws IllegalArgumentException {
- this("%s", locale);
- }
-
- /**
- * Creates a new number renderer.
- * <p>
- * The renderer is configured to render with the given format string in the
- * default locale.
- *
- * @param formatString
- * the format string with which to format the number
- * @throws IllegalArgumentException
- * if {@code formatString} is {@code null}
- * @see <a
- * href="http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">Format
- * String Syntax</a>
- */
- public NumberRenderer(String formatString) throws IllegalArgumentException {
- this(formatString, Locale.getDefault());
- }
-
- /**
- * Creates a new number renderer.
- * <p>
- * The renderer is configured to render with the given format string in the
- * given locale.
- *
- * @param formatString
- * the format string with which to format the number
- * @param locale
- * the locale in which to present numbers
- * @throws IllegalArgumentException
- * if either argument is {@code null}
- * @see <a
- * href="http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">Format
- * String Syntax</a>
- */
- public NumberRenderer(String formatString, Locale locale) {
- super(Number.class);
-
- if (formatString == null) {
- throw new IllegalArgumentException("Format string may not be null");
- }
-
- if (locale == null) {
- throw new IllegalArgumentException("Locale may not be null");
- }
-
- this.locale = locale;
- numberFormat = null;
- this.formatString = formatString;
- }
-
- @Override
- public JsonValue encode(Number value) {
- String stringValue;
- if (formatString != null && locale != null) {
- stringValue = String.format(locale, formatString, value);
- } else if (numberFormat != null) {
- stringValue = numberFormat.format(value);
- } else {
- throw new IllegalStateException(String.format("Internal bug: "
- + "%s is in an illegal state: "
- + "[locale: %s, numberFormat: %s, formatString: %s]",
- getClass().getSimpleName(), locale, numberFormat,
- formatString));
- }
- return encode(stringValue, String.class);
- }
-
- @Override
- public String toString() {
- final String fieldInfo;
- if (numberFormat != null) {
- fieldInfo = "numberFormat: " + numberFormat.toString();
- } else {
- fieldInfo = "locale: " + locale + ", formatString: " + formatString;
- }
-
- return String.format("%s [%s]", getClass().getSimpleName(), fieldInfo);
- }
-}
+++ /dev/null
-/*
- * 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 com.vaadin.ui.Grid.AbstractRenderer;
-
-import elemental.json.JsonValue;
-
-/**
- * A renderer that represents a double values as a graphical progress bar.
- *
- * @since
- * @author Vaadin Ltd
- */
-public class ProgressBarRenderer extends AbstractRenderer<Double> {
-
- /**
- * Creates a new text renderer
- */
- public ProgressBarRenderer() {
- super(Double.class);
- }
-
- @Override
- public JsonValue encode(Double value) {
- if (value != null) {
- value = Math.max(Math.min(value, 1), 0);
- }
- return super.encode(value);
- }
-}
+++ /dev/null
-/*
- * 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 com.vaadin.ui.Grid.AbstractRenderer;
-
-/**
- * A renderer for presenting simple plain-text string values.
- *
- * @since
- * @author Vaadin Ltd
- */
-public class TextRenderer extends AbstractRenderer<String> {
-
- /**
- * Creates a new text renderer
- */
- public TextRenderer() {
- super(String.class);
- }
-}
--- /dev/null
+/*
+ * 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.renderer;
+
+/**
+ * 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
+ * to the renderer, invoked when any of the rendered buttons is clicked.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class ButtonRenderer extends ClickableRenderer<String> {
+
+ /**
+ * Creates a new button renderer.
+ */
+ public ButtonRenderer() {
+ super(String.class);
+ }
+
+ /**
+ * 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);
+ }
+}
--- /dev/null
+/*
+ * 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.renderer;
+
+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.Grid;
+import com.vaadin.ui.Grid.AbstractRenderer;
+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);
+ }
+}
--- /dev/null
+/*
+ * 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.renderer;
+
+import java.text.DateFormat;
+import java.util.Date;
+import java.util.Locale;
+
+import com.vaadin.ui.Grid.AbstractRenderer;
+
+import elemental.json.JsonValue;
+
+/**
+ * A renderer for presenting date values.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class DateRenderer extends AbstractRenderer<Date> {
+ private final Locale locale;
+ private final String formatString;
+ private final DateFormat dateFormat;
+
+ /**
+ * Creates a new date renderer.
+ * <p>
+ * The renderer is configured to render with the {@link Date#toString()}
+ * representation for the default locale.
+ */
+ public DateRenderer() {
+ this(Locale.getDefault());
+ }
+
+ /**
+ * Creates a new date renderer.
+ * <p>
+ * The renderer is configured to render with the {@link Date#toString()}
+ * representation for the given locale.
+ *
+ * @param locale
+ * the locale in which to present dates
+ * @throws IllegalArgumentException
+ * if {@code locale} is {@code null}
+ */
+ public DateRenderer(Locale locale) throws IllegalArgumentException {
+ this("%s", locale);
+ }
+
+ /**
+ * Creates a new date renderer.
+ * <p>
+ * The renderer is configured to render with the given string format, as
+ * displayed in the default locale.
+ *
+ * @param formatString
+ * the format string with which to format the date
+ * @throws IllegalArgumentException
+ * if {@code formatString} is {@code null}
+ * @see <a
+ * href="http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">Format
+ * String Syntax</a>
+ */
+ public DateRenderer(String formatString) throws IllegalArgumentException {
+ this(formatString, Locale.getDefault());
+ }
+
+ /**
+ * Creates a new date renderer.
+ * <p>
+ * The renderer is configured to render with the given string format, as
+ * displayed in the given locale.
+ *
+ * @param formatString
+ * the format string to format the date with
+ * @param locale
+ * the locale to use
+ * @throws IllegalArgumentException
+ * if either argument is {@code null}
+ * @see <a
+ * href="http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">Format
+ * String Syntax</a>
+ */
+ public DateRenderer(String formatString, Locale locale)
+ throws IllegalArgumentException {
+ super(Date.class);
+
+ if (formatString == null) {
+ throw new IllegalArgumentException("format string may not be null");
+ }
+
+ if (locale == null) {
+ throw new IllegalArgumentException("locale may not be null");
+ }
+
+ this.locale = locale;
+ this.formatString = formatString;
+ dateFormat = null;
+ }
+
+ /**
+ * Creates a new date renderer.
+ * <p>
+ * The renderer is configured to render with he given date format.
+ *
+ * @param dateFormat
+ * the date format to use when rendering dates
+ * @throws IllegalArgumentException
+ * if {@code dateFormat} is {@code null}
+ */
+ public DateRenderer(DateFormat dateFormat) throws IllegalArgumentException {
+ super(Date.class);
+ if (dateFormat == null) {
+ throw new IllegalArgumentException("date format may not be null");
+ }
+
+ locale = null;
+ formatString = null;
+ this.dateFormat = dateFormat;
+ }
+
+ @Override
+ public JsonValue encode(Date value) {
+ String dateString;
+ if (dateFormat != null) {
+ dateString = dateFormat.format(value);
+ } else {
+ dateString = String.format(locale, formatString, value);
+ }
+ return encode(dateString, String.class);
+ }
+
+ @Override
+ public String toString() {
+ final String fieldInfo;
+ if (dateFormat != null) {
+ fieldInfo = "dateFormat: " + dateFormat.toString();
+ } else {
+ fieldInfo = "locale: " + locale + ", formatString: " + formatString;
+ }
+
+ return String.format("%s [%s]", getClass().getSimpleName(), fieldInfo);
+ }
+}
--- /dev/null
+/*
+ * 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.renderer;
+
+import com.vaadin.ui.Grid.AbstractRenderer;
+
+/**
+ * A renderer for presenting HTML content.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class HtmlRenderer extends AbstractRenderer<String> {
+ /**
+ * Creates a new HTML renderer.
+ */
+ public HtmlRenderer() {
+ super(String.class);
+ }
+}
--- /dev/null
+/*
+ * 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.renderer;
+
+import com.vaadin.server.ExternalResource;
+import com.vaadin.server.Resource;
+import com.vaadin.server.ResourceReference;
+import com.vaadin.server.ThemeResource;
+import com.vaadin.shared.communication.URLReference;
+
+import elemental.json.JsonValue;
+
+/**
+ * A renderer for presenting images.
+ * <p>
+ * The image for each rendered cell is read from a Resource-typed property in
+ * the data source. Only {@link ExternalResource}s and {@link ThemeResource}s
+ * are currently supported.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class ImageRenderer extends ClickableRenderer<Resource> {
+
+ /**
+ * Creates a new image renderer.
+ */
+ public ImageRenderer() {
+ 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)) {
+ throw new IllegalArgumentException(
+ "ImageRenderer only supports ExternalResource and ThemeResource ("
+ + resource.getClass().getSimpleName() + "given )");
+ }
+
+ return encode(ResourceReference.create(resource, this, null),
+ URLReference.class);
+ }
+}
--- /dev/null
+/*
+ * 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.renderer;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+import com.vaadin.ui.Grid.AbstractRenderer;
+
+import elemental.json.JsonValue;
+
+/**
+ * A renderer for presenting number values.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class NumberRenderer extends AbstractRenderer<Number> {
+ private final Locale locale;
+ private final NumberFormat numberFormat;
+ private final String formatString;
+
+ /**
+ * Creates a new number renderer.
+ * <p>
+ * The renderer is configured to render with the number's natural string
+ * representation in the default locale.
+ */
+ public NumberRenderer() {
+ this(Locale.getDefault());
+ }
+
+ /**
+ * Creates a new number renderer.
+ * <p>
+ * The renderer is configured to render the number as defined with the given
+ * number format.
+ *
+ * @param numberFormat
+ * the number format with which to display numbers
+ * @throws IllegalArgumentException
+ * if {@code numberFormat} is {@code null}
+ */
+ public NumberRenderer(NumberFormat numberFormat)
+ throws IllegalArgumentException {
+ super(Number.class);
+
+ if (numberFormat == null) {
+ throw new IllegalArgumentException("Number format may not be null");
+ }
+
+ locale = null;
+ this.numberFormat = numberFormat;
+ formatString = null;
+ }
+
+ /**
+ * Creates a new number renderer.
+ * <p>
+ * The renderer is configured to render with the number's natural string
+ * representation in the given locale.
+ *
+ * @param locale
+ * the locale in which to display numbers
+ * @throws IllegalArgumentException
+ * if {@code locale} is {@code null}
+ */
+ public NumberRenderer(Locale locale) throws IllegalArgumentException {
+ this("%s", locale);
+ }
+
+ /**
+ * Creates a new number renderer.
+ * <p>
+ * The renderer is configured to render with the given format string in the
+ * default locale.
+ *
+ * @param formatString
+ * the format string with which to format the number
+ * @throws IllegalArgumentException
+ * if {@code formatString} is {@code null}
+ * @see <a
+ * href="http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">Format
+ * String Syntax</a>
+ */
+ public NumberRenderer(String formatString) throws IllegalArgumentException {
+ this(formatString, Locale.getDefault());
+ }
+
+ /**
+ * Creates a new number renderer.
+ * <p>
+ * The renderer is configured to render with the given format string in the
+ * given locale.
+ *
+ * @param formatString
+ * the format string with which to format the number
+ * @param locale
+ * the locale in which to present numbers
+ * @throws IllegalArgumentException
+ * if either argument is {@code null}
+ * @see <a
+ * href="http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">Format
+ * String Syntax</a>
+ */
+ public NumberRenderer(String formatString, Locale locale) {
+ super(Number.class);
+
+ if (formatString == null) {
+ throw new IllegalArgumentException("Format string may not be null");
+ }
+
+ if (locale == null) {
+ throw new IllegalArgumentException("Locale may not be null");
+ }
+
+ this.locale = locale;
+ numberFormat = null;
+ this.formatString = formatString;
+ }
+
+ @Override
+ public JsonValue encode(Number value) {
+ String stringValue;
+ if (formatString != null && locale != null) {
+ stringValue = String.format(locale, formatString, value);
+ } else if (numberFormat != null) {
+ stringValue = numberFormat.format(value);
+ } else {
+ throw new IllegalStateException(String.format("Internal bug: "
+ + "%s is in an illegal state: "
+ + "[locale: %s, numberFormat: %s, formatString: %s]",
+ getClass().getSimpleName(), locale, numberFormat,
+ formatString));
+ }
+ return encode(stringValue, String.class);
+ }
+
+ @Override
+ public String toString() {
+ final String fieldInfo;
+ if (numberFormat != null) {
+ fieldInfo = "numberFormat: " + numberFormat.toString();
+ } else {
+ fieldInfo = "locale: " + locale + ", formatString: " + formatString;
+ }
+
+ return String.format("%s [%s]", getClass().getSimpleName(), fieldInfo);
+ }
+}
--- /dev/null
+/*
+ * 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.renderer;
+
+import com.vaadin.ui.Grid.AbstractRenderer;
+
+import elemental.json.JsonValue;
+
+/**
+ * A renderer that represents a double values as a graphical progress bar.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class ProgressBarRenderer extends AbstractRenderer<Double> {
+
+ /**
+ * Creates a new text renderer
+ */
+ public ProgressBarRenderer() {
+ super(Double.class);
+ }
+
+ @Override
+ public JsonValue encode(Double value) {
+ if (value != null) {
+ value = Math.max(Math.min(value, 1), 0);
+ }
+ return super.encode(value);
+ }
+}
--- /dev/null
+/*
+ * 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.renderer;
+
+import com.vaadin.server.ClientConnector;
+import com.vaadin.server.Extension;
+
+import elemental.json.JsonValue;
+
+/**
+ * A ClientConnector for controlling client-side
+ * {@link com.vaadin.client.ui.grid.Renderer Grid renderers}. Renderers
+ * currently extend the Extension interface, but this fact should be regarded as
+ * an implementation detail and subject to change in a future major or minor
+ * Vaadin revision.
+ *
+ * @param <T>
+ * the type this renderer knows how to present
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface Renderer<T> extends Extension {
+
+ /**
+ * Returns the class literal corresponding to the presentation type T.
+ *
+ * @return the class literal of T
+ */
+ Class<T> getPresentationType();
+
+ /**
+ * Encodes the given value into a {@link JsonValue}.
+ *
+ * @param value
+ * the value to encode
+ * @return a JSON representation of the given value
+ */
+ JsonValue encode(T value);
+
+ /**
+ * This method is inherited from Extension but should never be called
+ * directly with a Renderer.
+ */
+ @Override
+ @Deprecated
+ void remove();
+
+ /**
+ * This method is inherited from Extension but should never be called
+ * directly with a Renderer.
+ */
+ @Override
+ @Deprecated
+ void setParent(ClientConnector parent);
+}
--- /dev/null
+/*
+ * 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.renderer;
+
+import com.vaadin.ui.Grid.AbstractRenderer;
+
+/**
+ * A renderer for presenting simple plain-text string values.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class TextRenderer extends AbstractRenderer<String> {
+
+ /**
+ * Creates a new text renderer
+ */
+ public TextRenderer() {
+ super(String.class);
+ }
+}
+++ /dev/null
-/*
- * 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.tests.server.component.grid;
-
-import static org.junit.Assert.assertEquals;
-
-import java.io.File;
-
-import org.easymock.EasyMock;
-import org.junit.Before;
-import org.junit.Test;
-
-import com.vaadin.server.ClassResource;
-import com.vaadin.server.ExternalResource;
-import com.vaadin.server.FileResource;
-import com.vaadin.server.FontAwesome;
-import com.vaadin.server.ThemeResource;
-import com.vaadin.ui.Grid;
-import com.vaadin.ui.UI;
-import com.vaadin.ui.components.grid.renderers.ImageRenderer;
-
-import elemental.json.JsonObject;
-import elemental.json.JsonValue;
-
-public class ImageRendererTest {
-
- private ImageRenderer renderer;
-
- @Before
- public void setUp() {
- UI mockUI = EasyMock.createNiceMock(UI.class);
- EasyMock.replay(mockUI);
-
- Grid grid = new Grid();
- grid.setParent(mockUI);
-
- renderer = new ImageRenderer();
- renderer.setParent(grid);
- }
-
- @Test
- public void testThemeResource() {
- JsonValue v = renderer.encode(new ThemeResource("foo.png"));
- assertEquals("theme://foo.png", getUrl(v));
- }
-
- @Test
- public void testExternalResource() {
- JsonValue v = renderer.encode(new ExternalResource(
- "http://example.com/foo.png"));
- assertEquals("http://example.com/foo.png", getUrl(v));
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testFileResource() {
- renderer.encode(new FileResource(new File("/tmp/foo.png")));
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testClassResource() {
- renderer.encode(new ClassResource("img/foo.png"));
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testFontIcon() {
- renderer.encode(FontAwesome.AMBULANCE);
- }
-
- private String getUrl(JsonValue v) {
- return ((JsonObject) v).get("uRL").asString();
- }
-}
+++ /dev/null
-/*
- * 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.tests.server.component.grid;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-
-import java.util.Locale;
-
-import org.easymock.EasyMock;
-import org.junit.Before;
-import org.junit.Test;
-
-import com.vaadin.data.Item;
-import com.vaadin.data.RpcDataProviderExtension;
-import com.vaadin.data.util.IndexedContainer;
-import com.vaadin.data.util.converter.Converter;
-import com.vaadin.data.util.converter.StringToIntegerConverter;
-import com.vaadin.server.VaadinSession;
-import com.vaadin.tests.util.AlwaysLockedVaadinSession;
-import com.vaadin.ui.ConnectorTracker;
-import com.vaadin.ui.Grid;
-import com.vaadin.ui.Grid.Column;
-import com.vaadin.ui.UI;
-import com.vaadin.ui.components.grid.renderers.TextRenderer;
-
-import elemental.json.JsonValue;
-
-public class RendererTest {
-
- private static class TestBean {
- int i = 42;
- }
-
- private static class ExtendedBean extends TestBean {
- float f = 3.14f;
- }
-
- private static class TestRenderer extends TextRenderer {
- @Override
- public JsonValue encode(String value) {
- return super.encode("renderer(" + value + ")");
- }
- }
-
- private static class TestConverter implements Converter<String, TestBean> {
-
- @Override
- public TestBean convertToModel(String value,
- Class<? extends TestBean> targetType, Locale locale)
- throws ConversionException {
- return null;
- }
-
- @Override
- public String convertToPresentation(TestBean value,
- Class<? extends String> targetType, Locale locale)
- throws ConversionException {
- if (value instanceof ExtendedBean) {
- return "ExtendedBean(" + value.i + ", "
- + ((ExtendedBean) value).f + ")";
- } else {
- return "TestBean(" + value.i + ")";
- }
- }
-
- @Override
- public Class<TestBean> getModelType() {
- return TestBean.class;
- }
-
- @Override
- public Class<String> getPresentationType() {
- return String.class;
- }
- }
-
- private Grid grid;
-
- private Column foo;
- private Column bar;
- private Column baz;
- private Column bah;
-
- @Before
- public void setUp() {
- VaadinSession.setCurrent(new AlwaysLockedVaadinSession(null));
-
- IndexedContainer c = new IndexedContainer();
-
- c.addContainerProperty("foo", Integer.class, 0);
- c.addContainerProperty("bar", String.class, "");
- c.addContainerProperty("baz", TestBean.class, null);
- c.addContainerProperty("bah", ExtendedBean.class, null);
-
- Object id = c.addItem();
- Item item = c.getItem(id);
- item.getItemProperty("foo").setValue(123);
- item.getItemProperty("bar").setValue("321");
- item.getItemProperty("baz").setValue(new TestBean());
- item.getItemProperty("bah").setValue(new ExtendedBean());
-
- UI ui = EasyMock.createNiceMock(UI.class);
- ConnectorTracker ct = EasyMock.createNiceMock(ConnectorTracker.class);
- EasyMock.expect(ui.getConnectorTracker()).andReturn(ct).anyTimes();
- EasyMock.replay(ui, ct);
-
- grid = new Grid(c);
- grid.setParent(ui);
-
- foo = grid.getColumn("foo");
- bar = grid.getColumn("bar");
- baz = grid.getColumn("baz");
- bah = grid.getColumn("bah");
- }
-
- @Test
- public void testDefaultRendererAndConverter() throws Exception {
- assertSame(TextRenderer.class, foo.getRenderer().getClass());
- assertSame(StringToIntegerConverter.class, foo.getConverter()
- .getClass());
-
- assertSame(TextRenderer.class, bar.getRenderer().getClass());
- // String->String; converter not needed
- assertNull(bar.getConverter());
-
- assertSame(TextRenderer.class, baz.getRenderer().getClass());
- // MyBean->String; converter not found
- assertNull(baz.getConverter());
- }
-
- @Test
- public void testFindCompatibleConverter() throws Exception {
- foo.setRenderer(renderer());
- assertSame(StringToIntegerConverter.class, foo.getConverter()
- .getClass());
-
- bar.setRenderer(renderer());
- assertNull(bar.getConverter());
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testCannotFindConverter() {
- baz.setRenderer(renderer());
- }
-
- @Test
- public void testExplicitConverter() throws Exception {
- baz.setRenderer(renderer(), converter());
- bah.setRenderer(renderer(), converter());
- }
-
- @Test
- public void testEncoding() throws Exception {
- assertEquals("42", render(foo, 42).asString());
- foo.setRenderer(renderer());
- assertEquals("renderer(42)", render(foo, 42).asString());
-
- assertEquals("2.72", render(bar, "2.72").asString());
- bar.setRenderer(new TestRenderer());
- assertEquals("renderer(2.72)", render(bar, "2.72").asString());
- }
-
- @Test
- public void testEncodingWithoutConverter() throws Exception {
- assertEquals("", render(baz, new TestBean()).asString());
- }
-
- @Test
- public void testBeanEncoding() throws Exception {
- baz.setRenderer(renderer(), converter());
- bah.setRenderer(renderer(), converter());
-
- assertEquals("renderer(TestBean(42))", render(baz, new TestBean())
- .asString());
- assertEquals("renderer(ExtendedBean(42, 3.14))",
- render(baz, new ExtendedBean()).asString());
-
- assertEquals("renderer(ExtendedBean(42, 3.14))",
- render(bah, new ExtendedBean()).asString());
- }
-
- private TestConverter converter() {
- return new TestConverter();
- }
-
- private TestRenderer renderer() {
- return new TestRenderer();
- }
-
- private JsonValue render(Column column, Object value) {
- return RpcDataProviderExtension.encodeValue(value,
- column.getRenderer(), column.getConverter(), grid.getLocale());
- }
-}
--- /dev/null
+/*
+ * 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.tests.server.renderer;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.vaadin.server.ClassResource;
+import com.vaadin.server.ExternalResource;
+import com.vaadin.server.FileResource;
+import com.vaadin.server.FontAwesome;
+import com.vaadin.server.ThemeResource;
+import com.vaadin.ui.Grid;
+import com.vaadin.ui.UI;
+import com.vaadin.ui.renderer.ImageRenderer;
+
+import elemental.json.JsonObject;
+import elemental.json.JsonValue;
+
+public class ImageRendererTest {
+
+ private ImageRenderer renderer;
+
+ @Before
+ public void setUp() {
+ UI mockUI = EasyMock.createNiceMock(UI.class);
+ EasyMock.replay(mockUI);
+
+ Grid grid = new Grid();
+ grid.setParent(mockUI);
+
+ renderer = new ImageRenderer();
+ renderer.setParent(grid);
+ }
+
+ @Test
+ public void testThemeResource() {
+ JsonValue v = renderer.encode(new ThemeResource("foo.png"));
+ assertEquals("theme://foo.png", getUrl(v));
+ }
+
+ @Test
+ public void testExternalResource() {
+ JsonValue v = renderer.encode(new ExternalResource(
+ "http://example.com/foo.png"));
+ assertEquals("http://example.com/foo.png", getUrl(v));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testFileResource() {
+ renderer.encode(new FileResource(new File("/tmp/foo.png")));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testClassResource() {
+ renderer.encode(new ClassResource("img/foo.png"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testFontIcon() {
+ renderer.encode(FontAwesome.AMBULANCE);
+ }
+
+ private String getUrl(JsonValue v) {
+ return ((JsonObject) v).get("uRL").asString();
+ }
+}
--- /dev/null
+/*
+ * 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.tests.server.renderer;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+import java.util.Locale;
+
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.vaadin.data.Item;
+import com.vaadin.data.RpcDataProviderExtension;
+import com.vaadin.data.util.IndexedContainer;
+import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.StringToIntegerConverter;
+import com.vaadin.server.VaadinSession;
+import com.vaadin.tests.util.AlwaysLockedVaadinSession;
+import com.vaadin.ui.ConnectorTracker;
+import com.vaadin.ui.Grid;
+import com.vaadin.ui.Grid.Column;
+import com.vaadin.ui.UI;
+import com.vaadin.ui.renderer.TextRenderer;
+
+import elemental.json.JsonValue;
+
+public class RendererTest {
+
+ private static class TestBean {
+ int i = 42;
+ }
+
+ private static class ExtendedBean extends TestBean {
+ float f = 3.14f;
+ }
+
+ private static class TestRenderer extends TextRenderer {
+ @Override
+ public JsonValue encode(String value) {
+ return super.encode("renderer(" + value + ")");
+ }
+ }
+
+ private static class TestConverter implements Converter<String, TestBean> {
+
+ @Override
+ public TestBean convertToModel(String value,
+ Class<? extends TestBean> targetType, Locale locale)
+ throws ConversionException {
+ return null;
+ }
+
+ @Override
+ public String convertToPresentation(TestBean value,
+ Class<? extends String> targetType, Locale locale)
+ throws ConversionException {
+ if (value instanceof ExtendedBean) {
+ return "ExtendedBean(" + value.i + ", "
+ + ((ExtendedBean) value).f + ")";
+ } else {
+ return "TestBean(" + value.i + ")";
+ }
+ }
+
+ @Override
+ public Class<TestBean> getModelType() {
+ return TestBean.class;
+ }
+
+ @Override
+ public Class<String> getPresentationType() {
+ return String.class;
+ }
+ }
+
+ private Grid grid;
+
+ private Column foo;
+ private Column bar;
+ private Column baz;
+ private Column bah;
+
+ @Before
+ public void setUp() {
+ VaadinSession.setCurrent(new AlwaysLockedVaadinSession(null));
+
+ IndexedContainer c = new IndexedContainer();
+
+ c.addContainerProperty("foo", Integer.class, 0);
+ c.addContainerProperty("bar", String.class, "");
+ c.addContainerProperty("baz", TestBean.class, null);
+ c.addContainerProperty("bah", ExtendedBean.class, null);
+
+ Object id = c.addItem();
+ Item item = c.getItem(id);
+ item.getItemProperty("foo").setValue(123);
+ item.getItemProperty("bar").setValue("321");
+ item.getItemProperty("baz").setValue(new TestBean());
+ item.getItemProperty("bah").setValue(new ExtendedBean());
+
+ UI ui = EasyMock.createNiceMock(UI.class);
+ ConnectorTracker ct = EasyMock.createNiceMock(ConnectorTracker.class);
+ EasyMock.expect(ui.getConnectorTracker()).andReturn(ct).anyTimes();
+ EasyMock.replay(ui, ct);
+
+ grid = new Grid(c);
+ grid.setParent(ui);
+
+ foo = grid.getColumn("foo");
+ bar = grid.getColumn("bar");
+ baz = grid.getColumn("baz");
+ bah = grid.getColumn("bah");
+ }
+
+ @Test
+ public void testDefaultRendererAndConverter() throws Exception {
+ assertSame(TextRenderer.class, foo.getRenderer().getClass());
+ assertSame(StringToIntegerConverter.class, foo.getConverter()
+ .getClass());
+
+ assertSame(TextRenderer.class, bar.getRenderer().getClass());
+ // String->String; converter not needed
+ assertNull(bar.getConverter());
+
+ assertSame(TextRenderer.class, baz.getRenderer().getClass());
+ // MyBean->String; converter not found
+ assertNull(baz.getConverter());
+ }
+
+ @Test
+ public void testFindCompatibleConverter() throws Exception {
+ foo.setRenderer(renderer());
+ assertSame(StringToIntegerConverter.class, foo.getConverter()
+ .getClass());
+
+ bar.setRenderer(renderer());
+ assertNull(bar.getConverter());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testCannotFindConverter() {
+ baz.setRenderer(renderer());
+ }
+
+ @Test
+ public void testExplicitConverter() throws Exception {
+ baz.setRenderer(renderer(), converter());
+ bah.setRenderer(renderer(), converter());
+ }
+
+ @Test
+ public void testEncoding() throws Exception {
+ assertEquals("42", render(foo, 42).asString());
+ foo.setRenderer(renderer());
+ assertEquals("renderer(42)", render(foo, 42).asString());
+
+ assertEquals("2.72", render(bar, "2.72").asString());
+ bar.setRenderer(new TestRenderer());
+ assertEquals("renderer(2.72)", render(bar, "2.72").asString());
+ }
+
+ @Test
+ public void testEncodingWithoutConverter() throws Exception {
+ assertEquals("", render(baz, new TestBean()).asString());
+ }
+
+ @Test
+ public void testBeanEncoding() throws Exception {
+ baz.setRenderer(renderer(), converter());
+ bah.setRenderer(renderer(), converter());
+
+ assertEquals("renderer(TestBean(42))", render(baz, new TestBean())
+ .asString());
+ assertEquals("renderer(ExtendedBean(42, 3.14))",
+ render(baz, new ExtendedBean()).asString());
+
+ assertEquals("renderer(ExtendedBean(42, 3.14))",
+ render(bah, new ExtendedBean()).asString());
+ }
+
+ private TestConverter converter() {
+ return new TestConverter();
+ }
+
+ private TestRenderer renderer() {
+ return new TestRenderer();
+ }
+
+ private JsonValue render(Column column, Object value) {
+ return RpcDataProviderExtension.encodeValue(value,
+ column.getRenderer(), column.getConverter(), grid.getLocale());
+ }
+}
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.FooterRow;
import com.vaadin.ui.Grid.HeaderRow;
-import com.vaadin.ui.components.grid.renderers.NumberRenderer;
+import com.vaadin.ui.renderer.NumberRenderer;
public class GridColspans extends AbstractTestUI {
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.Column;
import com.vaadin.ui.Grid.SelectionMode;
-import com.vaadin.ui.components.grid.renderers.HtmlRenderer;
+import com.vaadin.ui.renderer.HtmlRenderer;
public class GridColumnAutoWidth extends AbstractTestUI {
@Override
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.SelectionMode;
-import com.vaadin.ui.components.grid.renderers.ButtonRenderer;
-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;
+import com.vaadin.ui.renderer.ButtonRenderer;
+import com.vaadin.ui.renderer.ClickableRenderer.RendererClickEvent;
+import com.vaadin.ui.renderer.ClickableRenderer.RendererClickListener;
+import com.vaadin.ui.renderer.ImageRenderer;
+import com.vaadin.ui.renderer.ProgressBarRenderer;
public class WidgetRenderers extends AbstractTestUI {
import com.vaadin.ui.Grid.SelectionMode;
import com.vaadin.ui.components.grid.SortOrderChangeEvent;
import com.vaadin.ui.components.grid.SortOrderChangeListener;
-import com.vaadin.ui.components.grid.renderers.DateRenderer;
-import com.vaadin.ui.components.grid.renderers.HtmlRenderer;
-import com.vaadin.ui.components.grid.renderers.NumberRenderer;
import com.vaadin.ui.components.grid.selection.MultiSelectionModel;
-import com.vaadin.ui.components.grid.selection.SelectionModel;
import com.vaadin.ui.components.grid.sort.Sort;
import com.vaadin.ui.components.grid.sort.SortOrder;
+import com.vaadin.ui.renderer.DateRenderer;
+import com.vaadin.ui.renderer.HtmlRenderer;
+import com.vaadin.ui.renderer.NumberRenderer;
/**
* Tests the basic features like columns, footers and headers
grid.setSelectionMode(SelectionMode.MULTI);
}
- ((MultiSelectionModel) grid.getSelectionModel()).setSelectionLimit(limit
- .intValue());
+ ((MultiSelectionModel) grid.getSelectionModel())
+ .setSelectionLimit(limit.intValue());
}
});