]> source.dussan.org Git - vaadin-framework.git/commitdiff
SplitPanel should have an event for the splitter being moved (#3855)
authorwodencafe <wodencafe@gmail.com>
Wed, 4 Feb 2015 17:28:51 +0000 (11:28 -0600)
committerAnna Koskinen <anna@vaadin.com>
Thu, 26 Mar 2015 10:52:11 +0000 (10:52 +0000)
Change-Id: I473e5a0f8420c95971af52ef230849f468ebbbad

server/src/com/vaadin/ui/AbstractSplitPanel.java
server/tests/src/com/vaadin/ui/SplitPositionChangeListenerTest.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/components/splitpanel/SplitPositionChange.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/components/splitpanel/SplitPositionChangeTest.java [new file with mode: 0644]

index af73fca6a846045db8cd4f7a0c11c562ee0a4860..8a7b9086c22df665c26f11edd15ca29b201143d6 100644 (file)
@@ -64,6 +64,8 @@ public abstract class AbstractSplitPanel extends AbstractComponentContainer {
         @Override
         public void setSplitterPosition(float position) {
             getSplitterState().position = position;
+            fireEvent(new SplitPositionChangeEvent(AbstractSplitPanel.this,
+                    position, getSplitPositionUnit()));
         }
     };
 
@@ -331,6 +333,8 @@ public abstract class AbstractSplitPanel extends AbstractComponentContainer {
         splitterState.positionUnit = unit.getSymbol();
         splitterState.positionReversed = reverse;
         posUnit = unit;
+        fireEvent(new SplitPositionChangeEvent(AbstractSplitPanel.this, pos,
+                posUnit));
     }
 
     /**
@@ -520,6 +524,54 @@ public abstract class AbstractSplitPanel extends AbstractComponentContainer {
 
     }
 
+    /**
+     * 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);
@@ -548,6 +600,31 @@ public abstract class AbstractSplitPanel extends AbstractComponentContainer {
         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();
diff --git a/server/tests/src/com/vaadin/ui/SplitPositionChangeListenerTest.java b/server/tests/src/com/vaadin/ui/SplitPositionChangeListenerTest.java
new file mode 100644 (file)
index 0000000..02dc412
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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));
+    }
+}
diff --git a/uitest/src/com/vaadin/tests/components/splitpanel/SplitPositionChange.java b/uitest/src/com/vaadin/tests/components/splitpanel/SplitPositionChange.java
new file mode 100644 (file)
index 0000000..81c4497
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * 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;
+    }
+
+}
diff --git a/uitest/src/com/vaadin/tests/components/splitpanel/SplitPositionChangeTest.java b/uitest/src/com/vaadin/tests/components/splitpanel/SplitPositionChangeTest.java
new file mode 100644 (file)
index 0000000..43edb56
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * 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));
+    }
+}