]> source.dussan.org Git - vaadin-framework.git/commitdiff
Convinience renderers for rendering common data types #12993
authorJohn Ahlroos <john@vaadin.com>
Wed, 18 Dec 2013 15:00:57 +0000 (17:00 +0200)
committerVaadin Code Review <review@vaadin.com>
Thu, 19 Dec 2013 13:45:56 +0000 (13:45 +0000)
Change-Id: I9d14df54d2d7700621f71ebec5297b6d37c92e2c

client/src/com/vaadin/client/ui/grid/renderers/DateRenderer.java [new file with mode: 0644]
client/src/com/vaadin/client/ui/grid/renderers/HtmlRenderer.java [new file with mode: 0644]
client/src/com/vaadin/client/ui/grid/renderers/NumberRenderer.java [new file with mode: 0644]

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 (file)
index 0000000..d1f770f
--- /dev/null
@@ -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 (file)
index 0000000..ceafcfc
--- /dev/null
@@ -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 (file)
index 0000000..f4efea3
--- /dev/null
@@ -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));
+    }
+}