]> source.dussan.org Git - vaadin-framework.git/commitdiff
Added method setContainerDataSource(Container newDataSource, 80/480/3
authorAnna Koskinen <anna@vaadin.com>
Mon, 17 Dec 2012 10:19:31 +0000 (12:19 +0200)
committerAnna Koskinen <anna@vaadin.com>
Mon, 17 Dec 2012 10:19:31 +0000 (12:19 +0200)
Collection<?> visibleIds) to Table. (#10419)

Change-Id: Ib10f430c786b1d0130f86eb5a97e271ac71e806f

server/src/com/vaadin/ui/Table.java
uitest/src/com/vaadin/tests/components/table/SetDataSourceWithPropertyIds.html [new file with mode: 0644]
uitest/src/com/vaadin/tests/components/table/SetDataSourceWithPropertyIds.java [new file with mode: 0644]

index c0814b84818badd5ab68d905aa2d1bd5a043b14d..65dea7594b4c2492bf056e3851767011a01f3240 100644 (file)
@@ -2070,7 +2070,7 @@ public class Table extends AbstractSelect implements Action.Container,
                     value = generatedRow.getText()[j];
                 }
             } else {
-                // check in current pageBuffer already has row
+                // check if current pageBuffer already has row
                 int index = firstIndex + i;
                 if (p != null || isGenerated) {
                     int indexInOldBuffer = index - pageBufferFirstIndex;
@@ -2395,14 +2395,69 @@ public class Table extends AbstractSelect implements Action.Container,
         refreshRenderedCells();
     }
 
+    /**
+     * Sets the Container that serves as the data source of the viewer. As a
+     * side-effect the table's selection value is set to null as the old
+     * selection might not exist in new Container.<br>
+     * <br>
+     * All rows and columns are generated as visible using this method. If the
+     * new container contains properties that are not meant to be shown you
+     * should use {@link Table#setContainerDataSource(Container, Collection)}
+     * instead, especially if the table is editable.
+     * 
+     * @param newDataSource
+     *            the new data source.
+     */
     @Override
     public void setContainerDataSource(Container newDataSource) {
+        if (newDataSource == null) {
+            newDataSource = new IndexedContainer();
+        }
+        Collection<Object> generated;
+        if (columnGenerators != null) {
+            generated = columnGenerators.keySet();
+        } else {
+            generated = Collections.emptyList();
+        }
+        List<Object> visibleIds = new ArrayList<Object>();
+        if (generated.isEmpty()) {
+            visibleIds.addAll(newDataSource.getContainerPropertyIds());
+        } else {
+            for (Object id : newDataSource.getContainerPropertyIds()) {
+                // don't add duplicates
+                if (!generated.contains(id)) {
+                    visibleIds.add(id);
+                }
+            }
+            // generated columns to the end
+            visibleIds.addAll(generated);
+        }
+        setContainerDataSource(newDataSource, visibleIds);
+    }
+
+    /**
+     * Sets the container data source and the columns that will be visible.
+     * Columns are shown in the collection's iteration order.
+     * 
+     * @see Table#setContainerDataSource(Container)
+     * @see Table#setVisibleColumns(Object[])
+     * 
+     * @param newDataSource
+     *            the new data source.
+     * @param visibleIds
+     *            IDs of the visible columns
+     */
+    public void setContainerDataSource(Container newDataSource,
+            Collection<?> visibleIds) {
 
         disableContentRefreshing();
 
         if (newDataSource == null) {
             newDataSource = new IndexedContainer();
         }
+        if (visibleIds == null) {
+            visibleIds = new ArrayList<Object>();
+        }
 
         // Assures that the data source is ordered by making unordered
         // containers ordered by wrapping them
@@ -2422,19 +2477,14 @@ public class Table extends AbstractSelect implements Action.Container,
             collapsedColumns.clear();
         }
 
-        // columnGenerators 'override' properties, don't add the same id twice
+        // don't add the same id twice
         Collection<Object> col = new LinkedList<Object>();
-        for (Iterator<?> it = getContainerPropertyIds().iterator(); it
-                .hasNext();) {
+        for (Iterator<?> it = visibleIds.iterator(); it.hasNext();) {
             Object id = it.next();
-            if (columnGenerators == null || !columnGenerators.containsKey(id)) {
+            if (!col.contains(id)) {
                 col.add(id);
             }
         }
-        // generators added last
-        if (columnGenerators != null && columnGenerators.size() > 0) {
-            col.addAll(columnGenerators.keySet());
-        }
 
         setVisibleColumns(col.toArray());
 
diff --git a/uitest/src/com/vaadin/tests/components/table/SetDataSourceWithPropertyIds.html b/uitest/src/com/vaadin/tests/components/table/SetDataSourceWithPropertyIds.html
new file mode 100644 (file)
index 0000000..67964d9
--- /dev/null
@@ -0,0 +1,91 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head profile="http://selenium-ide.openqa.org/profiles/test-case">
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+<link rel="selenium.base" href="" />
+<title>SetDataSourceWithPropertyIds</title>
+</head>
+<body>
+<table cellpadding="1" cellspacing="1" border="1">
+<thead>
+<tr><td rowspan="1" colspan="3">SetDataSourceWithPropertyIds</td></tr>
+</thead><tbody>
+<tr>
+       <td>open</td>
+       <td>/run/com.vaadin.tests.components.table.SetDataSourceWithPropertyIds?restartApplication</td>
+       <td></td>
+</tr>
+<tr>
+       <td>assertText</td>
+       <td>label</td>
+       <td>no ConversionException</td>
+</tr>
+<tr>
+       <td>assertElementPresent</td>
+       <td>vaadin=runcomvaadintestscomponentstableSetDataSourceWithPropertyIds::PID_Stable/FocusableScrollPanel[0]/VScrollTable$VScrollTableBody[0]/VScrollTable$VScrollTableBody$VScrollTableRow[0]/VTextField[0]</td>
+       <td></td>
+</tr>
+<tr>
+       <td>click</td>
+       <td>vaadin=runcomvaadintestscomponentstableSetDataSourceWithPropertyIds::PID_Sbutton/domChild[0]/domChild[0]</td>
+       <td></td>
+</tr>
+<tr>
+       <td>assertText</td>
+       <td>label</td>
+       <td>no ConversionException</td>
+</tr>
+<tr>
+       <td>assertElementNotPresent</td>
+       <td>vaadin=runcomvaadintestscomponentstableSetDataSourceWithPropertyIds::PID_Stable/FocusableScrollPanel[0]/VScrollTable$VScrollTableBody[0]/VScrollTable$VScrollTableBody$VScrollTableRow[0]/VTextField[0]</td>
+       <td></td>
+</tr>
+<tr>
+       <td>click</td>
+       <td>vaadin=runcomvaadintestscomponentstableSetDataSourceWithPropertyIds::PID_Sbutton/domChild[0]/domChild[0]</td>
+       <td></td>
+</tr>
+<tr>
+       <td>assertText</td>
+       <td>label</td>
+       <td>ConversionException caught</td>
+</tr>
+<tr>
+       <td>assertElementPresent</td>
+       <td>vaadin=runcomvaadintestscomponentstableSetDataSourceWithPropertyIds::PID_Stable/FocusableScrollPanel[0]/VScrollTable$VScrollTableBody[0]/VScrollTable$VScrollTableBody$VScrollTableRow[0]/VTextField[0]</td>
+       <td></td>
+</tr>
+<tr>
+       <td>click</td>
+       <td>vaadin=runcomvaadintestscomponentstableSetDataSourceWithPropertyIds::PID_Sbutton/domChild[0]/domChild[0]</td>
+       <td></td>
+</tr>
+<tr>
+       <td>assertText</td>
+       <td>label</td>
+       <td>no ConversionException</td>
+</tr>
+<tr>
+       <td>assertElementNotPresent</td>
+       <td>vaadin=runcomvaadintestscomponentstableSetDataSourceWithPropertyIds::PID_Stable/FocusableScrollPanel[0]/VScrollTable$VScrollTableBody[0]/VScrollTable$VScrollTableBody$VScrollTableRow[0]/VTextField[0]</td>
+       <td></td>
+</tr>
+<tr>
+       <td>click</td>
+       <td>vaadin=runcomvaadintestscomponentstableSetDataSourceWithPropertyIds::PID_Sbutton/domChild[0]/domChild[0]</td>
+       <td></td>
+</tr>
+<tr>
+       <td>assertText</td>
+       <td>label</td>
+       <td>ConversionException caught</td>
+</tr>
+<tr>
+       <td>assertElementPresent</td>
+       <td>vaadin=runcomvaadintestscomponentstableSetDataSourceWithPropertyIds::PID_Stable/FocusableScrollPanel[0]/VScrollTable$VScrollTableBody[0]/VScrollTable$VScrollTableBody$VScrollTableRow[0]/VTextField[0]</td>
+       <td></td>
+</tr>
+</tbody></table>
+</body>
+</html>
diff --git a/uitest/src/com/vaadin/tests/components/table/SetDataSourceWithPropertyIds.java b/uitest/src/com/vaadin/tests/components/table/SetDataSourceWithPropertyIds.java
new file mode 100644 (file)
index 0000000..dbdd810
--- /dev/null
@@ -0,0 +1,174 @@
+package com.vaadin.tests.components.table;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+import com.vaadin.data.util.BeanItemContainer;
+import com.vaadin.data.util.converter.Converter.ConversionException;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.shared.ui.MarginInfo;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.Table;
+
+public class SetDataSourceWithPropertyIds extends AbstractTestUI {
+
+    @Override
+    protected String getTestDescription() {
+        return "It should be possible to set a dataSource without generating columns that were never intended to be visible.<br>"
+                + "First initialization happens before the table is attached.";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 10419;
+    }
+
+    private static final String TABLE_NAME = "JOBS";
+    private static final String[] PK_COLUMN_NAMES = new String[] { "JOB_ID" };
+    private static final String SEQUENCE_NAME = "";
+    private static final String VERSION_COLUMN_NAME = "";
+
+    Table table = new Table();
+    BeanItemContainer<JobsBean> jobContainer = new BeanItemContainer(
+            JobsBean.class);
+    Label label = new Label();
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        getLayout().setSpacing(true);
+        getLayout().setMargin(new MarginInfo(true, false, false, false));
+
+        Button button = new Button("Toggle editability");
+        button.setId("button");
+        button.addClickListener(new Button.ClickListener() {
+            @Override
+            public void buttonClick(Button.ClickEvent clickEvent) {
+                refreshTable();
+            }
+        });
+
+        label.setSizeUndefined();
+        label.setId("label");
+
+        table.setId("table");
+        refreshTable();
+
+        addComponent(button);
+        addComponent(label);
+        addComponent(table);
+    }
+
+    private void refreshTable() {
+        // error only occurs when table is editable and already attached
+        table.setEditable(table.getParent() == null || !table.isEditable());
+
+        jobContainer.removeAllItems();
+        jobContainer.addAll(getBeanList());
+        try {
+            table.setContainerDataSource(jobContainer);
+            table.setVisibleColumns(new String[] { "jobId" });
+            label.setValue("no ConversionException");
+        } catch (ConversionException e) {
+            ArrayList<String> propertyIds = new ArrayList<String>();
+            propertyIds.add("jobId");
+            table.setContainerDataSource(jobContainer, propertyIds);
+            label.setValue("ConversionException caught");
+        }
+    }
+
+    private List<JobsBean> getBeanList() {
+
+        List<JobsBean> list = new ArrayList<JobsBean>();
+        JobsBean jobsBean = new JobsBean();
+        jobsBean.setJobId("1");
+        list.add(jobsBean);
+        return list;
+    }
+
+    public class JobsBean<T> implements Serializable {
+
+        private static final long serialVersionUID = 1932918476339138393L;
+        protected String jobId;
+        protected String jobTitle;
+        protected Long minSalary;
+        protected Long maxSalary;
+        private T auxiliaryData;
+
+        public T getAuxiliaryData() {
+            return auxiliaryData;
+        }
+
+        public void setAuxiliaryData(final T pAuxiliaryData) {
+            auxiliaryData = pAuxiliaryData;
+        }
+
+        public String getTableName() {
+            return TABLE_NAME;
+        }
+
+        public String[] getPrimaryKeyColumnNames() {
+            return PK_COLUMN_NAMES;
+        }
+
+        public String getSequenceName() {
+            return SEQUENCE_NAME;
+        }
+
+        public String getVersionColumnName() {
+            return VERSION_COLUMN_NAME;
+        }
+
+        public String getJobId() {
+            return jobId;
+        }
+
+        public void setJobId(final String pJobId) {
+            jobId = pJobId;
+        }
+
+        public String getJobTitle() {
+            return jobTitle;
+        }
+
+        public void setJobTitle(final String pJobTitle) {
+            jobTitle = pJobTitle;
+        }
+
+        public Long getMinSalary() {
+            return minSalary;
+        }
+
+        public void setMinSalary(final Long pMinSalary) {
+            minSalary = pMinSalary;
+        }
+
+        public Long getMaxSalary() {
+            return maxSalary;
+        }
+
+        public void setMaxSalary(final Long pMaxSalary) {
+            maxSalary = pMaxSalary;
+        }
+
+        @Override
+        public boolean equals(Object pObject) {
+            if (this == pObject) {
+                return true;
+            }
+            if (!(pObject instanceof JobsBean)) {
+                return false;
+            }
+            JobsBean other = (JobsBean) pObject;
+            return getJobId().equals(other.getJobId());
+        }
+
+        @Override
+        public int hashCode() {
+            return getJobId().hashCode();
+        }
+    }
+
+}