You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VTablePaging.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8. import java.util.Set;
  9. import com.google.gwt.event.dom.client.ClickEvent;
  10. import com.google.gwt.event.dom.client.ClickHandler;
  11. import com.google.gwt.user.client.DOM;
  12. import com.google.gwt.user.client.Event;
  13. import com.google.gwt.user.client.Window;
  14. import com.google.gwt.user.client.ui.Button;
  15. import com.google.gwt.user.client.ui.Composite;
  16. import com.google.gwt.user.client.ui.Grid;
  17. import com.google.gwt.user.client.ui.HTML;
  18. import com.google.gwt.user.client.ui.HorizontalPanel;
  19. import com.google.gwt.user.client.ui.Label;
  20. import com.google.gwt.user.client.ui.SimplePanel;
  21. import com.google.gwt.user.client.ui.VerticalPanel;
  22. import com.google.gwt.user.client.ui.Widget;
  23. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  24. import com.vaadin.terminal.gwt.client.Paintable;
  25. import com.vaadin.terminal.gwt.client.UIDL;
  26. /**
  27. * TODO make this work (just an early prototype). We may want to have paging
  28. * style table which will be much lighter than VScrollTable is.
  29. */
  30. public class VTablePaging extends Composite implements Table, Paintable,
  31. ClickHandler {
  32. private final Grid tBody = new Grid();
  33. private final Button nextPage = new Button(">");
  34. private final Button prevPage = new Button("<");
  35. private final Button firstPage = new Button("<<");
  36. private final Button lastPage = new Button(">>");
  37. private int pageLength = 15;
  38. private boolean rowHeaders = false;
  39. private ApplicationConnection client;
  40. private String id;
  41. private boolean immediate = false;
  42. private int selectMode = Table.SELECT_MODE_NONE;
  43. private final ArrayList<String> selectedRowKeys = new ArrayList<String>();
  44. private int totalRows;
  45. private final HashMap visibleColumns = new HashMap();
  46. private int rows;
  47. private int firstRow;
  48. private boolean sortAscending = true;
  49. private final HorizontalPanel pager;
  50. public HashMap rowKeysToTableRows = new HashMap();
  51. public VTablePaging() {
  52. tBody.setStyleName("itable-tbody");
  53. final VerticalPanel panel = new VerticalPanel();
  54. pager = new HorizontalPanel();
  55. pager.add(firstPage);
  56. firstPage.addClickHandler(this);
  57. pager.add(prevPage);
  58. prevPage.addClickHandler(this);
  59. pager.add(nextPage);
  60. nextPage.addClickHandler(this);
  61. pager.add(lastPage);
  62. lastPage.addClickHandler(this);
  63. panel.add(pager);
  64. panel.add(tBody);
  65. initWidget(panel);
  66. }
  67. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  68. if (client.updateComponent(this, uidl, true)) {
  69. return;
  70. }
  71. this.client = client;
  72. id = uidl.getStringAttribute("id");
  73. immediate = uidl.getBooleanAttribute("immediate");
  74. totalRows = uidl.getIntAttribute("totalrows");
  75. pageLength = uidl.getIntAttribute("pagelength");
  76. firstRow = uidl.getIntAttribute("firstrow");
  77. rows = uidl.getIntAttribute("rows");
  78. if (uidl.hasAttribute("selectmode")) {
  79. if (uidl.getStringAttribute("selectmode").equals("multi")) {
  80. selectMode = Table.SELECT_MODE_MULTI;
  81. } else {
  82. selectMode = Table.SELECT_MODE_SINGLE;
  83. }
  84. if (uidl.hasAttribute("selected")) {
  85. final Set<String> selectedKeys = uidl
  86. .getStringArrayVariableAsSet("selected");
  87. selectedRowKeys.clear();
  88. for (final Iterator<String> it = selectedKeys.iterator(); it
  89. .hasNext();) {
  90. selectedRowKeys.add(it.next());
  91. }
  92. }
  93. }
  94. if (uidl.hasVariable("sortascending")) {
  95. sortAscending = uidl.getBooleanVariable("sortascending");
  96. }
  97. if (uidl.hasAttribute("rowheaders")) {
  98. rowHeaders = true;
  99. }
  100. UIDL rowData = null;
  101. UIDL visibleColumns = null;
  102. for (final Iterator it = uidl.getChildIterator(); it.hasNext();) {
  103. final UIDL c = (UIDL) it.next();
  104. if (c.getTag().equals("rows")) {
  105. rowData = c;
  106. } else if (c.getTag().equals("actions")) {
  107. updateActionMap(c);
  108. } else if (c.getTag().equals("visiblecolumns")) {
  109. visibleColumns = c;
  110. }
  111. }
  112. tBody.resize(rows + 1, uidl.getIntAttribute("cols")
  113. + (rowHeaders ? 1 : 0));
  114. updateHeader(visibleColumns);
  115. updateBody(rowData);
  116. updatePager();
  117. }
  118. private void updateHeader(UIDL c) {
  119. final Iterator it = c.getChildIterator();
  120. visibleColumns.clear();
  121. int colIndex = (rowHeaders ? 1 : 0);
  122. while (it.hasNext()) {
  123. final UIDL col = (UIDL) it.next();
  124. final String cid = col.getStringAttribute("cid");
  125. if (!col.hasAttribute("collapsed")) {
  126. tBody.setWidget(0, colIndex,
  127. new HeaderCell(cid, col.getStringAttribute("caption")));
  128. }
  129. colIndex++;
  130. }
  131. }
  132. private void updateActionMap(UIDL c) {
  133. // TODO Auto-generated method stub
  134. }
  135. /**
  136. * Updates row data from uidl. UpdateFromUIDL delegates updating tBody to
  137. * this method.
  138. *
  139. * Updates may be to different part of tBody, depending on update type. It
  140. * can be initial row data, scroll up, scroll down...
  141. *
  142. * @param uidl
  143. * which contains row data
  144. */
  145. private void updateBody(UIDL uidl) {
  146. final Iterator it = uidl.getChildIterator();
  147. int curRowIndex = 1;
  148. while (it.hasNext()) {
  149. final UIDL rowUidl = (UIDL) it.next();
  150. final TableRow row = new TableRow(curRowIndex,
  151. String.valueOf(rowUidl.getIntAttribute("key")),
  152. rowUidl.hasAttribute("selected"));
  153. int colIndex = 0;
  154. if (rowHeaders) {
  155. tBody.setWidget(curRowIndex, colIndex, new BodyCell(row,
  156. rowUidl.getStringAttribute("caption")));
  157. colIndex++;
  158. }
  159. final Iterator cells = rowUidl.getChildIterator();
  160. while (cells.hasNext()) {
  161. final Object cell = cells.next();
  162. if (cell instanceof String) {
  163. tBody.setWidget(curRowIndex, colIndex, new BodyCell(row,
  164. (String) cell));
  165. } else {
  166. final Paintable cellContent = client
  167. .getPaintable((UIDL) cell);
  168. final BodyCell bodyCell = new BodyCell(row);
  169. bodyCell.setWidget((Widget) cellContent);
  170. tBody.setWidget(curRowIndex, colIndex, bodyCell);
  171. }
  172. colIndex++;
  173. }
  174. curRowIndex++;
  175. }
  176. }
  177. private void updatePager() {
  178. if (pageLength == 0) {
  179. pager.setVisible(false);
  180. return;
  181. }
  182. if (isFirstPage()) {
  183. firstPage.setEnabled(false);
  184. prevPage.setEnabled(false);
  185. } else {
  186. firstPage.setEnabled(true);
  187. prevPage.setEnabled(true);
  188. }
  189. if (hasNextPage()) {
  190. nextPage.setEnabled(true);
  191. lastPage.setEnabled(true);
  192. } else {
  193. nextPage.setEnabled(false);
  194. lastPage.setEnabled(false);
  195. }
  196. }
  197. private boolean hasNextPage() {
  198. if (firstRow + rows + 1 > totalRows) {
  199. return false;
  200. }
  201. return true;
  202. }
  203. private boolean isFirstPage() {
  204. if (firstRow == 0) {
  205. return true;
  206. }
  207. return false;
  208. }
  209. public void onClick(ClickEvent event) {
  210. Object sender = event.getSource();
  211. if (sender instanceof Button) {
  212. if (sender == firstPage) {
  213. client.updateVariable(id, "firstvisible", 0, true);
  214. } else if (sender == nextPage) {
  215. client.updateVariable(id, "firstvisible",
  216. firstRow + pageLength, true);
  217. } else if (sender == prevPage) {
  218. int newFirst = firstRow - pageLength;
  219. if (newFirst < 0) {
  220. newFirst = 0;
  221. }
  222. client.updateVariable(id, "firstvisible", newFirst, true);
  223. } else if (sender == lastPage) {
  224. client.updateVariable(id, "firstvisible", totalRows
  225. - pageLength, true);
  226. }
  227. }
  228. if (sender instanceof HeaderCell) {
  229. final HeaderCell hCell = (HeaderCell) sender;
  230. client.updateVariable(id, "sortcolumn", hCell.getCid(), false);
  231. client.updateVariable(id, "sortascending", (sortAscending ? false
  232. : true), true);
  233. }
  234. }
  235. private class HeaderCell extends HTML {
  236. private String cid;
  237. public String getCid() {
  238. return cid;
  239. }
  240. public void setCid(String pid) {
  241. cid = pid;
  242. }
  243. HeaderCell(String pid, String caption) {
  244. super();
  245. cid = pid;
  246. addClickHandler(VTablePaging.this);
  247. setText(caption);
  248. // TODO remove debug color
  249. DOM.setStyleAttribute(getElement(), "color", "brown");
  250. DOM.setStyleAttribute(getElement(), "font-weight", "bold");
  251. }
  252. }
  253. /**
  254. * Abstraction of table cell content. In needs to know on which row it is in
  255. * case of context click.
  256. *
  257. * @author mattitahvonen
  258. */
  259. public class BodyCell extends SimplePanel {
  260. private final TableRow row;
  261. public BodyCell(TableRow row) {
  262. super();
  263. sinkEvents(Event.BUTTON_LEFT | Event.BUTTON_RIGHT);
  264. this.row = row;
  265. }
  266. public BodyCell(TableRow row2, String textContent) {
  267. super();
  268. sinkEvents(Event.BUTTON_LEFT | Event.BUTTON_RIGHT);
  269. row = row2;
  270. setWidget(new Label(textContent));
  271. }
  272. @Override
  273. public void onBrowserEvent(Event event) {
  274. System.out.println("CEll event: " + event.toString());
  275. switch (DOM.eventGetType(event)) {
  276. case Event.BUTTON_RIGHT:
  277. row.showContextMenu(event);
  278. Window.alert("context menu un-implemented");
  279. DOM.eventCancelBubble(event, true);
  280. break;
  281. case Event.BUTTON_LEFT:
  282. if (selectMode > Table.SELECT_MODE_NONE) {
  283. row.toggleSelected();
  284. }
  285. break;
  286. default:
  287. break;
  288. }
  289. super.onBrowserEvent(event);
  290. }
  291. }
  292. private class TableRow {
  293. private final String key;
  294. private final int rowIndex;
  295. private boolean selected = false;
  296. public TableRow(int rowIndex, String rowKey, boolean selected) {
  297. rowKeysToTableRows.put(rowKey, this);
  298. this.rowIndex = rowIndex;
  299. key = rowKey;
  300. setSelected(selected);
  301. }
  302. /**
  303. * This method is used to set row status. Does not change value on
  304. * server.
  305. *
  306. * @param selected
  307. */
  308. public void setSelected(boolean sel) {
  309. selected = sel;
  310. if (selected) {
  311. selectedRowKeys.add(key);
  312. DOM.setStyleAttribute(
  313. tBody.getRowFormatter().getElement(rowIndex),
  314. "background", "yellow");
  315. } else {
  316. selectedRowKeys.remove(key);
  317. DOM.setStyleAttribute(
  318. tBody.getRowFormatter().getElement(rowIndex),
  319. "background", "transparent");
  320. }
  321. }
  322. public void setContextMenuOptions(HashMap options) {
  323. }
  324. /**
  325. * Toggles rows select state. Also updates state to server according to
  326. * tables immediate flag.
  327. *
  328. */
  329. public void toggleSelected() {
  330. if (selected) {
  331. setSelected(false);
  332. } else {
  333. if (selectMode == Table.SELECT_MODE_SINGLE) {
  334. deselectAll();
  335. }
  336. setSelected(true);
  337. }
  338. client.updateVariable(
  339. id,
  340. "selected",
  341. selectedRowKeys.toArray(new String[selectedRowKeys.size()]),
  342. immediate);
  343. }
  344. /**
  345. * Shows context menu for this row.
  346. *
  347. * @param event
  348. * Event which triggered context menu. Correct place for
  349. * context menu can be determined with it.
  350. */
  351. public void showContextMenu(Event event) {
  352. System.out.println("TODO: Show context menu");
  353. }
  354. }
  355. public void deselectAll() {
  356. final Object[] keys = selectedRowKeys.toArray();
  357. for (int i = 0; i < keys.length; i++) {
  358. final TableRow tableRow = (TableRow) rowKeysToTableRows
  359. .get(keys[i]);
  360. if (tableRow != null) {
  361. tableRow.setSelected(false);
  362. }
  363. }
  364. // still ensure all selects are removed from
  365. selectedRowKeys.clear();
  366. }
  367. public void add(Widget w) {
  368. // TODO Auto-generated method stub
  369. }
  370. public void clear() {
  371. // TODO Auto-generated method stub
  372. }
  373. public Iterator iterator() {
  374. // TODO Auto-generated method stub
  375. return null;
  376. }
  377. public boolean remove(Widget w) {
  378. // TODO Auto-generated method stub
  379. return false;
  380. }
  381. }