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 20KB

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