diff options
3 files changed, 162 insertions, 4 deletions
diff --git a/server/src/com/vaadin/ui/AbstractOrderedLayout.java b/server/src/com/vaadin/ui/AbstractOrderedLayout.java index 3aec3b2d7a..0214ff4be1 100644 --- a/server/src/com/vaadin/ui/AbstractOrderedLayout.java +++ b/server/src/com/vaadin/ui/AbstractOrderedLayout.java @@ -477,11 +477,32 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements public void readDesign(Element design, DesignContext designContext) { // process default attributes super.readDesign(design, designContext); - // handle margin + + // handle margins if (design.hasAttr("margin")) { setMargin(DesignAttributeHandler.readAttribute("margin", design.attributes(), Boolean.class)); + } else { + boolean marginLeft = DesignAttributeHandler.readAttribute( + "margin-left", design.attributes(), getMargin().hasLeft(), + Boolean.class); + + boolean marginRight = DesignAttributeHandler.readAttribute( + "margin-right", design.attributes(), + getMargin().hasRight(), Boolean.class); + + boolean marginTop = DesignAttributeHandler.readAttribute( + "margin-top", design.attributes(), getMargin().hasTop(), + Boolean.class); + + boolean marginBottom = DesignAttributeHandler.readAttribute( + "margin-bottom", design.attributes(), getMargin() + .hasBottom(), Boolean.class); + + setMargin(new MarginInfo(marginTop, marginBottom, marginLeft, + marginRight)); } + // handle children for (Element childComponent : design.children()) { Attributes attr = childComponent.attributes(); @@ -532,12 +553,36 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements public void writeDesign(Element design, DesignContext designContext) { // write default attributes super.writeDesign(design, designContext); - // handle margin + AbstractOrderedLayout def = (AbstractOrderedLayout) designContext .getDefaultInstance(this); - if (getMargin().getBitMask() != def.getMargin().getBitMask()) { - design.attr("margin", ""); + + // handle margin + MarginInfo marginInfo = getMargin(); + + if (marginInfo.hasAll()) { + DesignAttributeHandler.writeAttribute("margin", + design.attributes(), marginInfo.hasAll(), def.getMargin() + .hasAll(), Boolean.class); + } else { + + DesignAttributeHandler.writeAttribute("margin-left", design + .attributes(), marginInfo.hasLeft(), def.getMargin() + .hasLeft(), Boolean.class); + + DesignAttributeHandler.writeAttribute("margin-right", design + .attributes(), marginInfo.hasRight(), def.getMargin() + .hasRight(), Boolean.class); + + DesignAttributeHandler.writeAttribute("margin-top", design + .attributes(), marginInfo.hasTop(), def.getMargin() + .hasTop(), Boolean.class); + + DesignAttributeHandler.writeAttribute("margin-bottom", design + .attributes(), marginInfo.hasBottom(), def.getMargin() + .hasBottom(), Boolean.class); } + // handle children if (!designContext.shouldWriteChildren(this, def)) { return; @@ -578,6 +623,10 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements protected Collection<String> getCustomAttributes() { Collection<String> customAttributes = super.getCustomAttributes(); customAttributes.add("margin"); + customAttributes.add("margin-left"); + customAttributes.add("margin-right"); + customAttributes.add("margin-top"); + customAttributes.add("margin-bottom"); return customAttributes; } diff --git a/server/tests/src/com/vaadin/tests/server/component/AbstractLayoutDeclarativeMarginTest.java b/server/tests/src/com/vaadin/tests/server/component/AbstractLayoutDeclarativeMarginTest.java new file mode 100644 index 0000000000..70855f67dc --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/AbstractLayoutDeclarativeMarginTest.java @@ -0,0 +1,104 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.server.component; + +import org.junit.Test; + +import com.vaadin.shared.ui.MarginInfo; +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.AbstractLayout; +import com.vaadin.ui.VerticalLayout; + +public class AbstractLayoutDeclarativeMarginTest extends + DeclarativeTestBase<AbstractLayout> { + + @Test + public void testMarginInfo() { + VerticalLayout vl = new VerticalLayout(); + + String left = getMarginTag(true, false, false, false); + MarginInfo leftInfo = getMarginInfo(true, false, false, false); + + String right = getMarginTag(false, true, false, false); + MarginInfo rightInfo = getMarginInfo(false, true, false, false); + + String top = getMarginTag(false, false, true, false); + MarginInfo topInfo = getMarginInfo(false, false, true, false); + + String bottom = getMarginTag(false, false, false, true); + MarginInfo bottomInfo = getMarginInfo(false, false, false, true); + + String topLeft = getMarginTag(true, false, true, false); + MarginInfo topLeftInfo = getMarginInfo(true, false, true, false); + + String topRight = getMarginTag(false, true, true, false); + MarginInfo topRightInfo = getMarginInfo(false, true, true, false); + + String bottomLeft = getMarginTag(true, false, false, true); + MarginInfo bottomLeftInfo = getMarginInfo(true, false, false, true); + + String bottomRight = getMarginTag(false, true, false, true); + MarginInfo bottomRightInfo = getMarginInfo(false, true, false, true); + + testRW(vl, left, leftInfo); + testRW(vl, right, rightInfo); + testRW(vl, top, topInfo); + testRW(vl, bottom, bottomInfo); + + testRW(vl, topLeft, topLeftInfo); + testRW(vl, topRight, topRightInfo); + testRW(vl, bottomLeft, bottomLeftInfo); + testRW(vl, bottomRight, bottomRightInfo); + + // Test special case of all edges margin'ed + testRW(vl, getMarginTag(true, true, true, true), new MarginInfo(true)); + } + + private void testRW(VerticalLayout vl, String design, MarginInfo margin) { + vl.setMargin(margin); + testWrite(design, vl); + testRead(design, vl); + } + + private String getMarginTag(boolean left, boolean right, boolean top, + boolean bottom) { + String s = "<v-vertical-layout "; + + if (left && right && top && bottom) { + s += "margin='true'"; + } else { + if (left) { + s += "margin-left='true' "; + } + if (right) { + s += "margin-right='true' "; + } + if (top) { + s += "margin-top='true' "; + } + if (bottom) { + s += "margin-bottom='true' "; + } + } + return s + " />"; + } + + private MarginInfo getMarginInfo(boolean left, boolean right, boolean top, + boolean bottom) { + return new MarginInfo(top, right, bottom, left); + } + +} diff --git a/shared/src/com/vaadin/shared/ui/MarginInfo.java b/shared/src/com/vaadin/shared/ui/MarginInfo.java index 3b1fece88a..4c0255a9ba 100644 --- a/shared/src/com/vaadin/shared/ui/MarginInfo.java +++ b/shared/src/com/vaadin/shared/ui/MarginInfo.java @@ -24,6 +24,7 @@ public class MarginInfo implements Serializable { private static final int RIGHT = 2; private static final int BOTTOM = 4; private static final int LEFT = 8; + private static final int ALL = TOP | RIGHT | BOTTOM | LEFT; private int bitMask; @@ -51,6 +52,10 @@ public class MarginInfo implements Serializable { bitMask = marginInfo.bitMask; } + public boolean hasAll() { + return (bitMask & ALL) == ALL; + } + public boolean hasLeft() { return (bitMask & LEFT) == LEFT; } |