Skip to content

Commit

Permalink
Column collapse events for Table (#6914)
Browse files Browse the repository at this point in the history
Change-Id: Ifeb081086a4231f75f07f4d26c56ec22e72ce5d1
  • Loading branch information
Artur- authored and Vaadin Code Review committed Aug 21, 2015
1 parent 5c56d14 commit d40df1d
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 129 deletions.
89 changes: 87 additions & 2 deletions server/src/com/vaadin/ui/Table.java
Expand Up @@ -69,6 +69,7 @@
import com.vaadin.ui.declarative.DesignAttributeHandler; import com.vaadin.ui.declarative.DesignAttributeHandler;
import com.vaadin.ui.declarative.DesignContext; import com.vaadin.ui.declarative.DesignContext;
import com.vaadin.ui.declarative.DesignException; import com.vaadin.ui.declarative.DesignException;
import com.vaadin.util.ReflectTools;


/** /**
* <p> * <p>
Expand Down Expand Up @@ -1310,6 +1311,8 @@ public boolean isColumnCollapsed(Object propertyId) {
* the desired collapsedness. * the desired collapsedness.
* @throws IllegalStateException * @throws IllegalStateException
* if column collapsing is not allowed * if column collapsing is not allowed
* @throws IllegalArgumentException
* if the property id does not exist
*/ */
public void setColumnCollapsed(Object propertyId, boolean collapsed) public void setColumnCollapsed(Object propertyId, boolean collapsed)
throws IllegalStateException { throws IllegalStateException {
Expand All @@ -1319,11 +1322,19 @@ public void setColumnCollapsed(Object propertyId, boolean collapsed)
if (collapsed && noncollapsibleColumns.contains(propertyId)) { if (collapsed && noncollapsibleColumns.contains(propertyId)) {
throw new IllegalStateException("The column is noncollapsible!"); throw new IllegalStateException("The column is noncollapsible!");
} }
if (!getContainerPropertyIds().contains(propertyId)) {
throw new IllegalArgumentException("Property '" + propertyId
+ "' was not found in the container");
}


if (collapsed) { if (collapsed) {
collapsedColumns.add(propertyId); if (collapsedColumns.add(propertyId)) {
fireColumnCollapseEvent(propertyId);
}
} else { } else {
collapsedColumns.remove(propertyId); if (collapsedColumns.remove(propertyId)) {
fireColumnCollapseEvent(propertyId);
}
} }


// Assures the visual refresh // Assures the visual refresh
Expand Down Expand Up @@ -3182,6 +3193,10 @@ private void handleColumnResizeEvent(Map<String, Object> variables) {
} }
} }


private void fireColumnCollapseEvent(Object propertyId) {
fireEvent(new ColumnCollapseEvent(this, propertyId));
}

