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 package info.magnolia.ui.vaadin.gwt.client.grid;
35
36 import com.google.gwt.dom.client.Document;
37 import com.google.gwt.dom.client.NativeEvent;
38 import com.google.gwt.dom.client.SpanElement;
39 import com.google.gwt.dom.client.TableCellElement;
40 import com.google.gwt.event.logical.shared.ValueChangeEvent;
41 import com.google.gwt.event.logical.shared.ValueChangeHandler;
42 import com.google.gwt.user.client.DOM;
43 import com.google.gwt.user.client.Element;
44 import com.google.gwt.user.client.ui.CheckBox;
45 import com.google.gwt.user.client.ui.HTML;
46 import com.google.gwt.user.client.ui.Widget;
47 import com.googlecode.mgwt.dom.client.recognizer.tap.MultiTapEvent;
48 import com.googlecode.mgwt.dom.client.recognizer.tap.MultiTapHandler;
49 import com.googlecode.mgwt.dom.client.recognizer.tap.MultiTapRecognizer;
50 import com.googlecode.mgwt.ui.client.widget.touch.TouchDelegate;
51 import com.vaadin.client.BrowserInfo;
52 import com.vaadin.client.UIDL;
53 import com.vaadin.client.Util;
54 import com.vaadin.client.ui.VScrollTablePatched;
55
56
57
58
59
60 public class VMagnoliaTable extends VScrollTablePatched {
61
62 static int checkboxWidth = -1;
63
64 public VMagnoliaTable() {
65 super();
66 MagnoliaTableHead head = (MagnoliaTableHead) tHead;
67 head.addToDom();
68 }
69
70 @Override
71 protected TableHead createTableHead() {
72 return new MagnoliaTableHead();
73 }
74
75 @Override
76 protected VScrollTableBody createScrollBody() {
77 return new MagnoliaTableBody();
78 }
79
80 @Override
81 protected HeaderCell createHeaderCell(String colId, String headerText) {
82 return new MagnoliaHeaderCell(colId, headerText);
83 }
84
85 @Override
86 protected void setMultiSelectMode(int multiselectmode) {
87 this.multiselectmode = multiselectmode;
88 }
89
90
91
92
93 public class MagnoliaHeaderCell extends HeaderCell {
94
95 private Element caption = null;
96 private boolean canBeSorted = false;
97
98 public MagnoliaHeaderCell(String colId, String headerText) {
99 super(colId, headerText);
100 caption = DOM.createSpan();
101 captionContainer.appendChild(caption);
102 setText(headerText);
103 }
104
105 @Override
106 public void setText(String headerText) {
107 if (caption != null) {
108 caption.setInnerHTML(headerText);
109 }
110 }
111
112 @Override
113 protected void setSorted(boolean sorted) {
114 this.canBeSorted = true;
115 super.setSorted(sorted);
116 }
117
118 @Override
119 protected void updateStyleNames(String primaryStyleName) {
120 super.updateStyleNames(primaryStyleName);
121 if (this.canBeSorted) {
122 addStyleName("sortable");
123 }
124 }
125 }
126
127
128
129
130 public class MagnoliaTableHead extends TableHead {
131
132 private CheckBox selectAll = null;
133
134 public MagnoliaTableHead() {
135 super();
136 selectAll = new CheckBox();
137 selectAll.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
138
139 @Override
140 public void onValueChange(ValueChangeEvent<Boolean> event) {
141 client.updateVariable(paintableId, "selectAll", event.getValue(), true);
142 }
143 });
144 selectAll.addStyleName("v-select-all");
145 }
146
147 public void addToDom() {
148 div.appendChild(selectAll.getElement());
149 getChildren().add(selectAll);
150 adopt(selectAll);
151 }
152
153 public CheckBox getSelectAllCB() {
154 return selectAll;
155 }
156 }
157
158
159
160
161 public class MagnoliaTableBody extends VScrollTableBody {
162
163 @Override
164 protected VScrollTableRow createScrollTableRow(UIDL uidl, char[] aligns) {
165 return new MagnoliaTableRow(uidl, aligns);
166 }
167
168 @Override
169 protected VScrollTableRow createScrollTableRow() {
170 return new MagnoliaTableRow();
171 }
172
173 @Override
174 public int getColWidth(int columnIndex) {
175 return super.getColWidth(columnIndex);
176 }
177
178
179
180
181 public class MagnoliaTableRow extends VScrollTableRow {
182
183 private CheckBox selectionCheckBox;
184
185 private HTML selectionCheckBoxSpacer;
186
187 private String nodeIcon;
188
189 public MagnoliaTableRow(UIDL uidl, char[] aligns) {
190 super(uidl, aligns);
191 }
192
193 public MagnoliaTableRow() {
194 super();
195 }
196
197 @Override
198 protected void updateStyleNames(String primaryStyleName) {
199
200 if (rowStyle != null) {
201 String[] rowStyles = rowStyle.split(" ");
202 for (String style : rowStyles) {
203 if (style.startsWith("icon")) {
204 if (nodeIcon == null) {
205 nodeIcon = style;
206 } else {
207 nodeIcon += " " + style;
208 }
209 }
210 }
211 }
212 super.updateStyleNames(primaryStyleName);
213 }
214
215
216
217
218
219 @Override
220 protected void setElement(com.google.gwt.user.client.Element elem) {
221 super.setElement(elem);
222 privateConstruction();
223 }
224
225 @Override
226 protected void initCellWithText(String text, char align, String style, boolean textIsHTML, boolean sorted, String description, final TableCellElement td) {
227 super.initCellWithText(text, align, style, textIsHTML, sorted, description, td);
228 if (td.equals(this.getElement().getFirstChildElement())) {
229 insertNodeIcon(td);
230 if (isSingleSelectMode()) {
231 insertSelectionCheckboxSpacer(td);
232 } else {
233 insertSelectionCheckbox(td);
234 }
235 }
236 }
237
238 @Override
239 protected void initCellWithWidget(Widget w, char align, String style, boolean sorted, TableCellElement td) {
240 super.initCellWithWidget(w, align, style, sorted, td);
241 if (td.equals(this.getElement().getFirstChildElement())) {
242 insertNodeIcon(td);
243 if (isSingleSelectMode()) {
244 insertSelectionCheckboxSpacer(td);
245 } else {
246 insertSelectionCheckbox(td);
247 }
248 }
249 }
250
251
252
253
254 private void insertSelectionCheckbox(final TableCellElement td) {
255 com.google.gwt.dom.client.Element container = td.getFirstChildElement();
256 container.insertFirst(selectionCheckBox.getElement());
257 }
258
259
260
261
262 private void insertSelectionCheckboxSpacer(final TableCellElement td) {
263 com.google.gwt.dom.client.Element container = td.getFirstChildElement();
264 container.insertFirst(selectionCheckBoxSpacer.getElement());
265 }
266
267 protected void insertNodeIcon(TableCellElement td) {
268 if (nodeIcon != null) {
269 SpanElement iconElement = Document.get().createSpanElement();
270 iconElement.setClassName(nodeIcon);
271 iconElement.addClassName("v-table-icon-element");
272 td.getFirstChild().insertFirst(iconElement);
273 }
274 }
275
276 private void privateConstruction() {
277 selectionCheckBox = new CheckBox();
278 selectionCheckBox.setValue(isSelected(), false);
279 ValueChangeHandler<Boolean> selectionChangeHandler = new ValueChangeHandler<Boolean>() {
280
281 @Override
282 public void onValueChange(ValueChangeEvent<Boolean> event) {
283 if (event.getSource() instanceof Widget) {
284 final Widget source = (Widget) event.getSource();
285 final Element targetTd = source.getElement().getParentElement().cast();
286 VScrollTableRow row = Util.findWidget(targetTd, null);
287 if (row != null) {
288 boolean wasSelected = row.isSelected();
289
290 if (VMagnoliaTable.this.isSingleSelectMode() && !row.isSelected()) {
291 deselectAll();
292 }
293
294 row.toggleSelection();
295 setRowFocus(row);
296
297
298
299 selectionRangeStart = row;
300 if (wasSelected) {
301 removeRowFromUnsentSelectionRanges(row);
302 }
303
304 sendSelectedRows(true);
305 }
306 }
307 }
308 };
309
310 selectionCheckBox.addValueChangeHandler(selectionChangeHandler);
311 selectionCheckBox.addStyleName("v-selection-cb");
312 getChildren().add(selectionCheckBox);
313 VMagnoliaTable.this.adopt(selectionCheckBox);
314
315 selectionCheckBoxSpacer = new HTML();
316 selectionCheckBoxSpacer.addStyleName("v-selection-cb");
317 getChildren().add(selectionCheckBoxSpacer);
318 VMagnoliaTable.this.adopt(selectionCheckBoxSpacer);
319
320 final TouchDelegate delegate = new TouchDelegate(this);
321 delegate.addTouchHandler(new MultiTapRecognizer(delegate, 1, 2));
322 addHandler(new MultiTapHandler() {
323
324 @Override
325 public void onMultiTap(MultiTapEvent event) {
326 if (BrowserInfo.get().isTouchDevice()) {
327 final NativeEvent doubleClickEvent =
328 Document.get().createDblClickEvent(
329 0,
330 event.getTouchStarts().get(0).get(0).getPageX(),
331 event.getTouchStarts().get(0).get(0).getPageY(),
332 event.getTouchStarts().get(0).get(0).getPageX(),
333 event.getTouchStarts().get(0).get(0).getPageY(),
334 false,
335 false,
336 false,
337 false);
338 getElement().dispatchEvent(doubleClickEvent);
339 }
340 }
341 }, MultiTapEvent.getType());
342 }
343
344 @Override
345 public void toggleSelection() {
346 super.toggleSelection();
347
348 if (selectionCheckBox != null) {
349 selectionCheckBox.setValue(isSelected(), false);
350 }
351 MagnoliaTableHead head = (MagnoliaTableHead) tHead;
352 head.getSelectAllCB().setValue(selectedRowKeys.size() == scrollBody.renderedRows.size(), false);
353 }
354
355 @Override
356 protected boolean isRenderHtmlInCells() {
357 return true;
358 }
359 }
360
361 }
362 }