Browse Source

Fixes scrollToLine while resizing Grid via split position (#18424)

Change-Id: I5ecd8a91bd1ebea809d04a54307fcd752fbe3f39
tags/7.6.0.alpha3^2
Teppo Kurki 8 years ago
parent
commit
b9c0971afe

+ 55
- 44
client/src/com/vaadin/client/widgets/Escalator.java View File

@@ -6022,10 +6022,14 @@ public class Escalator extends Widget implements RequiresResize,
public void scrollToRow(final int rowIndex,
final ScrollDestination destination, final int padding)
throws IndexOutOfBoundsException, IllegalArgumentException {
validateScrollDestination(destination, padding);
verifyValidRowIndex(rowIndex);

scroller.scrollToRow(rowIndex, destination, padding);
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
validateScrollDestination(destination, padding);
verifyValidRowIndex(rowIndex);
scroller.scrollToRow(rowIndex, destination, padding);
}
});
}

private void verifyValidRowIndex(final int rowIndex) {
@@ -6086,55 +6090,62 @@ public class Escalator extends Widget implements RequiresResize,
* {@code destination == null}; or if {@code rowIndex == -1} and
* there is no spacer open at that index.
*/
public void scrollToRowAndSpacer(int rowIndex,
ScrollDestination destination, int padding)
public void scrollToRowAndSpacer(final int rowIndex,
final ScrollDestination destination, final int padding)
throws IllegalArgumentException {
validateScrollDestination(destination, padding);
if (rowIndex != -1) {
verifyValidRowIndex(rowIndex);
}
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
validateScrollDestination(destination, padding);
if (rowIndex != -1) {
verifyValidRowIndex(rowIndex);
}

// row range
final Range rowRange;
if (rowIndex != -1) {
int rowTop = (int) Math.floor(body.getRowTop(rowIndex));
int rowHeight = (int) Math.ceil(body.getDefaultRowHeight());
rowRange = Range.withLength(rowTop, rowHeight);
} else {
rowRange = Range.withLength(0, 0);
}
// row range
final Range rowRange;
if (rowIndex != -1) {
int rowTop = (int) Math.floor(body.getRowTop(rowIndex));
int rowHeight = (int) Math.ceil(body.getDefaultRowHeight());
rowRange = Range.withLength(rowTop, rowHeight);
} else {
rowRange = Range.withLength(0, 0);
}

// get spacer
final SpacerContainer.SpacerImpl spacer = body.spacerContainer
.getSpacer(rowIndex);
// get spacer
final SpacerContainer.SpacerImpl spacer = body.spacerContainer
.getSpacer(rowIndex);

if (rowIndex == -1 && spacer == null) {
throw new IllegalArgumentException("Cannot scroll to row index "
+ "-1, as there is no spacer open at that index.");
}
if (rowIndex == -1 && spacer == null) {
throw new IllegalArgumentException(
"Cannot scroll to row index "
+ "-1, as there is no spacer open at that index.");
}

// make into target range
final Range targetRange;
if (spacer != null) {
final int spacerTop = (int) Math.floor(spacer.getTop());
final int spacerHeight = (int) Math.ceil(spacer.getHeight());
Range spacerRange = Range.withLength(spacerTop, spacerHeight);
// make into target range
final Range targetRange;
if (spacer != null) {
final int spacerTop = (int) Math.floor(spacer.getTop());
final int spacerHeight = (int) Math.ceil(spacer.getHeight());
Range spacerRange = Range.withLength(spacerTop,
spacerHeight);

targetRange = rowRange.combineWith(spacerRange);
} else {
targetRange = rowRange;
}
targetRange = rowRange.combineWith(spacerRange);
} else {
targetRange = rowRange;
}

// get params
int targetStart = targetRange.getStart();
int targetEnd = targetRange.getEnd();
double viewportStart = getScrollTop();
double viewportEnd = viewportStart + body.getHeightOfSection();
// get params
int targetStart = targetRange.getStart();
int targetEnd = targetRange.getEnd();
double viewportStart = getScrollTop();
double viewportEnd = viewportStart + body.getHeightOfSection();

double scrollPos = getScrollPos(destination, targetStart, targetEnd,
viewportStart, viewportEnd, padding);
double scrollPos = getScrollPos(destination, targetStart,
targetEnd, viewportStart, viewportEnd, padding);

setScrollTop(scrollPos);
setScrollTop(scrollPos);
}
});
}

private static void validateScrollDestination(

+ 73
- 0
uitest/src/com/vaadin/tests/components/grid/GridScrollToLineWhileResizing.java View File

@@ -0,0 +1,73 @@
/*
* 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.grid;

import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.event.SelectionEvent;
import com.vaadin.event.SelectionEvent.SelectionListener;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.SelectionMode;
import com.vaadin.ui.VerticalSplitPanel;

public class GridScrollToLineWhileResizing extends AbstractTestUI {

@Override
protected void setup(VaadinRequest request) {

final VerticalSplitPanel vsp = new VerticalSplitPanel();
vsp.setWidth(500, Unit.PIXELS);
vsp.setHeight(500, Unit.PIXELS);
vsp.setSplitPosition(100, Unit.PERCENTAGE);
addComponent(vsp);

IndexedContainer indexedContainer = new IndexedContainer();
indexedContainer.addContainerProperty("column1", String.class, "");

for (int i = 0; i < 100; i++) {
Item addItem = indexedContainer.addItem(i);
addItem.getItemProperty("column1").setValue("cell" + i);
}

final Grid grid = new Grid(indexedContainer);
grid.setSizeFull();

grid.setSelectionMode(SelectionMode.SINGLE);
grid.addSelectionListener(new SelectionListener() {

@Override
public void select(SelectionEvent event) {
vsp.setSplitPosition(50, Unit.PERCENTAGE);
grid.scrollTo(event.getSelected().iterator().next());
}
});

vsp.setFirstComponent(grid);
}

@Override
protected String getTestDescription() {
return "Tests scrollToLine while moving SplitPanel split position to resize the Grid on the same round-trip.";
}

@Override
protected Integer getTicketNumber() {
return null;
}

}

+ 49
- 0
uitest/src/com/vaadin/tests/components/grid/GridScrollToLineWhileResizingTest.java View File

@@ -0,0 +1,49 @@
/*
* 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.grid;

import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.vaadin.testbench.elements.GridElement;
import com.vaadin.testbench.parallel.TestCategory;
import com.vaadin.tests.tb3.MultiBrowserTest;

@TestCategory("grid")
public class GridScrollToLineWhileResizingTest extends MultiBrowserTest {

@Test
public void testScrollToLineWorksWhileMovingSplitProgrammatically() {
openTestURL();

$(GridElement.class).first().getCell(21, 0).click();

List<WebElement> cells = findElements(By.className("v-grid-cell"));
boolean foundCell21 = false;
for (WebElement cell : cells) {
if ("cell21".equals(cell.getText())) {
foundCell21 = true;
}
}

assertTrue(foundCell21);
}
}

Loading…
Cancel
Save