]> source.dussan.org Git - vaadin-framework.git/commitdiff
fixes for windows size & implemented scroll positions
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Tue, 2 Oct 2007 07:15:59 +0000 (07:15 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Tue, 2 Oct 2007 07:15:59 +0000 (07:15 +0000)
svn changeset:2407/svn branch:trunk

src/com/itmill/toolkit/terminal/gwt/client/ui/IWindow.java
src/com/itmill/toolkit/terminal/gwt/public/default/window/window.css
src/com/itmill/toolkit/tests/TestForWindowing.java
src/com/itmill/toolkit/ui/Panel.java
src/com/itmill/toolkit/ui/Window.java

index 92bf3d4b97dec7e8e1a5be77519a44ed965a004a..98626adcfb04aca8829c23489590fafb3932c58f 100644 (file)
@@ -7,6 +7,8 @@ import com.google.gwt.user.client.Element;
 import com.google.gwt.user.client.Event;
 import com.google.gwt.user.client.ui.KeyboardListenerCollection;
 import com.google.gwt.user.client.ui.PopupPanel;
+import com.google.gwt.user.client.ui.ScrollListener;
+import com.google.gwt.user.client.ui.ScrollPanel;
 import com.google.gwt.user.client.ui.Widget;
 import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
 import com.itmill.toolkit.terminal.gwt.client.Paintable;
@@ -19,7 +21,7 @@ import com.itmill.toolkit.terminal.gwt.client.UIDL;
  * 
  * @author IT Mill Ltd
  */
-public class IWindow extends PopupPanel implements Paintable {
+public class IWindow extends PopupPanel implements Paintable, ScrollListener {
 
        private static Vector windowOrder = new Vector();
 
@@ -44,6 +46,8 @@ public class IWindow extends PopupPanel implements Paintable {
        private Element footer;
 
        private Element resizeBox;
+       
+       private ScrollPanel contentPanel = new ScrollPanel();
 
        private boolean dragging;
 
@@ -90,6 +94,7 @@ public class IWindow extends PopupPanel implements Paintable {
                constructDOM();
                setPopupPosition(order * STACKING_OFFSET_PIXELS, order
                                * STACKING_OFFSET_PIXELS);
+               contentPanel.addScrollListener(this);
        }
 
        private void bringToFront() {
@@ -145,7 +150,11 @@ public class IWindow extends PopupPanel implements Paintable {
                DOM.appendChild(wrapper, header);
                DOM.appendChild(wrapper, contents);
                DOM.appendChild(wrapper, footer);
-               setPixelSize(400, 200);
+               setWidget(contentPanel);
+               
+               // set default size
+               setWidth(400 + "px");
+               setHeight(300 + "px");
        }
 
        public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
@@ -161,23 +170,19 @@ public class IWindow extends PopupPanel implements Paintable {
                } else {
 
                        // Initialize the width from UIDL
-                       try {
+                       if(uidl.hasVariable("width")) {
                                String width = uidl.getStringVariable("width");
-                               String height = uidl.getStringVariable("width");
-                               if (width != null && width.endsWith("px")) {
-                                       uidlWidth = Integer.parseInt(width.substring(0, width
-                                                       .length() - 2));
-                                       setPixelWidth(uidlWidth);
-                               }
-                               if (height != null && height.endsWith("px")) {
-                                       uidlHeight = Integer.parseInt(height.substring(0, height
-                                                       .length() - 2));
-                                       setPixelHeight(uidlHeight);
-                               }
-                       } catch (IllegalArgumentException e) {
-                               // Silently ignored as width and height are not required
-                               // parameters
+                               setWidth(width);
+                       }
+                       if(uidl.hasVariable("height")) {
+                               String height = uidl.getStringVariable("height");
+                               setHeight(height);
                        }
+                       
+                       contentPanel.setScrollPosition(
+                                       uidl.getIntVariable("scrolltop"));
+                       contentPanel.setHorizontalScrollPosition(
+                                       uidl.getIntVariable("scrollleft"));
 
                        // Initialize the position form UIDL
                        try {
@@ -201,13 +206,13 @@ public class IWindow extends PopupPanel implements Paintable {
                        if (layout != lo) {
                                // remove old
                                client.unregisterPaintable(layout);
-                               this.remove((Widget) layout);
+                               contentPanel.remove((Widget) layout);
                                // add new
-                               setWidget((Widget) lo);
+                               contentPanel.setWidget((Widget) lo);
                                layout = lo;
                        }
                } else {
-                       setWidget((Widget) lo);
+                       contentPanel.setWidget((Widget) lo);
                }
                if (uidl.hasAttribute("caption")) {
                        setCaption(uidl.getStringAttribute("caption"));
@@ -242,37 +247,7 @@ public class IWindow extends PopupPanel implements Paintable {
        public void setCaption(String c) {
                DOM.setInnerHTML(header, c);
        }
-
-       public void setPixelSize(int width, int height) {
-               setPixelHeight(height);
-               setPixelWidth(width);
-       }
-
-       public void setPixelWidth(int width) {
-               DOM.setStyleAttribute(contents, "width",
-                               (width - BORDER_WIDTH_HORIZONTAL) + "px");
-               DOM.setStyleAttribute(header, "width",
-                               (width - BORDER_WIDTH_HORIZONTAL) + "px");
-               DOM.setStyleAttribute(footer, "width",
-                               (width - BORDER_WIDTH_HORIZONTAL) + "px");
-               DOM.setStyleAttribute(getElement(), "width", width + "px");
-               if (width != uidlWidth && client != null) {
-                       client.updateVariable(id, "width", width, false);
-                       uidlWidth = width;
-               }
-       }
-
-       public void setPixelHeight(int height) {
-               DOM.setStyleAttribute(contents, "height",
-                               (height - BORDER_WIDTH_VERTICAL) + "px");
-               DOM.setStyleAttribute(getElement(), "height", height + "px");
-               if (height != uidlHeight && client != null) {
-                       client.updateVariable(id, "height", height, false);
-                       uidlHeight = height;
-               }
-
-       }
-
+       
        protected Element getContainerElement() {
                return contents;
        }
@@ -317,16 +292,11 @@ public class IWindow extends PopupPanel implements Paintable {
                case Event.ONMOUSEUP:
                        resizing = false;
                        DOM.removeEventPreview(this);
+                       setSize(event, true);
                        break;
                case Event.ONMOUSEMOVE:
                        if (resizing) {
-                               int w = DOM.eventGetScreenX(event) - startX + origW;
-                               if (w < 60)
-                                       w = 60;
-                               int h = DOM.eventGetScreenY(event) - startY + origH;
-                               if (h < 60)
-                                       h = 60;
-                               setPixelSize(w, h);
+                               setSize(event, false);
                                DOM.eventPreventDefault(event);
                        }
                        break;
@@ -334,6 +304,27 @@ public class IWindow extends PopupPanel implements Paintable {
                        break;
                }
        }
+       
+       public void setSize(Event event, boolean updateVariables) {
+               int w = DOM.eventGetScreenX(event) - startX + origW;
+               if (w < 60)
+                       w = 60;
+               int h = DOM.eventGetScreenY(event) - startY + origH;
+               if (h < 60)
+                       h = 60;
+               setWidth(w + "px");
+               setHeight(h + "px");
+               if(updateVariables) {
+                       // sending width back always as pixels, no need for unit
+                       client.updateVariable(id, "width", w, false);
+                       client.updateVariable(id, "height", h, false);
+               }
+       }
+       
+       public void setWidth(String width) {
+               super.setWidth(width);
+               DOM.setStyleAttribute(header, "width", width);
+       }
 
        private void onHeaderEvent(Event event) {
                switch (DOM.eventGetType(event)) {
@@ -375,4 +366,9 @@ public class IWindow extends PopupPanel implements Paintable {
                return true;
        }
 
+       public void onScroll(Widget widget, int scrollLeft, int scrollTop) {
+               client.updateVariable(id, "scrolltop", scrollTop, false);
+               client.updateVariable(id, "scrollleft", scrollLeft, false);
+       }
+
 }
index a5cc9486b34cdb13d5f804d9e2bdc16aa14fd886..0b1b97b9712c569e0e10daaacea09719ec63b5a2 100644 (file)
@@ -7,6 +7,7 @@
        background: yellow;
        height:20px;
        overflow: hidden;
+       white-space: nowrap;
 }
 
 .i-window-footer {
@@ -15,7 +16,6 @@
 }
 
 .i-window-contents {
-       overflow:auto;
        background: #fff;
 }
 
index b007aa56a845263c19ca5061f5ea75422b6a68c9..78d51b4950e82f781a52aac067a522a165920401 100644 (file)
@@ -19,9 +19,13 @@ public class TestForWindowing extends Application {
                
                main.addComponent(new Button("Add new subwindow", new Button.ClickListener() {
                        public void buttonClick(ClickEvent event) {
-                               final Window w = new Window("Subwindow " + System.currentTimeMillis());
+                               final Window w = new Window("sw " + System.currentTimeMillis());
                                main.addWindow(w);
                                
+                               w.setWidth(100);
+                               w.setHeight(400);
+                               
+                               
                                Button closebutton = new Button("Close " + w.getCaption(), new Button.ClickListener() {
                                        public void buttonClick(ClickEvent event) {
                                                main.removeWindow(w);
@@ -29,6 +33,31 @@ public class TestForWindowing extends Application {
                                
                                });
                                w.addComponent(closebutton);
+                               
+                               w.addComponent(new Label("<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>" +
+                                               "<p>Lorem ipsum dolor sit amet.</p>", 
+                                               Label.CONTENT_XHTML));
+
                        }
                }));
                
@@ -43,6 +72,7 @@ public class TestForWindowing extends Application {
                        }
                }));
        
+               main.addComponent(new Button("Commit (saves window state: size, place, scrollpos)"));
        }
 
        public Window getWindow(String name) {
index 75d4b0cf5d124d8abdd46aa1050528ef79d433d4..19022851c03f95f6b61266ad9f80b5d768184fba 100644 (file)
@@ -350,10 +350,17 @@ public class Panel extends AbstractComponentContainer implements Sizeable,
                // Get new size
                Integer newWidth = (Integer) variables.get("width");
                Integer newHeight = (Integer) variables.get("height");
-               if (newWidth != null && newWidth.intValue() != getWidth())
+               if (newWidth != null && newWidth.intValue() != getWidth()) {
                        setWidth(newWidth.intValue());
-               if (newHeight != null && newHeight.intValue() != getHeight())
+                       // ensure units as we are reading pixels
+                       setWidthUnits(UNITS_PIXELS);
+                       
+               }
+               if (newHeight != null && newHeight.intValue() != getHeight()) {
                        setHeight(newHeight.intValue());
+                       // ensure units as we are reading pixels
+                       setHeightUnits(UNITS_PIXELS);
+               }
 
                // Scrolling
                Integer newScrollX = (Integer) variables.get("scrollleft");
index 3ac97df3523bbe21c1a6a566edfa438ad895b5cf..bea093221c00936e0548a28030335f7ed95ecdf5 100644 (file)
@@ -137,6 +137,16 @@ public class Window extends Panel implements URIHandler, ParameterHandler {
         * containing (main window) or -1 if unspecified .
         */
        private int positionX = -1;
+       
+       /**
+        * Distance scrolled from top in pixels.
+        */
+       private int scrollTop = 0;
+       
+       /**
+        * Distance scrolled from left in pixels.
+        */
+       private int scrollLeft = 0;
 
        /* ********************************************************************* */
 
@@ -470,6 +480,10 @@ public class Window extends Panel implements URIHandler, ParameterHandler {
                target.addVariable(this, "positionx", getPositionX());
                target.addVariable(this, "positiony", getPositionY());
 
+               // Window position
+               target.addVariable(this, "scrolltop", getScrollTop());
+               target.addVariable(this, "scrollleft", getScrollLeft());
+               
                // Window closing
                target.addVariable(this, "close", false);
 
@@ -799,6 +813,18 @@ public class Window extends Panel implements URIHandler, ParameterHandler {
                        setPositionY(y < 0 ? -1 : y);
                }
 
+               // Scroll position
+               Integer scrolltop = (Integer) variables.get("scrolltop");
+               if (scrolltop != null) {
+                       int top = scrolltop.intValue();
+                       setScrollTop(top < 0 ? 0 : top);
+               }
+               Integer scrollleft = (Integer) variables.get("scrollleft");
+               if (positiony != null) {
+                       int left = scrollleft.intValue();
+                       setScrollLeft(left < 0 ? 0 : left);
+               }
+
                // Closing
                Boolean close = (Boolean) variables.get("close");
                if (close != null && close.booleanValue()) {
@@ -1049,5 +1075,41 @@ public class Window extends Panel implements URIHandler, ParameterHandler {
        public Set getChildWindows() {
                return Collections.unmodifiableSet(subwindows);
        }
+       
+       /**
+        * Gets the current vertical scroll position of window.
+        * 
+        * @return pixels scrolled from top
+        */
+       public int getScrollTop() {
+               return scrollTop;
+       }
+
+       /**
+        * Scrolls window to given position.
+        * 
+        * @param scrollTop pixels to be scrolled from top
+        */
+       public void setScrollTop(int scrollTop) {
+               this.scrollTop = scrollTop;
+       }
+
+       /**
+        * Gets the current horizontal scroll position of window.
+        * 
+        * @return pixels scrolled from left
+        */
+       public int getScrollLeft() {
+               return scrollLeft;
+       }
+
+       /**
+        * Scrolls window to given position.
+        * 
+        * @param scrollLeft pixels to be scrolled from left
+        */
+       public void setScrollLeft(int scrollLeft) {
+               this.scrollLeft = scrollLeft;
+       }
 
 }