diff options
author | John Ahlroos <john@vaadin.com> | 2013-12-18 17:00:57 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2013-12-19 13:45:56 +0000 |
commit | ac623bb589ab0ec2f1aacc214480250099d01c03 (patch) | |
tree | e4214259221bc7b91741ea67693d049f8463e37f /client/src | |
parent | 4ca43cb3ff03996fc9e90ad29a5cdafd1e1a39e2 (diff) | |
download | vaadin-framework-ac623bb589ab0ec2f1aacc214480250099d01c03.tar.gz vaadin-framework-ac623bb589ab0ec2f1aacc214480250099d01c03.zip |
Convinience renderers for rendering common data types #12993
Change-Id: I9d14df54d2d7700621f71ebec5297b6d37c92e2c
Diffstat (limited to 'client/src')
3 files changed, 198 insertions, 0 deletions
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/DateRenderer.java b/client/src/com/vaadin/client/ui/grid/renderers/DateRenderer.java new file mode 100644 index 0000000000..d1f770f414 --- /dev/null +++ b/client/src/com/vaadin/client/ui/grid/renderers/DateRenderer.java @@ -0,0 +1,94 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.client.ui.grid.renderers; + +import java.util.Date; + +import com.google.gwt.i18n.client.DateTimeFormat; +import com.google.gwt.i18n.client.TimeZone; +import com.vaadin.client.ui.grid.Cell; +import com.vaadin.client.ui.grid.Renderer; + +/** + * A renderer for rendering dates into cells + * + * @since 7.2 + * @author Vaadin Ltd + */ +public class DateRenderer implements Renderer<Date> { + + private DateTimeFormat format = DateTimeFormat.getShortDateTimeFormat(); + + private TimeZone timeZone = TimeZone.createTimeZone(new Date() + .getTimezoneOffset()); + + @Override + public void renderCell(Cell cell, Date date) { + String dateStr = format.format(date, timeZone); + cell.getElement().setInnerText(dateStr); + } + + /** + * Gets the format of how the date is formatted. + * + * @return the format + * @see <a + * href="http://www.gwtproject.org/javadoc/latest/com/google/gwt/i18n/client/DateTimeFormat.html">GWT + * documentation on DateTimeFormat</a> + */ + public DateTimeFormat getFormat() { + return format; + } + + /** + * Sets the format used for formatting the dates. + * + * @param format + * the format to set + * @see <a + * href="http://www.gwtproject.org/javadoc/latest/com/google/gwt/i18n/client/DateTimeFormat.html">GWT + * documentation on DateTimeFormat</a> + */ + public void setFormat(DateTimeFormat format) { + if (format == null) { + throw new IllegalArgumentException("Format should not be null"); + } + this.format = format; + } + + /** + * Returns the time zone of the date. + * + * @return the time zone + */ + public TimeZone getTimeZone() { + return timeZone; + } + + /** + * Sets the time zone of the the date. By default uses the time zone of the + * browser. + * + * @param timeZone + * the timeZone to set + */ + public void setTimeZone(TimeZone timeZone) { + if (timeZone == null) { + throw new IllegalArgumentException("Timezone should not be null"); + } + this.timeZone = timeZone; + } +} diff --git a/client/src/com/vaadin/client/ui/grid/renderers/HtmlRenderer.java b/client/src/com/vaadin/client/ui/grid/renderers/HtmlRenderer.java new file mode 100644 index 0000000000..ceafcfcb96 --- /dev/null +++ b/client/src/com/vaadin/client/ui/grid/renderers/HtmlRenderer.java @@ -0,0 +1,40 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.client.ui.grid.renderers; + +import com.google.gwt.safehtml.shared.SafeHtmlUtils; +import com.vaadin.client.ui.grid.Cell; +import com.vaadin.client.ui.grid.Renderer; + +/** + * Renders a string as HTML into a cell. + * <p> + * The html string is HTML-escaped string before rendering. For more information + * about what kind of escaping is done see + * {@link SafeHtmlUtils#htmlEscape(String)}. + * + * @since 7.2 + * @author Vaadin Ltd + * @see SafeHtmlUtils#htmlEscape(String) + */ +public class HtmlRenderer implements Renderer<String> { + + @Override + public void renderCell(Cell cell, String htmlString) { + cell.getElement() + .setInnerSafeHtml(SafeHtmlUtils.fromString(htmlString)); + } +} diff --git a/client/src/com/vaadin/client/ui/grid/renderers/NumberRenderer.java b/client/src/com/vaadin/client/ui/grid/renderers/NumberRenderer.java new file mode 100644 index 0000000000..f4efea33a5 --- /dev/null +++ b/client/src/com/vaadin/client/ui/grid/renderers/NumberRenderer.java @@ -0,0 +1,64 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.client.ui.grid.renderers; + +import com.google.gwt.i18n.client.NumberFormat; +import com.vaadin.client.ui.grid.Cell; +import com.vaadin.client.ui.grid.Renderer; + +/** + * Renders a number into a cell using a specific {@link NumberFormat}. By + * default uses the default number format returned by + * {@link NumberFormat#getDecimalFormat()}. + * + * @since 7.2 + * @author Vaadin Ltd + * @param <T> + * The number type to render. + */ +public class NumberRenderer<T extends Number> implements Renderer<T> { + + private NumberFormat format = NumberFormat.getDecimalFormat(); + + /** + * Gets the number format that the number should be formatted in. + * + * @return the number format used to render the number + */ + public NumberFormat getFormat() { + return format; + } + + /** + * Sets the number format to use for formatting the number. + * + * @param format + * the format to use + * @throws IllegalArgumentException + * when the format is null + */ + public void setFormat(NumberFormat format) throws IllegalArgumentException { + if (format == null) { + throw new IllegalArgumentException("Format cannot be null"); + } + this.format = format; + } + + @Override + public void renderCell(Cell cell, Number number) { + cell.getElement().setInnerText(format.format(number)); + } +} |