View Javadoc
1   /*
2    * Copyright 2000-2018 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.v7.client.ui.treetable;
17  
18  import com.google.gwt.dom.client.Element;
19  import com.vaadin.client.ApplicationConnection;
20  import com.vaadin.client.TooltipInfo;
21  import com.vaadin.client.UIDL;
22  import com.vaadin.client.WidgetUtil;
23  import com.vaadin.client.ui.FocusableScrollPanel;
24  import com.vaadin.shared.ui.Connect;
25  import com.vaadin.v7.client.ui.VScrollTablePatched.VScrollTableBody.VScrollTableRow;
26  import com.vaadin.v7.client.ui.VTreeTablePatched;
27  import com.vaadin.v7.client.ui.VTreeTablePatched.PendingNavigationEvent;
28  import com.vaadin.v7.client.ui.VTreeTablePatched.VTreeTableScrollBody.VTreeTableRow;
29  import com.vaadin.v7.client.ui.table.TableConnectorPatched;
30  import com.vaadin.v7.shared.ui.treetable.TreeTableConstants;
31  import com.vaadin.v7.shared.ui.treetable.TreeTableState;
32  import com.vaadin.v7.ui.TreeTable;
33  
34  //@Connect(TreeTable.class)
35  public class TreeTableConnectorPatched extends TableConnectorPatched {
36  
37      @Override
38      public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
39          FocusableScrollPanel widget = null;
40          int scrollPosition = 0;
41  
42          // MGNLUI-961 Without lazy loading, we need to restore scrollTop not only for collapseRequests (part. 1)
43          // if (getWidget().collapseRequest) {
44              widget = (FocusableScrollPanel) getWidget().getWidget(1);
45              scrollPosition = widget.getScrollPosition();
46          // }
47          getWidget().animationsEnabled = uidl.getBooleanAttribute("animate");
48          getWidget().colIndexOfHierarchy = uidl.hasAttribute(
49                  TreeTableConstants.ATTRIBUTE_HIERARCHY_COLUMN_INDEX)
50                          ? uidl.getIntAttribute(
51                                  TreeTableConstants.ATTRIBUTE_HIERARCHY_COLUMN_INDEX)
52                          : 0;
53          int oldTotalRows = getWidget().getTotalRows();
54  
55          super.updateFromUIDL(uidl, client);
56          // super.updateFromUIDL set rendering to false, even though we continue
57          // rendering here. Set it back to true.
58          getWidget().rendering = true;
59  
60          if (getWidget().collapseRequest) {
61              if (getWidget().collapsedRowKey != null
62                      && getWidget().scrollBody != null) {
63                  VScrollTableRow row = getWidget()
64                          .getRenderedRowByKey(getWidget().collapsedRowKey);
65                  if (row != null) {
66                      getWidget().setRowFocus(row);
67                      getWidget().focus();
68                  }
69              }
70  
71              // MGNLUI-961 Without lazy loading, we need to restore scrollTop not only for collapseRequests (part. 2)
72          }
73              int scrollPosition2 = widget.getScrollPosition();
74              if (scrollPosition != scrollPosition2) {
75                  widget.setScrollPosition(scrollPosition);
76              }
77  
78              // check which rows are needed from the server and initiate a
79              // deferred fetch
80              // getWidget().onScroll(null);
81              // }
82          // Recalculate table size if collapse request, or if page length is zero
83          // (not sent by server) and row count changes (#7908).
84          if (getWidget().collapseRequest || (!uidl.hasAttribute("pagelength")
85                  && getWidget().getTotalRows() != oldTotalRows)) {
86              /*
87               * Ensure that possibly removed/added scrollbars are considered.
88               * Triggers row calculations, removes cached rows etc. Basically
89               * cleans up state. Be careful if touching this, you will break
90               * pageLength=0 if you remove this.
91               */
92              getWidget().triggerLazyColumnAdjustment(false);
93  
94              getWidget().collapseRequest = false;
95          }
96          if (uidl.hasAttribute("focusedRow")) {
97              String key = uidl.getStringAttribute("focusedRow");
98              getWidget().setRowFocus(getWidget().getRenderedRowByKey(key));
99              getWidget().focusParentResponsePending = false;
100         } else if (uidl.hasAttribute("clearFocusPending")) {
101             // Special case to detect a response to a focusParent request that
102             // does not return any focusedRow because the selected node has no
103             // parent
104             getWidget().focusParentResponsePending = false;
105         }
106 
107         while (!getWidget().collapseRequest
108                 && !getWidget().focusParentResponsePending
109                 && !getWidget().pendingNavigationEvents.isEmpty()) {
110             // Keep replaying any queued events as long as we don't have any
111             // potential content changes pending
112             PendingNavigationEvent event = getWidget().pendingNavigationEvents
113                     .removeFirst();
114             getWidget().handleNavigation(event.keycode, event.ctrl,
115                     event.shift);
116         }
117         getWidget().rendering = false;
118     }
119 
120     @Override
121     public VTreeTablePatched getWidget() {
122         return (VTreeTablePatched) super.getWidget();
123     }
124 
125     @Override
126     public TreeTableState getState() {
127         return (TreeTableState) super.getState();
128     }
129 
130     @Override
131     public TooltipInfo getTooltipInfo(Element element) {
132 
133         TooltipInfo info = null;
134 
135         if (element != getWidget().getElement()) {
136             Object node = WidgetUtil.findWidget(element, getWidget().scrollBody.iterator().next().getClass());
137 
138             if (node != null) {
139                 VTreeTableRow row = (VTreeTableRow) node;
140                 info = row.getTooltip(element);
141             }
142         }
143 
144         if (info == null) {
145             info = super.getTooltipInfo(element);
146         }
147 
148         return info;
149     }
150 
151     @Override
152     protected VScrollTableRow getScrollTableRow(Element e) {
153         return WidgetUtil.findWidget(e, VTreeTableRow.class);
154     }
155 }