]> source.dussan.org Git - vaadin-framework.git/commitdiff
Row Generator fixes (#12027)
authorAndrey B. Panfilov <andrew@panfilov.tel>
Wed, 12 Aug 2020 12:20:02 +0000 (15:20 +0300)
committerGitHub <noreply@github.com>
Wed, 12 Aug 2020 12:20:02 +0000 (15:20 +0300)
NOTE: components within row generator are not an officially supported feature and can cause issues within your application.

compatibility-client/src/main/java/com/vaadin/v7/client/ui/VScrollTable.java
compatibility-server/src/main/java/com/vaadin/v7/ui/Table.java

index c4294009d27befc0cf9ed4962ce4f39d3fe06c63..200d44f6c48e54dd893102f4233afabc57df3841 100644 (file)
@@ -6735,7 +6735,8 @@ public class VScrollTable extends FlowPanel
                             addSpannedCell(uidl, cell.toString(), aligns[0], "",
                                     htmlContentAllowed, false, null, colCount);
                         } else {
-                            addSpannedCell(uidl, (Widget) cell, aligns[0], "",
+                            final ComponentConnector cellContent = client.getPaintable((UIDL) cell);
+                            addSpannedCell(uidl, cellContent.getWidget(), aligns[0], "",
                                     false, colCount);
                         }
                         break;
index 2cd3ad075ead9f950d8dacf06d838778b07c0656..872f70feae54f38000240a43f12b6b8dfe175fdb 100644 (file)
@@ -2282,20 +2282,33 @@ public class Table extends AbstractSelect implements Action.Container,
             }
         }
 
-        GeneratedRow generatedRow = rowGenerator != null
-                ? rowGenerator.generateRow(this, id)
-                : null;
-        cells[CELL_GENERATED_ROW][i] = generatedRow;
+        int index = firstIndex + i;
+        int indexInOldBuffer = index - pageBufferFirstIndex;
+        boolean inPageBuffer = index < firstIndexNotInCache
+                && index >= pageBufferFirstIndex
+                && id.equals(pageBuffer[CELL_ITEMID][indexInOldBuffer]);
+
+        GeneratedRow generatedRow = null;
+        if (rowGenerator != null) {
+            if (inPageBuffer) {
+                generatedRow = (GeneratedRow) pageBuffer[CELL_GENERATED_ROW][indexInOldBuffer];
+            } else {
+                generatedRow = rowGenerator.generateRow(this, cells[CELL_ITEMID][i]);
+            }
+            cells[CELL_GENERATED_ROW][i] = generatedRow;
+        }
 
+        int firstNotCollapsed = -1;
         for (int j = 0; j < cols; j++) {
             if (isColumnCollapsed(colids[j])) {
                 continue;
+            } else if (firstNotCollapsed == -1) {
+                firstNotCollapsed = j;
             }
             Property<?> p = null;
             Object value = "";
-            boolean isGeneratedRow = generatedRow != null;
             boolean isGeneratedColumn = columnGenerators.containsKey(colids[j]);
-            boolean isGenerated = isGeneratedRow || isGeneratedColumn;
+            boolean isGenerated = isGeneratedColumn || generatedRow != null;
 
             if (!isGenerated) {
                 try {
@@ -2306,33 +2319,24 @@ public class Table extends AbstractSelect implements Action.Container,
                 }
             }
 
-            if (isGeneratedRow) {
-                if (generatedRow.isSpanColumns() && j > 0) {
-                    value = null;
-                } else if (generatedRow.isSpanColumns() && j == 0
-                        && generatedRow.getValue() instanceof Component) {
-                    value = generatedRow.getValue();
-                } else if (generatedRow.getText().length > j) {
-                    value = generatedRow.getText()[j];
-                }
-            } else {
-                // check if current pageBuffer already has row
-                int index = firstIndex + i;
-                if (p != null || isGenerated) {
-                    int indexInOldBuffer = index - pageBufferFirstIndex;
-                    if (index < firstIndexNotInCache
-                            && index >= pageBufferFirstIndex
-                            && pageBuffer[CELL_GENERATED_ROW][indexInOldBuffer] == null
-                            && id.equals(
-                                    pageBuffer[CELL_ITEMID][indexInOldBuffer])) {
-                        // we already have data in our cache,
-                        // recycle it instead of fetching it via
-                        // getValue/getPropertyValue
+            // check if current pageBuffer already has row
+            if (p != null || isGenerated) {
+                if (inPageBuffer) {
+                    // we already have data in our cache,
+                    // recycle it instead of fetching it via
+                    // getValue/getPropertyValue
+                    if (generatedRow != null) {
+                        value = extractGeneratedValue(generatedRow, j, j == firstNotCollapsed);
+                    } else {
                         value = pageBuffer[CELL_FIRSTCOL + j][indexInOldBuffer];
-                        if (!isGeneratedColumn && iscomponent[j]
-                                || !(value instanceof Component)) {
-                            listenProperty(p, oldListenedProperties);
-                        }
+                    }
+                    if (!isGeneratedColumn && iscomponent[j]
+                            || !(value instanceof Component)) {
+                        listenProperty(p, oldListenedProperties);
+                    }
+                } else {
+                    if (generatedRow != null) {
+                        value = extractGeneratedValue(generatedRow, j, j == firstNotCollapsed);
                     } else {
                         if (isGeneratedColumn) {
                             ColumnGenerator cg = columnGenerators
@@ -2395,6 +2399,35 @@ public class Table extends AbstractSelect implements Action.Container,
         }
     }
 
+    /**
+     * Extracts cell value from generated row
+     *
+     * @param generatedRow generated row
+     * @param index column index
+     * @param firstVisibleColumn whether the column is first visible column in the table (i.e. previous columns are hidden)
+     * @return cell value
+     */
+    private Object extractGeneratedValue(GeneratedRow generatedRow, int index, boolean firstVisibleColumn) {
+        Object value = generatedRow.getValue();
+        String[] text = generatedRow.getText();
+        if (generatedRow.isSpanColumns()) {
+            if (firstVisibleColumn) {
+                if (value instanceof Component) {
+                    return value;
+                }
+                if (text != null && text.length > 0) {
+                    return text[0];
+                }
+            }
+            return null;
+        }
+
+        if (text != null && text.length > index) {
+            return text[index];
+        }
+        return null;
+    }
+
     protected void registerComponent(Component component) {
         getLogger().log(Level.FINEST, "Registered {0}: {1}", new Object[] {
                 component.getClass().getSimpleName(), component.getCaption() });
@@ -3990,8 +4023,9 @@ public class Table extends AbstractSelect implements Action.Container,
             target.addAttribute("gen_html",
                     generatedRow.isHtmlContentAllowed());
             target.addAttribute("gen_span", generatedRow.isSpanColumns());
+            // todo: actually gen_widget is never used
             target.addAttribute("gen_widget",
-                    generatedRow.getValue() instanceof Component);
+                    cells[CELL_FIRSTCOL][indexInRowBuffer] instanceof Component);
         }
     }