View Javadoc

1   package org.appfuse.webapp.client.application.utils.tables;
2   
3   import com.google.gwt.cell.client.Cell;
4   import com.google.gwt.cell.client.Cell.Context;
5   import com.google.gwt.cell.client.ClickableTextCell;
6   import com.google.gwt.cell.client.FieldUpdater;
7   import com.google.gwt.safehtml.shared.SafeHtml;
8   import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
9   import com.google.gwt.safehtml.shared.SafeHtmlUtils;
10  import com.google.gwt.text.shared.SafeHtmlRenderer;
11  import com.google.gwt.user.cellview.client.Column;
12  
13  public abstract class CustomColumn<T, V> extends Column<T, V> {
14  
15      private static final SafeHtmlRenderer<Object> SAFE_HTML_RENDERER = new SafeHtmlRenderer<Object>() {
16          @Override
17          public SafeHtml render(Object object) {
18              return (object == null) ? SafeHtmlUtils.EMPTY_SAFE_HTML : SafeHtmlUtils.fromString(object.toString());
19          }
20  
21          @Override
22          public void render(Object object, SafeHtmlBuilder appendable) {
23              appendable.append(render(object));
24          }
25      };
26  
27      private SafeHtmlRenderer<V> renderer = (SafeHtmlRenderer<V>) SAFE_HTML_RENDERER;
28      private FieldUpdater<T, V> fieldUpdater;
29      private String propertyName;
30      private boolean sortable = false;
31  
32      public CustomColumn(String propertyName) {
33          this(propertyName, false, null);
34      }
35  
36      /**
37       * @param headerText
38       * @param sortable
39       */
40      public CustomColumn(String propertyName, boolean sortable) {
41          this(propertyName, sortable, null);
42      }
43  
44      /**
45       * @param anchorRenderer
46       * @param headerText
47       * @param valueUpdater
48       * @param sortable
49       */
50      public CustomColumn(String propertyName, boolean sortable, FieldUpdater<T, V> fieldUpdater) {
51          super((Cell) new ClickableTextCell());
52          this.fieldUpdater = fieldUpdater;
53          this.sortable = sortable;
54          this.propertyName = propertyName;
55  
56          setSortable(isSortable());
57          if (fieldUpdater != null) {
58              setFieldUpdater(fieldUpdater);
59          }
60      }
61  
62      public abstract V getValue(T row);
63  
64      public void render(Context context, T object, SafeHtmlBuilder sb) {
65          renderer.render(getValue(object), sb);
66      }
67  
68      public FieldUpdater<T, V> getFieldUpdater() {
69          return fieldUpdater;
70      }
71  
72      public String getPropertyName() {
73          return propertyName;
74      }
75  
76      public boolean isSortable() {
77          return sortable;
78      }
79  }