From d2d4279e9946f9b193c0b706ecad9e01a5bf77c3 Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Wed, 13 Mar 2013 09:50:41 +0200 Subject: Fixed setting column width back to undefined by setting it to -1. #7922 svn changeset:25591/svn branch:6.8 Conflicts: server/src/com/vaadin/ui/Table.java Reverted change included by mistake in changeset [25591] for #7922 svn changeset:25606/svn branch:6.8 Change-Id: I6765a52ab213c131cca053227ee72b0007552f7f --- client/src/com/vaadin/client/ui/VScrollTable.java | 24 +++++-- server/src/com/vaadin/ui/Table.java | 71 ++++++++++++------ .../table/TableColumnWidthsAndExpandRatios.html | 83 ++++++++++++++++++++++ .../table/TableColumnWidthsAndExpandRatios.java | 57 +++++++++++++++ 4 files changed, 208 insertions(+), 27 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/components/table/TableColumnWidthsAndExpandRatios.html create mode 100644 uitest/src/com/vaadin/tests/components/table/TableColumnWidthsAndExpandRatios.java diff --git a/client/src/com/vaadin/client/ui/VScrollTable.java b/client/src/com/vaadin/client/ui/VScrollTable.java index 6f9fd6da88..4d61fba429 100644 --- a/client/src/com/vaadin/client/ui/VScrollTable.java +++ b/client/src/com/vaadin/client/ui/VScrollTable.java @@ -3056,7 +3056,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, .hasNext(); columnIndex++) { if (it.next() == this) { break; - } + } } } final int cw = scrollBody.getColWidth(columnIndex); @@ -3266,10 +3266,9 @@ public class VScrollTable extends FlowPanel implements HasWidgets, } if (col.hasAttribute("width")) { - final String widthStr = col.getStringAttribute("width"); // Make sure to accomodate for the sort indicator if // necessary. - int width = Integer.parseInt(widthStr); + int width = col.getIntAttribute("width"); int widthWithoutAddedIndent = width; // get min width with indent, no padding @@ -3301,12 +3300,25 @@ public class VScrollTable extends FlowPanel implements HasWidgets, // save min width without indent c.setWidth(widthWithoutAddedIndent, true); } + } else if (col.hasAttribute("er")) { + c.setExpandRatio(col.getFloatAttribute("er")); + } else if (recalcWidths) { c.setUndefinedWidth(); + + } else { + boolean hadExpandRatio = c.getExpandRatio() > 0; + boolean hadDefinedWidth = c.isDefinedWidth(); + if (hadExpandRatio || hadDefinedWidth) { + // Someone has removed a expand width or the defined + // width on the server side (setting it to -1), make the + // column undefined again and measure columns again. + c.setUndefinedWidth(); + c.setExpandRatio(0); + refreshContentWidths = true; + } } - if (col.hasAttribute("er")) { - c.setExpandRatio(col.getFloatAttribute("er")); - } + if (col.hasAttribute("collapsed")) { // ensure header is properly removed from parent (case when // collapsing happens via servers side api) diff --git a/server/src/com/vaadin/ui/Table.java b/server/src/com/vaadin/ui/Table.java index 641c0ff1f6..3d7cb42050 100644 --- a/server/src/com/vaadin/ui/Table.java +++ b/server/src/com/vaadin/ui/Table.java @@ -396,10 +396,14 @@ public class Table extends AbstractSelect implements Action.Container, private HashMap columnAlignments = new HashMap(); /** - * Holds column widths in pixels (Integer) or expand ratios (Float) for - * visible columns (by propertyId). + * Holds column widths in pixels for visible columns (by propertyId). */ - private final HashMap columnWidths = new HashMap(); + private final HashMap columnWidths = new HashMap(); + + /** + * Holds column expand rations for visible columns (by propertyId). + */ + private final HashMap columnExpandRatios = new HashMap(); /** * Holds column generators @@ -886,10 +890,14 @@ public class Table extends AbstractSelect implements Action.Container, // id to store the width of the row header. propertyId = ROW_HEADER_FAKE_PROPERTY_ID; } + + // Setting column width should remove any expand ratios as well + columnExpandRatios.remove(propertyId); + if (width < 0) { columnWidths.remove(propertyId); } else { - columnWidths.put(propertyId, Integer.valueOf(width)); + columnWidths.put(propertyId, width); } markAsDirty(); } @@ -930,21 +938,39 @@ public class Table extends AbstractSelect implements Action.Container, * the expandRatio used to divide excess space for this column */ public void setColumnExpandRatio(Object propertyId, float expandRatio) { + if (propertyId == null) { + // Since propertyId is null, this is the row header. Use the magic + // id to store the width of the row header. + propertyId = ROW_HEADER_FAKE_PROPERTY_ID; + } + + // Setting the column expand ratio should remove and defined column + // width + columnWidths.remove(propertyId); + if (expandRatio < 0) { - columnWidths.remove(propertyId); + columnExpandRatios.remove(propertyId); } else { - columnWidths.put(propertyId, new Float(expandRatio)); + columnExpandRatios.put(propertyId, expandRatio); } + + requestRepaint(); } + /** + * Gets the column expand ratio for a columnd. See + * {@link #setColumnExpandRatio(Object, float)} + * + * @param propertyId + * columns property id + * @return the expandRatio used to divide excess space for this column + */ public float getColumnExpandRatio(Object propertyId) { - final Object width = columnWidths.get(propertyId); - if (width == null || !(width instanceof Float)) { + final Float width = columnExpandRatios.get(propertyId); + if (width == null) { return -1; } - final Float value = (Float) width; - return value.floatValue(); - + return width.floatValue(); } /** @@ -959,12 +985,11 @@ public class Table extends AbstractSelect implements Action.Container, // id to retrieve the width of the row header. propertyId = ROW_HEADER_FAKE_PROPERTY_ID; } - final Object width = columnWidths.get(propertyId); - if (width == null || !(width instanceof Integer)) { + final Integer width = columnWidths.get(propertyId); + if (width == null) { return -1; } - final Integer value = (Integer) width; - return value.intValue(); + return width.intValue(); } /** @@ -3434,6 +3459,7 @@ public class Table extends AbstractSelect implements Action.Container, target.startTag("column"); target.addAttribute("cid", ROW_HEADER_COLUMN_KEY); paintColumnWidth(target, ROW_HEADER_FAKE_PROPERTY_ID); + paintColumnExpandRatio(target, ROW_HEADER_FAKE_PROPERTY_ID); target.endTag("column"); } final Collection sortables = getSortableContainerPropertyIds(); @@ -3461,6 +3487,7 @@ public class Table extends AbstractSelect implements Action.Container, .toString()); } paintColumnWidth(target, colId); + paintColumnExpandRatio(target, colId); target.endTag("column"); } } @@ -3706,12 +3733,14 @@ public class Table extends AbstractSelect implements Action.Container, private void paintColumnWidth(PaintTarget target, final Object columnId) throws PaintException { if (columnWidths.containsKey(columnId)) { - if (getColumnWidth(columnId) > -1) { - target.addAttribute("width", - String.valueOf(getColumnWidth(columnId))); - } else { - target.addAttribute("er", getColumnExpandRatio(columnId)); - } + target.addAttribute("width", getColumnWidth(columnId)); + } + } + + private void paintColumnExpandRatio(PaintTarget target, + final Object columnId) throws PaintException { + if (columnExpandRatios.containsKey(columnId)) { + target.addAttribute("er", getColumnExpandRatio(columnId)); } } diff --git a/uitest/src/com/vaadin/tests/components/table/TableColumnWidthsAndExpandRatios.html b/uitest/src/com/vaadin/tests/components/table/TableColumnWidthsAndExpandRatios.html new file mode 100644 index 0000000000..75d98ce2e6 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/table/TableColumnWidthsAndExpandRatios.html @@ -0,0 +1,83 @@ + + + + + + +New Test + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
New Test
open/run/com.vaadin.tests.components.table.TableColumnWidthsAndExpandRatios?restartApplication
screenCaptureinitial-all-columns-undefined
dragAndDropvaadin=runcomvaadintestscomponentstableTableColumnWidthsAndExpandRatios::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VScrollTable[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]-300,0
dragAndDropvaadin=runcomvaadintestscomponentstableTableColumnWidthsAndExpandRatios::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VScrollTable[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[1]/domChild[0]-300,0
dragAndDropvaadin=runcomvaadintestscomponentstableTableColumnWidthsAndExpandRatios::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VScrollTable[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[2]/domChild[0]-300,0
screenCapturecolumns-defined-width
mouseClickvaadin=runcomvaadintestscomponentstableTableColumnWidthsAndExpandRatios::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VHorizontalLayout[0]/ChildComponentContainer[0]/VNativeButton[0]114,4
screenCapturecolumn1-undefined
mouseClickvaadin=runcomvaadintestscomponentstableTableColumnWidthsAndExpandRatios::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VHorizontalLayout[0]/ChildComponentContainer[1]/VNativeButton[0]98,13
screenCapturecolumn2-undefined
mouseClickvaadin=runcomvaadintestscomponentstableTableColumnWidthsAndExpandRatios::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VHorizontalLayout[0]/ChildComponentContainer[2]/VNativeButton[0]40,10
screenCapturecolumns-undefined-width
+ + diff --git a/uitest/src/com/vaadin/tests/components/table/TableColumnWidthsAndExpandRatios.java b/uitest/src/com/vaadin/tests/components/table/TableColumnWidthsAndExpandRatios.java new file mode 100644 index 0000000000..747c99468f --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/table/TableColumnWidthsAndExpandRatios.java @@ -0,0 +1,57 @@ +package com.vaadin.tests.components.table; + +import com.vaadin.tests.components.TestBase; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.NativeButton; +import com.vaadin.ui.Table; + +public class TableColumnWidthsAndExpandRatios extends TestBase { + + @Override + protected void setup() { + getLayout().setSizeFull(); + + final Table table = new Table(); + table.setSizeFull(); + + table.addContainerProperty("column1", String.class, "Humpty"); + table.addContainerProperty("column2", String.class, "Dumpty"); + table.addContainerProperty("column3", String.class, "Doe"); + + for (int row = 0; row < 100; row++) { + table.addItem(); + } + + HorizontalLayout buttons = new HorizontalLayout(); + for (Object col : table.getContainerPropertyIds()) { + buttons.addComponent(createResetButton(col, table)); + } + + addComponent(table); + addComponent(buttons); + } + + private NativeButton createResetButton(final Object property, + final Table table) { + return new NativeButton("Reset " + property + " width", + new Button.ClickListener() { + + public void buttonClick(ClickEvent event) { + table.setColumnWidth(property, -1); + } + }); + } + + @Override + protected String getDescription() { + return "Changing column width to -1 should remove any previous size measurements"; + } + + @Override + protected Integer getTicketNumber() { + return 7922; + } + +} -- cgit v1.2.3