diff options
author | Mika Murtojarvi <mika@vaadin.com> | 2014-12-11 18:25:40 +0200 |
---|---|---|
committer | Mika Murtojarvi <mika@vaadin.com> | 2014-12-12 16:36:20 +0200 |
commit | 628a2edf50c355e6e43e1da2e32224d32651327d (patch) | |
tree | 9814351d253b08720742237cd1ea84931636d215 /server/src/com/vaadin/ui/AbstractSplitPanel.java | |
parent | a025d511bfe4e81993d33fc424a9bed754586a52 (diff) | |
download | vaadin-framework-628a2edf50c355e6e43e1da2e32224d32651327d.tar.gz vaadin-framework-628a2edf50c355e6e43e1da2e32224d32651327d.zip |
Declarative support for split panel (#7749).
Change-Id: I96b7be150c9b6511f9fb701c5ee26afca67f1512
Diffstat (limited to 'server/src/com/vaadin/ui/AbstractSplitPanel.java')
-rw-r--r-- | server/src/com/vaadin/ui/AbstractSplitPanel.java | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/server/src/com/vaadin/ui/AbstractSplitPanel.java b/server/src/com/vaadin/ui/AbstractSplitPanel.java index 09f881cf46..1400bcf092 100644 --- a/server/src/com/vaadin/ui/AbstractSplitPanel.java +++ b/server/src/com/vaadin/ui/AbstractSplitPanel.java @@ -18,16 +18,23 @@ package com.vaadin.ui; import java.io.Serializable; import java.lang.reflect.Method; +import java.util.Collection; import java.util.Iterator; +import org.jsoup.nodes.Element; + import com.vaadin.event.ConnectorEventListener; import com.vaadin.event.MouseEvents.ClickEvent; +import com.vaadin.server.SizeWithUnit; import com.vaadin.server.Sizeable; import com.vaadin.shared.EventId; import com.vaadin.shared.MouseEventDetails; import com.vaadin.shared.ui.splitpanel.AbstractSplitPanelRpc; import com.vaadin.shared.ui.splitpanel.AbstractSplitPanelState; import com.vaadin.shared.ui.splitpanel.AbstractSplitPanelState.SplitterState; +import com.vaadin.ui.declarative.DesignAttributeHandler; +import com.vaadin.ui.declarative.DesignContext; +import com.vaadin.ui.declarative.DesignException; import com.vaadin.util.ReflectTools; /** @@ -546,4 +553,136 @@ public abstract class AbstractSplitPanel extends AbstractComponentContainer { private SplitterState getSplitterState(boolean markAsDirty) { return ((AbstractSplitPanelState) super.getState(markAsDirty)).splitterState; } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.ui.AbstractComponent#synchronizeFromDesign(org.jsoup.nodes + * .Element, com.vaadin.ui.declarative.DesignContext) + */ + @Override + public void synchronizeFromDesign(Element design, + DesignContext designContext) { + // handle default attributes + super.synchronizeFromDesign(design, designContext); + // handle custom attributes, use default values if no explicit value + // set + AbstractSplitPanel def = designContext.getDefaultInstance(this + .getClass()); + // There is no setter for reversed, so it will be handled using + // setSplitPosition. + boolean reversed = DesignAttributeHandler.readAttribute("reversed", + design.attributes(), def.getSplitterState().positionReversed, + Boolean.class); + if (design.hasAttr("split-position")) { + SizeWithUnit splitPosition = SizeWithUnit.parseStringSize( + design.attr("split-position"), def.getSplitPositionUnit()); + setSplitPosition(splitPosition.getSize(), splitPosition.getUnit(), + reversed); + } else { // default value for split position + setSplitPosition(def.getSplitPosition(), + def.getSplitPositionUnit(), reversed); + } + if (design.hasAttr("min-split-position")) { + SizeWithUnit minSplitPosition = SizeWithUnit.parseStringSize( + design.attr("min-split-position"), + def.getMinSplitPositionUnit()); + setMinSplitPosition(minSplitPosition.getSize(), + minSplitPosition.getUnit()); + } else { // default value for min-split-position + setMinSplitPosition(def.getMinSplitPosition(), + def.getMinSplitPositionUnit()); + } + if (design.hasAttr("max-split-position")) { + SizeWithUnit maxSplitPosition = SizeWithUnit.parseStringSize( + design.attr("max-split-position"), + def.getMaxSplitPositionUnit()); + setMaxSplitPosition(maxSplitPosition.getSize(), + maxSplitPosition.getUnit()); + } else { // default value for max-split-position + setMaxSplitPosition(def.getMaxSplitPosition(), + def.getMaxSplitPositionUnit()); + } + // remove current children + removeAllComponents(); + // handle children + if (design.children().size() > 2) { + throw new DesignException( + "A split panel can contain at most two components."); + } + for (Element childElement : design.children()) { + Component childComponent = designContext.createChild(childElement); + if (childElement.hasAttr(":second")) { + setSecondComponent(childComponent); + } else { + addComponent(childComponent); + } + } + } + + @Override + protected Collection<String> getCustomAttributes() { + Collection<String> attributes = super.getCustomAttributes(); + // the setters of the properties do not accept strings such as "20px" + attributes.add("split-position"); + attributes.add("min-split-position"); + attributes.add("max-split-position"); + // no explicit setter for reversed + attributes.add("reversed"); + return attributes; + } + + @Override + public void synchronizeToDesign(Element design, DesignContext designContext) { + // handle default attributes (also clears children and attributes) + super.synchronizeToDesign(design, designContext); + // handle custom attributes (write only if a value is not the + // default value) + AbstractSplitPanel def = designContext.getDefaultInstance(this + .getClass()); + if (getSplitPosition() != def.getSplitPosition() + || !def.getSplitPositionUnit().equals(getSplitPositionUnit())) { + String splitPositionString = asString(getSplitPosition()) + + getSplitPositionUnit(); + design.attr("split-position", splitPositionString); + } + if (getMinSplitPosition() != def.getMinSplitPosition() + || !def.getMinSplitPositionUnit().equals( + getMinSplitPositionUnit())) { + design.attr("min-split-position", asString(getMinSplitPosition()) + + getMinSplitPositionUnit()); + } + if (getMaxSplitPosition() != def.getMaxSplitPosition() + || !def.getMaxSplitPositionUnit().equals( + getMaxSplitPositionUnit())) { + design.attr("max-split-position", asString(getMaxSplitPosition()) + + getMaxSplitPositionUnit()); + } + if (getSplitterState().positionReversed) { + design.attr("reversed", ""); + } + // handle child components + Component firstComponent = getFirstComponent(); + Component secondComponent = getSecondComponent(); + if (firstComponent != null) { + Element childElement = designContext.createNode(firstComponent); + design.appendChild(childElement); + } + if (secondComponent != null) { + Element childElement = designContext.createNode(secondComponent); + if (firstComponent == null) { + childElement.attr(":second", ""); + } + design.appendChild(childElement); + } + } + + private String asString(float number) { + int truncated = (int) number; + if (truncated == number) { + return "" + truncated; + } + return "" + number; + } } |