aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/com/vaadin
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2014-12-15 23:02:22 +0200
committerArtur Signell <artur@vaadin.com>2014-12-15 23:04:41 +0200
commit353206974a542d0aadc278dea4adeff226fb3a9e (patch)
treebd6511b2d62cefaaa88f5131abf69f11a1cd12e5 /server/src/com/vaadin
parent8eafe7aee31b3e2f80c87b39c4e42e260a86a5eb (diff)
downloadvaadin-framework-353206974a542d0aadc278dea4adeff226fb3a9e.tar.gz
vaadin-framework-353206974a542d0aadc278dea4adeff226fb3a9e.zip
Fix writing issues (#7749)
* A root component with @DesignRoot must always use its superclass for default values. Otherwise the written design will be empty as the design is read in the constructor. * A component which is not the root must not write its component tree if the component tree is generated in the constructor. This is a simplification which should be good enough for most cases (can't add children in constructor and also using addComponent - in this case the component added with addComponent will not be written). * Test cases for nested templates Change-Id: I3a384d1d8654b9865a3a790ebeb055a300a62135
Diffstat (limited to 'server/src/com/vaadin')
-rw-r--r--server/src/com/vaadin/ui/AbstractComponent.java3
-rw-r--r--server/src/com/vaadin/ui/AbstractField.java2
-rw-r--r--server/src/com/vaadin/ui/AbstractOrderedLayout.java8
-rw-r--r--server/src/com/vaadin/ui/AbstractSplitPanel.java4
-rw-r--r--server/src/com/vaadin/ui/AbstractTextField.java4
-rw-r--r--server/src/com/vaadin/ui/Button.java2
-rw-r--r--server/src/com/vaadin/ui/Panel.java2
-rw-r--r--server/src/com/vaadin/ui/PasswordField.java4
-rw-r--r--server/src/com/vaadin/ui/TabSheet.java2
-rw-r--r--server/src/com/vaadin/ui/TextField.java4
-rw-r--r--server/src/com/vaadin/ui/declarative/DesignContext.java63
11 files changed, 73 insertions, 25 deletions
diff --git a/server/src/com/vaadin/ui/AbstractComponent.java b/server/src/com/vaadin/ui/AbstractComponent.java
index 0b9f8bd244..83833b75ce 100644
--- a/server/src/com/vaadin/ui/AbstractComponent.java
+++ b/server/src/com/vaadin/ui/AbstractComponent.java
@@ -1206,8 +1206,7 @@ public abstract class AbstractComponent extends AbstractClientConnector
public void writeDesign(Element design, DesignContext designContext) {
// clear element contents
DesignAttributeHandler.clearElement(design);
- AbstractComponent def = designContext.getDefaultInstance(this
- .getClass());
+ AbstractComponent def = designContext.getDefaultInstance(this);
Attributes attr = design.attributes();
// handle default attributes
for (String attribute : getDefaultAttributes()) {
diff --git a/server/src/com/vaadin/ui/AbstractField.java b/server/src/com/vaadin/ui/AbstractField.java
index c3e2036936..e40bdbde68 100644
--- a/server/src/com/vaadin/ui/AbstractField.java
+++ b/server/src/com/vaadin/ui/AbstractField.java
@@ -1803,7 +1803,7 @@ public abstract class AbstractField<T> extends AbstractComponent implements
@Override
public void writeDesign(Element design, DesignContext designContext) {
super.writeDesign(design, designContext);
- AbstractField def = designContext.getDefaultInstance(this.getClass());
+ AbstractField def = designContext.getDefaultInstance(this);
Attributes attr = design.attributes();
// handle readonly
DesignAttributeHandler.writeAttribute("readonly", attr,
diff --git a/server/src/com/vaadin/ui/AbstractOrderedLayout.java b/server/src/com/vaadin/ui/AbstractOrderedLayout.java
index cd5609f091..b105d53fce 100644
--- a/server/src/com/vaadin/ui/AbstractOrderedLayout.java
+++ b/server/src/com/vaadin/ui/AbstractOrderedLayout.java
@@ -533,13 +533,17 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements
// synchronize default attributes
super.writeDesign(design, designContext);
// handle margin
- AbstractOrderedLayout def = designContext.getDefaultInstance(this
- .getClass());
+ AbstractOrderedLayout def = (AbstractOrderedLayout) designContext
+ .getDefaultInstance(this);
if (getMargin().getBitMask() != def.getMargin().getBitMask()) {
design.attr("margin", "");
}
// handle children
Element designElement = design;
+ if (!designContext.shouldWriteChildren(this, def)) {
+ return;
+ }
+
for (Component child : this) {
Element childNode = designContext.createNode(child);
designElement.appendChild(childNode);
diff --git a/server/src/com/vaadin/ui/AbstractSplitPanel.java b/server/src/com/vaadin/ui/AbstractSplitPanel.java
index 9c33665c1c..4c85311075 100644
--- a/server/src/com/vaadin/ui/AbstractSplitPanel.java
+++ b/server/src/com/vaadin/ui/AbstractSplitPanel.java
@@ -627,8 +627,8 @@ public abstract class AbstractSplitPanel extends AbstractComponentContainer {
super.writeDesign(design, designContext);
// handle custom attributes (write only if a value is not the
// default value)
- AbstractSplitPanel def = designContext.getDefaultInstance(this
- .getClass());
+ AbstractSplitPanel def = (AbstractSplitPanel) designContext
+ .getDefaultInstance(this);
if (getSplitPosition() != def.getSplitPosition()
|| !def.getSplitPositionUnit().equals(getSplitPositionUnit())) {
String splitPositionString = asString(getSplitPosition())
diff --git a/server/src/com/vaadin/ui/AbstractTextField.java b/server/src/com/vaadin/ui/AbstractTextField.java
index 8bca74936a..adeb1cb69f 100644
--- a/server/src/com/vaadin/ui/AbstractTextField.java
+++ b/server/src/com/vaadin/ui/AbstractTextField.java
@@ -803,8 +803,8 @@ public abstract class AbstractTextField extends AbstractField<String> implements
@Override
public void writeDesign(Element design, DesignContext designContext) {
super.writeDesign(design, designContext);
- AbstractTextField def = designContext.getDefaultInstance(this
- .getClass());
+ AbstractTextField def = (AbstractTextField) designContext
+ .getDefaultInstance(this);
Attributes attr = design.attributes();
DesignAttributeHandler.writeAttribute("maxlength", attr,
getMaxLength(), def.getMaxLength(), Integer.class);
diff --git a/server/src/com/vaadin/ui/Button.java b/server/src/com/vaadin/ui/Button.java
index 9b50b5eebc..7063568f26 100644
--- a/server/src/com/vaadin/ui/Button.java
+++ b/server/src/com/vaadin/ui/Button.java
@@ -727,7 +727,7 @@ public class Button extends AbstractComponent implements
public void writeDesign(Element design, DesignContext designContext) {
super.writeDesign(design, designContext);
Attributes attr = design.attributes();
- Button def = designContext.getDefaultInstance(this.getClass());
+ Button def = (Button) designContext.getDefaultInstance(this);
String content = getCaption();
if (content != null) {
design.html(content);
diff --git a/server/src/com/vaadin/ui/Panel.java b/server/src/com/vaadin/ui/Panel.java
index b0242cc32d..0c2a3f580b 100644
--- a/server/src/com/vaadin/ui/Panel.java
+++ b/server/src/com/vaadin/ui/Panel.java
@@ -365,7 +365,7 @@ public class Panel extends AbstractSingleComponentContainer implements
public void writeDesign(Element design, DesignContext designContext) {
super.writeDesign(design, designContext);
// handle tabindex
- Panel def = designContext.getDefaultInstance(this.getClass());
+ Panel def = (Panel) designContext.getDefaultInstance(this);
DesignAttributeHandler.writeAttribute("tabindex", design.attributes(),
getTabIndex(), def.getTabIndex(), Integer.class);
}
diff --git a/server/src/com/vaadin/ui/PasswordField.java b/server/src/com/vaadin/ui/PasswordField.java
index 974a1c2c10..1894804775 100644
--- a/server/src/com/vaadin/ui/PasswordField.java
+++ b/server/src/com/vaadin/ui/PasswordField.java
@@ -107,8 +107,8 @@ public class PasswordField extends AbstractTextField {
@Override
public void writeDesign(Element design, DesignContext designContext) {
super.writeDesign(design, designContext);
- AbstractTextField def = designContext.getDefaultInstance(this
- .getClass());
+ AbstractTextField def = (AbstractTextField) designContext
+ .getDefaultInstance(this);
Attributes attr = design.attributes();
DesignAttributeHandler.writeAttribute("value", attr, getValue(),
def.getValue(), String.class);
diff --git a/server/src/com/vaadin/ui/TabSheet.java b/server/src/com/vaadin/ui/TabSheet.java
index 330acacfd0..a264692acb 100644
--- a/server/src/com/vaadin/ui/TabSheet.java
+++ b/server/src/com/vaadin/ui/TabSheet.java
@@ -1623,7 +1623,7 @@ public class TabSheet extends AbstractComponentContainer implements Focusable,
@Override
public void writeDesign(Element design, DesignContext designContext) {
super.writeDesign(design, designContext);
- TabSheet def = designContext.getDefaultInstance(this.getClass());
+ TabSheet def = (TabSheet) designContext.getDefaultInstance(this);
Attributes attr = design.attributes();
// handle tab index
DesignAttributeHandler.writeAttribute("tabindex", attr, getTabIndex(),
diff --git a/server/src/com/vaadin/ui/TextField.java b/server/src/com/vaadin/ui/TextField.java
index cd919ded9c..563810c76a 100644
--- a/server/src/com/vaadin/ui/TextField.java
+++ b/server/src/com/vaadin/ui/TextField.java
@@ -129,8 +129,8 @@ public class TextField extends AbstractTextField {
@Override
public void writeDesign(Element design, DesignContext designContext) {
super.writeDesign(design, designContext);
- AbstractTextField def = designContext.getDefaultInstance(this
- .getClass());
+ AbstractTextField def = (AbstractTextField) designContext
+ .getDefaultInstance(this);
Attributes attr = design.attributes();
DesignAttributeHandler.writeAttribute("value", attr, getValue(),
def.getValue(), String.class);
diff --git a/server/src/com/vaadin/ui/declarative/DesignContext.java b/server/src/com/vaadin/ui/declarative/DesignContext.java
index b196c84b02..214040b54d 100644
--- a/server/src/com/vaadin/ui/declarative/DesignContext.java
+++ b/server/src/com/vaadin/ui/declarative/DesignContext.java
@@ -28,7 +28,10 @@ import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
+import com.vaadin.annotations.DesignRoot;
+import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.Component;
+import com.vaadin.ui.HasComponents;
/**
* This class contains contextual information that is collected when a component
@@ -42,8 +45,8 @@ import com.vaadin.ui.Component;
public class DesignContext implements Serializable {
// cache for object instances
- private static Map<Class<?>, Object> instanceCache = Collections
- .synchronizedMap(new HashMap<Class<?>, Object>());
+ private static Map<Class<?>, Component> instanceCache = Collections
+ .synchronizedMap(new HashMap<Class<?>, Component>());
// The root component of the component hierarchy
private Component rootComponent = null;
@@ -254,22 +257,38 @@ public class DesignContext implements Serializable {
* Returns the default instance for the given class. The instance must not
* be modified by the caller.
*
- * @param instanceClass
+ * @param abstractComponent
* @return the default instance for the given class. The return value must
* not be modified by the caller
*/
- public <T> T getDefaultInstance(Class<T> instanceClass) {
- T instance = (T) instanceCache.get(instanceClass);
+ public <T> T getDefaultInstance(AbstractComponent abstractComponent) {
+ // If the root is a @DesignRoot component, it can't use itself as a
+ // reference or the written design will be empty
+
+ // If the root component in some other way initializes itself in the
+ // constructor
+ if (getRootComponent() == abstractComponent
+ && abstractComponent.getClass().isAnnotationPresent(
+ DesignRoot.class)) {
+ return (T) getDefaultInstance((Class<? extends Component>) abstractComponent
+ .getClass().getSuperclass());
+ }
+ return (T) getDefaultInstance(abstractComponent.getClass());
+ }
+
+ private Component getDefaultInstance(
+ Class<? extends Component> componentClass) {
+ Component instance = instanceCache.get(componentClass);
if (instance == null) {
try {
- instance = instanceClass.newInstance();
- instanceCache.put(instanceClass, instance);
+ instance = componentClass.newInstance();
+ instanceCache.put(componentClass, instance);
} catch (InstantiationException e) {
throw new RuntimeException("Could not instantiate "
- + instanceClass.getName());
+ + componentClass.getName());
} catch (IllegalAccessException e) {
throw new RuntimeException("Could not instantiate "
- + instanceClass.getName());
+ + componentClass.getName());
}
}
return instance;
@@ -663,4 +682,30 @@ public class DesignContext implements Serializable {
}
}
+ /**
+ * Helper method for component write implementors to determine whether their
+ * children should be written out or not
+ *
+ * @param c
+ * The component being written
+ * @param defaultC
+ * The default instance for the component
+ * @return
+ */
+ public boolean shouldWriteChildren(Component c, Component defaultC) {
+ if (c == getRootComponent()) {
+ // The root component should always write its children - otherwise
+ // the result is empty
+ return true;
+ }
+
+ if (defaultC instanceof HasComponents
+ && ((HasComponents) defaultC).iterator().hasNext()) {
+ // Easy version which assumes that this is a custom component if the
+ // constructor adds children
+ return false;
+ }
+
+ return true;
+ }
}