From 3058750c7652c229d0beeaf562bc9d4658b62c33 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Thu, 18 Dec 2008 10:05:37 +0000 Subject: [PATCH] Refactored layout size checking method, related to #2139 svn changeset:6264/svn branch:trunk --- .../gwt/server/CommunicationManager.java | 4 +- ...ities.java => ComponentSizeValidator.java} | 114 +++++++++++------- .../itmill/toolkit/ui/AbstractComponent.java | 13 +- 3 files changed, 78 insertions(+), 53 deletions(-) rename src/com/itmill/toolkit/terminal/gwt/server/{DebugUtilities.java => ComponentSizeValidator.java} (91%) diff --git a/src/com/itmill/toolkit/terminal/gwt/server/CommunicationManager.java b/src/com/itmill/toolkit/terminal/gwt/server/CommunicationManager.java index acdc8665d1..270ea42cc7 100644 --- a/src/com/itmill/toolkit/terminal/gwt/server/CommunicationManager.java +++ b/src/com/itmill/toolkit/terminal/gwt/server/CommunicationManager.java @@ -54,7 +54,7 @@ import com.itmill.toolkit.terminal.VariableOwner; import com.itmill.toolkit.terminal.Paintable.RepaintRequestEvent; import com.itmill.toolkit.terminal.Terminal.ErrorEvent; import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection; -import com.itmill.toolkit.terminal.gwt.server.DebugUtilities.InvalidLayout; +import com.itmill.toolkit.terminal.gwt.server.ComponentSizeValidator.InvalidLayout; import com.itmill.toolkit.ui.AbstractField; import com.itmill.toolkit.ui.Component; import com.itmill.toolkit.ui.Upload; @@ -446,7 +446,7 @@ public class CommunicationManager implements Paintable.RepaintRequestListener { paintablePainted(p); if (analyzeLayouts) { - invalidComponentRelativeSizes = DebugUtilities + invalidComponentRelativeSizes = ComponentSizeValidator .validateComponentRelativeSizes(((Window) p) .getLayout(), null, null); } diff --git a/src/com/itmill/toolkit/terminal/gwt/server/DebugUtilities.java b/src/com/itmill/toolkit/terminal/gwt/server/ComponentSizeValidator.java similarity index 91% rename from src/com/itmill/toolkit/terminal/gwt/server/DebugUtilities.java rename to src/com/itmill/toolkit/terminal/gwt/server/ComponentSizeValidator.java index 01c0c94f74..cca56e3034 100644 --- a/src/com/itmill/toolkit/terminal/gwt/server/DebugUtilities.java +++ b/src/com/itmill/toolkit/terminal/gwt/server/ComponentSizeValidator.java @@ -24,7 +24,7 @@ import com.itmill.toolkit.ui.VerticalLayout; import com.itmill.toolkit.ui.Window; import com.itmill.toolkit.ui.GridLayout.Area; -public class DebugUtilities { +public class ComponentSizeValidator { private final static int LAYERS_SHOWN = 4; @@ -108,7 +108,11 @@ public class DebugUtilities { if (component instanceof Window) { return true; } - return !(component.getParent() != null && parentCannotDefineHeight(component)); + if (component.getParent() == null) { + return true; + } + + return parentCanDefineHeight(component); } catch (Exception e) { e.printStackTrace(); return true; @@ -123,7 +127,11 @@ public class DebugUtilities { if (component instanceof Window) { return true; } - return !(component.getParent() != null && parentCannotDefineWidth(component)); + if (component.getParent() == null) { + return true; + } + + return parentCanDefineWidth(component); } catch (Exception e) { e.printStackTrace(); return true; @@ -227,7 +235,7 @@ public class DebugUtilities { printServerError(msg, attributes, true, serverErrorStream); } if (subErrors.size() > 0) { - serverErrorStream.println("Sub erros >>"); + serverErrorStream.println("Sub errors >>"); clientJSON.write(", \"subErrors\" : ["); boolean first = true; for (InvalidLayout subError : subErrors) { @@ -377,32 +385,34 @@ public class DebugUtilities { return false; } - public static boolean parentCannotDefineHeight(Component component) { + public static boolean parentCanDefineHeight(Component component) { Component parent = component.getParent(); if (parent == null) { // main window, valid situation - return false; + return true; } if (parent.getHeight() < 0) { + // Undefined height if (parent instanceof Window) { Window w = (Window) parent; if (w.getParent() == null) { // main window is considered to have size - return false; + return true; } } - if (parent instanceof VerticalLayout) { - return true; - } else if (parent instanceof AbstractOrderedLayout) { + + if (parent instanceof AbstractOrderedLayout) { boolean horizontal = true; if (parent instanceof OrderedLayout) { horizontal = ((OrderedLayout) parent).getOrientation() == OrderedLayout.ORIENTATION_HORIZONTAL; + } else if (parent instanceof VerticalLayout) { + horizontal = false; } if (horizontal && hasNonRelativeHeightComponent((AbstractOrderedLayout) parent)) { - return false; - } else { return true; + } else { + return false; } } else if (parent instanceof GridLayout) { @@ -420,6 +430,9 @@ public class DebugUtilities { } } if (!rowHasHeight) { + return false; + } else { + // Other components define row height return true; } } @@ -430,20 +443,24 @@ public class DebugUtilities { // height undefined, we know how how component works and no // exceptions // TODO horiz SplitPanel ?? - return true; + return false; } else { - // we cannot generally know if undefined component can serve + // We cannot generally know if undefined component can serve // space for children (like CustomLayout or component built by - // third party) - return false; + // third party) so we assume they can + return true; } - } else { - if (hasRelativeHeight(parent) && parent.getParent() != null) { - return parentCannotDefineHeight(parent); + } else if (hasRelativeHeight(parent)) { + // Relative height + if (parent.getParent() != null) { + return parentCanDefineHeight(parent); } else { - return false; + return true; } + } else { + // Absolute height + return true; } } @@ -467,30 +484,25 @@ public class DebugUtilities { && paintable.getWidthUnits() == Sizeable.UNITS_PERCENTAGE; } - public static boolean parentCannotDefineWidth(Component component) { + public static boolean parentCanDefineWidth(Component component) { Component parent = component.getParent(); if (parent == null) { // main window, valid situation - return false; + return true; } if (parent instanceof Window) { Window w = (Window) parent; if (w.getParent() == null) { // main window is considered to have size - return false; + return true; } } if (parent.getWidth() < 0) { - if (parent instanceof Panel) { - if (parent.getCaption() != null - && !parent.getCaption().equals("")) { - return false; - } else { - return true; - } - } else if (parent instanceof AbstractOrderedLayout) { + // Undefined width + + if (parent instanceof AbstractOrderedLayout) { AbstractOrderedLayout ol = (AbstractOrderedLayout) parent; boolean horizontal = true; if (ol instanceof OrderedLayout) { @@ -501,12 +513,10 @@ public class DebugUtilities { horizontal = false; } - if (horizontal) { - return true; - } else if (!hasNonRelativeWidthComponent(ol)) { + if (!horizontal && hasNonRelativeWidthComponent(ol)) { + // valid situation, other components defined width return true; } else { - // valid situation, other components defined width return false; } } else if (parent instanceof GridLayout) { @@ -523,25 +533,37 @@ public class DebugUtilities { } } if (!columnHasWidth) { - return true; - } else { - // valid situation return false; + } else { + // Other components define column width + return true; } } else if (parent instanceof SplitPanel || parent instanceof TabSheet || parent instanceof CustomComponent) { // TODO vertical splitpanel with another non relative component? - return true; - } else { return false; - } - } else { - if (hasRelativeWidth(parent) && parent.getParent() != null) { - return parentCannotDefineWidth(parent); - } else { + } else if (parent instanceof Panel) { + // TODO Panel should be able to define width based on caption return false; + // if (parent.getCaption() != null + // && !parent.getCaption().equals("")) { + // return true; + // } else { + // return false; + // } + } else { + return true; + } + } else if (hasRelativeWidth(parent)) { + // Relative width + if (parent.getParent() == null) { + return true; } + + return parentCanDefineWidth(parent); + } else { + return true; } } @@ -591,7 +613,7 @@ public class DebugUtilities { } cls = Class.forName(className); - if (cls == DebugUtilities.class || cls == Thread.class) { + if (cls == ComponentSizeValidator.class || cls == Thread.class) { continue; } diff --git a/src/com/itmill/toolkit/ui/AbstractComponent.java b/src/com/itmill/toolkit/ui/AbstractComponent.java index 94e194d351..0d7f40d5b2 100644 --- a/src/com/itmill/toolkit/ui/AbstractComponent.java +++ b/src/com/itmill/toolkit/ui/AbstractComponent.java @@ -22,7 +22,7 @@ import com.itmill.toolkit.terminal.PaintException; import com.itmill.toolkit.terminal.PaintTarget; import com.itmill.toolkit.terminal.Resource; import com.itmill.toolkit.terminal.Terminal; -import com.itmill.toolkit.terminal.gwt.server.DebugUtilities; +import com.itmill.toolkit.terminal.gwt.server.ComponentSizeValidator; /** * An abstract class that defines default implementation for the @@ -135,6 +135,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource * Constructs a new Component. */ public AbstractComponent() { + // ComponentSizeValidator.setCreationLocation(this); } /* Get/Set component properties */ @@ -610,14 +611,14 @@ public abstract class AbstractComponent implements Component, MethodEventSource // Only paint content of visible components. if (isVisible()) { if (getHeight() >= 0 - && (getHeightUnits() != UNITS_PERCENTAGE || !DebugUtilities - .parentCannotDefineHeight(this))) { + && (getHeightUnits() != UNITS_PERCENTAGE || ComponentSizeValidator + .parentCanDefineHeight(this))) { target.addAttribute("height", "" + getCSSHeight()); } if (getWidth() >= 0 - && (getWidthUnits() != UNITS_PERCENTAGE || !DebugUtilities - .parentCannotDefineWidth(this))) { + && (getWidthUnits() != UNITS_PERCENTAGE || ComponentSizeValidator + .parentCanDefineWidth(this))) { target.addAttribute("width", "" + getCSSWidth()); } if (styles != null && styles.size() > 0) { @@ -1107,6 +1108,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource this.height = height; heightUnit = unit; requestRepaint(); + // ComponentSizeValidator.setHeightLocation(this); } /* @@ -1158,6 +1160,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource this.width = width; widthUnit = unit; requestRepaint(); + // ComponentSizeValidator.setWidthLocation(this); } /* -- 2.39.5