]> source.dussan.org Git - vaadin-framework.git/commitdiff
Provide old value of position in SplitPositionChangeEvent of *SplitPanel (#9578)
authorYuriy Artamonov <jreznot@users.noreply.github.com>
Thu, 29 Jun 2017 05:44:16 +0000 (09:44 +0400)
committerHenri Sara <henri.sara@gmail.com>
Thu, 29 Jun 2017 05:44:16 +0000 (08:44 +0300)
Provide old value of position in SplitPositionChangeEvent of *SplitPanel

New properties have been added to SplitPositionChangeEvent:
oldSplitPositionUnit, oldSplitPosition, userOriginated

Resolves #9472

server/src/main/java/com/vaadin/ui/AbstractSplitPanel.java
server/src/test/java/com/vaadin/ui/SplitPositionChangeListenerTest.java

index 93afdcfc2a07fefd3f8c5b0456989332f809e6d5..898a25674fd78f7c6c26ecd817ccb0bab7579dc7 100644 (file)
@@ -64,9 +64,12 @@ public abstract class AbstractSplitPanel extends AbstractComponentContainer {
 
         @Override
         public void setSplitterPosition(float position) {
+            float oldPosition = getSplitPosition();
+
             getSplitterState().position = position;
-            fireEvent(new SplitPositionChangeEvent(AbstractSplitPanel.this,
-                    position, getSplitPositionUnit()));
+
+            fireEvent(new SplitPositionChangeEvent(AbstractSplitPanel.this, true,
+                    oldPosition, getSplitPositionUnit(), position, getSplitPositionUnit()));
         }
     };
 
@@ -331,13 +334,16 @@ public abstract class AbstractSplitPanel extends AbstractComponentContainer {
         if (unit != Unit.PERCENTAGE) {
             pos = Math.round(pos);
         }
+        float oldPosition = getSplitPosition();
+        Unit oldUnit = getSplitPositionUnit();
+
         SplitterState splitterState = getSplitterState();
         splitterState.position = pos;
         splitterState.positionUnit = unit.getSymbol();
         splitterState.positionReversed = reverse;
         posUnit = unit;
-        fireEvent(new SplitPositionChangeEvent(AbstractSplitPanel.this, pos,
-                posUnit));
+        fireEvent(new SplitPositionChangeEvent(AbstractSplitPanel.this, false,
+                oldPosition, oldUnit, pos, posUnit));
     }
 
     /**
@@ -559,24 +565,84 @@ public abstract class AbstractSplitPanel extends AbstractComponentContainer {
      */
     public static class SplitPositionChangeEvent extends Component.Event {
 
+        private final float oldPosition;
+        private final Unit oldUnit;
+
         private final float position;
         private final Unit unit;
 
+        private final boolean userOriginated;
+
         public SplitPositionChangeEvent(final Component source,
-                final float position, final Unit unit) {
+                                        final boolean userOriginated,
+                                        final float oldPosition, final Unit oldUnit,
+                                        final float position, final Unit unit) {
             super(source);
+            this.userOriginated = userOriginated;
+            this.oldUnit = oldUnit;
+            this.oldPosition = oldPosition;
             this.position = position;
             this.unit = unit;
         }
 
+        /**
+         * Returns the new split position that triggered this change event.
+         *
+         * @since 7.5.0
+         *
+         * @return the new value of split position
+         */
         public float getSplitPosition() {
             return position;
         }
 
+        /**
+         * Returns the new split position unit that triggered this change event.
+         *
+         * @since 7.5.0
+         *
+         * @return the new value of split position
+         */
         public Unit getSplitPositionUnit() {
             return unit;
         }
 
+        /**
+         * Returns the position of the split before this change event occurred.
+         *
+         * @since 8.1
+         *
+         * @return the split position previously set to the source of this event
+         */
+        public float getOldSplitPosition() {
+            return oldPosition;
+        }
+
+        /**
+         * Returns the position unit of the split before this change event
+         * occurred.
+         *
+         * @since 8.1
+         *
+         * @return the split position unit previously set to the source of
+         *         this event
+         */
+        public Unit getOldSplitPositionUnit() {
+            return oldUnit;
+        }
+
+        /**
+         * Returns whether this event was triggered by user interaction, on the
+         * client side, or programmatically, on the server side.
+         *
+         * @since 8.1
+         *
+         * @return {@code true} if this event originates from the client,
+         *         {@code false} otherwise.
+         */
+        public boolean isUserOriginated() {
+            return userOriginated;
+        }
     }
 
     public Registration addSplitterClickListener(
index 10bde6e15116e0a04ef3f6bbb37046a07f73b79d..2c2509560fad0ec41725d7b2a77e30473c095f33 100644 (file)
  */
 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;
+import org.junit.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 
 /**
  * Test for {@link SplitPositionChangeListener}
@@ -42,4 +46,29 @@ public class SplitPositionChangeListenerTest {
         verify(splitPositionChangeListener)
                 .onSplitPositionChanged(any(SplitPositionChangeEvent.class));
     }
+
+    @Test
+    public void testSplitPositionListenerContainsOldValues() throws Exception {
+        final HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
+
+        float previousPosition = 50.0f;
+        float newPosition = 125.0f;
+
+        AtomicBoolean executed = new AtomicBoolean(false);
+
+        splitPanel.setSplitPosition(previousPosition, Unit.PERCENTAGE);
+        splitPanel.addSplitPositionChangeListener(event -> {
+            assertFalse(event.isUserOriginated());
+
+            assertTrue(previousPosition == event.getOldSplitPosition());
+            assertEquals(Unit.PERCENTAGE, event.getOldSplitPositionUnit());
+
+            assertTrue(newPosition == event.getSplitPosition());
+            assertEquals(Unit.PIXELS, event.getSplitPositionUnit());
+
+            executed.set(true);
+        });
+        splitPanel.setSplitPosition(newPosition, Unit.PIXELS);
+        assertTrue(executed.get());
+    }
 }