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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Copyright 2000-2013 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui.table;
  17. import java.util.Iterator;
  18. import com.google.gwt.core.client.Scheduler;
  19. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  20. import com.google.gwt.dom.client.Element;
  21. import com.google.gwt.dom.client.Style.Position;
  22. import com.google.gwt.user.client.Command;
  23. import com.google.gwt.user.client.ui.Widget;
  24. import com.vaadin.client.ApplicationConnection;
  25. import com.vaadin.client.BrowserInfo;
  26. import com.vaadin.client.ComponentConnector;
  27. import com.vaadin.client.ConnectorHierarchyChangeEvent;
  28. import com.vaadin.client.DirectionalManagedLayout;
  29. import com.vaadin.client.Paintable;
  30. import com.vaadin.client.ServerConnector;
  31. import com.vaadin.client.TooltipInfo;
  32. import com.vaadin.client.UIDL;
  33. import com.vaadin.client.Util;
  34. import com.vaadin.client.ui.AbstractHasComponentsConnector;
  35. import com.vaadin.client.ui.PostLayoutListener;
  36. import com.vaadin.client.ui.VScrollTable;
  37. import com.vaadin.client.ui.VScrollTable.ContextMenuDetails;
  38. import com.vaadin.client.ui.VScrollTable.VScrollTableBody.VScrollTableRow;
  39. import com.vaadin.shared.ui.Connect;
  40. import com.vaadin.shared.ui.table.TableConstants;
  41. import com.vaadin.shared.ui.table.TableState;
  42. @Connect(com.vaadin.ui.Table.class)
  43. public class TableConnector extends AbstractHasComponentsConnector implements
  44. Paintable, DirectionalManagedLayout, PostLayoutListener {
  45. @Override
  46. protected void init() {
  47. super.init();
  48. getWidget().init(getConnection());
  49. }
  50. /*
  51. * (non-Javadoc)
  52. *
  53. * @see com.vaadin.client.Paintable#updateFromUIDL(com.vaadin.client.UIDL,
  54. * com.vaadin.client.ApplicationConnection)
  55. */
  56. @Override
  57. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  58. getWidget().rendering = true;
  59. // If a row has an open context menu, it will be closed as the row is
  60. // detached. Retain a reference here so we can restore the menu if
  61. // required.
  62. ContextMenuDetails contextMenuBeforeUpdate = getWidget().contextMenu;
  63. if (uidl.hasAttribute(TableConstants.ATTRIBUTE_PAGEBUFFER_FIRST)) {
  64. getWidget().serverCacheFirst = uidl
  65. .getIntAttribute(TableConstants.ATTRIBUTE_PAGEBUFFER_FIRST);
  66. getWidget().serverCacheLast = uidl
  67. .getIntAttribute(TableConstants.ATTRIBUTE_PAGEBUFFER_LAST);
  68. } else {
  69. getWidget().serverCacheFirst = -1;
  70. getWidget().serverCacheLast = -1;
  71. }
  72. /*
  73. * We need to do this before updateComponent since updateComponent calls
  74. * this.setHeight() which will calculate a new body height depending on
  75. * the space available.
  76. */
  77. if (uidl.hasAttribute("colfooters")) {
  78. getWidget().showColFooters = uidl.getBooleanAttribute("colfooters");
  79. }
  80. getWidget().tFoot.setVisible(getWidget().showColFooters);
  81. if (!isRealUpdate(uidl)) {
  82. getWidget().rendering = false;
  83. return;
  84. }
  85. getWidget().enabled = isEnabled();
  86. if (BrowserInfo.get().isIE8() && !getWidget().enabled) {
  87. /*
  88. * The disabled shim will not cover the table body if it is relative
  89. * in IE8. See #7324
  90. */
  91. getWidget().scrollBodyPanel.getElement().getStyle()
  92. .setPosition(Position.STATIC);
  93. } else if (BrowserInfo.get().isIE8()) {
  94. getWidget().scrollBodyPanel.getElement().getStyle()
  95. .setPosition(Position.RELATIVE);
  96. }
  97. getWidget().paintableId = uidl.getStringAttribute("id");
  98. getWidget().immediate = getState().immediate;
  99. int previousTotalRows = getWidget().totalRows;
  100. getWidget().updateTotalRows(uidl);
  101. boolean totalRowsChanged = (getWidget().totalRows != previousTotalRows);
  102. getWidget().updateDragMode(uidl);
  103. getWidget().updateSelectionProperties(uidl, getState(), isReadOnly());
  104. if (uidl.hasAttribute("alb")) {
  105. getWidget().bodyActionKeys = uidl.getStringArrayAttribute("alb");
  106. } else {
  107. // Need to clear the actions if the action handlers have been
  108. // removed
  109. getWidget().bodyActionKeys = null;
  110. }
  111. getWidget().setCacheRateFromUIDL(uidl);
  112. getWidget().recalcWidths = uidl.hasAttribute("recalcWidths");
  113. if (getWidget().recalcWidths) {
  114. getWidget().tHead.clear();
  115. getWidget().tFoot.clear();
  116. }
  117. getWidget().updatePageLength(uidl);
  118. getWidget().updateFirstVisibleAndScrollIfNeeded(uidl);
  119. getWidget().showRowHeaders = uidl.getBooleanAttribute("rowheaders");
  120. getWidget().showColHeaders = uidl.getBooleanAttribute("colheaders");
  121. getWidget().updateSortingProperties(uidl);
  122. boolean keyboardSelectionOverRowFetchInProgress = getWidget()
  123. .selectSelectedRows(uidl);
  124. getWidget().updateActionMap(uidl);
  125. getWidget().updateColumnProperties(uidl);
  126. UIDL ac = uidl.getChildByTagName("-ac");
  127. if (ac == null) {
  128. if (getWidget().dropHandler != null) {
  129. // remove dropHandler if not present anymore
  130. getWidget().dropHandler = null;
  131. }
  132. } else {
  133. if (getWidget().dropHandler == null) {
  134. getWidget().dropHandler = getWidget().new VScrollTableDropHandler();
  135. }
  136. getWidget().dropHandler.updateAcceptRules(ac);
  137. }
  138. UIDL partialRowAdditions = uidl.getChildByTagName("prows");
  139. UIDL partialRowUpdates = uidl.getChildByTagName("urows");
  140. if (partialRowUpdates != null || partialRowAdditions != null) {
  141. getWidget().postponeSanityCheckForLastRendered = true;
  142. // we may have pending cache row fetch, cancel it. See #2136
  143. getWidget().rowRequestHandler.cancel();
  144. getWidget().updateRowsInBody(partialRowUpdates);
  145. getWidget().addAndRemoveRows(partialRowAdditions);
  146. // sanity check (in case the value has slipped beyond the total
  147. // amount of rows)
  148. getWidget().scrollBody.setLastRendered(getWidget().scrollBody
  149. .getLastRendered());
  150. getWidget().updateMaxIndent();
  151. } else {
  152. getWidget().postponeSanityCheckForLastRendered = false;
  153. UIDL rowData = uidl.getChildByTagName("rows");
  154. if (rowData != null) {
  155. // we may have pending cache row fetch, cancel it. See #2136
  156. getWidget().rowRequestHandler.cancel();
  157. if (!getWidget().recalcWidths
  158. && getWidget().initializedAndAttached) {
  159. getWidget().updateBody(rowData,
  160. uidl.getIntAttribute("firstrow"),
  161. uidl.getIntAttribute("rows"));
  162. if (getWidget().headerChangedDuringUpdate) {
  163. getWidget().triggerLazyColumnAdjustment(true);
  164. } else if (!getWidget().isScrollPositionVisible()
  165. || totalRowsChanged
  166. || getWidget().lastRenderedHeight != getWidget().scrollBody
  167. .getOffsetHeight()) {
  168. // webkits may still bug with their disturbing scrollbar
  169. // bug, see #3457
  170. // Run overflow fix for the scrollable area
  171. // #6698 - If there's a scroll going on, don't abort it
  172. // by changing overflows as the length of the contents
  173. // *shouldn't* have changed (unless the number of rows
  174. // or the height of the widget has also changed)
  175. Scheduler.get().scheduleDeferred(new Command() {
  176. @Override
  177. public void execute() {
  178. Util.runWebkitOverflowAutoFix(getWidget().scrollBodyPanel
  179. .getElement());
  180. }
  181. });
  182. }
  183. } else {
  184. getWidget().initializeRows(uidl, rowData);
  185. }
  186. }
  187. }
  188. // If a row had an open context menu before the update, and after the
  189. // update there's a row with the same key as that row, restore the
  190. // context menu. See #8526.
  191. showSavedContextMenu(contextMenuBeforeUpdate);
  192. if (!getWidget().isSelectable()) {
  193. getWidget().scrollBody.addStyleName(getWidget()
  194. .getStylePrimaryName() + "-body-noselection");
  195. } else {
  196. getWidget().scrollBody.removeStyleName(getWidget()
  197. .getStylePrimaryName() + "-body-noselection");
  198. }
  199. getWidget().hideScrollPositionAnnotation();
  200. // selection is no in sync with server, avoid excessive server visits by
  201. // clearing to flag used during the normal operation
  202. if (!keyboardSelectionOverRowFetchInProgress) {
  203. getWidget().selectionChanged = false;
  204. }
  205. /*
  206. * This is called when the Home or page up button has been pressed in
  207. * selectable mode and the next selected row was not yet rendered in the
  208. * client
  209. */
  210. if (getWidget().selectFirstItemInNextRender
  211. || getWidget().focusFirstItemInNextRender) {
  212. getWidget().selectFirstRenderedRowInViewPort(
  213. getWidget().focusFirstItemInNextRender);
  214. getWidget().selectFirstItemInNextRender = getWidget().focusFirstItemInNextRender = false;
  215. }
  216. /*
  217. * This is called when the page down or end button has been pressed in
  218. * selectable mode and the next selected row was not yet rendered in the
  219. * client
  220. */
  221. if (getWidget().selectLastItemInNextRender
  222. || getWidget().focusLastItemInNextRender) {
  223. getWidget().selectLastRenderedRowInViewPort(
  224. getWidget().focusLastItemInNextRender);
  225. getWidget().selectLastItemInNextRender = getWidget().focusLastItemInNextRender = false;
  226. }
  227. getWidget().multiselectPending = false;
  228. if (getWidget().focusedRow != null) {
  229. if (!getWidget().focusedRow.isAttached()
  230. && !getWidget().rowRequestHandler.isRunning()) {
  231. // focused row has been orphaned, can't focus
  232. getWidget().focusRowFromBody();
  233. }
  234. }
  235. /*
  236. * If the server has (re)initialized the rows, our selectionRangeStart
  237. * row will point to an index that the server knows nothing about,
  238. * causing problems if doing multi selection with shift. The field will
  239. * be cleared a little later when the row focus has been restored.
  240. * (#8584)
  241. */
  242. if (uidl.hasAttribute(TableConstants.ATTRIBUTE_KEY_MAPPER_RESET)
  243. && uidl.getBooleanAttribute(TableConstants.ATTRIBUTE_KEY_MAPPER_RESET)
  244. && getWidget().selectionRangeStart != null) {
  245. assert !getWidget().selectionRangeStart.isAttached();
  246. getWidget().selectionRangeStart = getWidget().focusedRow;
  247. }
  248. getWidget().tabIndex = getState().tabIndex;
  249. getWidget().setProperTabIndex();
  250. getWidget().resizeSortedColumnForSortIndicator();
  251. // Remember this to detect situations where overflow hack might be
  252. // needed during scrolling
  253. getWidget().lastRenderedHeight = getWidget().scrollBody
  254. .getOffsetHeight();
  255. getWidget().rendering = false;
  256. getWidget().headerChangedDuringUpdate = false;
  257. }
  258. @Override
  259. public VScrollTable getWidget() {
  260. return (VScrollTable) super.getWidget();
  261. }
  262. @Override
  263. public void updateCaption(ComponentConnector component) {
  264. // NOP, not rendered
  265. }
  266. @Override
  267. public void layoutVertically() {
  268. getWidget().updateHeight();
  269. }
  270. @Override
  271. public void layoutHorizontally() {
  272. getWidget().updateWidth();
  273. }
  274. @Override
  275. public void postLayout() {
  276. VScrollTable table = getWidget();
  277. if (table.sizeNeedsInit) {
  278. table.sizeInit();
  279. Scheduler.get().scheduleFinally(new ScheduledCommand() {
  280. @Override
  281. public void execute() {
  282. getLayoutManager().setNeedsMeasure(TableConnector.this);
  283. ServerConnector parent = getParent();
  284. if (parent instanceof ComponentConnector) {
  285. getLayoutManager().setNeedsMeasure(
  286. (ComponentConnector) parent);
  287. }
  288. getLayoutManager().setNeedsVerticalLayout(
  289. TableConnector.this);
  290. getLayoutManager().layoutNow();
  291. }
  292. });
  293. }
  294. }
  295. @Override
  296. public boolean isReadOnly() {
  297. return super.isReadOnly() || getState().propertyReadOnly;
  298. }
  299. @Override
  300. public TableState getState() {
  301. return (TableState) super.getState();
  302. }
  303. /**
  304. * Shows a saved row context menu if the row for the context menu is still
  305. * visible. Does nothing if a context menu has not been saved.
  306. *
  307. * @param savedContextMenu
  308. */
  309. public void showSavedContextMenu(ContextMenuDetails savedContextMenu) {
  310. if (isEnabled() && savedContextMenu != null) {
  311. Iterator<Widget> iterator = getWidget().scrollBody.iterator();
  312. while (iterator.hasNext()) {
  313. Widget w = iterator.next();
  314. VScrollTableRow row = (VScrollTableRow) w;
  315. if (row.getKey().equals(savedContextMenu.rowKey)) {
  316. getWidget().contextMenu = savedContextMenu;
  317. getConnection().getContextMenu().showAt(row,
  318. savedContextMenu.left, savedContextMenu.top);
  319. }
  320. }
  321. }
  322. }
  323. @Override
  324. public TooltipInfo getTooltipInfo(Element element) {
  325. TooltipInfo info = null;
  326. if (element != getWidget().getElement()) {
  327. Object node = Util.findWidget(
  328. (com.google.gwt.user.client.Element) element,
  329. VScrollTableRow.class);
  330. if (node != null) {
  331. VScrollTableRow row = (VScrollTableRow) node;
  332. info = row.getTooltip(element);
  333. }
  334. }
  335. if (info == null) {
  336. info = super.getTooltipInfo(element);
  337. }
  338. return info;
  339. }
  340. @Override
  341. public void onConnectorHierarchyChange(
  342. ConnectorHierarchyChangeEvent connectorHierarchyChangeEvent) {
  343. // TODO Move code from updateFromUIDL to this method
  344. }
  345. }