Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.GWT.client.widgets.datatables;

import org.litesoft.commonfoundation.html.*;

import com.google.gwt.gen2.table.client.*;
import com.google.gwt.gen2.table.client.TableDefinition.*;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.*;
import com.google.gwt.user.client.ui.*;

/**
 * A basic cell renderer. By default, the model element for the cell is
 * converted to a String via {@link String#valueOf(Object)} and written to the
 * cell. The default conversion can be overriden by setting a custom formatter.
 * <p/>
 * see #setFormatter(StringFormatter)
 */
public class TableCellRenderer<T> implements CellRenderer<T, Object>
{
    public enum EscapeCtrl
    {
        NoWrap( HTMLize.INSTANCE_NO_WRAP ), //
        NoEscape( HTMLize.INSTANCE_NO_ESCAPE ), //
        Regular( HTMLize.INSTANCE );

        private HTMLize mHTMLizer;

        EscapeCtrl( HTMLize pHTMLizer )
        {
            mHTMLizer = pHTMLizer;
        }

        public String process( String pText )
        {
            return mHTMLizer.process( pText );
        }
    }

    public static class Builder
    {
        private String mElementStyleName;
        private StringFormatter mFormatter;
        private EscapeCtrl mEscapeCtrl = EscapeCtrl.NoWrap;

        public String getElementBaseStyleName()
        {
            return (mElementStyleName != null) ? mElementStyleName : "";
        }

        public Builder setElementBaseStyleName( String pStyleName )
        {
            mElementStyleName = pStyleName;
            return this;
        }

        public StringFormatter getFormatter()
        {
            return (mFormatter != null) ? mFormatter : StringFormatterDefault.INSTANCE;
        }

        public Builder setFormatter( StringFormatter pFormatter )
        {
            if ( pFormatter == null )
            {
                throw new NullPointerException();
            }

            mFormatter = pFormatter;
            return this;
        }

        public EscapeCtrl getEscapeCtrl()
        {
            return (mEscapeCtrl != null) ? mEscapeCtrl : EscapeCtrl.NoWrap;
        }

        /**
         * Instructs the renderer to escape HTML markup in values to be rendered.
         * Default is <code>NoWrap</code>. Specifying <code>NoEscape</code> will enable
         * HTML script injection if the data being rendererd is not from a trusted
         * source.
         *
         * @param pEscapeCtrl <code>NoEscape</code> to allow HTML markup to be rendered in
         *                    cells
         *                    <p/>
         *                    see #escapeMarkup(String)
         */
        public Builder setEscapeCtrl( EscapeCtrl pEscapeCtrl )
        {
            mEscapeCtrl = pEscapeCtrl;
            return this;
        }
    }

    public static Builder NO_WRAP = new Builder().setEscapeCtrl( EscapeCtrl.NoWrap );
    public static Builder NO_ESCAPE = new Builder().setEscapeCtrl( EscapeCtrl.NoEscape );

    private String mElementStyleName;
    private StringFormatter mFormatter;
    private EscapeCtrl mEscapeCtrl;

    public TableCellRenderer( Builder pBuilder )
    {
        if ( pBuilder == null )
        {
            pBuilder = NO_WRAP;
        }
        mElementStyleName = pBuilder.getElementBaseStyleName();
        mFormatter = pBuilder.getFormatter();
        mEscapeCtrl = pBuilder.getEscapeCtrl();
    }

    public TableCellRenderer()
    {
        this( null );
    }

