My favorites | Sign in
Project Home Issues Source
Checkout   Browse   Changes    
 
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
package com.gwt2go.dev.client.ui.table;

import com.google.gwt.cell.client.AbstractSafeHtmlCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.EventTarget;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.safecss.shared.SafeStyles;
import com.google.gwt.safecss.shared.SafeStylesUtils;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.text.shared.SafeHtmlRenderer;
import com.google.gwt.text.shared.SimpleSafeHtmlRenderer;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.gwt2go.dev.client.ui.icons.DocIcons;

/**
* Using the new GWT 2.2 way to implement cell.
*
* @author L.Pelov
*/
public class ImagesCell extends AbstractSafeHtmlCell<String> {
/**
* The HTML templates used to render the cell.
*/
interface Templates extends SafeHtmlTemplates {
/**
* The template for this Cell, which includes styles and a value.
*
* @param styles
* the styles to include in the style attribute of the div
* @param value
* the safe value. Since the value type is {@link SafeHtml},
* it will not be escaped before including it in the
* template. Alternatively, you could make the value type
* String, in which case the value would be escaped.
* @return a {@link SafeHtml} instance
*/
@SafeHtmlTemplates.Template("<div name=\"{0}\" style=\"{1}\">{2}</div>")
SafeHtml cell(String name, SafeStyles styles, SafeHtml value);
}

public ImagesCell() {
super(SimpleSafeHtmlRenderer.getInstance(), "click", "keydown");
}

public ImagesCell(SafeHtmlRenderer<String> renderer) {
super(renderer, "click", "keydown");
}

/**
* Create a singleton instance of the templates used to render the cell.
*/
private static Templates templates = GWT.create(Templates.class);

private static final SafeHtml ICON_PDF = makeImage(DocIcons.RESOURCES
.icon_doc_pdf());
private static final SafeHtml ICON_WORD = makeImage(DocIcons.RESOURCES
.icon_doc_word());

private static final SafeHtml ICON_EXCEL = makeImage(DocIcons.RESOURCES
.icon_doc_excel());

/**
* Called when an event occurs in a rendered instance of this Cell. The
* parent element refers to the element that contains the rendered cell, NOT
* to the outermost element that the Cell rendered.
*/
@Override
public void onBrowserEvent(com.google.gwt.cell.client.Cell.Context context,
Element parent, String value, NativeEvent event,
com.google.gwt.cell.client.ValueUpdater<String> valueUpdater) {

// Let AbstractCell handle the keydown event.
super.onBrowserEvent(context, parent, value, event, valueUpdater);

// Handle the click event.
if ("click".equals(event.getType())) {

// Ignore clicks that occur outside of the outermost element.
EventTarget eventTarget = event.getEventTarget();

if (parent.isOrHasChild(Element.as(eventTarget))) {
// if (parent.getFirstChildElement().isOrHasChild(
// Element.as(eventTarget))) {

// use this to get the selected element!!
Element el = Element.as(eventTarget);

// check if we really click on the image
if (el.getNodeName().equalsIgnoreCase("IMG")) {
doAction(el.getParentElement().getAttribute("name"),
valueUpdater);
}

}
}

};

/**
* onEnterKeyDown is called when the user presses the ENTER key will the
* Cell is selected. You are not required to override this method, but its a
* common convention that allows your cell to respond to key events.
*/
@Override
protected void onEnterKeyDown(Context context, Element parent,
String value, NativeEvent event, ValueUpdater<String> valueUpdater) {
doAction(value, valueUpdater);
}

/**
* Intern action
*
* @param value
* selected value
* @param valueUpdater
* value updater or the custom value update to be called
*/
private void doAction(String value, ValueUpdater<String> valueUpdater) {
// Trigger a value updater. In this case, the value doesn't actually
// change, but we use a ValueUpdater to let the app know that a value
// was clicked.
if (valueUpdater != null)
valueUpdater.update(value);
}

@Override
protected void render(com.google.gwt.cell.client.Cell.Context context,
SafeHtml data, SafeHtmlBuilder sb) {
/*
* Always do a null check on the value. Cell widgets can pass null to
* cells if the underlying data contains a null, or if the data arrives
* out of order.
*/
if (data == null) {
return;
}

// If the value comes from the user, we escape it to avoid XSS attacks.
// SafeHtml safeValue = SafeHtmlUtils.fromString(data.asString());

// Use the template to create the Cell's html.
// SafeStyles styles = SafeStylesUtils.fromTrustedString(safeValue
// .asString());

// generate the image cell
SafeStyles imgStyle = SafeStylesUtils
.fromTrustedString("float:left;cursor:hand;cursor:pointer;");

SafeHtml rendered = templates.cell("ICON_PDF", imgStyle, ICON_PDF);
sb.append(rendered);

rendered = templates.cell("ICON_WORD", imgStyle, ICON_WORD);
sb.append(rendered);

rendered = templates.cell("ICON_EXCEL", imgStyle, ICON_EXCEL);
sb.append(rendered);
}

/**
* Make icons available as SafeHtml
*
* @param resource
* @return
*/
private static SafeHtml makeImage(ImageResource resource) {
AbstractImagePrototype proto = AbstractImagePrototype.create(resource);

// String html = proto.getHTML().replace("style='",
// "style='left:0px;top:0px;"); // position:absolute;
//
// return SafeHtmlUtils.fromTrustedString(html);

return proto.getSafeHtml();
}
}

Change log

r45 by lyudmil.pelov on Nov 4, 2011   Diff
image cell with multiple images
Go to: 

Older revisions

r42 by lyudmil.pelov on Oct 26, 2011   Diff
experiment with custom cells
All revisions of this file

File info

Size: 6072 bytes, 180 lines
Powered by Google Project Hosting