]> source.dussan.org Git - vaadin-framework.git/commitdiff
Optimized Table uidl, fixed IScrollTable when attaching before initial uidl update...
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Fri, 13 Jul 2007 08:33:24 +0000 (08:33 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Fri, 13 Jul 2007 08:33:24 +0000 (08:33 +0000)
svn changeset:1852/svn branch:trunk

src/com/itmill/toolkit/terminal/gwt/client/DefaultWidgetFactory.java
src/com/itmill/toolkit/terminal/gwt/client/ui/IScrollTable.java
src/com/itmill/toolkit/terminal/gwt/client/ui/ITablePaging.java
src/com/itmill/toolkit/ui/Table.java

index aab422574d524b741c04bf7d6cdd16533b02ee4c..ba124ef6b82907786c2c4102994b4b7db1d6c91c 100644 (file)
@@ -79,10 +79,10 @@ public class DefaultWidgetFactory implements WidgetFactory {
                }
                if ("table".equals(tag)) {
                        if(uidl.hasAttribute("style")) {
-                               if("scrolling".equals(uidl.getStringAttribute("style")))
-                                               return new IScrollTable();
+                               if("paging".equals(uidl.getStringAttribute("style")))
+                                               return new ITablePaging();
                        }
-                       return new ITablePaging();
+                       return new IScrollTable();
                }
                if("datefield".equals(tag)) {
                        if(uidl.hasAttribute("style"))
index 8711cc83f84c973e806e91f5cf2dcb61a74374b5..1b521837a3f42acf62c8d4bfdf378f8295532a3e 100644 (file)
@@ -14,6 +14,7 @@ import com.google.gwt.user.client.Event;
 import com.google.gwt.user.client.Timer;
 import com.google.gwt.user.client.ui.Composite;
 import com.google.gwt.user.client.ui.Panel;
+import com.google.gwt.user.client.ui.RootPanel;
 import com.google.gwt.user.client.ui.ScrollListener;
 import com.google.gwt.user.client.ui.ScrollPanel;
 import com.google.gwt.user.client.ui.VerticalPanel;
@@ -80,6 +81,7 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
         */
        private HashMap actionMap = new HashMap();
        private String[] visibleColOrder;
+       private boolean initialContentReceived = false;
        
        public IScrollTable() {
                // TODO move headerContainer and column selector into TableHead
@@ -88,7 +90,6 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
                
                VerticalPanel panel = new VerticalPanel();
                
-               
                panel.add(tHead);
                panel.add(bodyContainer);
                
@@ -121,11 +122,12 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
                        this.sortColumn = uidl.getStringVariable("sortcolumn");
                }
                
-               Set selectedKeys = uidl.getStringArrayVariableAsSet("selected");
-               selectedRowKeys.clear();
-               for(Iterator it = selectedKeys.iterator();it.hasNext();)
-                       selectedRowKeys.add((String) it.next());
-
+               if(uidl.hasVariable("selected")) {
+                       Set selectedKeys = uidl.getStringArrayVariableAsSet("selected");
+                       selectedRowKeys.clear();
+                       for(Iterator it = selectedKeys.iterator();it.hasNext();)
+                               selectedRowKeys.add((String) it.next());
+               }
                
                if(uidl.hasAttribute("selectmode")) {
                        if(uidl.getStringAttribute("selectmode").equals("multi"))
@@ -150,16 +152,14 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
                UIDL rowData = null;
                for(Iterator it = uidl.getChildIterator(); it.hasNext();) {
                        UIDL c = (UIDL) it.next();
-                       if(c.getTag().equals("cols"))
-                               columnInfo = c;
-                       else if(c.getTag().equals("rows"))
+                       if(c.getTag().equals("rows"))
                                rowData = c;
                        else if(c.getTag().equals("actions"))
                                updateActionMap(c);
                        else if(c.getTag().equals("visiblecolumns"))
                                updateVisibleColumns(c);
                }
-               updateHeader(columnInfo);
+               updateHeader(uidl.getStringArrayAttribute("vcolorder"));
                
                if(initializedAndAttached) {
                        updateBody(rowData, uidl.getIntAttribute("firstrow"),uidl.getIntAttribute("rows"));
@@ -169,27 +169,37 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
                                        uidl.getIntAttribute("rows"), 
                                        totalRows);
                        bodyContainer.add(tBody);
-                       initializedAndAttached = true;
+                       initialContentReceived  = true;
+                       if(isAttached()) {
+                               sizeInit();
+                       }
                }
        }
        
-       private void updateVisibleColumns(UIDL c) {
+       private void updateVisibleColumns(UIDL uidl) {
                if(!initializedAndAttached) {
                        // add empty cell for col headers
                        tHead.addAvailableCell(new RowHeadersHeaderCell());
-                       Iterator it = c.getChildIterator();
+                       Iterator it = uidl.getChildIterator();
                        while(it.hasNext()) {
                                UIDL col = (UIDL) it.next();
-                                       tHead.addAvailableCell(
-                                               new HeaderCell(
-                                                               col.getStringAttribute("cid"),
-                                                               col.getStringAttribute("caption")
-                                                       )
-                                               );
+                               String cid = col.getStringAttribute("cid");
+                               HeaderCell c = new HeaderCell(
+                                               cid,
+                                               col.getStringAttribute("caption")
+                                       );
+                               tHead.addAvailableCell(c);
+                               if(col.hasAttribute("sortable")) {
+                                       c.setSortable(true);
+                                       if(cid.equals(sortColumn))
+                                               c.setSorted(true);
+                                       else
+                                               c.setSorted(false);
+                               }
+                               // TODO icon, align, width
                        }
                } else {
-                       // update existing cells
-                       
+                       // TODO update existing cells (matters if server changes captions)
                }
                                        
        }
@@ -227,12 +237,12 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
        }
        
 
-       private void updateHeader(UIDL uidl) {
-               if(uidl == null)
+       private void updateHeader(String[] strings) {
+               if(strings == null)
                        return;
 
+               int visibleCols = strings.length;
                int colIndex = 0;
-               int visibleCols = uidl.getChidlCount();
                if(rowHeaders) {
                        tHead.enableColumn("0",colIndex);
                        visibleCols++;
@@ -242,24 +252,15 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
                } else {
                        visibleColOrder = new String[visibleCols];
                }
-                       
-               for(Iterator it = uidl.getChildIterator();it.hasNext();) {
-                       UIDL col = (UIDL) it.next();
-                       String cid = col.getStringAttribute("cid");
+
+               for (int i = 0; i < strings.length; i++) {
+                       String cid = strings[i];
                        visibleColOrder[colIndex] = cid;
-                       
                        tHead.enableColumn(cid, colIndex);
-                       
-                       if(col.hasAttribute("sortable")) {
-                               HeaderCell c = tHead.getHeaderCell(cid);
-                               c.setSortable(true);
-                               if(cid.equals(sortColumn))
-                                       c.setSorted(true);
-                               else
-                                       c.setSorted(false);
-                       }
                        colIndex++;
+                       
                }
+
        }
        
        /**
@@ -394,12 +395,30 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
        }
 
        protected void onAttach() {
-               
                super.onAttach();
-               
-               // sync column widths
-               initColumnWidths();
+               if(initialContentReceived) {
+                       sizeInit();
+               }
+       }
 
+       /**
+        * Run only once when component is attached and received its initial
+        * content. This function :
+        *  * Syncs headers and bodys "natural widths and saves the values.
+        *  * Sets proper width and height
+        *  * Makes deferred request to get some cache rows
+       */
+       private void sizeInit() {
+               Iterator headCells = tHead.iterator();
+               int i = 0;
+               while(headCells.hasNext()) {
+                       Element hCell = ((HeaderCell) headCells.next()).getElement();
+                       int hw = DOM.getIntAttribute(hCell, "offsetWidth");
+                       int cw = tBody.getColWidth(i);
+                       int w = (hw > cw ? hw : cw) + IScrollTableBody.CELL_EXTRA_WIDTH;
+                       setColWidth(i , w);
+                       i++;
+               }
                if(height  < 0) {
                        bodyContainer.setHeight((tBody.getRowHeight()*pageLength) + "px");
                } else {
@@ -429,25 +448,7 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
                                }
                        }
                });
-
-               
-       }
-
-       /**
-        * Run when receices its initial content. Syncs headers and bodys
-        * "natural widths and saves the values.
-       */
-       private void initColumnWidths() {
-               Iterator headCells = tHead.iterator();
-               int i = 0;
-               while(headCells.hasNext()) {
-                       Element hCell = ((HeaderCell) headCells.next()).getElement();
-                       int hw = DOM.getIntAttribute(hCell, "offsetWidth");
-                       int cw = tBody.getColWidth(i);
-                       int w = (hw > cw ? hw : cw) + IScrollTableBody.CELL_EXTRA_WIDTH;
-                       setColWidth(i , w);
-                       i++;
-               }
+               initializedAndAttached = true;
        }
 
        private int getScrollBarWidth() {
@@ -691,7 +692,7 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
                        DOM.setStyleAttribute(floatingCopyOfHeaderCell, "opacity", "0.5");
                        DOM.setStyleAttribute(floatingCopyOfHeaderCell, "filter", "alpha(opacity=100)");
                        updateFloatingCopysPosition(DOM.getAbsoluteLeft(td), DOM.getAbsoluteTop(td));
-                       DOM.appendChild(IScrollTable.this.getElement(), floatingCopyOfHeaderCell);
+                       DOM.appendChild(RootPanel.get().getElement(), floatingCopyOfHeaderCell);
                }
                
                private void updateFloatingCopysPosition(int x, int y) {
@@ -702,7 +703,7 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
                }
                
                private void hideFloatingCopy() {
-                       DOM.removeChild(IScrollTable.this.getElement(), floatingCopyOfHeaderCell);
+                       DOM.removeChild(RootPanel.get().getElement(), floatingCopyOfHeaderCell);
                        floatingCopyOfHeaderCell = null;
                }
                
