summaryrefslogtreecommitdiffstats
path: root/server/src/com
diff options
context:
space:
mode:
authorMatti Hosio <mhosio@vaadin.com>2014-12-03 13:41:05 +0200
committerMatti Hosio <mhosio@vaadin.com>2014-12-04 15:08:06 +0200
commitccbed3fcfa5870c376014036bde8455aaa952384 (patch)
tree9415a79a4e2a8a19c2e2a5db3b3d01c832ab3614 /server/src/com
parent9730b6def11af6f0bcb1e8b0535e0ec081ac84dd (diff)
downloadvaadin-framework-ccbed3fcfa5870c376014036bde8455aaa952384.tar.gz
vaadin-framework-ccbed3fcfa5870c376014036bde8455aaa952384.zip
Declarative support for AbsoluteLayout (#7749)
Change-Id: Icd90d78b3ddd14ffaef48f610c043e7a816db106
Diffstat (limited to 'server/src/com')
-rw-r--r--server/src/com/vaadin/ui/AbsoluteLayout.java110
-rw-r--r--server/src/com/vaadin/ui/AbstractComponent.java4
-rw-r--r--server/src/com/vaadin/ui/AbstractOrderedLayout.java4
-rw-r--r--server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java32
4 files changed, 144 insertions, 6 deletions
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);
+ }
+ }
+
}
diff --git a/server/src/com/vaadin/ui/AbstractComponent.java b/server/src/com/vaadin/ui/AbstractComponent.java
index b5721a0bba..8ee3f722ab 100644
--- a/server/src/com/vaadin/ui/AbstractComponent.java
+++ b/server/src/com/vaadin/ui/AbstractComponent.java
@@ -1029,7 +1029,7 @@ public abstract class AbstractComponent extends AbstractClientConnector
attributes.put("width-auto", "true");
} else {
String widthString = DesignAttributeHandler
- .formatDesignAttribute(getWidth())
+ .formatFloat(getWidth())
+ getWidthUnits().getSymbol();
attributes.put("width", widthString);
@@ -1043,7 +1043,7 @@ public abstract class AbstractComponent extends AbstractClientConnector
attributes.put("height-auto", "true");
} else {
String heightString = DesignAttributeHandler
- .formatDesignAttribute(getHeight())
+ .formatFloat(getHeight())
+ getHeightUnits().getSymbol();
attributes.put("height", heightString);
}
diff --git a/server/src/com/vaadin/ui/AbstractOrderedLayout.java b/server/src/com/vaadin/ui/AbstractOrderedLayout.java
index bbaaffe789..139a4eb545 100644
--- a/server/src/com/vaadin/ui/AbstractOrderedLayout.java
+++ b/server/src/com/vaadin/ui/AbstractOrderedLayout.java
@@ -559,8 +559,8 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements
if (expandRatio == 1.0f) {
childNode.attr(":expand", "");
} else if (expandRatio > 0) {
- childNode.attr(":expand", DesignAttributeHandler
- .formatDesignAttribute(expandRatio));
+ childNode.attr(":expand",
+ DesignAttributeHandler.formatFloat(expandRatio));
}
}
}
diff --git a/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java b/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java
index 423b2fb60b..daebb1c09a 100644
--- a/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java
+++ b/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java
@@ -244,12 +244,35 @@ public class DesignAttributeHandler {
* the number to be formatted
* @return the formatted number
*/
- public static String formatDesignAttribute(float number) {
+ public static String formatFloat(float number) {
+ return getDecimalFormat().format(number);
+ }
+
+ /**
+ * Formats the given design attribute value. The method is provided to
+ * ensure consistent number formatting for design attribute values
+ *
+ * @since 7.4
+ * @param number
+ * the number to be formatted
+ * @return the formatted number
+ */
+ public static String formatDouble(double number) {
+ return getDecimalFormat().format(number);
+ }
+
+ /**
+ * Creates the decimal format used when writing attributes to the design
+ *
+ * @since 7.4
+ * @return the decimal format
+ */
+ private static DecimalFormat getDecimalFormat() {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(new Locale(
"en_US"));
DecimalFormat fmt = new DecimalFormat("0.###", symbols);
fmt.setGroupingUsed(false);
- return fmt.format(number);
+ return fmt;
}
/**
@@ -359,8 +382,13 @@ public class DesignAttributeHandler {
"Unknown resource type " + value.getClass().getName());
return null;
}
+ } else if (sourceType == Float.class || sourceType == Float.TYPE) {
+ return formatFloat(((Float) value).floatValue());
+ } else if (sourceType == Double.class || sourceType == Double.TYPE) {
+ return formatDouble(((Double) value).doubleValue());
} else {
return value.toString();
+
}
}