From: Teemu Suo-Anttila Date: Thu, 11 Dec 2014 11:42:06 +0000 (+0200) Subject: Move Renderers to com.vaadin.ui.renderer package (#13334) X-Git-Tag: 7.4.0.beta1~9^2~41 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=66e07930b9749046e65434eb7d5dc8125a1f761c;p=vaadin-framework.git Move Renderers to com.vaadin.ui.renderer package (#13334) Change-Id: I80370f05cfa8123d64728abd5ede9642931e0cd8 --- diff --git a/client/src/com/vaadin/client/connectors/ButtonRendererConnector.java b/client/src/com/vaadin/client/connectors/ButtonRendererConnector.java index 45556d6176..e4f850fb48 100644 --- a/client/src/com/vaadin/client/connectors/ButtonRendererConnector.java +++ b/client/src/com/vaadin/client/connectors/ButtonRendererConnector.java @@ -27,7 +27,7 @@ import com.vaadin.shared.ui.Connect; * @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 { @Override diff --git a/client/src/com/vaadin/client/connectors/DateRendererConnector.java b/client/src/com/vaadin/client/connectors/DateRendererConnector.java index b9fad95f0d..2df8184eaf 100644 --- a/client/src/com/vaadin/client/connectors/DateRendererConnector.java +++ b/client/src/com/vaadin/client/connectors/DateRendererConnector.java @@ -28,7 +28,7 @@ import com.vaadin.shared.ui.Connect; * @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 } diff --git a/client/src/com/vaadin/client/connectors/ImageRendererConnector.java b/client/src/com/vaadin/client/connectors/ImageRendererConnector.java index 33e108cd1e..c5dfd97f69 100644 --- a/client/src/com/vaadin/client/connectors/ImageRendererConnector.java +++ b/client/src/com/vaadin/client/connectors/ImageRendererConnector.java @@ -31,7 +31,7 @@ import com.vaadin.shared.ui.Connect; * @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 { @Override diff --git a/client/src/com/vaadin/client/connectors/NumberRendererConnector.java b/client/src/com/vaadin/client/connectors/NumberRendererConnector.java index cf8ef3e076..68c96d21bc 100644 --- a/client/src/com/vaadin/client/connectors/NumberRendererConnector.java +++ b/client/src/com/vaadin/client/connectors/NumberRendererConnector.java @@ -29,7 +29,7 @@ import com.vaadin.shared.ui.Connect; * @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 } diff --git a/client/src/com/vaadin/client/connectors/ProgressBarRendererConnector.java b/client/src/com/vaadin/client/connectors/ProgressBarRendererConnector.java index ec7e8d0d08..c3dfa66dbd 100644 --- a/client/src/com/vaadin/client/connectors/ProgressBarRendererConnector.java +++ b/client/src/com/vaadin/client/connectors/ProgressBarRendererConnector.java @@ -24,7 +24,7 @@ import com.vaadin.shared.ui.Connect; * @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 { diff --git a/client/src/com/vaadin/client/connectors/TextRendererConnector.java b/client/src/com/vaadin/client/connectors/TextRendererConnector.java index 6af095c540..53aee83497 100644 --- a/client/src/com/vaadin/client/connectors/TextRendererConnector.java +++ b/client/src/com/vaadin/client/connectors/TextRendererConnector.java @@ -24,7 +24,7 @@ import com.vaadin.shared.ui.Connect; * @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 { @Override diff --git a/client/src/com/vaadin/client/connectors/UnsafeHtmlRendererConnector.java b/client/src/com/vaadin/client/connectors/UnsafeHtmlRendererConnector.java index d696b50214..fd93dfd9e2 100644 --- a/client/src/com/vaadin/client/connectors/UnsafeHtmlRendererConnector.java +++ b/client/src/com/vaadin/client/connectors/UnsafeHtmlRendererConnector.java @@ -25,7 +25,7 @@ import com.vaadin.shared.ui.Connect; * @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 { diff --git a/server/src/com/vaadin/data/RpcDataProviderExtension.java b/server/src/com/vaadin/data/RpcDataProviderExtension.java index aa0d6b3716..9f7c783537 100644 --- a/server/src/com/vaadin/data/RpcDataProviderExtension.java +++ b/server/src/com/vaadin/data/RpcDataProviderExtension.java @@ -52,7 +52,7 @@ import com.vaadin.shared.ui.grid.Range; 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; @@ -439,8 +439,8 @@ public class RpcDataProviderExtension extends AbstractExtension { * @param removedPropertyIds * the property ids that have been removed from the container */ - public void propertiesRemoved( - @SuppressWarnings("unused") Collection removedPropertyIds) { + public void propertiesRemoved(@SuppressWarnings("unused") + Collection removedPropertyIds) { /* * no-op, for now. * diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index 3896c53268..7a6e6abef2 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -77,10 +77,8 @@ import com.vaadin.shared.ui.grid.ScrollDestination; 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; @@ -90,6 +88,8 @@ import com.vaadin.ui.components.grid.selection.SelectionModel; 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; diff --git a/server/src/com/vaadin/ui/components/grid/Renderer.java b/server/src/com/vaadin/ui/components/grid/Renderer.java deleted file mode 100644 index 1e6361081e..0000000000 --- a/server/src/com/vaadin/ui/components/grid/Renderer.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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 - * the type this renderer knows how to present - * - * @since - * @author Vaadin Ltd - */ -public interface Renderer extends Extension { - - /** - * Returns the class literal corresponding to the presentation type T. - * - * @return the class literal of T - */ - Class 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); -} diff --git a/server/src/com/vaadin/ui/components/grid/renderers/ButtonRenderer.java b/server/src/com/vaadin/ui/components/grid/renderers/ButtonRenderer.java deleted file mode 100644 index dece41c7ab..0000000000 --- a/server/src/com/vaadin/ui/components/grid/renderers/ButtonRenderer.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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 { - - /** - * 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); - } -} diff --git a/server/src/com/vaadin/ui/components/grid/renderers/ClickableRenderer.java b/server/src/com/vaadin/ui/components/grid/renderers/ClickableRenderer.java deleted file mode 100644 index c08ef73ae2..0000000000 --- a/server/src/com/vaadin/ui/components/grid/renderers/ClickableRenderer.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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 - * the type presented by the renderer - * - * @since - * @author Vaadin Ltd - */ -public class ClickableRenderer extends AbstractRenderer { - - /** - * 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 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/DateRenderer.java b/server/src/com/vaadin/ui/components/grid/renderers/DateRenderer.java deleted file mode 100644 index 94eeab7d34..0000000000 --- a/server/src/com/vaadin/ui/components/grid/renderers/DateRenderer.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * 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 { - private final Locale locale; - private final String formatString; - private final DateFormat dateFormat; - - /** - * Creates a new date renderer. - *

- * 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. - *

- * 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. - *

- * 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 Format - * String Syntax - */ - public DateRenderer(String formatString) throws IllegalArgumentException { - this(formatString, Locale.getDefault()); - } - - /** - * Creates a new date renderer. - *

- * 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 Format - * String Syntax - */ - 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. - *

- * 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); - } -} diff --git a/server/src/com/vaadin/ui/components/grid/renderers/HtmlRenderer.java b/server/src/com/vaadin/ui/components/grid/renderers/HtmlRenderer.java deleted file mode 100644 index 6507792cac..0000000000 --- a/server/src/com/vaadin/ui/components/grid/renderers/HtmlRenderer.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 { - /** - * Creates a new HTML renderer. - */ - public HtmlRenderer() { - super(String.class); - } -} diff --git a/server/src/com/vaadin/ui/components/grid/renderers/ImageRenderer.java b/server/src/com/vaadin/ui/components/grid/renderers/ImageRenderer.java deleted file mode 100644 index be57a5ec19..0000000000 --- a/server/src/com/vaadin/ui/components/grid/renderers/ImageRenderer.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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. - *

