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.Node;
39 import com.google.gwt.dom.client.SpanElement;
40 import com.google.gwt.dom.client.TableCellElement;
41 import com.google.gwt.event.logical.shared.ValueChangeEvent;
42 import com.google.gwt.event.logical.shared.ValueChangeHandler;
43 import com.google.gwt.user.client.DOM;
44 import com.google.gwt.user.client.Element;
45 import com.google.gwt.user.client.ui.CheckBox;
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 boolean firstColumnInitialized;
186
187 public MagnoliaTableRow(UIDL uidl, char[] aligns) {
188 super(uidl, aligns);
189
190 if (uidl.hasAttribute("rowstyle")) {
191 String iconFontStyle = uidl.getStringAttribute("rowstyle");
192 if (iconFontStyle.startsWith("icon-")) {
193 SpanElement iconElement = Document.get().createSpanElement();
194 iconElement.setClassName(iconFontStyle);
195 iconElement.addClassName("v-table-icon-element");
196 Node parentNode = selectionCheckBox.getElement().getParentNode();
197
198 parentNode.insertBefore(iconElement, parentNode.getLastChild());
199 }
200 }
201 }
202
203 public MagnoliaTableRow() {
204 super();
205 }
206
207
208
209
210
211 @Override
212 protected void setElement(com.google.gwt.user.client.Element elem) {
213 super.setElement(elem);
214 privateConstruction();
215 }
216
217 @Override
218 protected void initCellWithText(String text, char align, String style, boolean textIsHTML, boolean sorted, String description, final TableCellElement td) {
219 super.initCellWithText(text, align, style, textIsHTML, sorted, description, td);
220 insertSelectionCheckbox(td);
221 }
222
223 @Override
224 protected void initCellWithWidget(Widget w, char align, String style, boolean sorted, TableCellElement td) {
225 super.initCellWithWidget(w, align, style, sorted, td);
226 insertSelectionCheckbox(td);
227 }
228
229
230
231
232 private void insertSelectionCheckbox(final TableCellElement td) {
233 if (!firstColumnInitialized) {
234 com.google.gwt.dom.client.Element container = td.getFirstChildElement();
235 container.insertFirst(selectionCheckBox.getElement());
236 firstColumnInitialized = true;
237 }
238 }
239
240 private void privateConstruction() {
241 firstColumnInitialized = false;
242 selectionCheckBox = new CheckBox();
243 selectionCheckBox.setValue(isSelected(), false);
244 ValueChangeHandler<Boolean> selectionChangeHandler = new ValueChangeHandler<Boolean>() {
245
246 @Override
247 public void onValueChange(ValueChangeEvent<Boolean> event) {
248 if (event.getSource() instanceof Widget) {
249 final Widget source = (Widget) event.getSource();
250 final Element targetTd = source.getElement().getParentElement().cast();
251 VScrollTableRow row = Util.findWidget(targetTd, null);
252 if (row != null) {
253 boolean wasSelected = row.isSelected();
254
255 if (VMagnoliaTable.this.isSingleSelectMode() && !row.isSelected()) {
256 deselectAll();
257 }
258
259 row.toggleSelection();
260 setRowFocus(row);
261
262
263
264 selectionRangeStart = row;
265 if (wasSelected) {
266 removeRowFromUnsentSelectionRanges(row);
267 }
268
269 sendSelectedRows(true);
270 }
271 }
272 }
273 };
274
275 selectionCheckBox.addValueChangeHandler(selectionChangeHandler);
276 selectionCheckBox.addStyleName("v-selection-cb");
277 getChildren().add(selectionCheckBox);
278 VMagnoliaTable.this.adopt(selectionCheckBox);
279
280 final TouchDelegate delegate = new TouchDelegate(this);
281 delegate.addTouchHandler(new MultiTapRecognizer(delegate, 1, 2));
282 addHandler(new MultiTapHandler() {
283
284 @Override
285 public void onMultiTap(MultiTapEvent event) {
286 if (BrowserInfo.get().isTouchDevice()) {
287 final NativeEvent doubleClickEvent =
288 Document.get().createDblClickEvent(
289 0,
290 event.getTouchStarts().get(0).get(0).getPageX(),
291 event.getTouchStarts().get(0).get(0).getPageY(),
292 event.getTouchStarts().get(0).get(0).getPageX(),
293 event.getTouchStarts().get(0).get(0).getPageY(),
294 false,
295 false,
296 false,
297 false);
298 getElement().dispatchEvent(doubleClickEvent);
299 }
300 }
301 }, MultiTapEvent.getType());
302 }
303
304 @Override
305 public void toggleSelection() {
306 super.toggleSelection();
307
308 if (selectionCheckBox != null) {
309 selectionCheckBox.setValue(isSelected(), false);
310 }
311 MagnoliaTableHead head = (MagnoliaTableHead) tHead;
312 head.getSelectAllCB().setValue(selectedRowKeys.size() == scrollBody.renderedRows.size(), false);
313 }
314
315 @Override
316 protected boolean isRenderHtmlInCells() {
317 return true;
318 }
319 }
320
321 }
322 }