    @Override
    public void renderRowValue( T pRowValue, ColumnDefinition<T, Object> pColumnDef, AbstractCellView<T> pView )
    {
        Object cellValue = pColumnDef.getCellValue( pRowValue );
        prepareElement( pRowValue, pColumnDef, pView, cellValue );
        renderContent( pRowValue, pColumnDef, pView, cellValue );
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public void prepareElement( T rowValue, ColumnDefinition<T, Object> pColumnDef, AbstractCellView<T> pView, Object pCellValue )
    {
        // set cell class name(s)
        StringBuilder styleName = new StringBuilder();
        buildStyleName( styleName, pCellValue );
        pView.setStyleName( styleName.toString() );

        // align has a special setter b/c css "text-align" does not work for block context
        pView.setHorizontalAlignment( pColumnDef.getColumnProperty( HorizontalAlignmentProperty.TYPE ).getValue() );

        pView.setVerticalAlignment( pColumnDef.getColumnProperty( VerticalAlignmentProperty.TYPE ).getValue() );

        // set cell CSS properties
        pView.setStyleAttribute( "paddingLeft", pColumnDef.getColumnProperty( PaddingProperty.LEFT ).getValue() + "px" );
        pView.setStyleAttribute( "paddingRight", pColumnDef.getColumnProperty( PaddingProperty.RIGHT ).getValue() + "px" );
        pView.setStyleAttribute( "paddingTop", pColumnDef.getColumnProperty( PaddingProperty.TOP ).getValue() + "px" );
        pView.setStyleAttribute( "paddingBottom", pColumnDef.getColumnProperty( PaddingProperty.BOTTOM ).getValue() + "px" );
    }

    @SuppressWarnings({"UnusedDeclaration"})
    protected StringBuilder buildStyleName( StringBuilder result, Object pCellValue )
    {
        result.append( mElementStyleName );
        return result;
    }

    protected StringBuilder appendDependantStyle( StringBuilder styleIn, String dependentStyleName )
    {
        return appendDependantStyle( styleIn, dependentStyleName, true );
    }

    protected StringBuilder appendDependantStyle( StringBuilder styleIn, String dependentStyleName, boolean add )
    {
        if ( add )
        {
            styleIn.append( ' ' );
            styleIn.append( mElementStyleName );
            styleIn.append( '-' );
            styleIn.append( dependentStyleName );
        }
        return styleIn;
    }

    public void renderContent( T pRowValue, ColumnDefinition<T, Object> pColumnDef, AbstractCellView<T> pView, Object pCellValue )
    {
        if ( pCellValue instanceof Widget )
        {
            Widget widget = (Widget) pCellValue;
            Element element = widget.getElement();
            HorizontalAlignmentConstant align = pColumnDef.getColumnProperty( HorizontalAlignmentProperty.TYPE ).getValue();
            pView.setHorizontalAlignment( align );
            if ( element.getTagName().equalsIgnoreCase( "table" ) )
            {
                DOM.setElementAttribute( element, "cellpadding", "0" );
                DOM.setElementAttribute( element, "cellspacing", "0" );
            }
            pView.setWidget( widget );
        }
        else
        {
            String valueString = getValueString( pCellValue );
            String safeValueString = mEscapeCtrl.process( valueString );
            String nonEmptyValueString = ("".equals( safeValueString )) ? HTMLConstants.NBSP : safeValueString;
            String cellString = getCellString( pRowValue, nonEmptyValueString, pCellValue );
            pView.setHTML( cellString );
        }
    }

    /**
     * Calls the formatter.
     *
     * @return an HTML snippet representing <code>modelElement</code>
     */
    protected String getValueString( Object modelElement )
    {
        return mFormatter.getString( modelElement );
    }

    /**
     * Template method for decorating text returned by the formatter.
     * Implementors can use this method to create complex cells.
     * <p/>
     * This implementation simply returns the input string.
     *
     * @param pRowValue
     * @param pValueString the string provided by the formatter
     *
     * @return an HTML snippet describing the entire content of the cell
     */
    protected String getCellString( T pRowValue, String pValueString, Object pCellValue )
    {
        return pValueString;
    }
}

Commits for litesoft/trunk/Java/GWT/Client/src/org/litesoft/GWT/client/widgets/datatables/TableCellRenderer.java

Diff revisions: vs.
Revision Author Commited Message
942 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 23:41:46 +0000

Extracting commonfoundation

720 Diff Diff GeorgeS picture GeorgeS Sun 10 Jun, 2012 16:20:42 +0000
712 Diff Diff GeorgeS picture GeorgeS Sat 09 Jun, 2012 22:46:04 +0000

Move PAV stuff into LiteSoft

151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

23 Diff Diff GeorgeS picture GeorgeS Wed 24 Feb, 2010 00:34:32 +0000
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000