View Javadoc
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.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.Util;
23  import com.vaadin.client.ui.FocusableScrollPanel;
24  import com.vaadin.client.ui.VScrollTable.VScrollTableBody.VScrollTableRow;
25  import com.vaadin.client.ui.VTreeTable;
26  import com.vaadin.client.ui.VTreeTable.PendingNavigationEvent;
27  import com.vaadin.client.ui.VTreeTable.VTreeTableScrollBody.VTreeTableRow;
28  import com.vaadin.client.ui.table.TableConnector;
29  import com.vaadin.shared.ui.Connect;
30  import com.vaadin.shared.ui.treetable.TreeTableConstants;
31  import com.vaadin.shared.ui.treetable.TreeTableState;
32  import com.vaadin.ui.TreeTable;
33  
34  @Connect(TreeTable.class)
35  public class TreeTableConnector extends TableConnector {
36  
37      @Override
38      public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
39          FocusableScrollPanel widget = null;
40          int scrollPosition = 0;
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          super.updateFromUIDL(uidl, client);
52          if (getWidget().collapseRequest) {
53              if (getWidget().collapsedRowKey != null
54                      && getWidget().scrollBody != null) {
55                  VScrollTableRow row = getWidget().getRenderedRowByKey(
56                          getWidget().collapsedRowKey);
57                  if (row != null) {
58                      getWidget().setRowFocus(row);
59                      getWidget().focus();
60                  }
61              }
62  
63              int scrollPosition2 = widget.getScrollPosition();
64              if (scrollPosition != scrollPosition2) {
65                  widget.setScrollPosition(scrollPosition);
66              }
67  
68              // check which rows are needed from the server and initiate a
69              // deferred fetch
70              getWidget().onScroll(null);
71          }
72          // Recalculate table size if collapse request, or if page length is zero
73          // (not sent by server) and row count changes (#7908).
74          if (getWidget().collapseRequest
75                  || (!uidl.hasAttribute("pagelength") && getWidget()
76                          .getTotalRows() != oldTotalRows)) {
77              /*
78               * Ensure that possibly removed/added scrollbars are considered.
79               * Triggers row calculations, removes cached rows etc. Basically
80               * cleans up state. Be careful if touching this, you will break
81               * pageLength=0 if you remove this.
82               */
83              getWidget().triggerLazyColumnAdjustment(true);
84  
85              getWidget().collapseRequest = false;
86          }
87          if (uidl.hasAttribute("focusedRow")) {
88              String key = uidl.getStringAttribute("focusedRow");
89              getWidget().setRowFocus(getWidget().getRenderedRowByKey(key));
90              getWidget().focusParentResponsePending = false;
91          } else if (uidl.hasAttribute("clearFocusPending")) {
92              // Special case to detect a response to a focusParent request that
93              // does not return any focusedRow because the selected node has no
94              // parent
95              getWidget().focusParentResponsePending = false;
96          }
97  
98          while (!getWidget().collapseRequest
99                  && !getWidget().focusParentResponsePending
100                 && !getWidget().pendingNavigationEvents.isEmpty()) {
101             // Keep replaying any queued events as long as we don't have any
102             // potential content changes pending
103             PendingNavigationEvent event = getWidget().pendingNavigationEvents
104                     .removeFirst();
105             getWidget()
106                     .handleNavigation(event.keycode, event.ctrl, event.shift);
107         }
108     }
109 
110     @Override
111     public VTreeTable getWidget() {
112         return (VTreeTable) super.getWidget();
113     }
114 
115     @Override
116     public TreeTableState getState() {
117         return (TreeTableState) super.getState();
118     }
119 
120     @Override
121     public TooltipInfo getTooltipInfo(Element element) {
122 
123         TooltipInfo info = null;
124 
125         if (element != getWidget().getElement()) {
126             Object node = Util.findWidget(
127                     (com.google.gwt.user.client.Element) element,
128                     VTreeTableRow.class);
129 
130             if (node != null) {
131                 VTreeTableRow row = (VTreeTableRow) node;
132                 info = row.getTooltip(element);
133             }
134         }
135 
136         if (info == null) {
137             info = super.getTooltipInfo(element);
138         }
139 
140         return info;
141     }
142 }