]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix for #2180 - Tabsheet with undefined width
authorArtur Signell <artur.signell@itmill.com>
Thu, 30 Oct 2008 12:24:40 +0000 (12:24 +0000)
committerArtur Signell <artur.signell@itmill.com>
Thu, 30 Oct 2008 12:24:40 +0000 (12:24 +0000)
svn changeset:5770/svn branch:trunk

src/com/itmill/toolkit/terminal/gwt/client/Util.java
src/com/itmill/toolkit/terminal/gwt/client/ui/ITabsheet.java
src/com/itmill/toolkit/tests/tickets/Ticket2180.java [new file with mode: 0644]

index 2e31b42cc8278b7c1c6a4e554abd720a155be4a7..cc25f1aad5d91daa1b34885012504fec1faeb90d 100644 (file)
@@ -212,18 +212,30 @@ public class Util {
 
     public static int measureHorizontalBorder(Element element) {
         int borders;
-        if (BrowserInfo.get().isIE6()) {
-            String originalWidth = DOM.getStyleAttribute(element, "width");
-            int originalOffsetWidth = element.getOffsetWidth();
-            if (originalOffsetWidth < 1) {
-                originalOffsetWidth = 10;
+        if (BrowserInfo.get().isIE()) {
+            String width = element.getStyle().getProperty("width");
+            String height = element.getStyle().getProperty("height");
+
+            int offsetWidth = element.getOffsetWidth();
+            int offsetHeight = element.getOffsetHeight();
+            if (BrowserInfo.get().isIE6()) {
+                if (offsetHeight < 1) {
+                    offsetHeight = 1;
+                }
+                if (offsetWidth < 1) {
+                    offsetWidth = 10;
+                }
+                element.getStyle().setPropertyPx("height", offsetHeight);
             }
+            element.getStyle().setPropertyPx("width", offsetWidth);
 
-            DOM.setStyleAttribute(element, "width", originalOffsetWidth + "px");
-            int cw = element.getPropertyInt("clientWidth");
-            borders = element.getOffsetWidth() - cw;
+            borders = element.getOffsetWidth()
+                    - element.getPropertyInt("clientWidth");
 
-            DOM.setStyleAttribute(element, "width", originalWidth);
+            element.getStyle().setProperty("width", width);
+            if (BrowserInfo.get().isIE6()) {
+                element.getStyle().setProperty("height", height);
+            }
         } else {
             borders = element.getOffsetWidth()
                     - element.getPropertyInt("clientWidth");
index 98c15b4790b8d1704d9c1785c3d33637f357fd68..6c5343cdd082010c686ba281943b63d59867f337 100644 (file)
@@ -8,6 +8,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Set;
 
+import com.google.gwt.dom.client.Style;
 import com.google.gwt.user.client.Command;
 import com.google.gwt.user.client.DOM;
 import com.google.gwt.user.client.DeferredCommand;
@@ -163,6 +164,10 @@ public class ITabsheet extends ITabsheetBase implements
         }
     }
 
+    private boolean isDynamicWidth() {
+        return width == null || width.equals("");
+    }
+
     public ITabsheet() {
         super(CLASSNAME);
 
@@ -271,7 +276,7 @@ public class ITabsheet extends ITabsheetBase implements
         }
 
         // tabs; push or not
-        if (uidl.hasAttribute("width")) {
+        if (!isDynamicWidth()) {
             // update width later, in updateTabScroller();
             DOM.setStyleAttribute(tabs, "width", "1px");
             DOM.setStyleAttribute(tabs, "overflow", "hidden");
@@ -279,12 +284,52 @@ public class ITabsheet extends ITabsheetBase implements
             showAllTabs();
             DOM.setStyleAttribute(tabs, "width", "");
             DOM.setStyleAttribute(tabs, "overflow", "visible");
+            updateDynamicWidth();
         }
 
         updateTabScroller();
         waitingForResponse = false;
     }
 
+    private void updateDynamicWidth() {
+        // Find tab width
+        int tabsWidth = 0;
+
+        int count = tb.getTabCount();
+        for (int i = 0; i < count; i++) {
+            Element tabTd = tb.getTab(i).getElement().getParentElement().cast();
+            tabsWidth += tabTd.getOffsetWidth();
+        }
+
+        // Find content width
+        Style style = tp.getElement().getStyle();
+        String overflow = style.getProperty("overflow");
+        style.setProperty("overflow", "hidden");
+        String width = style.getProperty("width");
+        style.setProperty("width", tabsWidth + "px");
+        // Get content width from actual widget
+        int contentWidth = tp.getWidget(tp.getVisibleWidget()).getOffsetWidth();
+        style.setProperty("width", width);
+        style.setProperty("overflow", overflow);
+
+        // Set widths to max(tabs,content)
+        if (tabsWidth < contentWidth) {
+            tabsWidth = contentWidth;
+        }
+
+        tabs.getStyle().setPropertyPx("width", tabsWidth);
+
+        /*
+         * tb width includes the spacerTd width so the content area will be as
+         * wide as the tab bar
+         */
+        int realWidth = tb.getOffsetWidth();
+        contentWidth = realWidth - getContentAreaBorderWidth();
+
+        contentNode.getStyle().setPropertyPx("width", contentWidth);
+        super.setWidth(realWidth + "px");
+    }
+
     protected void renderTab(final UIDL tabUidl, int index, boolean selected) {
         ICaption c = tb.getTab(index);
         if (c == null) {
@@ -379,12 +424,18 @@ public class ITabsheet extends ITabsheetBase implements
 
     public void setWidth(String width) {
         super.setWidth(width);
+        if (width.equals("")) {
+            width = null;
+        }
         this.width = width;
-        if ("".equals(width)) {
+        if (width == null) {
             renderSpace.setWidth(0);
             contentNode.getStyle().setProperty("width", "");
         } else {
             int contentWidth = getOffsetWidth() - getContentAreaBorderWidth();
+            if (contentWidth < 0) {
+                contentWidth = 0;
+            }
             contentNode.getStyle().setProperty("width", contentWidth + "px");
             renderSpace.setWidth(contentWidth);
         }
@@ -508,17 +559,9 @@ public class ITabsheet extends ITabsheetBase implements
 
     private int borderW = -1;
 
-    private void detectBorder() {
-        String property = contentNode.getStyle().getProperty("overflow");
-        contentNode.getStyle().setProperty("overflow", "hidden");
-        borderW = contentNode.getOffsetWidth()
-                - contentNode.getPropertyInt("clientWidth");
-        contentNode.getStyle().setProperty("overflow", property);
-    }
-
     private int getContentAreaBorderWidth() {
         if (borderW < 0) {
-            detectBorder();
+            borderW = Util.measureHorizontalBorder(contentNode);
         }
         return borderW;
     }
diff --git a/src/com/itmill/toolkit/tests/tickets/Ticket2180.java b/src/com/itmill/toolkit/tests/tickets/Ticket2180.java
new file mode 100644 (file)
index 0000000..6a966d3
--- /dev/null
@@ -0,0 +1,41 @@
+package com.itmill.toolkit.tests.tickets;
+
+import com.itmill.toolkit.Application;
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.Label;
+import com.itmill.toolkit.ui.TabSheet;
+import com.itmill.toolkit.ui.Window;
+
+public class Ticket2180 extends Application {
+
+    private Window mainWindow;
+    private TabSheet tabSheet;
+
+    @Override
+    public void init() {
+        mainWindow = new Window("Tabsheet should cause scrollbars");
+        setMainWindow(mainWindow);
+        // mainWindow.getLayout().setSizeFull();
+        tabSheet = new TabSheet();
+        // tabSheet.setWidth("100%");
+        Button button = new Button("Blah");
+        button.setWidth("100%");
+        Label label1 = new Label("Lorem ipsum");
+        Label label2 = new Label("Lorem");
+        Label label3 = new Label(
+                "Lorema jsdfhak sjdfh kajsdh fkajhd kfjah dkfjah ksfdjh kajsfh kj 1 2 3 4 5 6 7 8 9 10");
+
+        label3.setWidth("800px");
+        tabSheet.addTab(label1, "Tab 1", null);
+        tabSheet.addTab(label2, "Tab 2", null);
+        tabSheet.addTab(label3, "Tab 3", null);
+        tabSheet.addTab(new Label("a"), "Tab 4", null);
+        tabSheet.addTab(new Label("a"), "Tab 5", null);
+        tabSheet.addTab(new Label("a"), "Tab 6", null);
+        // mainWindow.addComponent(new Label("123"));
+        mainWindow.addComponent(tabSheet);
+        mainWindow.addComponent(button);
+        // mainWindow.addComponent(new Label("abc"));
+    }
+
+}