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
package com.temp.client.foundation.widget.table.cell;

import com.temp.client.foundation.handler.DataClickHandler;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;

/**
 * @author paule, dhoude
 *
 * @param <T> - the data type
 */
public class ClickableImageCell<T> extends AbstractCell<T> {

	private DataClickHandler<T> clickHandler;
	private String trustedInnerHTML;
	private HorizontalAlignmentConstant horizontalAlignment;

	private static final String CLICK_EVENT = "click";
	private static final String TEXT_ALIGN_CSS_PROPERTY_NAME = "text-align";

    public ClickableImageCell(String trustedHtmlImageElement, DataClickHandler<T> clickHandler,
            HorizontalAlignmentConstant horizontalAlignment) {
        super(CLICK_EVENT);
        trustedInnerHTML = trustedHtmlImageElement;
        this.clickHandler = clickHandler;
        this.horizontalAlignment = horizontalAlignment;
    }

    public ClickableImageCell(String trustedHtmlImageElement, DataClickHandler<T> clickHandler) {
        this(trustedHtmlImageElement, clickHandler, null);
    }

    public String getTrustedInnerHTML(T value) {
        return trustedInnerHTML;
    }

    @Override
	public void render(Context context, T value, SafeHtmlBuilder sb) {
		// Use concatenation here, since SafeHtmlBuilder only accept complete tags
    	sb.appendHtmlConstant("<div style='" + this.getHorizontalAlignmentStyle() + "'>");
		sb.appendHtmlConstant(getTrustedInnerHTML(value));
		sb.appendHtmlConstant("</div>");
	}

	@Override
	public void onBrowserEvent(Context context, Element parent, T value, NativeEvent event, ValueUpdater<T> valueUpdater) {
		if (event.getType().equals(CLICK_EVENT) && value != null) {
			clickHandler.onClick(value);
		}
	}

	public void setHorizontalAlignment(HorizontalAlignmentConstant horizontalAlignment) {
		this.horizontalAlignment = horizontalAlignment;
	}

	private String getHorizontalAlignmentStyle() {
		StringBuilder styleStringBuilder = new StringBuilder();

		if(HasHorizontalAlignment.ALIGN_LEFT.equals(horizontalAlignment)) {
			styleStringBuilder.append(TEXT_ALIGN_CSS_PROPERTY_NAME);
			styleStringBuilder.append(": left; ");
		} else if(HasHorizontalAlignment.ALIGN_CENTER.equals(horizontalAlignment)) {
			styleStringBuilder.append(TEXT_ALIGN_CSS_PROPERTY_NAME);
			styleStringBuilder.append(": center; ");
		} else if(HasHorizontalAlignment.ALIGN_RIGHT.equals(horizontalAlignment)) {
			styleStringBuilder.append(TEXT_ALIGN_CSS_PROPERTY_NAME);
			styleStringBuilder.append(": right; ");
		}

		return styleStringBuilder.toString();
	}

}

Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/client/foundation/widget/table/cell/ClickableImageCell.java

Diff revisions: vs.
Revision Author Commited Message
626 GeorgeS picture GeorgeS Wed 11 Apr, 2012 19:39:41 +0000