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.

TableConnector.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * Copyright 2000-2016 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.Collections;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import com.google.gwt.core.client.Scheduler;
  21. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  22. import com.google.gwt.dom.client.Element;
  23. import com.google.gwt.dom.client.EventTarget;
  24. import com.google.gwt.event.shared.HandlerRegistration;
  25. import com.google.gwt.user.client.ui.Widget;
  26. import com.vaadin.client.ApplicationConnection;
  27. import com.vaadin.client.ComponentConnector;
  28. import com.vaadin.client.ConnectorHierarchyChangeEvent;
  29. import com.vaadin.client.ConnectorHierarchyChangeEvent.ConnectorHierarchyChangeHandler;
  30. import com.vaadin.client.DirectionalManagedLayout;
  31. import com.vaadin.client.HasChildMeasurementHintConnector;
  32. import com.vaadin.client.HasComponentsConnector;
  33. import com.vaadin.client.Paintable;
  34. import com.vaadin.client.ServerConnector;
  35. import com.vaadin.client.TooltipInfo;
  36. import com.vaadin.client.UIDL;
  37. import com.vaadin.client.WidgetUtil;
  38. import com.vaadin.client.ui.AbstractFieldConnector;
  39. import com.vaadin.client.ui.PostLayoutListener;
  40. import com.vaadin.client.ui.VScrollTable;
  41. import com.vaadin.client.ui.VScrollTable.ContextMenuDetails;
  42. import com.vaadin.client.ui.VScrollTable.FooterCell;
  43. import com.vaadin.client.ui.VScrollTable.HeaderCell;
  44. import com.vaadin.client.ui.VScrollTable.VScrollTableBody.VScrollTableRow;
  45. import com.vaadin.shared.MouseEventDetails;
  46. import com.vaadin.shared.ui.Connect;
  47. import com.vaadin.shared.ui.table.TableConstants;
  48. import com.vaadin.shared.ui.table.TableConstants.Section;
  49. import com.vaadin.shared.ui.table.TableServerRpc;
  50. import com.vaadin.shared.ui.table.TableState;
  51. @Connect(com.vaadin.ui.Table.class)
  52. public class TableConnector extends AbstractFieldConnector
  53. implements HasComponentsConnector, ConnectorHierarchyChangeHandler,
  54. Paintable, DirectionalManagedLayout, PostLayoutListener,
  55. HasChildMeasurementHintConnector {
  56. private List<ComponentConnector> childComponents;
  57. public TableConnector() {
  58. addConnectorHierarchyChangeHandler(this);
  59. }
  60. @Override
  61. protected void init() {
  62. super.init();
  63. getWidget().init(getConnection());
  64. }
  65. /*
  66. * (non-Javadoc)
  67. *
  68. * @see com.vaadin.client.ui.AbstractComponentConnector#onUnregister()
  69. */
  70. @Override
  71. public void onUnregister() {
  72. super.onUnregister();
  73. getWidget().onUnregister();
  74. }
  75. @Override
  76. protected void sendContextClickEvent(MouseEventDetails details,
  77. EventTarget eventTarget) {
  78. if (!Element.is(eventTarget)) {
  79. return;
  80. }
  81. Element e = Element.as(eventTarget);
  82. Section section;
  83. String colKey = null;
  84. String rowKey = null;
  85. if (getWidget().tFoot.getElement().isOrHasChild(e)) {
  86. section = Section.FOOTER;
  87. FooterCell w = WidgetUtil.findWidget(e, FooterCell.class);
  88. colKey = w.getColKey();
  89. } else if (getWidget().tHead.getElement().isOrHasChild(e)) {
  90. section = Section.HEADER;
  91. HeaderCell w = WidgetUtil.findWidget(e, HeaderCell.class);
  92. colKey = w.getColKey();
  93. } else {
  94. section = Section.BODY;
  95. if (getWidget().scrollBody.getElement().isOrHasChild(e)) {
  96. VScrollTableRow w = getScrollTableRow(e);
  97. /*
  98. * if w is null because we've clicked on an empty area, we will
  99. * let rowKey and colKey be null too, which will then lead to
  100. * the server side returning a null object.
  101. */
  102. if (w != null) {
  103. rowKey = w.getKey();
  104. colKey = getWidget().tHead
  105. .getHeaderCell(getElementIndex(e, w.getElement()))
  106. .getColKey();
  107. }
  108. }
  109. }
  110. getRpcProxy(TableServerRpc.class).contextClick(rowKey, colKey, section,
  111. details);
  112. WidgetUtil.clearTextSelection();
  113. }
  114. protected VScrollTableRow getScrollTableRow(Element e) {
  115. return WidgetUtil.findWidget(e, VScrollTableRow.class);
  116. }
  117. private int getElementIndex(Element e,
  118. com.google.gwt.user.client.Element element) {
  119. int i = 0;
  120. Element current = element.getFirstChildElement();
  121. while (!current.isOrHasChild(e)) {
  122. current = current.getNextSiblingElement();
  123. ++i;
  124. }
  125. return i;
  126. }
  127. /*
  128. * (non-Javadoc)
  129. *
  130. * @see com.vaadin.client.Paintable#updateFromUIDL(com.vaadin.client.UIDL,
  131. * com.vaadin.client.ApplicationConnection)
  132. */
  133. @Override
  134. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  135. getWidget().rendering = true;
  136. // If a row has an open context menu, it will be closed as the row is
  137. // detached. Retain a reference here so we can restore the menu if
  138. // required.
  139. ContextMenuDetails contextMenuBeforeUpdate = getWidget().contextMenu;
  140. if (uidl.hasAttribute(TableConstants.ATTRIBUTE_PAGEBUFFER_FIRST)) {
  141. getWidget().serverCacheFirst = uidl
  142. .getIntAttribute(TableConstants.ATTRIBUTE_PAGEBUFFER_FIRST);
  143. getWidget().serverCacheLast = uidl
  144. .getIntAttribute(TableConstants.ATTRIBUTE_PAGEBUFFER_LAST);
  145. } else {
  146. getWidget().serverCacheFirst = -1;
  147. getWidget().serverCacheLast = -1;
  148. }
  149. /*
  150. * We need to do this before updateComponent since updateComponent calls
  151. * this.setHeight() which will calculate a new body height depending on
  152. * the space available.
  153. */
  154. if (uidl.hasAttribute("colfooters")) {
  155. getWidget().showColFooters = uidl.getBooleanAttribute("colfooters");
  156. }
  157. getWidget().tFoot.setVisible(getWidget().showColFooters);
  158. if (!isRealUpdate(uidl)) {
  159. getWidget().rendering = false;
  160. return;
  161. }
  162. getWidget().paintableId = uidl.getStringAttribute("id");
  163. getWidget().immediate = getState().immediate;
  164. int previousTotalRows = getWidget().totalRows;
  165. getWidget().updateTotalRows(uidl);
  166. boolean totalRowsHaveChanged = (getWidget().totalRows != previousTotalRows);
  167. getWidget().updateDragMode(uidl);
  168. // Update child measure hint
  169. int childMeasureHint = uidl.hasAttribute("measurehint")
  170. ? uidl.getIntAttribute("measurehint") : 0;
  171. getWidget().setChildMeasurementHint(
  172. ChildMeasurementHint.values()[childMeasureHint]);
  173. getWidget().updateSelectionProperties(uidl, getState(), isReadOnly());
  174. if (uidl.hasAttribute("alb")) {
  175. getWidget().bodyActionKeys = uidl.getStringArrayAttribute("alb");
  176. } else {
  177. // Need to clear the actions if the action handlers have been
  178. // removed
  179. getWidget().bodyActionKeys = null;
  180. }
  181. getWidget().setCacheRateFromUIDL(uidl);
  182. getWidget().recalcWidths = uidl.hasAttribute("recalcWidths");
  183. if (getWidget().recalcWidths) {
  184. getWidget().tHead.clear();
  185. getWidget().tFoot.clear();
  186. }
  187. getWidget().updatePageLength(uidl);
  188. getWidget().updateFirstVisibleAndScrollIfNeeded(uidl);
  189. getWidget().showRowHeaders = uidl.getBooleanAttribute("rowheaders");
  190. getWidget().showColHeaders = uidl.getBooleanAttribute("colheaders");
  191. getWidget().updateSortingProperties(uidl);
  192. getWidget().updateActionMap(uidl);
  193. getWidget().updateColumnProperties(uidl);
  194. UIDL ac = uidl.getChildByTagName("-ac");
  195. if (ac == null) {
  196. if (getWidget().dropHandler != null) {
  197. // remove dropHandler if not present anymore
  198. getWidget().dropHandler = null;
  199. }
  200. } else {
  201. if (getWidget().dropHandler == null) {
  202. getWidget().dropHandler = getWidget().new VScrollTableDropHandler();
  203. }
  204. getWidget().dropHandler.updateAcceptRules(ac);
  205. }
  206. UIDL partialRowAdditions = uidl.getChildByTagName("prows");
  207. UIDL partialRowUpdates = uidl.getChildByTagName("urows");
  208. if (partialRowUpdates != null || partialRowAdditions != null) {
  209. getWidget().postponeSanityCheckForLastRendered = true;
  210. // we may have pending cache row fetch, cancel it. See #2136
  211. getWidget().rowRequestHandler.cancel();
  212. getWidget().updateRowsInBody(partialRowUpdates);
  213. getWidget().addAndRemoveRows(partialRowAdditions);
  214. // sanity check (in case the value has slipped beyond the total
  215. // amount of rows)
  216. getWidget().scrollBody
  217. .setLastRendered(getWidget().scrollBody.getLastRendered());
  218. getWidget().updateMaxIndent();
  219. } else {
  220. getWidget().postponeSanityCheckForLastRendered = false;
  221. UIDL rowData = uidl.getChildByTagName("rows");
  222. if (rowData != null) {
  223. // we may have pending cache row fetch, cancel it. See #2136
  224. getWidget().rowRequestHandler.cancel();
  225. if (!getWidget().recalcWidths
  226. && getWidget().initializedAndAttached) {
  227. getWidget().updateBody(rowData,
  228. uidl.getIntAttribute("firstrow"),
  229. uidl.getIntAttribute("rows"));
  230. if (getWidget().headerChangedDuringUpdate) {
  231. getWidget().triggerLazyColumnAdjustment(true);
  232. }
  233. } else {
  234. getWidget().initializeRows(uidl, rowData);
  235. }
  236. }
  237. }
  238. boolean keyboardSelectionOverRowFetchInProgress = getWidget()
  239. .selectSelectedRows(uidl);
  240. // If a row had an open context menu before the update, and after the
  241. // update there's a row with the same key as that row, restore the
  242. // context menu. See #8526.
  243. showSavedContextMenu(contextMenuBeforeUpdate);
  244. if (!getWidget().isSelectable()) {
  245. getWidget().scrollBody.addStyleName(
  246. getWidget().getStylePrimaryName() + "-body-noselection");
  247. } else {
  248. getWidget().scrollBody.removeStyleName(
  249. getWidget().getStylePrimaryName() + "-body-noselection");
  250. }
  251. getWidget().hideScrollPositionAnnotation();
  252. // selection is no in sync with server, avoid excessive server visits by
  253. // clearing to flag used during the normal operation
  254. if (!keyboardSelectionOverRowFetchInProgress) {
  255. getWidget().selectionChanged = false;
  256. }
  257. /*
  258. * This is called when the Home or page up button has been pressed in
  259. * selectable mode and the next selected row was not yet rendered in the
  260. * client
  261. */
  262. if (getWidget().selectFirstItemInNextRender
  263. || getWidget().focusFirstItemInNextRender) {
  264. getWidget().selectFirstRenderedRowInViewPort(
  265. getWidget().focusFirstItemInNextRender);
  266. getWidget().selectFirstItemInNextRender = getWidget().focusFirstItemInNextRender = false;
  267. }
  268. /*
  269. * This is called when the page down or end button has been pressed in
  270. * selectable mode and the next selected row was not yet rendered in the
  271. * client
  272. */
  273. if (getWidget().selectLastItemInNextRender
  274. || getWidget().focusLastItemInNextRender) {
  275. getWidget().selectLastRenderedRowInViewPort(
  276. getWidget().focusLastItemInNextRender);
  277. getWidget().selectLastItemInNextRender = getWidget().focusLastItemInNextRender = false;
  278. }
  279. getWidget().multiselectPending = false;
  280. if (getWidget().focusedRow != null) {
  281. if (!getWidget().focusedRow.isAttached()
  282. && !getWidget().rowRequestHandler
  283. .isRequestHandlerRunning()) {
  284. // focused row has been orphaned, can't focus
  285. if (getWidget().selectedRowKeys
  286. .contains(getWidget().focusedRow.getKey())) {
  287. // if row cache was refreshed, focused row should be
  288. // in selection and exists with same index
  289. getWidget().setRowFocus(getWidget().getRenderedRowByKey(
  290. getWidget().focusedRow.getKey()));
  291. } else if (getWidget().selectedRowKeys.size() > 0) {
  292. // try to focus any row in selection
  293. getWidget().setRowFocus(getWidget().getRenderedRowByKey(
  294. getWidget().selectedRowKeys.iterator().next()));
  295. } else {
  296. // try to focus any row
  297. getWidget().focusRowFromBody();
  298. }
  299. }
  300. }
  301. /*
  302. * If the server has (re)initialized the rows, our selectionRangeStart
  303. * row will point to an index that the server knows nothing about,
  304. * causing problems if doing multi selection with shift. The field will
  305. * be cleared a little later when the row focus has been restored.
  306. * (#8584)
  307. */
  308. if (uidl.hasAttribute(TableConstants.ATTRIBUTE_KEY_MAPPER_RESET)
  309. && uidl.getBooleanAttribute(
  310. TableConstants.ATTRIBUTE_KEY_MAPPER_RESET)
  311. && getWidget().selectionRangeStart != null) {
  312. assert !getWidget().selectionRangeStart.isAttached();
  313. getWidget().selectionRangeStart = getWidget().focusedRow;
  314. }
  315. getWidget().tabIndex = getState().tabIndex;
  316. getWidget().setProperTabIndex();
  317. Scheduler.get().scheduleFinally(new ScheduledCommand() {
  318. @Override
  319. public void execute() {
  320. getWidget().resizeSortedColumnForSortIndicator();
  321. }
  322. });
  323. // Remember this to detect situations where overflow hack might be
  324. // needed during scrolling
  325. getWidget().lastRenderedHeight = getWidget().scrollBody
  326. .getOffsetHeight();
  327. getWidget().rendering = false;
  328. getWidget().headerChangedDuringUpdate = false;
  329. getWidget().collapsibleMenuContent = getState().collapseMenuContent;
  330. }
  331. @Override
  332. public void updateEnabledState(boolean enabledState) {
  333. super.updateEnabledState(enabledState);
  334. getWidget().enabled = isEnabled();
  335. }
  336. @Override
  337. public VScrollTable getWidget() {
  338. return (VScrollTable) super.getWidget();
  339. }
  340. @Override
  341. public void updateCaption(ComponentConnector component) {
  342. // NOP, not rendered
  343. }
  344. @Override
  345. public void layoutVertically() {
  346. getWidget().updateHeight();
  347. }
  348. @Override
  349. public void layoutHorizontally() {
  350. getWidget().updateWidth();
  351. }
  352. @Override
  353. public void postLayout() {
  354. VScrollTable table = getWidget();
  355. if (table.sizeNeedsInit) {
  356. table.sizeInit();
  357. Scheduler.get().scheduleFinally(new ScheduledCommand() {
  358. @Override
  359. public void execute() {
  360. getLayoutManager().setNeedsMeasure(TableConnector.this);
  361. ServerConnector parent = getParent();
  362. if (parent instanceof ComponentConnector) {
  363. getLayoutManager()
  364. .setNeedsMeasure((ComponentConnector) parent);
  365. }
  366. getLayoutManager()
  367. .setNeedsVerticalLayout(TableConnector.this);
  368. getLayoutManager().layoutNow();
  369. }
  370. });
  371. }
  372. }
  373. @Override
  374. public boolean isReadOnly() {
  375. return super.isReadOnly() || getState().propertyReadOnly;
  376. }
  377. @Override
  378. public TableState getState() {
  379. return (TableState) super.getState();
  380. }
  381. /**
  382. * Shows a saved row context menu if the row for the context menu is still
  383. * visible. Does nothing if a context menu has not been saved.
  384. *
  385. * @param savedContextMenu
  386. */
  387. public void showSavedContextMenu(ContextMenuDetails savedContextMenu) {
  388. if (isEnabled() && savedContextMenu != null) {
  389. Iterator<Widget> iterator = getWidget().scrollBody.iterator();
  390. while (iterator.hasNext()) {
  391. Widget w = iterator.next();
  392. VScrollTableRow row = (VScrollTableRow) w;
  393. if (row.getKey().equals(savedContextMenu.rowKey)) {
  394. row.showContextMenu(savedContextMenu.left,
  395. savedContextMenu.top);
  396. }
  397. }
  398. }
  399. }
  400. @Override
  401. public TooltipInfo getTooltipInfo(Element element) {
  402. TooltipInfo info = null;
  403. if (element != getWidget().getElement()) {
  404. Object node = WidgetUtil.findWidget(element, VScrollTableRow.class);
  405. if (node != null) {
  406. VScrollTableRow row = (VScrollTableRow) node;
  407. info = row.getTooltip(element);
  408. }
  409. }
  410. if (info == null) {
  411. info = super.getTooltipInfo(element);
  412. }
  413. return info;
  414. }
  415. @Override
  416. public boolean hasTooltip() {
  417. /*
  418. * Tooltips for individual rows and cells are not processed until
  419. * updateFromUIDL, so we can't be sure that there are no tooltips during
  420. * onStateChange when this method is used.
  421. */
  422. return true;
  423. }
  424. @Override
  425. public void onConnectorHierarchyChange(
  426. ConnectorHierarchyChangeEvent connectorHierarchyChangeEvent) {
  427. // TODO Move code from updateFromUIDL to this method
  428. }
  429. @Override
  430. protected void updateComponentSize(String newWidth, String newHeight) {
  431. super.updateComponentSize(newWidth, newHeight);
  432. if ("".equals(newWidth)) {
  433. getWidget().updateWidth();
  434. }
  435. if ("".equals(newHeight)) {
  436. getWidget().updateHeight();
  437. }
  438. }
  439. @Override
  440. public List<ComponentConnector> getChildComponents() {
  441. if (childComponents == null) {
  442. return Collections.emptyList();
  443. }
  444. return childComponents;
  445. }
  446. @Override
  447. public void setChildComponents(List<ComponentConnector> childComponents) {
  448. this.childComponents = childComponents;
  449. }
  450. @Override
  451. public HandlerRegistration addConnectorHierarchyChangeHandler(
  452. ConnectorHierarchyChangeHandler handler) {
  453. return ensureHandlerManager()
  454. .addHandler(ConnectorHierarchyChangeEvent.TYPE, handler);
  455. }
  456. @Override
  457. public void setChildMeasurementHint(ChildMeasurementHint hint) {
  458. getWidget().setChildMeasurementHint(hint);
  459. }
  460. @Override
  461. public ChildMeasurementHint getChildMeasurementHint() {
  462. return getWidget().getChildMeasurementHint();
  463. }
  464. }