private void fireColumnResizeEvent(Object propertyId, int previousWidth, private void fireColumnResizeEvent(Object propertyId, int previousWidth,
int currentWidth) { int currentWidth) {
/* /*
Expand Down Expand Up @@ -5741,6 +5756,53 @@ public interface ColumnReorderListener extends Serializable {
public void columnReorder(ColumnReorderEvent event); public void columnReorder(ColumnReorderEvent event);
} }


/**
* This event is fired when the collapse state of a column changes
*/
public static class ColumnCollapseEvent extends Component.Event {

public static final Method METHOD = ReflectTools.findMethod(
ColumnCollapseListener.class, "columnCollapseStateChange",
ColumnCollapseEvent.class);
private Object propertyId;

/**
* Constructor
*
* @param source
* The source of the event
* @param propertyId
* The id of the coumn
*/
public ColumnCollapseEvent(Component source, Object propertyId) {
super(source);
this.propertyId = propertyId;
}

/**
* Gets the id of the column whose collapse state changed
*
* @return the property id of the column
*/
public Object getPropertyId() {
return propertyId;
}
}

/**
* Interface for listening to column collapse events.
*/
public interface ColumnCollapseListener extends Serializable {

/**
* This method is triggered when the collapse state for a column has
* changed
*
* @param event
*/
public void columnCollapseStateChange(ColumnCollapseEvent event);
}

/** /**
* Adds a column reorder listener to the Table. A column reorder listener is * Adds a column reorder listener to the Table. A column reorder listener is
* called when a user reorders columns. * called when a user reorders columns.
Expand Down Expand Up @@ -5782,6 +5844,29 @@ public void removeListener(ColumnReorderListener listener) {
removeColumnReorderListener(listener); removeColumnReorderListener(listener);
} }


/**
* Adds a column collapse listener to the Table. A column collapse listener
* is called when the collapsed state of a column changes.
*
* @param listener
* The listener to attach
*/
public void addColumnCollapseListener(ColumnCollapseListener listener) {
addListener(TableConstants.COLUMN_COLLAPSE_EVENT_ID,
ColumnCollapseEvent.class, listener, ColumnCollapseEvent.METHOD);
}

/**
* Removes a column reorder listener from the Table.
*
* @param listener
* The listener to remove
*/
public void removeColumnCollapseListener(ColumnCollapseListener listener) {
removeListener(TableConstants.COLUMN_COLLAPSE_EVENT_ID,
ColumnCollapseEvent.class, listener);
}

/** /**
* Set the item description generator which generates tooltips for cells and * Set the item description generator which generates tooltips for cells and
* rows in the Table * rows in the Table
Expand Down
1 change: 1 addition & 0 deletions shared/src/com/vaadin/shared/ui/table/TableConstants.java
Expand Up @@ -23,6 +23,7 @@ public class TableConstants implements Serializable {
public static final String FOOTER_CLICK_EVENT_ID = "handleFooterClick"; public static final String FOOTER_CLICK_EVENT_ID = "handleFooterClick";
public static final String COLUMN_RESIZE_EVENT_ID = "columnResize"; public static final String COLUMN_RESIZE_EVENT_ID = "columnResize";
public static final String COLUMN_REORDER_EVENT_ID = "columnReorder"; public static final String COLUMN_REORDER_EVENT_ID = "columnReorder";
public static final String COLUMN_COLLAPSE_EVENT_ID = "columnCollapse";


@Deprecated @Deprecated
public static final String ATTRIBUTE_PAGEBUFFER_FIRST = "pb-ft"; public static final String ATTRIBUTE_PAGEBUFFER_FIRST = "pb-ft";
Expand Down
Expand Up @@ -2,21 +2,22 @@


import com.vaadin.event.Action; import com.vaadin.event.Action;
import com.vaadin.event.Action.Handler; import com.vaadin.event.Action.Handler;
import com.vaadin.tests.components.TestBase; import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Alignment; import com.vaadin.tests.components.AbstractTestUIWithLog;
import com.vaadin.ui.Button; import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table; import com.vaadin.ui.Table;
import com.vaadin.ui.TextField; import com.vaadin.ui.Table.ColumnCollapseEvent;
import com.vaadin.ui.Table.ColumnCollapseListener;


public class ColumnCollapsingAndColumnExpansion extends TestBase { public class ColumnCollapsingAndColumnExpansion extends AbstractTestUIWithLog {


private Table table; private Table table;


@Override @Override
public void setup() { protected void setup(VaadinRequest request) {


table = new Table(); table = new Table();


Expand Down Expand Up @@ -50,41 +51,44 @@ public void handleAction(Action action, Object sender, Object target) {
"cell " + 2 + "-" + y, "cell " + 3 + "-" + y, }, "cell " + 2 + "-" + y, "cell " + 3 + "-" + y, },
new Object()); new Object());
} }

table.addColumnCollapseListener(new ColumnCollapseListener() {
addComponent(table);

HorizontalLayout hl = new HorizontalLayout();
final TextField tf = new TextField("Column name (ColX)");
Button hide = new Button("Collapse", new ClickListener() {


@Override @Override
public void buttonClick(ClickEvent event) { public void columnCollapseStateChange(ColumnCollapseEvent event) {
table.setColumnCollapsed(tf.getValue(), true); log("Collapse state for " + event.getPropertyId()
} + " changed to "

+ table.isColumnCollapsed(event.getPropertyId()));
});


Button show = new Button("Show", new ClickListener() {

@Override
public void buttonClick(ClickEvent event) {
table.setColumnCollapsed(tf.getValue(), false);
} }

}); });
addComponent(table);


hl.addComponent(tf); for (int i = 1; i <= 3; i++) {
hl.addComponent(hide); HorizontalLayout hl = new HorizontalLayout();
hl.addComponent(show); final String id = "Col" + i;
hl.setComponentAlignment(tf, Alignment.BOTTOM_LEFT); Button hide = new Button("Collapse " + id, new ClickListener() {
hl.setComponentAlignment(hide, Alignment.BOTTOM_LEFT); @Override
hl.setComponentAlignment(show, Alignment.BOTTOM_LEFT); public void buttonClick(ClickEvent event) {
addComponent(hl); table.setColumnCollapsed(id, true);
}
});

Button show = new Button("Show " + id, new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
table.setColumnCollapsed(id, false);
}
});

hl.addComponent(hide);
hl.addComponent(show);
addComponent(hl);
}


} }


@Override @Override
protected String getDescription() { protected String getTestDescription() {
return "After hiding column 2 the remaining columns (1 and 3) should use all available space in the table"; return "After hiding column 2 the remaining columns (1 and 3) should use all available space in the table";
} }


Expand Down
@@ -0,0 +1,112 @@
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.tests.components.table;

import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

import com.vaadin.testbench.TestBenchElement;
import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.parallel.BrowserUtil;
import com.vaadin.tests.components.table.CustomTableElement.ContextMenuElement;
import com.vaadin.tests.tb3.MultiBrowserTest;

public class ColumnCollapsingAndColumnExpansionTest extends MultiBrowserTest {

@Test
public void expandCorrectlyAfterCollapse() throws IOException {
openTestURL();

CustomTableElement table = $(CustomTableElement.class).first();

// Hide col2 through UI
table.openCollapseMenu().getItem(1).click();
compareScreen(table, "col1-col3");

// Hide col1 using button
ButtonElement hide1 = $(ButtonElement.class).caption("Collapse Col1")
.first();
hide1.click();
compareScreen(table, "col3");

// Show column 2 using context menu (first action)
if (BrowserUtil.isIE8(getDesiredCapabilities())) {
// IE8 can context click but the popup is off screen so it can't be
// interacted with...
ButtonElement show2 = $(ButtonElement.class).caption("Show Col2")
.first();
show2.click();
} else {
contextClick(table.getCell(0, 0));
ContextMenuElement contextMenu = table.getContextMenu();
WebElement i = contextMenu.getItem(0);
i.click();
}
compareScreen(table, "col2-col3");

// Show column 1 again
ButtonElement show1 = $(ButtonElement.class).caption("Show Col1")
.first();
show1.click();

compareScreen(table, "col1-col2-col3");
}

private void contextClick(TestBenchElement e) {
if (e.isPhantomJS()) {
JavascriptExecutor js = e.getCommandExecutor();
String scr = "var element=arguments[0];"
+ "var ev = document.createEvent('HTMLEvents');"
+ "ev.initEvent('contextmenu', true, false);"
+ "element.dispatchEvent(ev);";
js.executeScript(scr, e);
} else {
e.contextClick();
}

}

@Test
public void collapseEvents() {
openTestURL();
CustomTableElement table = $(CustomTableElement.class).first();

// Through menu
table.openCollapseMenu().getItem(0).click();
Assert.assertEquals("1. Collapse state for Col1 changed to true",
getLogRow(0));

// Through button
$(ButtonElement.class).caption("Collapse Col2").first().click();
Assert.assertEquals("2. Collapse state for Col2 changed to true",
getLogRow(0));

// Show through menu
table.openCollapseMenu().getItem(1).click();
Assert.assertEquals("3. Collapse state for Col1 changed to false",
getLogRow(0));

// Show through button
$(ButtonElement.class).caption("Show Col2").first().click();
Assert.assertEquals("4. Collapse state for Col2 changed to false",
getLogRow(0));

}
}

0 comments on commit d40df1d

Please sign in to comment.