- * 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 { - - /** - * 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); - } -} diff --git a/server/src/com/vaadin/ui/components/grid/renderers/NumberRenderer.java b/server/src/com/vaadin/ui/components/grid/renderers/NumberRenderer.java deleted file mode 100644 index 955b36d0a9..0000000000 --- a/server/src/com/vaadin/ui/components/grid/renderers/NumberRenderer.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * 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 { - private final Locale locale; - private final NumberFormat numberFormat; - private final String formatString; - - /** - * Creates a new number renderer. - *

- * 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. - *

- * 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. - *

- * 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. - *

- * 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 Format - * String Syntax - */ - public NumberRenderer(String formatString) throws IllegalArgumentException { - this(formatString, Locale.getDefault()); - } - - /** - * Creates a new number renderer. - *

- * 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 Format - * String Syntax - */ - 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); - } -} diff --git a/server/src/com/vaadin/ui/components/grid/renderers/ProgressBarRenderer.java b/server/src/com/vaadin/ui/components/grid/renderers/ProgressBarRenderer.java deleted file mode 100644 index 7021916cd7..0000000000 --- a/server/src/com/vaadin/ui/components/grid/renderers/ProgressBarRenderer.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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 { - - /** - * 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); - } -} diff --git a/server/src/com/vaadin/ui/components/grid/renderers/TextRenderer.java b/server/src/com/vaadin/ui/components/grid/renderers/TextRenderer.java deleted file mode 100644 index 554d57f295..0000000000 --- a/server/src/com/vaadin/ui/components/grid/renderers/TextRenderer.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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 { - - /** - * Creates a new text renderer - */ - public TextRenderer() { - super(String.class); - } -} diff --git a/server/src/com/vaadin/ui/renderer/ButtonRenderer.java b/server/src/com/vaadin/ui/renderer/ButtonRenderer.java new file mode 100644 index 0000000000..4a7a86daa2 --- /dev/null +++ b/server/src/com/vaadin/ui/renderer/ButtonRenderer.java @@ -0,0 +1,45 @@ +/* + * 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 { + + /** + * 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); + } +} diff --git a/server/src/com/vaadin/ui/renderer/ClickableRenderer.java b/server/src/com/vaadin/ui/renderer/ClickableRenderer.java new file mode 100644 index 0000000000..0d745ab29e --- /dev/null +++ b/server/src/com/vaadin/ui/renderer/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.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 + * the type presented by the renderer + * + * @since + * @author Vaadin Ltd + */ +public class ClickableRenderer extends AbstractRenderer { + + /** + * 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 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/renderer/DateRenderer.java b/server/src/com/vaadin/ui/renderer/DateRenderer.java new file mode 100644 index 0000000000..4d1d702993 --- /dev/null +++ b/server/src/com/vaadin/ui/renderer/DateRenderer.java @@ -0,0 +1,156 @@ +/* + * 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 { + private final Locale locale; + private final String formatString; + private final DateFormat dateFormat; + + /** + * Creates a new date renderer. + *

+ * 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. + *

+ * 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. + *

+ * 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 Format + * String Syntax + */ + public DateRenderer(String formatString) throws IllegalArgumentException { + this(formatString, Locale.getDefault()); + } + + /** + * Creates a new date renderer. + *

+ * 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 Format + * String Syntax + */ + 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. + *

+ * 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); + } +} diff --git a/server/src/com/vaadin/ui/renderer/HtmlRenderer.java b/server/src/com/vaadin/ui/renderer/HtmlRenderer.java new file mode 100644 index 0000000000..bdab51991c --- /dev/null +++ b/server/src/com/vaadin/ui/renderer/HtmlRenderer.java @@ -0,0 +1,33 @@ +/* + * 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 { + /** + * Creates a new HTML renderer. + */ + public HtmlRenderer() { + super(String.class); + } +} diff --git a/server/src/com/vaadin/ui/renderer/ImageRenderer.java b/server/src/com/vaadin/ui/renderer/ImageRenderer.java new file mode 100644 index 0000000000..87a044c4a6 --- /dev/null +++ b/server/src/com/vaadin/ui/renderer/ImageRenderer.java @@ -0,0 +1,67 @@ +/* + * 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. + *

+ * 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 { + + /** + * 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); + } +} diff --git a/server/src/com/vaadin/ui/renderer/NumberRenderer.java b/server/src/com/vaadin/ui/renderer/NumberRenderer.java new file mode 100644 index 0000000000..9af5c4cffb --- /dev/null +++ b/server/src/com/vaadin/ui/renderer/NumberRenderer.java @@ -0,0 +1,163 @@ +/* + * 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 { + private final Locale locale; + private final NumberFormat numberFormat; + private final String formatString; + + /** + * Creates a new number renderer. + *

+ * 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. + *

+ * 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. + *

+ * 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. + *

+ * 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 Format + * String Syntax + */ + public NumberRenderer(String formatString) throws IllegalArgumentException { + this(formatString, Locale.getDefault()); + } + + /** + * Creates a new number renderer. + *

+ * 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 Format + * String Syntax + */ + 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); + } +} diff --git a/server/src/com/vaadin/ui/renderer/ProgressBarRenderer.java b/server/src/com/vaadin/ui/renderer/ProgressBarRenderer.java new file mode 100644 index 0000000000..cb688b178b --- /dev/null +++ b/server/src/com/vaadin/ui/renderer/ProgressBarRenderer.java @@ -0,0 +1,44 @@ +/* + * 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 { + + /** + * 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); + } +} diff --git a/server/src/com/vaadin/ui/renderer/Renderer.java b/server/src/com/vaadin/ui/renderer/Renderer.java new file mode 100644 index 0000000000..6adddd1a20 --- /dev/null +++ b/server/src/com/vaadin/ui/renderer/Renderer.java @@ -0,0 +1,69 @@ +/* + * 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 + * the type this renderer knows how to present + * + * @since + * @author Vaadin Ltd + */ +public interface Renderer extends Extension { + + /** + * Returns the class literal corresponding to the presentation type T. + * + * @return the class literal of T + */ + Class 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); +} diff --git a/server/src/com/vaadin/ui/renderer/TextRenderer.java b/server/src/com/vaadin/ui/renderer/TextRenderer.java new file mode 100644 index 0000000000..42bd02fa2a --- /dev/null +++ b/server/src/com/vaadin/ui/renderer/TextRenderer.java @@ -0,0 +1,34 @@ +/* + * 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 { + + /** + * Creates a new text renderer + */ + public TextRenderer() { + super(String.class); + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/ImageRendererTest.java b/server/tests/src/com/vaadin/tests/server/component/grid/ImageRendererTest.java deleted file mode 100644 index cdf386ef4b..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/grid/ImageRendererTest.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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(); - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/RendererTest.java b/server/tests/src/com/vaadin/tests/server/component/grid/RendererTest.java deleted file mode 100644 index c2779bfe89..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/grid/RendererTest.java +++ /dev/null @@ -1,209 +0,0 @@ -/* - * 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 { - - @Override - public TestBean convertToModel(String value, - Class targetType, Locale locale) - throws ConversionException { - return null; - } - - @Override - public String convertToPresentation(TestBean value, - Class targetType, Locale locale) - throws ConversionException { - if (value instanceof ExtendedBean) { - return "ExtendedBean(" + value.i + ", " - + ((ExtendedBean) value).f + ")"; - } else { - return "TestBean(" + value.i + ")"; - } - } - - @Override - public Class getModelType() { - return TestBean.class; - } - - @Override - public Class 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()); - } -} diff --git a/server/tests/src/com/vaadin/tests/server/renderer/ImageRendererTest.java b/server/tests/src/com/vaadin/tests/server/renderer/ImageRendererTest.java new file mode 100644 index 0000000000..08f045277d --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/renderer/ImageRendererTest.java @@ -0,0 +1,85 @@ +/* + * 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(); + } +} diff --git a/server/tests/src/com/vaadin/tests/server/renderer/RendererTest.java b/server/tests/src/com/vaadin/tests/server/renderer/RendererTest.java new file mode 100644 index 0000000000..464d409543 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/renderer/RendererTest.java @@ -0,0 +1,209 @@ +/* + * 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 { + + @Override + public TestBean convertToModel(String value, + Class targetType, Locale locale) + throws ConversionException { + return null; + } + + @Override + public String convertToPresentation(TestBean value, + Class targetType, Locale locale) + throws ConversionException { + if (value instanceof ExtendedBean) { + return "ExtendedBean(" + value.i + ", " + + ((ExtendedBean) value).f + ")"; + } else { + return "TestBean(" + value.i + ")"; + } + } + + @Override + public Class getModelType() { + return TestBean.class; + } + + @Override + public Class 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()); + } +} diff --git a/uitest/src/com/vaadin/tests/components/grid/GridColspans.java b/uitest/src/com/vaadin/tests/components/grid/GridColspans.java index 4c23c3bcab..f61a39a1f2 100644 --- a/uitest/src/com/vaadin/tests/components/grid/GridColspans.java +++ b/uitest/src/com/vaadin/tests/components/grid/GridColspans.java @@ -25,7 +25,7 @@ import com.vaadin.ui.Button.ClickEvent; 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 { diff --git a/uitest/src/com/vaadin/tests/components/grid/GridColumnAutoWidth.java b/uitest/src/com/vaadin/tests/components/grid/GridColumnAutoWidth.java index 6c61846e0e..5d9f4285a1 100644 --- a/uitest/src/com/vaadin/tests/components/grid/GridColumnAutoWidth.java +++ b/uitest/src/com/vaadin/tests/components/grid/GridColumnAutoWidth.java @@ -22,7 +22,7 @@ import com.vaadin.tests.components.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 diff --git a/uitest/src/com/vaadin/tests/components/grid/WidgetRenderers.java b/uitest/src/com/vaadin/tests/components/grid/WidgetRenderers.java index 8c6037938c..de6fb30e1c 100644 --- a/uitest/src/com/vaadin/tests/components/grid/WidgetRenderers.java +++ b/uitest/src/com/vaadin/tests/components/grid/WidgetRenderers.java @@ -25,11 +25,11 @@ import com.vaadin.ui.Button; 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 { diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java index f03a1ae607..88579154dc 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java @@ -46,13 +46,12 @@ import com.vaadin.ui.Grid.HeaderRow; 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 @@ -267,8 +266,8 @@ public class GridBasicFeatures extends AbstractComponentTest { grid.setSelectionMode(SelectionMode.MULTI); } - ((MultiSelectionModel) grid.getSelectionModel()).setSelectionLimit(limit - .intValue()); + ((MultiSelectionModel) grid.getSelectionModel()) + .setSelectionLimit(limit.intValue()); } });