]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fixes #3322: SplitPanel with split position 100% doesn't resize correctly
authorJouni Koivuviita <jouni.koivuviita@itmill.com>
Fri, 5 Mar 2010 12:07:53 +0000 (12:07 +0000)
committerJouni Koivuviita <jouni.koivuviita@itmill.com>
Fri, 5 Mar 2010 12:07:53 +0000 (12:07 +0000)
 * Percentage sizes now remain percentages, even if the end user drags the split handle to a new position. An rounding to nearest percentage is done at that point.

svn changeset:11660/svn branch:6.2

src/com/vaadin/terminal/gwt/client/ui/VSplitPanel.java
src/com/vaadin/ui/SplitPanel.java

index d7a11427fb92e957bfba2f0a80a2a8332c7f6c69..101c1e9848baf00c1b86f4a62f8a6b80ee415307 100644 (file)
@@ -122,6 +122,9 @@ public class VSplitPanel extends ComplexPanel implements Container,
 
     private boolean rendering = false;
 
+    /* The current position of the split handle in either percentages or pixels */
+    private String position;
+
     public VSplitPanel() {
         this(ORIENTATION_HORIZONTAL);
     }
@@ -208,7 +211,8 @@ public class VSplitPanel extends ComplexPanel implements Container,
 
         setStylenames();
 
-        setSplitPosition(uidl.getStringAttribute("position"));
+        position = uidl.getStringAttribute("position");
+        setSplitPosition(position);
 
         final Paintable newFirstChild = client.getPaintable(uidl
                 .getChildUIDL(0));
@@ -257,14 +261,26 @@ public class VSplitPanel extends ComplexPanel implements Container,
     }
 
     private void setSplitPosition(String pos) {
+        if (pos == null) {
+            return;
+        }
+
+        // Convert percentage values to pixels
+        if (pos.indexOf("%") > 0) {
+            pos = Float.parseFloat(pos.substring(0, pos.length() - 1))
+                    / 100
+                    * (orientation == ORIENTATION_HORIZONTAL ? getOffsetWidth()
+                            : getOffsetHeight()) + "px";
+        }
+
         if (orientation == ORIENTATION_HORIZONTAL) {
             DOM.setStyleAttribute(splitter, "left", pos);
         } else {
             DOM.setStyleAttribute(splitter, "top", pos);
         }
+
         iLayout();
         client.runDescendentsLayout(this);
-
     }
 
     /*
@@ -441,9 +457,6 @@ public class VSplitPanel extends ComplexPanel implements Container,
             onVerticalMouseMove(y);
             break;
         }
-        iLayout();
-        // TODO Check if this is needed
-        client.runDescendentsLayout(this);
 
     }
 
@@ -455,7 +468,18 @@ public class VSplitPanel extends ComplexPanel implements Container,
         if (newX + getSplitterSize() > getOffsetWidth()) {
             newX = getOffsetWidth() - getSplitterSize();
         }
-        DOM.setStyleAttribute(splitter, "left", newX + "px");
+
+        if (position.indexOf("%") > 0) {
+            float pos = newX;
+            // 100% needs special handling
+            if (newX + getSplitterSize() >= getOffsetWidth()) {
+                pos = getOffsetWidth();
+            }
+            position = pos / getOffsetWidth() * 100 + "%";
+        }
+
+        setSplitPosition(newX + "px");
+
         if (origX != newX) {
             resized = true;
         }
@@ -470,7 +494,18 @@ public class VSplitPanel extends ComplexPanel implements Container,
         if (newY + getSplitterSize() > getOffsetHeight()) {
             newY = getOffsetHeight() - getSplitterSize();
         }
-        DOM.setStyleAttribute(splitter, "top", newY + "px");
+
+        if (position.indexOf("%") > 0) {
+            float pos = newY;
+            // 100% needs special handling
+            if (newY + getSplitterSize() >= getOffsetHeight()) {
+                pos = getOffsetHeight();
+            }
+            position = pos / getOffsetHeight() * 100 + "%";
+        }
+
+        setSplitPosition(newY + "px");
+
         if (origY != newY) {
             resized = true;
         }
@@ -554,9 +589,9 @@ public class VSplitPanel extends ComplexPanel implements Container,
 
         this.height = height;
         super.setHeight(height);
+
         if (!rendering && client != null) {
-            iLayout();
-            client.runDescendentsLayout(this);
+            setSplitPosition(position);
         }
     }
 
@@ -568,9 +603,9 @@ public class VSplitPanel extends ComplexPanel implements Container,
 
         this.width = width;
         super.setWidth(width);
+
         if (!rendering && client != null) {
-            iLayout();
-            client.runDescendentsLayout(this);
+            setSplitPosition(position);
         }
     }
 
@@ -627,12 +662,14 @@ public class VSplitPanel extends ComplexPanel implements Container,
      * Updates the new split position back to server.
      */
     private void updateSplitPositionToServer() {
-        // We always send pixel values to server
-        final String position = orientation == ORIENTATION_HORIZONTAL ? splitter
-                .getStyle().getProperty("left")
-                : splitter.getStyle().getProperty("top");
-        final int pos = Integer.parseInt(position.substring(0, position
-                .length() - 2));
+        int pos = 0;
+        if (position.indexOf("%") > 0) {
+            pos = Float.valueOf(position.substring(0, position.length() - 1))
+                    .intValue();
+        } else {
+            pos = Integer
+                    .parseInt(position.substring(0, position.length() - 2));
+        }
         client.updateVariable(id, "position", pos, immediate);
     }
 
index 5e67b9336924e42864e83a0c396b4490dfd10024..1a149d7394325619ce2d724d161173546dcdaa9f 100644 (file)
@@ -281,10 +281,11 @@ public class SplitPanel extends AbstractLayout {
      * Moves the position of the splitter.
      * 
      * @param pos
-     *            the new size of the first region in percentage
+     *            the new size of the first region in the unit that was last
+     *            used (default is percentage)
      */
     public void setSplitPosition(int pos) {
-        setSplitPosition(pos, UNITS_PERCENTAGE, true);
+        setSplitPosition(pos, posUnit, true);
     }
 
     /**
@@ -303,7 +304,7 @@ public class SplitPanel extends AbstractLayout {
      * Moves the position of the splitter.
      * 
      * @param pos
-     *            the new size of the first region in percentage
+     *            the new size of the first region
      * @param unit
      *            the unit (from {@link Sizeable}) in which the size is given.
      * @param repaintNotNeeded
@@ -312,6 +313,10 @@ public class SplitPanel extends AbstractLayout {
      *            knows the position.
      */
     private void setSplitPosition(int pos, int unit, boolean repaintNeeded) {
+        if (unit != UNITS_PERCENTAGE && unit != UNITS_PIXELS) {
+            throw new IllegalArgumentException(
+                    "Only percentage and pixel units are allowed");
+        }
         this.pos = pos;
         posUnit = unit;
         if (repaintNeeded) {
@@ -353,8 +358,7 @@ public class SplitPanel extends AbstractLayout {
 
         if (variables.containsKey("position") && !isLocked()) {
             Integer newPos = (Integer) variables.get("position");
-            // Client always sends pixel values. Repaint is not needed.
-            setSplitPosition(newPos, UNITS_PIXELS, false);
+            setSplitPosition(newPos, posUnit, false);
         }
 
         if (variables.containsKey(SPLITTER_CLICK_EVENT)) {