From ccbed3fcfa5870c376014036bde8455aaa952384 Mon Sep 17 00:00:00 2001 From: Matti Hosio Date: Wed, 3 Dec 2014 13:41:05 +0200 Subject: Declarative support for AbsoluteLayout (#7749) Change-Id: Icd90d78b3ddd14ffaef48f610c043e7a816db106 --- server/src/com/vaadin/ui/AbsoluteLayout.java | 110 +++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) (limited to 'server/src/com/vaadin/ui/AbsoluteLayout.java') diff --git a/server/src/com/vaadin/ui/AbsoluteLayout.java b/server/src/com/vaadin/ui/AbsoluteLayout.java index afc73f5ecc..c4c0de764f 100644 --- a/server/src/com/vaadin/ui/AbsoluteLayout.java +++ b/server/src/com/vaadin/ui/AbsoluteLayout.java @@ -21,6 +21,10 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.nodes.Node; + import com.vaadin.event.LayoutEvents.LayoutClickEvent; import com.vaadin.event.LayoutEvents.LayoutClickListener; import com.vaadin.event.LayoutEvents.LayoutClickNotifier; @@ -30,6 +34,8 @@ import com.vaadin.shared.EventId; import com.vaadin.shared.MouseEventDetails; import com.vaadin.shared.ui.absolutelayout.AbsoluteLayoutServerRpc; import com.vaadin.shared.ui.absolutelayout.AbsoluteLayoutState; +import com.vaadin.ui.declarative.DesignAttributeHandler; +import com.vaadin.ui.declarative.DesignContext; /** * AbsoluteLayout is a layout implementation that mimics html absolute @@ -40,6 +46,13 @@ import com.vaadin.shared.ui.absolutelayout.AbsoluteLayoutState; public class AbsoluteLayout extends AbstractLayout implements LayoutClickNotifier { + // constants for design attributes + private static final String ATTR_TOP = ":top"; + private static final String ATTR_RIGHT = ":right"; + private static final String ATTR_BOTTOM = ":bottom"; + private static final String ATTR_LEFT = ":left"; + private static final String ATTR_Z_INDEX = ":z-index"; + private AbsoluteLayoutServerRpc rpc = new AbsoluteLayoutServerRpc() { @Override @@ -660,4 +673,101 @@ public class AbsoluteLayout extends AbstractLayout implements public void removeListener(LayoutClickListener listener) { removeLayoutClickListener(listener); } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.ui.AbstractComponent#synchronizeFromDesign(org.jsoup.nodes + * .Node, com.vaadin.ui.declarative.DesignContext) + */ + @Override + public void synchronizeFromDesign(Element design, + DesignContext designContext) { + // process default attributes + super.synchronizeFromDesign(design, designContext); + // remove current children + removeAllComponents(); + // handle children + for (Element childComponent : design.children()) { + Attributes attr = childComponent.attributes(); + DesignSynchronizable newChild = designContext + .createChild(childComponent); + StringBuilder css = new StringBuilder(); + if (attr.hasKey(ATTR_TOP)) { + css.append("top:").append(attr.get(ATTR_TOP)).append(";"); + } + if (attr.hasKey(ATTR_RIGHT)) { + css.append("right:").append(attr.get(ATTR_RIGHT)).append(";"); + } + if (attr.hasKey(ATTR_BOTTOM)) { + css.append("bottom:").append(attr.get(ATTR_BOTTOM)).append(";"); + } + if (attr.hasKey(ATTR_LEFT)) { + css.append("left:").append(attr.get(ATTR_LEFT)).append(";"); + } + if (attr.hasKey(ATTR_Z_INDEX)) { + css.append("z-index:").append(attr.get(ATTR_Z_INDEX)) + .append(";"); + } + addComponent(newChild, css.toString()); + } + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.ui.AbstractComponent#synchronizeToDesign(org.jsoup.nodes.Node, + * com.vaadin.ui.declarative.DesignContext) + */ + @Override + public void synchronizeToDesign(Element design, DesignContext designContext) { + super.synchronizeToDesign(design, designContext); + // handle children + Element designElement = design; + for (Component child : this) { + DesignSynchronizable childComponent = (DesignSynchronizable) child; + Element childNode = designContext.createNode(childComponent); + designElement.appendChild(childNode); + childComponent.synchronizeToDesign(childNode, designContext); + // handle position + ComponentPosition position = getPosition(child); + writePositionAttribute(childNode, ATTR_TOP, position.getTopUnits() + .getSymbol(), position.getTopValue()); + writePositionAttribute(childNode, ATTR_RIGHT, position + .getRightUnits().getSymbol(), position.getRightValue()); + writePositionAttribute(childNode, ATTR_BOTTOM, position + .getBottomUnits().getSymbol(), position.getBottomValue()); + writePositionAttribute(childNode, ATTR_LEFT, position + .getLeftUnits().getSymbol(), position.getLeftValue()); + // handle z-index + if (position.getZIndex() >= 0) { + childNode.attr(ATTR_Z_INDEX, String.valueOf(position.zIndex)); + } + } + } + + /** + * Private method for writing position attributes + * + * @since + * @param node + * target node + * @param key + * attribute key + * @param symbol + * value symbol + * @param value + * the value + */ + private void writePositionAttribute(Node node, String key, String symbol, + Float value) { + if (value != null) { + String valueString = DesignAttributeHandler.formatFloat(value + .floatValue()); + node.attr(key, valueString + symbol); + } + } + } -- cgit v1.2.3