From 820bd060fad2bf0c1763aba2564183ad65eb008c Mon Sep 17 00:00:00 2001 From: Joonas Lehtinen Date: Wed, 27 Jun 2007 09:15:37 +0000 Subject: [PATCH] Implemented missing features for Custom Layout, reviewed and commented code svn changeset:1793/svn branch:trunk --- .../terminal/gwt/client/ui/ICustomLayout.java | 132 ++++++++++++++---- 1 file changed, 102 insertions(+), 30 deletions(-) diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/ICustomLayout.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/ICustomLayout.java index 6fe29ab7d0..0efe36ee6b 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/ui/ICustomLayout.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/ICustomLayout.java @@ -16,43 +16,79 @@ import com.itmill.toolkit.terminal.gwt.client.Layout; import com.itmill.toolkit.terminal.gwt.client.Paintable; import com.itmill.toolkit.terminal.gwt.client.UIDL; +/** + * Custom Layout implements complext layouting defined with HTML template. + * + * @author IT Mill + * + */ public class ICustomLayout extends ComplexPanel implements Paintable, Layout { + /** Location-name to containing element in DOM map */ private HashMap locationToElement = new HashMap(); - + + /** Location-name to contained widget map */ private HashMap locationToWidget = new HashMap(); + /** Widget to captionwrapper map */ + private HashMap widgetToCaptionWrapper = new HashMap(); + + /** Currently rendered style */ String currentStyle; - String scripts = ""; - - String pid; + /** Unexecuted scripts loaded from the template */ + private String scripts = ""; + + /** Paintable ID of this paintable */ + private String pid; public ICustomLayout() { setElement(DOM.createDiv()); } + /** + * Add new widget to the map with location name. + * + * @throws IllegalArgumentException + * if no such location is found in the layout. + */ public void add(Widget widget, String location) { + + if (widget == null) + return; + + // If no given location is found in the layout, and exception is throws Element elem = (Element) locationToElement.get(location); if (elem == null) { - throw new NoSuchElementException(); + throw new IllegalArgumentException("No location " + location + + " found"); } + + // Remove previous widget if there is one Widget previous = (Widget) locationToWidget.get(location); - if (widget.equals(previous)) return; + if (previous == widget) + return; remove(previous); + + // Add widget to location super.add(widget, elem); - locationToWidget.put(location,widget); + locationToWidget.put(location, widget); } + /** Update the layout from UIDL */ public void updateFromUIDL(UIDL uidl, Client client) { + // Client manages general cases if (client.updateComponent(this, uidl, false)) return; + // Update PID pid = uidl.getId(); - + + // Update HTML template if needed updateHTML(uidl, client); + // For all contained widgets for (Iterator i = uidl.getChildIterator(); i.hasNext();) { UIDL uidlForChild = (UIDL) i.next(); if (uidlForChild.getTag().equals("location")) { @@ -60,44 +96,50 @@ public class ICustomLayout extends ComplexPanel implements Paintable, Layout { Widget child = client.getWidget(uidlForChild.getChildUIDL(0)); try { add(child, location); - } catch (Exception e) { + ((Paintable) child).updateFromUIDL(uidlForChild + .getChildUIDL(0), client); + } catch (IllegalArgumentException e) { // If no location is found, this component is not visible } - ((Paintable) child).updateFromUIDL( - uidlForChild.getChildUIDL(0), client); - } } - } /** Update implementing HTML-layout if needed. */ private void updateHTML(UIDL uidl, Client client) { + + // Update only if style has changed String newStyle = uidl.getStringAttribute("style"); if (currentStyle != null && currentStyle.equals(newStyle)) return; + // Get the HTML-template from client String template = client.getResource("layout/" + newStyle + ".html"); if (template == null) { - template = "Layout " + newStyle + " is missing"; + template = "Layout file layout/" + newStyle + ".html is missing."; } else { currentStyle = newStyle; } + + // Connect body of the template to DOM template = extractBodyAndScriptsFromTemplate(template); DOM.setInnerHTML(getElement(), template); + // Remap locations to elements locationToElement.clear(); scanForLocations(getElement()); + // Remap image srcs in layout Widget parent = getParent(); while (parent != null && !(parent instanceof IWindow)) parent = parent.getParent(); if (parent != null && ((IWindow) parent).getTheme() != null) ; - prefixImgSrcs(getElement(), "../theme/" - + ((IWindow) parent).getTheme() + "/layout/"); + prefixImgSrcs(getElement(), "../theme/" + ((IWindow) parent).getTheme() + + "/layout/"); } + /** Collect locations from template */ private void scanForLocations(Element elem) { String location = getLocation(elem); @@ -106,20 +148,19 @@ public class ICustomLayout extends ComplexPanel implements Paintable, Layout { DOM.setInnerHTML(elem, ""); } else { int len = DOM.getChildCount(elem); - for (int i=0; i