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