index b76026010e93edb45777bfe11dab9d81d817a110..912733cc645c2848be721977a79b6626cdd85ec8 100644 (file)
@@ -115,36 +115,38 @@ public class ITablePaging extends Composite implements ITable, Paintable, ClickL
                if(uidl.hasAttribute("rowheaders"))
                        rowHeaders = true;
                
-               UIDL columnInfo = null;
                UIDL rowData = null;
+               UIDL visibleColumns = null;
                for(Iterator it = uidl.getChildIterator(); it.hasNext();) {
                        UIDL c = (UIDL) it.next();
-                       if(c.getTag().equals("cols"))
-                               columnInfo = c;
-                       else if(c.getTag().equals("rows"))
+                       if(c.getTag().equals("rows"))
                                rowData = c;
                        else if(c.getTag().equals("actions"))
                                updateActionMap(c);
                        else if(c.getTag().equals("visiblecolumns"))
-                               updateVisibleColumns(c);
+                               visibleColumns = c;
                }
-               tBody.resize(rows+1, columnInfo.getChidlCount() + (rowHeaders ? 1 : 0 ));
-
-               updateHeader(columnInfo);
-               
+               tBody.resize(rows+1, uidl.getIntAttribute("cols") + (rowHeaders ? 1 : 0 ));
+               updateHeader(visibleColumns);
                updateBody(rowData);
                
                updatePager();
        }
        
