diff options
6 files changed, 718 insertions, 32 deletions
diff --git a/WebContent/VAADIN/themes/base/grid/grid.scss b/WebContent/VAADIN/themes/base/grid/grid.scss index 14de4a2447..87b936a1b9 100644 --- a/WebContent/VAADIN/themes/base/grid/grid.scss +++ b/WebContent/VAADIN/themes/base/grid/grid.scss @@ -52,6 +52,8 @@ $v-grid-editor-background-color: $v-grid-row-background-color !default; .#{$primaryStyleName}-tablewrapper { border: $v-grid-border; } + + // Column drag and drop elements .#{$primaryStyleName} .header-drag-table { border-spacing: 0; @@ -76,6 +78,31 @@ $v-grid-editor-background-color: $v-grid-row-background-color !default; } } } + + // Sidebar + + .#{$primaryStyleName}-sidebar { + position: absolute; + top: 1px; + right : 0; + + background-color: $v-grid-header-background-color; + border-left: $v-grid-header-border; + border-bottom: $v-grid-header-border; + z-index: 5; + + .#{$primaryStyleName}-sidebar-button { + height: $v-grid-header-row-height; + + &:after { + content: "\f0c9"; + font-family: FontAwesome, sans-serif; + font-size: $v-grid-header-font-size; + line-height: $v-grid-header-row-height; + padding: 0 $v-grid-cell-padding-horizontal; + } + } + } // Common cell styles diff --git a/client/src/com/vaadin/client/widgets/Grid.java b/client/src/com/vaadin/client/widgets/Grid.java index ea9525f157..c6ac35bba0 100644 --- a/client/src/com/vaadin/client/widgets/Grid.java +++ b/client/src/com/vaadin/client/widgets/Grid.java @@ -62,9 +62,13 @@ import com.google.gwt.user.client.Event.NativePreviewHandler; import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.CheckBox; +import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.HasEnabled; +import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant; import com.google.gwt.user.client.ui.HasWidgets; import com.google.gwt.user.client.ui.ResizeComposite; +import com.google.gwt.user.client.ui.ToggleButton; +import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.BrowserInfo; import com.vaadin.client.DeferredWorker; @@ -75,6 +79,7 @@ import com.vaadin.client.renderers.ComplexRenderer; import com.vaadin.client.renderers.Renderer; import com.vaadin.client.renderers.WidgetRenderer; import com.vaadin.client.ui.SubPartAware; +import com.vaadin.client.ui.VButton; import com.vaadin.client.ui.dd.DragAndDropHandler; import com.vaadin.client.ui.dd.DragAndDropHandler.DragAndDropCallback; import com.vaadin.client.widget.escalator.Cell; @@ -2882,6 +2887,258 @@ public class Grid<T> extends ResizeComposite implements } /** + * Sidebar displaying toggles for hidable columns and additional custom + * widgets. + * <p> + * The button for opening the sidebar is automatically visible inside the + * grid, if it contains any column hiding options or custom widgets. The + * column hiding toggles and custom widgets become visible once the sidebar + * has been opened. + * + * @since + */ + public static class Sidebar extends Composite { + + private final ClickHandler openCloseButtonHandler = new ClickHandler() { + + @Override + public void onClick(ClickEvent event) { + if (!open) { + open(); + } else { + close(); + } + } + }; + + /** + * Contains all the widgets which should be shown once the sidebar is + * opened + */ + private final List<Widget> widgets = new ArrayList<Widget>(); + + private final VerticalPanel rootContainer; + + private final VButton openCloseButton; + + private final Grid<?> grid; + + private boolean open; + + public Sidebar(Grid<?> grid) { + this.grid = grid; + + rootContainer = new VerticalPanel(); + initWidget(rootContainer); + + openCloseButton = new VButton(); + openCloseButton.addClickHandler(openCloseButtonHandler); + + rootContainer.add(openCloseButton); + rootContainer + .setCellHorizontalAlignment( + openCloseButton, + HorizontalAlignmentConstant + .endOf(com.google.gwt.i18n.client.HasDirection.Direction.LTR)); + } + + /** + * Opens the sidebar if not yet opened. + * + * @since + */ + public void open() { + if (!open) { + addStyleName("opened"); + open = true; + for (Widget w : widgets) { + rootContainer.add(w); + } + } + } + + /** + * Closes the sidebar if not yet closed. + * + * @since + */ + public void close() { + if (open) { + removeStyleName("opened"); + open = false; + rootContainer.clear(); + rootContainer.add(openCloseButton); + } + } + + /** + * Returns whether the sidebar is open or not. + * <p> + * <em>Note:</em> The sidebar can be in "open state" but not actually + * visible inside grid. See {@link #isVisibleInGrid()}. + * + * @since + * @return <code>true</code> if open, <code>false</code> if not + */ + public boolean isOpen() { + return open; + } + + /** + * Adds or moves the given widget to the end of the sidebar. + * + * @param widget + * the widget to add or move + */ + public void add(Widget widget) { + widgets.remove(widget); + widgets.add(widget); + if (open) { + rootContainer.add(widget); + } + updateVisibility(); + } + + /** + * Removes the given widget from the sidebar. + * + * @param widget + * the widget to remove + */ + public void remove(Widget widget) { + widgets.remove(widget); + if (open) { + rootContainer.remove(widget); + } + updateVisibility(); + } + + /** + * Inserts given widget to the given index inside the sidebar. If the + * widget is already in the sidebar, then it is moved to the new index. + * <p> + * See + * {@link VerticalPanel#insert(com.google.gwt.user.client.ui.IsWidget, int)} + * for further details. + * + * @param widget + * the widget to insert + * @param beforeIndex + * 0-based index position for the widget. + */ + public void insert(Widget widget, int beforeIndex) { + widgets.remove(widget); + widgets.add(beforeIndex, widget); + if (open) { + // the first widget in the container is always the open button + rootContainer.insert(widget, beforeIndex + 1); + } + updateVisibility(); + } + + @Override + public void setStylePrimaryName(String styleName) { + super.setStylePrimaryName(styleName); + openCloseButton.setStylePrimaryName(styleName + "-button"); + } + + private void updateVisibility() { + final boolean hasWidgets = widgets.size() > 0; + final boolean isVisible = getParent() != null; + if (isVisible && !hasWidgets) { + getElement().removeFromParent(); + removeFromParent(); + } else if (!isVisible && hasWidgets) { + grid.getElement().appendChild(getElement()); + Grid.setParent(this, grid); + } + } + + } + + /** + * UI and functionality related to hiding columns with toggles in the + * sidebar. + */ + private final class ColumnHider extends VerticalPanel { + + ColumnHider() { + setStyleName("column-hiding-panel"); + } + + /** Map from columns to their hiding toggles, component might change */ + private HashMap<Column<?, T>, ToggleButton> columnToHidingToggleMap = new HashMap<Grid.Column<?, T>, ToggleButton>(); + + private void updateColumnHidable(final Column<?, T> column) { + if (column.isHidable()) { + ToggleButton cb = columnToHidingToggleMap.get(column); + if (cb == null) { + cb = createToggle(column); + } + updateToggleValue(cb, column.isHidden()); + } else if (columnToHidingToggleMap.containsValue(column)) { + ((Widget) columnToHidingToggleMap.remove(column)) + .removeFromParent(); + } + updateTogglesOrder(); + updatePanelVisibility(); + } + + private ToggleButton createToggle(final Column<?, T> column) { + ToggleButton toggle = new ToggleButton(column.headerCaption); + toggle.addStyleName("column-hiding-toggle"); + toggle.addValueChangeHandler(new ValueChangeHandler<Boolean>() { + + @Override + public void onValueChange(ValueChangeEvent<Boolean> event) { + column.setHidden(!event.getValue(), true); + } + }); + columnToHidingToggleMap.put(column, toggle); + return toggle; + } + + private void updateTogglesOrder() { + clear(); + for (Column<?, T> c : getColumns()) { + if (c.isHidable()) { + add(columnToHidingToggleMap.get(c)); + } + } + } + + private void updatePanelVisibility() { + final boolean columnHidable = getWidgetCount() > 0; + // parent for the panel might be null sidebar is not open + final boolean columnTogglesPanelIsVisible = sidebar.widgets + .contains(this); + + if (columnHidable && !columnTogglesPanelIsVisible) { + sidebar.insert(this, 0); + } else if (!columnHidable && columnTogglesPanelIsVisible) { + sidebar.remove(this); + } + } + + private void updateToggleValue(Column<?, T> column) { + if (column.isHidable()) { + updateToggleValue(columnToHidingToggleMap.get(column), + column.isHidden()); + } // else we can just ignore + } + + private void updateToggleValue(ToggleButton hasValue, boolean hidden) { + hasValue.setValue(!hidden, false); + hasValue.setStyleName("hidden", hidden); + } + + private void updateColumnHidingToggleCaption(Column<?, T> column) { + columnToHidingToggleMap.get(column).setText(column.headerCaption); + } + + } + + /** * Escalator used internally by grid to render the rows */ private Escalator escalator = GWT.create(Escalator.class); @@ -2890,6 +3147,8 @@ public class Grid<T> extends ResizeComposite implements private final Footer footer = GWT.create(Footer.class); + private final Sidebar sidebar = new Sidebar(this); + /** * List of columns in the grid. Order defines the visible order. */ @@ -2974,6 +3233,8 @@ public class Grid<T> extends ResizeComposite implements private boolean columnReorderingAllowed; + private ColumnHider columnHider = new ColumnHider(); + private DragAndDropHandler dndHandler = new DragAndDropHandler(); private AutoScroller autoScroller = new AutoScroller(this); @@ -3488,7 +3749,7 @@ public class Grid<T> extends ResizeComposite implements private boolean hidden = false; - private boolean hideable = false; + private boolean hidable = false; private String headerCaption = ""; @@ -3600,6 +3861,9 @@ public class Grid<T> extends ResizeComposite implements HeaderRow row = grid.getHeader().getDefaultRow(); if (row != null) { row.getCell(this).setText(headerCaption); + if (isHidable()) { + grid.columnHider.updateColumnHidingToggleCaption(this); + } } } @@ -3767,6 +4031,10 @@ public class Grid<T> extends ResizeComposite implements * to show */ public void setHidden(boolean hidden) { + setHidden(hidden, false); + } + + private void setHidden(boolean hidden, boolean userOriginated) { if (this.hidden != hidden) { if (hidden) { grid.escalator.getColumnConfiguration().removeColumns( @@ -3777,9 +4045,10 @@ public class Grid<T> extends ResizeComposite implements grid.escalator.getColumnConfiguration().insertColumns( grid.getVisibleColumns().indexOf(this), 1); } + grid.columnHider.updateToggleValue(this); scheduleColumnWidthRecalculator(); this.grid.fireEvent(new ColumnVisibilityChangeEvent<T>(this, - hidden, false)); + hidden, userOriginated)); } } @@ -3802,13 +4071,15 @@ public class Grid<T> extends ResizeComposite implements * programmatically using {@link #setHidden(boolean)}. * * @since - * @param hideable + * @param hidable * <code>true</code> if the user can hide this column, * <code>false</code> if not */ - public void setHideable(boolean hideable) { - this.hideable = hideable; - // TODO update whether sidebar/popup can be opened + public void setHidable(boolean hidable) { + if (this.hidable != hidable) { + this.hidable = hidable; + grid.columnHider.updateColumnHidable(this); + } } /** @@ -3822,8 +4093,8 @@ public class Grid<T> extends ResizeComposite implements * @return <code>true</code> if the user can hide the column, * <code>false</code> if not */ - public boolean isHideable() { - return hideable; + public boolean isHidable() { + return hidable; } @Override @@ -4547,6 +4818,7 @@ public class Grid<T> extends ResizeComposite implements super.setStylePrimaryName(style); escalator.setStylePrimaryName(style); editor.setStylePrimaryName(style); + sidebar.setStylePrimaryName(style + "-sidebar"); String rowStyle = getStylePrimaryName() + "-row"; rowHasDataStyleName = rowStyle + "-has-data"; @@ -6641,6 +6913,8 @@ public class Grid<T> extends ResizeComposite implements row.calculateColspans(); } + columnHider.updateTogglesOrder(); + fireEvent(new ColumnReorderEvent<T>()); } @@ -7079,4 +7353,18 @@ public class Grid<T> extends ResizeComposite implements public void recalculateColumnWidths() { autoColumnWidthsRecalculator.schedule(); } + + /** + * Returns the sidebar for this grid. + * <p> + * The grid's sidebar shows the column hiding options for those columns that + * have been set as {@link Column#setHidable(boolean) hidable}. + * + * @since + * @return the sidebar widget for this grid + */ + public Sidebar getSidebar() { + return sidebar; + } + } diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicClientFeaturesTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicClientFeaturesTest.java index 5d02189787..754529d10d 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicClientFeaturesTest.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicClientFeaturesTest.java @@ -99,4 +99,9 @@ public abstract class GridBasicClientFeaturesTest extends GridBasicFeaturesTest assertColumnHeader("HEADER (0," + indices[i] + ")", headers.get(i)); } } + + protected void toggleColumnReorder() { + selectMenuPath("Component", "State", "Column Reordering"); + } + } diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridColumnHidingTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridColumnHidingTest.java index 6d38c25fb6..87af43af12 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridColumnHidingTest.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridColumnHidingTest.java @@ -16,9 +16,15 @@ package com.vaadin.tests.components.grid.basicfeatures; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; @@ -38,12 +44,12 @@ public class GridColumnHidingTest extends GridBasicClientFeaturesTest { selectMenuPath("Component", "State", "Width", "1000px"); assertColumnHeaderOrder(0, 1, 2, 3, 4, 5, 6); - toggleHideColumn(0); + toggleHideColumnAPI(0); assertColumnHeaderOrder(1, 2, 3, 4, 5, 6); - toggleHideColumn(1); - toggleHideColumn(2); - toggleHideColumn(3); + toggleHideColumnAPI(1); + toggleHideColumnAPI(2); + toggleHideColumnAPI(3); assertColumnHeaderOrder(4, 5, 6, 7, 8); } @@ -52,19 +58,19 @@ public class GridColumnHidingTest extends GridBasicClientFeaturesTest { selectMenuPath("Component", "State", "Width", "1000px"); assertColumnHeaderOrder(0, 1, 2, 3, 4, 5, 6); - toggleHideColumn(0); + toggleHideColumnAPI(0); assertColumnHeaderOrder(1, 2, 3, 4, 5, 6); - toggleHideColumn(0); + toggleHideColumnAPI(0); assertColumnHeaderOrder(0, 1, 2, 3, 4, 5, 6); - toggleHideColumn(1); - toggleHideColumn(2); - toggleHideColumn(3); + toggleHideColumnAPI(1); + toggleHideColumnAPI(2); + toggleHideColumnAPI(3); assertColumnHeaderOrder(0, 4, 5, 6, 7, 8); - toggleHideColumn(1); - toggleHideColumn(2); + toggleHideColumnAPI(1); + toggleHideColumnAPI(2); assertColumnHeaderOrder(0, 1, 2, 4, 5, 6); } @@ -73,26 +79,26 @@ public class GridColumnHidingTest extends GridBasicClientFeaturesTest { selectMenuPath("Component", "State", "Width", "1000px"); assertColumnHeaderOrder(0, 1, 2, 3, 4, 5, 6); - toggleHideColumn(2); + toggleHideColumnAPI(2); assertColumnHeaderOrder(0, 1, 3, 4, 5, 6); - toggleHideColumn(2); + toggleHideColumnAPI(2); assertColumnHeaderOrder(0, 1, 2, 3, 4, 5, 6); - toggleHideColumn(2); + toggleHideColumnAPI(2); assertColumnHeaderOrder(0, 1, 3, 4, 5, 6); - toggleHideColumn(2); + toggleHideColumnAPI(2); assertColumnHeaderOrder(0, 1, 2, 3, 4, 5, 6); } @Test - public void testColumnHiding_onVisibilityChange_triggersClientSideEvent() { + public void testColumnHiding_changeVisibilityAPI_triggersClientSideEvent() { assertColumnHeaderOrder(0, 1, 2, 3, 4); selectMenuPath("Component", "Internals", "Listeners", "Add Column Visibility Change listener"); - toggleHideColumn(2); + toggleHideColumnAPI(2); assertColumnHeaderOrder(0, 1, 3, 4); WebElement webElement = findElement(By.id("columnvisibility")); @@ -110,7 +116,7 @@ public class GridColumnHidingTest extends GridBasicClientFeaturesTest { assertEquals(false, userOriginated); assertEquals(true, hidden); - toggleHideColumn(2); + toggleHideColumnAPI(2); assertColumnHeaderOrder(0, 1, 2, 3, 4); webElement = findElement(By.id("columnvisibility")); @@ -127,7 +133,366 @@ public class GridColumnHidingTest extends GridBasicClientFeaturesTest { assertEquals(false, hidden); } - private void toggleHideColumn(int columnIndex) { + @Test + public void testColumnHiding_changeVisibilityToggle_triggersClientSideEvent() { + assertColumnHeaderOrder(0, 1, 2, 3, 4); + selectMenuPath("Component", "Internals", "Listeners", + "Add Column Visibility Change listener"); + + toggleHidableColumnAPI(2); + clickSidebarOpenButton(); + getColumnHidingToggle(2).click(); + assertColumnHeaderOrder(0, 1, 3, 4); + + WebElement webElement = findElement(By.id("columnvisibility")); + int counter = Integer.parseInt(webElement.getAttribute("counter")); + int columnIndex = Integer.parseInt(webElement + .getAttribute("columnindex")); + boolean userOriginated = Boolean.parseBoolean(webElement + .getAttribute("useroriginated")); + boolean hidden = Boolean.parseBoolean(webElement + .getAttribute("ishidden")); + + assertNotNull("no event fired", webElement); + assertEquals(1, counter); + assertEquals(2, columnIndex); + assertEquals(true, userOriginated); + assertEquals(true, hidden); + + getColumnHidingToggle(2).click(); + assertColumnHeaderOrder(0, 1, 2, 3, 4); + + webElement = findElement(By.id("columnvisibility")); + counter = Integer.parseInt(webElement.getAttribute("counter")); + columnIndex = Integer.parseInt(webElement.getAttribute("columnIndex")); + userOriginated = Boolean.parseBoolean(webElement + .getAttribute("userOriginated")); + hidden = Boolean.parseBoolean(webElement.getAttribute("ishidden")); + + assertNotNull("no event fired", webElement); + assertEquals(2, counter); + assertEquals(2, columnIndex); + assertEquals(true, userOriginated); + assertEquals(false, hidden); + } + + @Test + public void testColumnHidability_onTriggerColumnHidability_showsSidebarButton() { + WebElement sidebar = getSidebar(); + assertNull(sidebar); + + toggleHidableColumnAPI(0); + + sidebar = getSidebar(); + assertNotNull(sidebar); + } + + @Test + public void testColumnHidability_triggeringColumnHidabilityWithSeveralColumns_showsAndHidesSiderbarButton() { + verifySidebarNotVisible(); + + toggleHidableColumnAPI(3); + toggleHidableColumnAPI(4); + + verifySidebarVisible(); + + toggleHidableColumnAPI(3); + + verifySidebarVisible(); + + toggleHidableColumnAPI(4); + + verifySidebarNotVisible(); + } + + @Test + public void testColumnHidability_clickingSidebarButton_opensClosesSidebar() { + toggleHidableColumnAPI(0); + verifySidebarClosed(); + + clickSidebarOpenButton(); + + verifySidebarOpened(); + + clickSidebarOpenButton(); + + verifySidebarClosed(); + } + + @Test + public void testColumnHidability_settingColumnHidable_showsToggleInSidebar() { + toggleHidableColumnAPI(0); + verifySidebarClosed(); + clickSidebarOpenButton(); + verifySidebarOpened(); + + verifyColumnHidingOption(0, false); + } + + @Test + public void testColumnHiding_hidingColumnWithToggle_works() { + assertColumnHeaderOrder(0, 1, 2, 3, 4); + toggleHidableColumnAPI(0); + verifySidebarClosed(); + clickSidebarOpenButton(); + verifySidebarOpened(); + verifyColumnHidingOption(0, false); + + getColumnHidingToggle(0).click(); + verifyColumnHidingOption(0, true); + assertColumnHeaderOrder(1, 2, 3, 4); + + getColumnHidingToggle(0).click(); + verifyColumnHidingOption(0, false); + assertColumnHeaderOrder(0, 1, 2, 3, 4); + } + + @Test + public void testColumnHiding_updatingHiddenWhileSidebarClosed_updatesToggleValue() { + toggleHidableColumnAPI(0); + toggleHidableColumnAPI(3); + toggleHideColumnAPI(3); + assertColumnHeaderOrder(0, 1, 2, 4); + verifySidebarClosed(); + + clickSidebarOpenButton(); + verifySidebarOpened(); + verifyColumnHidingOption(0, false); + verifyColumnHidingOption(3, true); + + clickSidebarOpenButton(); + verifySidebarClosed(); + + toggleHideColumnAPI(0); + toggleHideColumnAPI(3); + + clickSidebarOpenButton(); + verifySidebarOpened(); + verifyColumnHidingOption(0, true); + verifyColumnHidingOption(3, false); + + } + + @Test + public void testColumnHiding_hidingMultipleColumnsWithToggle_hidesColumns() { + assertColumnHeaderOrder(0, 1, 2, 3, 4); + + toggleHideColumnAPI(1); + toggleHidableColumnAPI(0); + toggleHidableColumnAPI(1); + toggleHidableColumnAPI(2); + toggleHidableColumnAPI(3); + toggleHidableColumnAPI(4); + verifySidebarClosed(); + assertColumnHeaderOrder(0, 2, 3, 4); + + clickSidebarOpenButton(); + verifySidebarOpened(); + verifyColumnHidingOption(0, false); + verifyColumnHidingOption(1, true); + verifyColumnHidingOption(2, false); + verifyColumnHidingOption(3, false); + verifyColumnHidingOption(4, false); + + // must be done in a funny order so that the header indexes won't break + // (because of data source uses counter) + getColumnHidingToggle(1).click(); + getColumnHidingToggle(2).click(); + getColumnHidingToggle(3).click(); + getColumnHidingToggle(4).click(); + getColumnHidingToggle(0).click(); + verifyColumnHidingOption(0, true); + verifyColumnHidingOption(1, false); + verifyColumnHidingOption(2, true); + verifyColumnHidingOption(3, true); + verifyColumnHidingOption(4, true); + + assertColumnHeaderOrder(1, 5, 6, 7); + + getColumnHidingToggle(0).click(); + getColumnHidingToggle(2).click(); + getColumnHidingToggle(1).click(); + verifyColumnHidingOption(0, false); + verifyColumnHidingOption(1, true); + verifyColumnHidingOption(2, false); + assertColumnHeaderOrder(0, 2, 5, 6); + } + + @Test + public void testColumnHidability_changingHidabilityWhenSidebarClosed_addsRemovesToggles() { + toggleHideColumnAPI(0); + toggleHideColumnAPI(4); + assertColumnHeaderOrder(1, 2, 3, 5); + toggleHidableColumnAPI(0); + toggleHidableColumnAPI(3); + toggleHidableColumnAPI(4); + verifySidebarClosed(); + + clickSidebarOpenButton(); + verifySidebarOpened(); + verifyColumnHidingOption(0, true); + verifyColumnHidingOption(3, false); + verifyColumnHidingOption(4, true); + + clickSidebarOpenButton(); + verifySidebarClosed(); + + toggleHidableColumnAPI(0); + toggleHidableColumnAPI(3); + + verifySidebarClosed(); + clickSidebarOpenButton(); + verifySidebarOpened(); + verifyColumnHidingOption(4, true); + + assertNull(getColumnHidingToggle(0)); + assertNull(getColumnHidingToggle(3)); + } + + @Test + public void testColumnHidability_togglingHidability_placesTogglesInRightOrder() { + toggleHidableColumnAPI(3); + toggleHidableColumnAPI(2); + clickSidebarOpenButton(); + + verifyColumnHidingTogglesOrder(2, 3); + + toggleHidableColumnAPI(1); + toggleHidableColumnAPI(2); + toggleHidableColumnAPI(6); + toggleHidableColumnAPI(0); + + verifyColumnHidingTogglesOrder(0, 1, 3, 6); + + clickSidebarOpenButton(); + + toggleHidableColumnAPI(2); + toggleHidableColumnAPI(4); + toggleHidableColumnAPI(7); + + clickSidebarOpenButton(); + + verifyColumnHidingTogglesOrder(0, 1, 2, 3, 4, 6, 7); + } + + @Test + public void testColumnHidability_reorderingColumns_updatesColumnToggleOrder() { + selectMenuPath("Component", "State", "Width", "1000px"); + toggleHidableColumnAPI(0); + toggleHidableColumnAPI(1); + toggleHidableColumnAPI(3); + toggleHidableColumnAPI(4); + clickSidebarOpenButton(); + verifyColumnHidingTogglesOrder(0, 1, 3, 4); + clickSidebarOpenButton(); + + toggleColumnReorder(); + dragAndDropColumnHeader(0, 3, 0, 5); + + assertColumnHeaderOrder(3, 0, 1, 2, 4); + clickSidebarOpenButton(); + verifyColumnHidingTogglesOrder(3, 0, 1, 4); + + clickSidebarOpenButton(); + dragAndDropColumnHeader(0, 1, 3, 100); + dragAndDropColumnHeader(0, 4, 0, 5); + dragAndDropColumnHeader(0, 3, 0, 5); + + assertColumnHeaderOrder(2, 4, 3, 1, 0); + clickSidebarOpenButton(); + verifyColumnHidingTogglesOrder(4, 3, 1, 0); + } + + // know issue, will be fixed in next patch + @Test + @Ignore + public void testColumnHidingAndReorder_reorderingOverHiddenColumns_orderIsKept() { + toggleColumnReorder(); + toggleHideColumnAPI(0); + assertColumnHeaderOrder(1, 2, 3, 4, 5); + + dragAndDropColumnHeader(0, 1, 0, 5); + assertColumnHeaderOrder(2, 1, 3, 4, 5); + + toggleHideColumnAPI(0); + assertColumnHeaderOrder(0, 2, 1, 3, 4, 5); + } + + private void verifyColumnHidingTogglesOrder(int... indices) { + WebElement sidebar = getSidebar(); + List<WebElement> elements = sidebar.findElements(By + .className("column-hiding-toggle")); + for (int i = 0; i < indices.length; i++) { + WebElement e = elements.get(i); + assertTrue(("Header (0," + indices[i] + ")").equalsIgnoreCase(e + .getText())); + } + } + + private void verifyColumnHidingOption(int columnIndex, boolean hidden) { + WebElement columnHidingToggle = getColumnHidingToggle(columnIndex); + assertEquals(hidden, + columnHidingToggle.getAttribute("class").contains("hidden")); + } + + private void verifySidebarOpened() { + WebElement sidebar = getSidebar(); + assertTrue(sidebar.getAttribute("class").contains("opened")); + } + + private void verifySidebarClosed() { + WebElement sidebar = getSidebar(); + assertFalse(sidebar.getAttribute("class").contains("opened")); + } + + private void verifySidebarNotVisible() { + WebElement sidebar = getSidebar(); + assertNull(sidebar); + } + + private void verifySidebarVisible() { + WebElement sidebar = getSidebar(); + assertNotNull(sidebar); + } + + private WebElement getSidebar() { + List<WebElement> elements = findElements(By.className("v-grid-sidebar")); + return elements.isEmpty() ? null : elements.get(0); + } + + private WebElement getSidebarOpenButton() { + List<WebElement> elements = findElements(By + .className("v-grid-sidebar-button")); + return elements.isEmpty() ? null : elements.get(0); + } + + /** + * Returns the toggle inside the sidebar for hiding the column at the given + * index, or null if not found. + */ + private WebElement getColumnHidingToggle(int columnIndex) { + WebElement sidebar = getSidebar(); + List<WebElement> elements = sidebar.findElements(By + .className("column-hiding-toggle")); + for (WebElement e : elements) { + if (("Header (0," + columnIndex + ")") + .equalsIgnoreCase(e.getText())) { + return e; + } + } + return null; + } + + private void clickSidebarOpenButton() { + getSidebarOpenButton().click(); + } + + private void toggleHidableColumnAPI(int columnIndex) { + selectMenuPath("Component", "Columns", "Column " + columnIndex, + "Hidable"); + } + + private void toggleHideColumnAPI(int columnIndex) { selectMenuPath("Component", "Columns", "Column " + columnIndex, "Hidden"); } diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridColumnReorderTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridColumnReorderTest.java index 27e98bf460..a79a954faa 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridColumnReorderTest.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridColumnReorderTest.java @@ -601,10 +601,6 @@ public class GridColumnReorderTest extends GridBasicClientFeaturesTest { assertColumnHeaderOrder(1, 2, 0, 3, 4); } - private void toggleColumnReorder() { - selectMenuPath("Component", "State", "Column Reordering"); - } - private void toggleSortableColumn(int index) { selectMenuPath("Component", "Columns", "Column " + index, "Sortable"); } diff --git a/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java b/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java index 97f8e13ab1..6615cda8da 100644 --- a/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java +++ b/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java @@ -786,7 +786,12 @@ public class GridBasicClientFeaturesWidget extends column.setHidden(!column.isHidden()); } }, "Component", "Columns", "Column " + i); - + addMenuCommand("Hidable", new ScheduledCommand() { + @Override + public void execute() { + column.setHidable(!column.isHidable()); + } + }, "Component", "Columns", "Column " + i); addMenuCommand("auto", new ScheduledCommand() { @Override public void execute() { |