2 * Copyright (C) 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
6 * GWT-Glom is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
11 * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with GWT-Glom. If not, see <http://www.gnu.org/licenses/>.
20 package org.glom.web.client.ui.cell;
22 import org.glom.web.shared.layout.LayoutItemField.GlomFieldType;
24 import com.google.gwt.cell.client.AbstractCell;
25 import com.google.gwt.i18n.client.NumberFormat;
26 import com.google.gwt.safehtml.shared.SafeHtml;
27 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
28 import com.google.gwt.safehtml.shared.SafeHtmlUtils;
31 * Cell renderer for {@link GlomFieldType} TYPE_NUMERIC.
33 * @author Ben Konrath <ben@bagu.org>
36 public class NumericCell extends AbstractCell<Double> {
37 private SafeHtml colourCSSProp;
38 private SafeHtml backgroundColourCSSProp;
39 private NumberFormat numberFormat;
40 private boolean useAltColourForNegatives;
41 private String currencyCode;
43 // TODO Find a way to set the colours on the whole column
44 public NumericCell(String foregroundColour, String backgroundColour, NumberFormat numberFormat,
45 boolean useAltColourForNegatives, String currencyCode) {
46 if (foregroundColour != null && !foregroundColour.isEmpty()) {
47 colourCSSProp = SafeHtmlUtils.fromString("color:" + foregroundColour + ";");
49 colourCSSProp = SafeHtmlUtils.fromSafeConstant("");
51 if (backgroundColour != null && !backgroundColour.isEmpty()) {
52 backgroundColourCSSProp = SafeHtmlUtils.fromString("background-color:" + backgroundColour + ";");
54 backgroundColourCSSProp = SafeHtmlUtils.fromSafeConstant("");
56 this.numberFormat = numberFormat;
57 this.useAltColourForNegatives = useAltColourForNegatives;
58 this.currencyCode = currencyCode.isEmpty() ? "" : currencyCode + " ";
64 * @see com.google.gwt.cell.client.AbstractCell#render(com.google.gwt.cell.client.Cell.Context, java.lang.Object,
65 * com.google.gwt.safehtml.shared.SafeHtmlBuilder)
68 public void render(Context context, Double value, SafeHtmlBuilder sb) {
70 // The value is from an empty row.
71 sb.appendHtmlConstant(" ");
75 // set the foreground colour to red if the number is negative and this is requested
76 if (useAltColourForNegatives && value.doubleValue() < 0) {
77 // The default alternative colour in libglom is red.
78 colourCSSProp = SafeHtmlUtils.fromString("color: #FF0000;");
81 // Convert the number to a string and set some CSS properties on the text.
82 // The overflow and text-overflow properties tell the browser to add an ellipsis when the text overflows the
84 // FIXME this isn't using safe html correctly!
85 sb.appendHtmlConstant("<div style=\"overflow: hidden; text-overflow: ellipsis; " + colourCSSProp.asString()
86 + backgroundColourCSSProp.asString() + "\">");
87 sb.append(SafeHtmlUtils.fromString(currencyCode + numberFormat.format(value)));
88 sb.appendHtmlConstant("</div>");