-       private void updateVisibleColumns(UIDL c) {
+       private void updateHeader(UIDL c) {
                Iterator it = c.getChildIterator();
-               int count = 0;
                visibleColumns.clear();
+               int colIndex = (rowHeaders ? 1 : 0);
                while(it.hasNext()) {
-                       count++;
                        UIDL col = (UIDL) it.next();
-                       visibleColumns.put(col.getStringAttribute("cid"), col.getStringAttribute("caption"));
+                       String cid = col.getStringAttribute("cid");
+                       if(!col.hasAttribute("collapsed")) {
+                               tBody.setWidget(0, colIndex, 
+                                               new HeaderCell(cid, 
+                                                               col.getStringAttribute("caption")));
+
+                       }
+                       colIndex++;
                }
        }
 
@@ -153,19 +155,6 @@ public class ITablePaging extends Composite implements ITable, Paintable, ClickL
                
        }
 
-       private void updateHeader(UIDL uidl) {
-               if(uidl == null)
-                       return;
-               int colIndex = (rowHeaders ? 1 : 0);
-
-               for(Iterator it = uidl.getChildIterator();it.hasNext();) {
-                       UIDL col = (UIDL) it.next();
-                       String cid = col.getStringAttribute("cid");
-                       tBody.setWidget(0, colIndex, new HeaderCell(cid, col.getStringAttribute("caption")));
-                       colIndex++;
-               }
-       }
-       
        /**
         * Updates row data from uidl. UpdateFromUIDL delegates updating 
         * tBody to this method.
index c1fdd0586ee51bcc1ee84a96d1708e3850a0d160..714f167ec58ed63a2091c2644f5983ba61dfab2b 100644 (file)
@@ -1443,33 +1443,16 @@ public class Table extends Select implements Action.Container,
                if (rowheads)
                        target.addAttribute("rowheaders", true);
 
-               // Columns
-               target.startTag("cols");
+               // Visible column order
                Collection sortables = getSortableContainerPropertyIds();
+               ArrayList visibleColOrder = new ArrayList();
                for (Iterator it = this.visibleColumns.iterator(); it.hasNext();) {
                        Object columnId = it.next();
                        if (!isColumnCollapsed(columnId)) {
-                               target.startTag("ch");
-                               if (colheads) {
-                                       if (this.getColumnIcon(columnId) != null)
-                                               target.addAttribute("icon", this
-                                                               .getColumnIcon(columnId));
-                                       if (sortables.contains(columnId))
-                                               target.addAttribute("sortable", true);
-                                       String header = (String) this.getColumnHeader(columnId);
-                                       target.addAttribute("caption", (header != null ? header
-                                                       : ""));
-                               }
-                               target.addAttribute("cid", this.columnIdMap.key(columnId));
-                               if (!ALIGN_LEFT.equals(this.getColumnAlignment(columnId)))
-                                       target.addAttribute("align", this
-                                                       .getColumnAlignment(columnId));
-                               if(getColumnWidth(columnId) > -1)
-                                       target.addAttribute("width", String.valueOf(getColumnWidth(columnId)));
-                               target.endTag("ch");
+                               visibleColOrder.add(this.columnIdMap.key(columnId));
                        }
                }
-               target.endTag("cols");
+               target.addAttribute("vcolorder", visibleColOrder.toArray());
 
                // Rows
                Set actionSet = new LinkedHashSet();
@@ -1611,23 +1594,36 @@ public class Table extends Select implements Action.Container,
                                }
                        }
                        target.addVariable(this, "collapsedcolumns", collapsedkeys);
-                       target.startTag("visiblecolumns");
-                       int i = 0;
-                       for (Iterator it = this.visibleColumns.iterator(); it.hasNext(); i++) {
-                               Object columnId = it.next();
-                               if (columnId != null) {
-                                       target.startTag("column");
-                                       target.addAttribute("cid", this.columnIdMap.key(columnId));
-                                       String head = getColumnHeader(columnId);
-                                       target.addAttribute("caption", (head != null ? head : ""));
-                                       if (this.isColumnCollapsed(columnId)) {
-                                               target.addAttribute("collapsed", true);
-                                       }
-                                       target.endTag("column");
+               }
+               target.startTag("visiblecolumns");
+               int i = 0;
+               for (Iterator it = this.visibleColumns.iterator(); it.hasNext(); i++) {
+                       Object columnId = it.next();
+                       if (columnId != null) {
+                               target.startTag("column");
+                               target.addAttribute("cid", this.columnIdMap.key(columnId));
+                               String head = getColumnHeader(columnId);
+                               target.addAttribute("caption", (head != null ? head : ""));
+                               if (this.isColumnCollapsed(columnId)) {
+                                       target.addAttribute("collapsed", true);
                                }
+                               if (colheads) {
+                                       if (this.getColumnIcon(columnId) != null)
+                                               target.addAttribute("icon", this
+                                                               .getColumnIcon(columnId));
+                                       if (sortables.contains(columnId))
+                                               target.addAttribute("sortable", true);
+                               }
+                               if (!ALIGN_LEFT.equals(this.getColumnAlignment(columnId)))
+                                       target.addAttribute("align", this
+                                                       .getColumnAlignment(columnId));
+                               if(getColumnWidth(columnId) > -1)
+                                       target.addAttribute("width", String.valueOf(getColumnWidth(columnId)));
+                       
+                               target.endTag("column");
                        }
-                       target.endTag("visiblecolumns");
                }
+               target.endTag("visiblecolumns");
        }
 
        /**