From 4b6bf302b620ddc8edfeb1e0e29204076c282708 Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Fri, 13 Jul 2007 08:33:24 +0000 Subject: [PATCH] Optimized Table uidl, fixed IScrollTable when attaching before initial uidl update, made IScrollTable default table component svn changeset:1852/svn branch:trunk --- .../gwt/client/DefaultWidgetFactory.java | 6 +- .../terminal/gwt/client/ui/IScrollTable.java | 127 +++++++++--------- .../terminal/gwt/client/ui/ITablePaging.java | 41 +++--- src/com/itmill/toolkit/ui/Table.java | 66 +++++---- 4 files changed, 113 insertions(+), 127 deletions(-) diff --git a/src/com/itmill/toolkit/terminal/gwt/client/DefaultWidgetFactory.java b/src/com/itmill/toolkit/terminal/gwt/client/DefaultWidgetFactory.java index aab422574d..ba124ef6b8 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/DefaultWidgetFactory.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/DefaultWidgetFactory.java @@ -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")) diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/IScrollTable.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/IScrollTable.java index 8711cc83f8..1b521837a3 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/ui/IScrollTable.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/IScrollTable.java @@ -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; } diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/ITablePaging.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/ITablePaging.java index b76026010e..912733cc64 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/ui/ITablePaging.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/ITablePaging.java @@ -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. diff --git a/src/com/itmill/toolkit/ui/Table.java b/src/com/itmill/toolkit/ui/Table.java index c1fdd0586e..714f167ec5 100644 --- a/src/com/itmill/toolkit/ui/Table.java +++ b/src/com/itmill/toolkit/ui/Table.java @@ -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"); } /** -- 2.39.5