Make ListViewTable and RelatedListTable a consistent height.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / ui / cell / NumericCell.java
1 /*
2  * Copyright (C) 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
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.
10  *
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
14  * for more details.
15  *
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/>.
18  */
19
20 package org.glom.web.client.ui.cell;
21
22 import org.glom.web.shared.layout.LayoutItemField.GlomFieldType;
23
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;
29
30 /**
31  * Cell renderer for {@link GlomFieldType} TYPE_NUMERIC.
32  * 
33  * @author Ben Konrath <ben@bagu.org>
34  * 
35  */
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;
42
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 + ";");
48                 } else {
49                         colourCSSProp = SafeHtmlUtils.fromSafeConstant("");
50                 }
51                 if (backgroundColour != null && !backgroundColour.isEmpty()) {
52                         backgroundColourCSSProp = SafeHtmlUtils.fromString("background-color:" + backgroundColour + ";");
53                 } else {
54                         backgroundColourCSSProp = SafeHtmlUtils.fromSafeConstant("");
55                 }
56                 this.numberFormat = numberFormat;
57                 this.useAltColourForNegatives = useAltColourForNegatives;
58                 this.currencyCode = currencyCode.isEmpty() ? "" : currencyCode + " ";
59         }
60
61         /*
62          * (non-Javadoc)
63          * 
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)
66          */
67         @Override
68         public void render(Context context, Double value, SafeHtmlBuilder sb) {
69                 if (value == null) {
70                         // The value is from an empty row.
71                         sb.appendHtmlConstant("&nbsp;");
72                         return;
73                 }
74
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;");
79                 }
80
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
83                 // table cell.
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>");
89
90         }
91 }