@Override
public void setSplitterPosition(float position) {
getSplitterState().position = position;
+ fireEvent(new SplitPositionChangeEvent(AbstractSplitPanel.this,
+ position, getSplitPositionUnit()));
}
};
splitterState.positionUnit = unit.getSymbol();
splitterState.positionReversed = reverse;
posUnit = unit;
+ fireEvent(new SplitPositionChangeEvent(AbstractSplitPanel.this, pos,
+ posUnit));
}
/**
}
+ /**
+ * Interface for listening for {@link SplitPositionChangeEvent}s fired by a
+ * SplitPanel.
+ *
+ * @since
+ */
+ public interface SplitPositionChangeListener extends ConnectorEventListener {
+
+ public static final Method moveMethod = ReflectTools.findMethod(
+ SplitPositionChangeListener.class, "onSplitPositionChanged",
+ SplitPositionChangeEvent.class);
+
+ /**
+ * SplitPanel splitter position has been changed.
+ *
+ * @param event
+ * SplitPositionChangeEvent event.
+ */
+ public void onSplitPositionChanged(SplitPositionChangeEvent event);
+ }
+
+ /**
+ * Event that indicates a change in SplitPanel's splitter position.
+ *
+ * @since
+ */
+ public static class SplitPositionChangeEvent extends Component.Event {
+
+ private final float position;
+ private final Unit unit;
+
+ public SplitPositionChangeEvent(final Component source,
+ final float position, final Unit unit) {
+ super(source);
+ this.position = position;
+ this.unit = unit;
+ }
+
+ public float getSplitPosition() {
+ return position;
+ }
+
+ public Unit getSplitPositionUnit() {
+ return unit;
+ }
+
+ }
+
public void addSplitterClickListener(SplitterClickListener listener) {
addListener(EventId.CLICK_EVENT_IDENTIFIER, SplitterClickEvent.class,
listener, SplitterClickListener.clickMethod);
removeSplitterClickListener(listener);
}
+ /**
+ * Register a listener to handle {@link SplitPositionChangeEvent}s.
+ *
+ * @since
+ * @param listener
+ * {@link SplitPositionChangeListener} to be registered.
+ */
+ public void addSplitPositionChangeListener(
+ SplitPositionChangeListener listener) {
+ addListener(SplitPositionChangeEvent.class, listener,
+ SplitPositionChangeListener.moveMethod);
+ }
+
+ /**
+ * Removes a {@link SplitPositionChangeListener}.
+ *
+ * @since
+ * @param listener
+ * SplitPositionChangeListener to be removed.
+ */
+ public void removeSplitPositionChangeListener(
+ SplitPositionChangeListener listener) {
+ removeListener(SplitPositionChangeEvent.class, listener);
+ }
+
@Override
protected AbstractSplitPanelState getState() {
return (AbstractSplitPanelState) super.getState();
--- /dev/null
+/*
+ * 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.ui;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import org.junit.Test;
+
+import com.vaadin.server.Sizeable.Unit;
+import com.vaadin.ui.AbstractSplitPanel.SplitPositionChangeEvent;
+import com.vaadin.ui.AbstractSplitPanel.SplitPositionChangeListener;
+
+/**
+ * Test for {@link SplitPositionChangeListener}
+ *
+ * @author Vaadin Ltd
+ */
+public class SplitPositionChangeListenerTest {
+
+ @Test
+ public void testSplitPositionListenerIsTriggered() throws Exception {
+ final HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
+ SplitPositionChangeListener splitPositionChangeListener = mock(SplitPositionChangeListener.class);
+ splitPanel.addSplitPositionChangeListener(splitPositionChangeListener);
+ splitPanel.setSplitPosition(50, Unit.PERCENTAGE);
+ verify(splitPositionChangeListener).onSplitPositionChanged(
+ any(SplitPositionChangeEvent.class));
+ }
+}
--- /dev/null
+/*
+ * 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.splitpanel;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUIWithLog;
+import com.vaadin.ui.AbstractSplitPanel;
+import com.vaadin.ui.HorizontalSplitPanel;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.Panel;
+import com.vaadin.ui.VerticalLayout;
+import com.vaadin.ui.VerticalSplitPanel;
+
+/**
+ * Test for {@link SplitPositionChangeListeners}.
+ *
+ * @author Vaadin Ltd
+ */
+public class SplitPositionChange extends AbstractTestUIWithLog {
+
+ @Override
+ protected void setup(VaadinRequest request) {
+ addSplitPanel(true, "Left", "Right");
+ addSplitPanel(false, "Top", "Bottom");
+ }
+
+ private void addSplitPanel(final boolean horizontal, String firstCaption,
+ String secondCaption) {
+ AbstractSplitPanel splitPanel;
+ if (horizontal) {
+ splitPanel = new HorizontalSplitPanel();
+ } else {
+ splitPanel = new VerticalSplitPanel();
+ }
+ splitPanel.setWidth("200px");
+ splitPanel.setHeight("200px");
+ splitPanel.addComponent(buildPanel(firstCaption));
+ splitPanel.addComponent(buildPanel(secondCaption));
+ splitPanel
+ .addSplitPositionChangeListener(new AbstractSplitPanel.SplitPositionChangeListener() {
+
+ @Override
+ public void onSplitPositionChanged(
+ AbstractSplitPanel.SplitPositionChangeEvent event) {
+ log(String.format(
+ "Split position changed: %s, position: %s %s",
+ (horizontal ? "horizontal" : "vertical"),
+ event.getSplitPosition(),
+ event.getSplitPositionUnit()));
+ }
+ });
+ addComponent(splitPanel);
+ }
+
+ private Panel buildPanel(String caption) {
+ VerticalLayout pl = new VerticalLayout();
+ pl.setMargin(true);
+ pl.addComponent(new Label("content"));
+ Panel panel = new Panel(caption, pl);
+ panel.setSizeFull();
+ return panel;
+ }
+
+ @Override
+ protected String getTestDescription() {
+ return "SplitPanel should have an event for the splitter being moved";
+ }
+
+ @Override
+ protected Integer getTicketNumber() {
+ return 3855;
+ }
+
+}
--- /dev/null
+/*
+ * 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.splitpanel;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.openqa.selenium.By;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.interactions.Actions;
+
+import com.vaadin.testbench.elements.HorizontalSplitPanelElement;
+import com.vaadin.testbench.elements.VerticalSplitPanelElement;
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+/**
+ * Test for {@link SplitPositionChangeListeners}.
+ *
+ * @author Vaadin Ltd
+ */
+public class SplitPositionChangeTest extends MultiBrowserTest {
+
+ @Override
+ public void setup() throws Exception {
+ super.setup();
+ openTestURL();
+ waitForElementPresent(By.className("v-splitpanel-horizontal"));
+ }
+
+ @Test
+ public void testHorizontalSplit() {
+ HorizontalSplitPanelElement split = $(HorizontalSplitPanelElement.class)
+ .first();
+ WebElement splitter = split.findElement(By
+ .className("v-splitpanel-hsplitter"));
+ int position = splitter.getLocation().getX();
+ Actions actions = new Actions(driver);
+ actions.clickAndHold(splitter).moveByOffset(50, 0).release().perform();
+ assertPosition(position, splitter.getLocation().getX());
+ assertLogText(true);
+ }
+
+ @Test
+ public void testVerticalSplit() {
+ VerticalSplitPanelElement split = $(VerticalSplitPanelElement.class)
+ .first();
+ WebElement splitter = split.findElement(By
+ .className("v-splitpanel-vsplitter"));
+ int position = splitter.getLocation().getY();
+ Actions actions = new Actions(driver);
+ actions.clickAndHold(splitter).moveByOffset(0, 50).release().perform();
+ assertPosition(position, splitter.getLocation().getY());
+ assertLogText(false);
+ }
+
+ private void assertPosition(int original, int current) {
+ Assert.assertFalse("Position didn't change", original == current);
+ }
+
+ private void assertLogText(boolean horizontal) {
+ String expected = String.format(
+ "1. Split position changed: %s, position: .*",
+ horizontal ? "horizontal" : "vertical");
+ String actual = getLogRow(0);
+ Assert.assertTrue(
+ String.format(
+ "Log content didn't match the expected format.\nexpected: '%s'\nwas: '%s'",
+ expected, actual), actual.matches(expected));
+ }
+}