Ensure the pager buttons are always visible for related lists.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / ui / cell / TextCell.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.safehtml.shared.SafeHtml;
26 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
27 import com.google.gwt.safehtml.shared.SafeHtmlUtils;
28
29 /**
30  * Cell renderer for {@link GlomFieldType} TYPE_TEXT.
31  * 
32  * @author Ben Konrath <ben@bagu.org>
33  * 
34  */
35 public class TextCell extends AbstractCell<String> {
36         SafeHtml colourCSSProp;
37         SafeHtml backgroundColourCSSProp;
38
39         // TODO Find a way to set the colours on the whole column
40         public TextCell(String foregroundColour, String backgroundColour) {
41                 if (foregroundColour != null && !foregroundColour.isEmpty()) {
42                         colourCSSProp = SafeHtmlUtils.fromString("color:" + foregroundColour + ";");
43                 } else {
44                         colourCSSProp = SafeHtmlUtils.fromSafeConstant("");
45                 }
46                 if (backgroundColour != null && !backgroundColour.isEmpty()) {
47                         backgroundColourCSSProp = SafeHtmlUtils.fromString("background-color:" + backgroundColour + ";");
48                 } else {
49                         backgroundColourCSSProp = SafeHtmlUtils.fromSafeConstant("");
50                 }
51         }
52
53         /*
54          * (non-Javadoc)
55          * 
56          * @see com.google.gwt.cell.client.AbstractCell#render(com.google.gwt.cell.client.Cell.Context, java.lang.Object,
57          * com.google.gwt.safehtml.shared.SafeHtmlBuilder)
58          */
59         @Override
60         public void render(Context context, String value, SafeHtmlBuilder sb) {
61                 if (value == null) {
62                         // The value is from an empty row.
63                         sb.appendHtmlConstant("&nbsp;");
64                         return;
65                 }
66
67                 // Set the text and some CSS properties for the text.
68                 // The overflow and text-overflow properties tell the browser to add an ellipsis when the text overflows the
69                 // table cell.
70                 // FIXME this isn't using safe html correctly!
71                 sb.appendHtmlConstant("<div style=\"overflow: hidden; text-overflow: ellipsis; " + colourCSSProp.asString()
72                                 + backgroundColourCSSProp.asString() + "\">");
73                 sb.append(SafeHtmlUtils.fromString(value));
74                 sb.appendHtmlConstant("</div>");
75
76         }
77 }