diff options
author | Denis Anisimov <denis@vaadin.com> | 2016-10-24 11:32:16 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-10-26 05:59:27 +0000 |
commit | 9abe01a5881ae7fb43d2706ca9302aa68cc0221b (patch) | |
tree | 97b3fabfd917c5513b4adbce20b37af1a78dac87 | |
parent | 5bc31e8267230fc4abfef34dcaa77ce5eae447b3 (diff) | |
download | vaadin-framework-9abe01a5881ae7fb43d2706ca9302aa68cc0221b.tar.gz vaadin-framework-9abe01a5881ae7fb43d2706ca9302aa68cc0221b.zip |
Introduce "shouldWriteDefaultValues" property to DesignContext.
Change-Id: I979448e5af032333a6a2f7b99b11ab16c41d7b18
36 files changed, 163 insertions, 123 deletions
diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractColorPicker.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractColorPicker.java index 668ffd568a..9f2224bed3 100644 --- a/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractColorPicker.java +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractColorPicker.java @@ -572,12 +572,14 @@ public abstract class AbstractColorPicker extends AbstractLegacyComponent Attributes attribute = design.attributes(); DesignAttributeHandler.writeAttribute("color", attribute, - color.getCSS(), Color.WHITE.getCSS(), String.class); + color.getCSS(), Color.WHITE.getCSS(), String.class, + designContext); DesignAttributeHandler.writeAttribute("popup-style", attribute, popupStyle == PopupStyle.POPUP_NORMAL ? "normal" : "simple", - "normal", String.class); + "normal", String.class, designContext); DesignAttributeHandler.writeAttribute("position", attribute, - positionX + "," + positionY, "0,0", String.class); + positionX + "," + positionY, "0,0", String.class, + designContext); } @Override diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractField.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractField.java index 8ac94f6592..eb6255e37a 100644 --- a/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractField.java +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractField.java @@ -1802,7 +1802,8 @@ public abstract class AbstractField<T> extends AbstractLegacyComponent Attributes attr = design.attributes(); // handle readonly DesignAttributeHandler.writeAttribute("readonly", attr, - super.isReadOnly(), def.isReadOnly(), Boolean.class); + super.isReadOnly(), def.isReadOnly(), Boolean.class, + designContext); } private static final Logger getLogger() { diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractLegacyComponent.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractLegacyComponent.java index 11ac309089..28c1078de7 100644 --- a/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractLegacyComponent.java +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractLegacyComponent.java @@ -99,7 +99,8 @@ public class AbstractLegacyComponent extends AbstractComponent { // handle immediate if (explicitImmediateValue != null) { DesignAttributeHandler.writeAttribute("immediate", attr, - explicitImmediateValue, def.isImmediate(), Boolean.class); + explicitImmediateValue, def.isImmediate(), Boolean.class, + designContext); } } diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractSelect.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractSelect.java index 55962df2cf..127bdf6e7b 100644 --- a/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractSelect.java +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractSelect.java @@ -2343,7 +2343,7 @@ public abstract class AbstractSelect extends AbstractField<Object> implements Resource icon = getItemIcon(itemId); if (icon != null) { DesignAttributeHandler.writeAttribute("icon", element.attributes(), - icon, null, Resource.class); + icon, null, Resource.class, context); } if (isSelected(itemId)) { diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractTextField.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractTextField.java index 8a6b4849f8..482dd0b2af 100644 --- a/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractTextField.java +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/AbstractTextField.java @@ -806,7 +806,7 @@ public abstract class AbstractTextField extends AbstractField<String> implements .getDefaultInstance(this); Attributes attr = design.attributes(); DesignAttributeHandler.writeAttribute("maxlength", attr, getMaxLength(), - def.getMaxLength(), Integer.class); + def.getMaxLength(), Integer.class, designContext); } } diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/CheckBox.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/CheckBox.java index 779acb10ea..d3c7b1a50c 100644 --- a/compatibility-server/src/main/java/com/vaadin/v7/ui/CheckBox.java +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/CheckBox.java @@ -252,7 +252,7 @@ public class CheckBox extends AbstractField<Boolean> { CheckBox def = (CheckBox) designContext.getDefaultInstance(this); Attributes attr = design.attributes(); DesignAttributeHandler.writeAttribute("checked", attr, getValue(), - def.getValue(), Boolean.class); + def.getValue(), Boolean.class, designContext); } @Override diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/Grid.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/Grid.java index e213d7d585..db4d75bbfc 100644 --- a/compatibility-server/src/main/java/com/vaadin/v7/ui/Grid.java +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/Grid.java @@ -3143,7 +3143,8 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, if (section.grid.getDefaultHeaderRow() == this) { DesignAttributeHandler.writeAttribute("default", - trElement.attributes(), true, null, boolean.class); + trElement.attributes(), true, null, boolean.class, + designContext); } } @@ -4071,31 +4072,35 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, GridColumnState def = new GridColumnState(); DesignAttributeHandler.writeAttribute("property-id", attributes, - getPropertyId(), null, Object.class); + getPropertyId(), null, Object.class, designContext); // Sortable is a special attribute that depends on the container. DesignAttributeHandler.writeAttribute("sortable", attributes, - isSortable(), null, boolean.class); + isSortable(), null, boolean.class, designContext); DesignAttributeHandler.writeAttribute("editable", attributes, - isEditable(), def.editable, boolean.class); + isEditable(), def.editable, boolean.class, designContext); DesignAttributeHandler.writeAttribute("resizable", attributes, - isResizable(), def.resizable, boolean.class); + isResizable(), def.resizable, boolean.class, designContext); DesignAttributeHandler.writeAttribute("hidable", attributes, - isHidable(), def.hidable, boolean.class); + isHidable(), def.hidable, boolean.class, designContext); DesignAttributeHandler.writeAttribute("hidden", attributes, - isHidden(), def.hidden, boolean.class); + isHidden(), def.hidden, boolean.class, designContext); DesignAttributeHandler.writeAttribute("hiding-toggle-caption", - attributes, getHidingToggleCaption(), null, String.class); + attributes, getHidingToggleCaption(), null, String.class, + designContext); DesignAttributeHandler.writeAttribute("width", attributes, - getWidth(), def.width, Double.class); + getWidth(), def.width, Double.class, designContext); DesignAttributeHandler.writeAttribute("min-width", attributes, - getMinimumWidth(), def.minWidth, Double.class); + getMinimumWidth(), def.minWidth, Double.class, + designContext); DesignAttributeHandler.writeAttribute("max-width", attributes, - getMaximumWidth(), def.maxWidth, Double.class); + getMaximumWidth(), def.maxWidth, Double.class, + designContext); DesignAttributeHandler.writeAttribute("expand", attributes, - getExpandRatio(), def.expandRatio, Integer.class); + getExpandRatio(), def.expandRatio, Integer.class, + designContext); } /** @@ -7332,14 +7337,17 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, Grid def = context.getDefaultInstance(this); DesignAttributeHandler.writeAttribute("editable", attrs, - isEditorEnabled(), def.isEditorEnabled(), boolean.class); + isEditorEnabled(), def.isEditorEnabled(), boolean.class, + context); DesignAttributeHandler.writeAttribute("frozen-columns", attrs, - getFrozenColumnCount(), def.getFrozenColumnCount(), int.class); + getFrozenColumnCount(), def.getFrozenColumnCount(), int.class, + context); if (getHeightMode() == HeightMode.ROW) { DesignAttributeHandler.writeAttribute("rows", attrs, - getHeightByRows(), def.getHeightByRows(), double.class); + getHeightByRows(), def.getHeightByRows(), double.class, + context); } SelectionMode selectionMode = null; @@ -7357,7 +7365,8 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, + selectionModel.getClass().getName(); DesignAttributeHandler.writeAttribute("selection-mode", attrs, - selectionMode, getDefaultSelectionMode(), SelectionMode.class); + selectionMode, getDefaultSelectionMode(), SelectionMode.class, + context); if (columns.isEmpty()) { // Empty grid. Structure not needed. diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/PasswordField.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/PasswordField.java index 787e024cc6..cf00f64b8b 100644 --- a/compatibility-server/src/main/java/com/vaadin/v7/ui/PasswordField.java +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/PasswordField.java @@ -112,7 +112,7 @@ public class PasswordField extends AbstractTextField { .getDefaultInstance(this); Attributes attr = design.attributes(); DesignAttributeHandler.writeAttribute("value", attr, getValue(), - def.getValue(), String.class); + def.getValue(), String.class, designContext); } @Override diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/ProgressBar.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/ProgressBar.java index 14831d7d81..12b0286577 100644 --- a/compatibility-server/src/main/java/com/vaadin/v7/ui/ProgressBar.java +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/ProgressBar.java @@ -171,7 +171,7 @@ public class ProgressBar extends AbstractField<Float> Float defaultValue = ((ProgressBar) designContext .getDefaultInstance(this)).getValue(); DesignAttributeHandler.writeAttribute("value", design.attributes(), - getValue(), defaultValue, Float.class); + getValue(), defaultValue, Float.class, designContext); } @Override diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/Slider.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/Slider.java index 15b083e38d..a898773f7c 100644 --- a/compatibility-server/src/main/java/com/vaadin/v7/ui/Slider.java +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/Slider.java @@ -387,7 +387,7 @@ public class Slider extends AbstractField<Double> { } Slider defaultSlider = context.getDefaultInstance(this); DesignAttributeHandler.writeAttribute(this, "value", - design.attributes(), defaultSlider); + design.attributes(), defaultSlider, context); } @Override diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/Table.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/Table.java index dc7b762c13..bc8f2eada4 100644 --- a/compatibility-server/src/main/java/com/vaadin/v7/ui/Table.java +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/Table.java @@ -6327,14 +6327,14 @@ public class Table extends AbstractSelect implements Action.Container, Table def = context.getDefaultInstance(this); DesignAttributeHandler.writeAttribute("sortable", design.attributes(), - isSortEnabled(), def.isSortEnabled(), boolean.class); + isSortEnabled(), def.isSortEnabled(), boolean.class, context); Element table = null; boolean hasColumns = getVisibleColumns().length != 0; if (hasColumns) { table = design.appendElement("table"); - writeColumns(table, def); - writeHeader(table, def); + writeColumns(table, def, context); + writeHeader(table, def, context); } super.writeDesign(design, context); if (hasColumns) { @@ -6342,7 +6342,7 @@ public class Table extends AbstractSelect implements Action.Container, } } - private void writeColumns(Element table, Table def) { + private void writeColumns(Element table, Table def, DesignContext context) { Object[] columns = getVisibleColumns(); if (columns.length == 0) { return; @@ -6361,23 +6361,24 @@ public class Table extends AbstractSelect implements Action.Container, } DesignAttributeHandler.writeAttribute("width", col.attributes(), - getColumnWidth(id), def.getColumnWidth(null), int.class); + getColumnWidth(id), def.getColumnWidth(null), int.class, + context); DesignAttributeHandler.writeAttribute("expand", col.attributes(), getColumnExpandRatio(id), def.getColumnExpandRatio(null), - float.class); + float.class, context); DesignAttributeHandler.writeAttribute("collapsible", col.attributes(), isColumnCollapsible(id), - def.isColumnCollapsible(null), boolean.class); + def.isColumnCollapsible(null), boolean.class, context); DesignAttributeHandler.writeAttribute("collapsed", col.attributes(), isColumnCollapsed(id), def.isColumnCollapsed(null), - boolean.class); + boolean.class, context); } } - private void writeHeader(Element table, Table def) { + private void writeHeader(Element table, Table def, DesignContext context) { Object[] columns = getVisibleColumns(); if (columns.length == 0 || (columnIcons.isEmpty() && columnHeaders.isEmpty())) { @@ -6389,7 +6390,8 @@ public class Table extends AbstractSelect implements Action.Container, Element th = header.appendElement("th"); th.html(getColumnHeader(id)); DesignAttributeHandler.writeAttribute("icon", th.attributes(), - getColumnIcon(id), def.getColumnIcon(null), Resource.class); + getColumnIcon(id), def.getColumnIcon(null), Resource.class, + context); } } diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/TextField.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/TextField.java index 2f958e629b..d7f9d07b0b 100644 --- a/compatibility-server/src/main/java/com/vaadin/v7/ui/TextField.java +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/TextField.java @@ -135,7 +135,7 @@ public class TextField extends AbstractTextField { .getDefaultInstance(this); Attributes attr = design.attributes(); DesignAttributeHandler.writeAttribute("value", attr, getValue(), - def.getValue(), String.class); + def.getValue(), String.class, designContext); } /* diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/Tree.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/Tree.java index 9a3403e3d4..9be95578b4 100644 --- a/compatibility-server/src/main/java/com/vaadin/v7/ui/Tree.java +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/Tree.java @@ -1944,7 +1944,7 @@ public class Tree extends AbstractSelect implements Container.Hierarchical, Resource icon = getItemIcon(itemId); if (icon != null) { DesignAttributeHandler.writeAttribute("icon", element.attributes(), - icon, null, Resource.class); + icon, null, Resource.class, context); } if (isSelected(itemId)) { diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/TreeTable.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/TreeTable.java index 4192883098..e5b18472e8 100644 --- a/compatibility-server/src/main/java/com/vaadin/v7/ui/TreeTable.java +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/TreeTable.java @@ -962,7 +962,7 @@ public class TreeTable extends Table implements Hierarchical { for (Object itemId : itemIds) { Element tr = writeItem(tbody, itemId, context); DesignAttributeHandler.writeAttribute("depth", tr.attributes(), - depth, 0, int.class); + depth, 0, int.class, context); if (getChildren(itemId) != null) { writeItems(tbody, getChildren(itemId), depth + 1, context); @@ -975,7 +975,7 @@ public class TreeTable extends Table implements Hierarchical { DesignContext context) { Element tr = super.writeItem(tbody, itemId, context); DesignAttributeHandler.writeAttribute("collapsed", tr.attributes(), - isCollapsed(itemId), true, boolean.class); + isCollapsed(itemId), true, boolean.class, context); return tr; } diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/ComponentDesignWriterUtility.java b/compatibility-server/src/test/java/com/vaadin/tests/server/ComponentDesignWriterUtility.java index 15ae1c9cf4..6009385ccc 100644 --- a/compatibility-server/src/test/java/com/vaadin/tests/server/ComponentDesignWriterUtility.java +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/ComponentDesignWriterUtility.java @@ -24,7 +24,6 @@ import com.vaadin.ui.components.colorpicker.ColorPickerPopup; import com.vaadin.ui.components.colorpicker.ColorPickerPreview; import com.vaadin.ui.components.colorpicker.ColorPickerSelect; import com.vaadin.ui.declarative.Design; -import com.vaadin.ui.declarative.DesignAttributeHandler; import com.vaadin.ui.declarative.DesignContext; /** @@ -33,6 +32,10 @@ import com.vaadin.ui.declarative.DesignContext; public class ComponentDesignWriterUtility { private static final Set<String> WHITE_LIST_FQNS = new HashSet<>(); + private static final Document document = new Document(""); + private static final DesignContext designContext = new DesignContext( + document); + static { WHITE_LIST_FQNS.add(DragAndDropWrapper.class.getName()); WHITE_LIST_FQNS.add(Navigator.EmptyView.class.getName()); @@ -86,11 +89,9 @@ public class ComponentDesignWriterUtility { "com.vaadin.server.VaadinPortlet$VaadinWebSpherePortalRequest"); WHITE_LIST_FQNS.add("com.vaadin.server.VaadinPortlet"); WHITE_LIST_FQNS.add("com.vaadin.server.VaadinPortletRequest"); - } - private static final Document document = new Document(""); - private static final DesignContext designContext = new DesignContext( - document); + designContext.setShouldWriteDefaultValues(true); + } @SafeVarargs public static List<String> getDeclarativeSyntax( @@ -100,13 +101,10 @@ public class ComponentDesignWriterUtility { public static List<String> getDeclarativeSyntax( List<Class<? extends Component>> components) { - DesignAttributeHandler.setWriteDefaultValues(true); - List<String> declarativeStrings = components.stream() .map(ComponentDesignWriterUtility::getDeclarativeSyntax) .collect(Collectors.toList()); - DesignAttributeHandler.setWriteDefaultValues(false); return declarativeStrings; } @@ -156,10 +154,8 @@ public class ComponentDesignWriterUtility { private static void printFullDeclarativeSyntax( List<Class<? extends Component>> components) { - DesignAttributeHandler.setWriteDefaultValues(true); components.stream().forEach(component -> System.out .println(getDeclarativeSyntax(component))); - DesignAttributeHandler.setWriteDefaultValues(false); } private static List<Class<? extends Component>> getVaadin8Components() diff --git a/server/src/main/java/com/vaadin/ui/AbstractColorPicker.java b/server/src/main/java/com/vaadin/ui/AbstractColorPicker.java index a537df0b21..b950361798 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractColorPicker.java +++ b/server/src/main/java/com/vaadin/ui/AbstractColorPicker.java @@ -514,12 +514,14 @@ public abstract class AbstractColorPicker extends AbstractField<Color> { Attributes attribute = design.attributes(); DesignAttributeHandler.writeAttribute("color", attribute, - getValue().getCSS(), Color.WHITE.getCSS(), String.class); + getValue().getCSS(), Color.WHITE.getCSS(), String.class, + designContext); DesignAttributeHandler.writeAttribute("popup-style", attribute, popupStyle == PopupStyle.POPUP_NORMAL ? "normal" : "simple", - "normal", String.class); + "normal", String.class, designContext); DesignAttributeHandler.writeAttribute("position", attribute, - positionX + "," + positionY, "0,0", String.class); + positionX + "," + positionY, "0,0", String.class, + designContext); } @Override diff --git a/server/src/main/java/com/vaadin/ui/AbstractComponent.java b/server/src/main/java/com/vaadin/ui/AbstractComponent.java index 0681612996..94fa738dd3 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractComponent.java +++ b/server/src/main/java/com/vaadin/ui/AbstractComponent.java @@ -1240,7 +1240,8 @@ public abstract class AbstractComponent extends AbstractClientConnector Attributes attr = design.attributes(); // handle default attributes for (String attribute : getDefaultAttributes()) { - DesignAttributeHandler.writeAttribute(this, attribute, attr, def); + DesignAttributeHandler.writeAttribute(this, attribute, attr, def, + designContext); } // handle locale if (getLocale() != null && (getParent() == null @@ -1261,7 +1262,8 @@ public abstract class AbstractComponent extends AbstractClientConnector if (this instanceof Focusable) { DesignAttributeHandler.writeAttribute("tabindex", attr, ((Focusable) this).getTabIndex(), - ((Focusable) def).getTabIndex(), Integer.class); + ((Focusable) def).getTabIndex(), Integer.class, + designContext); } // handle custom attributes Map<String, String> customAttributes = designContext diff --git a/server/src/main/java/com/vaadin/ui/AbstractEmbedded.java b/server/src/main/java/com/vaadin/ui/AbstractEmbedded.java index 978e82d468..a91332f948 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractEmbedded.java +++ b/server/src/main/java/com/vaadin/ui/AbstractEmbedded.java @@ -100,7 +100,8 @@ public abstract class AbstractEmbedded extends AbstractComponent { super.writeDesign(design, designContext); AbstractEmbedded def = designContext.getDefaultInstance(this); DesignAttributeHandler.writeAttribute("alt", design.attributes(), - getAlternateText(), def.getAlternateText(), String.class); + getAlternateText(), def.getAlternateText(), String.class, + designContext); } @Override diff --git a/server/src/main/java/com/vaadin/ui/AbstractField.java b/server/src/main/java/com/vaadin/ui/AbstractField.java index c87a72e430..4add30183e 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractField.java @@ -121,7 +121,8 @@ public abstract class AbstractField<T> extends AbstractComponent AbstractField<T> def = designContext.getDefaultInstance(this); Attributes attr = design.attributes(); DesignAttributeHandler.writeAttribute("readonly", attr, - super.isReadOnly(), def.isReadOnly(), Boolean.class); + super.isReadOnly(), def.isReadOnly(), Boolean.class, + designContext); } @Override diff --git a/server/src/main/java/com/vaadin/ui/AbstractLayout.java b/server/src/main/java/com/vaadin/ui/AbstractLayout.java index 3c31e59e2c..a4b4f06bf7 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractLayout.java +++ b/server/src/main/java/com/vaadin/ui/AbstractLayout.java @@ -102,24 +102,25 @@ public abstract class AbstractLayout extends AbstractComponentContainer MarginInfo defMargin, DesignContext context) { if (margin.hasAll()) { DesignAttributeHandler.writeAttribute("margin", design.attributes(), - margin.hasAll(), defMargin.hasAll(), boolean.class); + margin.hasAll(), defMargin.hasAll(), boolean.class, + context); } else { DesignAttributeHandler.writeAttribute("margin-left", design.attributes(), margin.hasLeft(), defMargin.hasLeft(), - boolean.class); + boolean.class, context); DesignAttributeHandler.writeAttribute("margin-right", design.attributes(), margin.hasRight(), - defMargin.hasRight(), boolean.class); + defMargin.hasRight(), boolean.class, context); DesignAttributeHandler.writeAttribute("margin-top", design.attributes(), margin.hasTop(), defMargin.hasTop(), - boolean.class); + boolean.class, context); DesignAttributeHandler.writeAttribute("margin-bottom", design.attributes(), margin.hasBottom(), - defMargin.hasBottom(), boolean.class); + defMargin.hasBottom(), boolean.class, context); } } } diff --git a/server/src/main/java/com/vaadin/ui/AbstractMedia.java b/server/src/main/java/com/vaadin/ui/AbstractMedia.java index f56f6a7f27..a992554d67 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractMedia.java +++ b/server/src/main/java/com/vaadin/ui/AbstractMedia.java @@ -275,7 +275,7 @@ public abstract class AbstractMedia extends AbstractComponent { for (Resource r : getSources()) { Attributes attr = design.appendElement("source").attributes(); DesignAttributeHandler.writeAttribute("href", attr, r, null, - Resource.class); + Resource.class, designContext); } } diff --git a/server/src/main/java/com/vaadin/ui/AbstractTextField.java b/server/src/main/java/com/vaadin/ui/AbstractTextField.java index ee2ccc6604..714f4b8e64 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractTextField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractTextField.java @@ -285,7 +285,7 @@ public abstract class AbstractTextField extends AbstractField<String> .getDefaultInstance(this); Attributes attr = design.attributes(); DesignAttributeHandler.writeAttribute("maxlength", attr, getMaxLength(), - def.getMaxLength(), Integer.class); + def.getMaxLength(), Integer.class, designContext); } @Override diff --git a/server/src/main/java/com/vaadin/ui/Button.java b/server/src/main/java/com/vaadin/ui/Button.java index 499713ffbd..86321f357c 100644 --- a/server/src/main/java/com/vaadin/ui/Button.java +++ b/server/src/main/java/com/vaadin/ui/Button.java @@ -637,11 +637,11 @@ public class Button extends AbstractFocusable // icon-alt DesignAttributeHandler.writeAttribute("icon-alt", attr, getIconAlternateText(), def.getIconAlternateText(), - String.class); + String.class, designContext); // click-shortcut if (clickShortcut != null) { DesignAttributeHandler.writeAttribute("click-shortcut", attr, - clickShortcut, null, ShortcutAction.class); + clickShortcut, null, ShortcutAction.class, designContext); } } } diff --git a/server/src/main/java/com/vaadin/ui/CheckBox.java b/server/src/main/java/com/vaadin/ui/CheckBox.java index 02d7b0db8b..9a6394863e 100644 --- a/server/src/main/java/com/vaadin/ui/CheckBox.java +++ b/server/src/main/java/com/vaadin/ui/CheckBox.java @@ -218,7 +218,7 @@ public class CheckBox extends AbstractField<Boolean> CheckBox def = (CheckBox) designContext.getDefaultInstance(this); Attributes attr = design.attributes(); DesignAttributeHandler.writeAttribute("checked", attr, getValue(), - def.getValue(), Boolean.class); + def.getValue(), Boolean.class, designContext); } } diff --git a/server/src/main/java/com/vaadin/ui/GridLayout.java b/server/src/main/java/com/vaadin/ui/GridLayout.java index 40b8575c36..08ca2cb2b3 100644 --- a/server/src/main/java/com/vaadin/ui/GridLayout.java +++ b/server/src/main/java/com/vaadin/ui/GridLayout.java @@ -27,7 +27,6 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; import java.util.Set; import org.jsoup.nodes.Attributes; @@ -1453,7 +1452,7 @@ public class GridLayout extends AbstractLayout // Row Expand DesignAttributeHandler.writeAttribute("expand", row.attributes(), - (int) getRowExpandRatio(i), 0, int.class); + (int) getRowExpandRatio(i), 0, int.class, designContext); int colspan = 1; Element col; diff --git a/server/src/main/java/com/vaadin/ui/Link.java b/server/src/main/java/com/vaadin/ui/Link.java index 24c1433968..b80e8cd206 100644 --- a/server/src/main/java/com/vaadin/ui/Link.java +++ b/server/src/main/java/com/vaadin/ui/Link.java @@ -226,9 +226,11 @@ public class Link extends AbstractComponent { super.writeDesign(design, designContext); Link def = designContext.getDefaultInstance(this); DesignAttributeHandler.writeAttribute("target", design.attributes(), - getTargetName(), def.getTargetName(), String.class); + getTargetName(), def.getTargetName(), String.class, + designContext); DesignAttributeHandler.writeAttribute("href", design.attributes(), - getResource(), def.getResource(), Resource.class); + getResource(), def.getResource(), Resource.class, + designContext); } @Override diff --git a/server/src/main/java/com/vaadin/ui/MenuBar.java b/server/src/main/java/com/vaadin/ui/MenuBar.java index 24f2dd89b3..884c3d4eb3 100644 --- a/server/src/main/java/com/vaadin/ui/MenuBar.java +++ b/server/src/main/java/com/vaadin/ui/MenuBar.java @@ -947,13 +947,14 @@ public class MenuBar extends AbstractComponent public void writeDesign(Element design, DesignContext designContext) { super.writeDesign(design, designContext); for (MenuItem item : getItems()) { - design.appendChild(createMenuElement(item)); + design.appendChild(createMenuElement(item, designContext)); } // in many cases there seems to be an empty more menu item if (getMoreMenuItem() != null && !getMoreMenuItem().getText().isEmpty()) { - Element moreMenu = createMenuElement(getMoreMenuItem()); + Element moreMenu = createMenuElement(getMoreMenuItem(), + designContext); moreMenu.attr("more", true); design.appendChild(moreMenu); } @@ -963,34 +964,35 @@ public class MenuBar extends AbstractComponent } } - protected Element createMenuElement(MenuItem item) { + protected Element createMenuElement(MenuItem item, DesignContext context) { Element menuElement = new Element(Tag.valueOf("menu"), ""); // Defaults MenuItem def = new MenuItem("", null, null); Attributes attr = menuElement.attributes(); DesignAttributeHandler.writeAttribute("icon", attr, item.getIcon(), - def.getIcon(), Resource.class); + def.getIcon(), Resource.class, context); DesignAttributeHandler.writeAttribute("disabled", attr, - !item.isEnabled(), !def.isEnabled(), boolean.class); + !item.isEnabled(), !def.isEnabled(), boolean.class, context); DesignAttributeHandler.writeAttribute("visible", attr, item.isVisible(), - def.isVisible(), boolean.class); + def.isVisible(), boolean.class, context); DesignAttributeHandler.writeAttribute("separator", attr, - item.isSeparator(), def.isSeparator(), boolean.class); + item.isSeparator(), def.isSeparator(), boolean.class, context); DesignAttributeHandler.writeAttribute("checkable", attr, - item.isCheckable(), def.isCheckable(), boolean.class); + item.isCheckable(), def.isCheckable(), boolean.class, context); DesignAttributeHandler.writeAttribute("checked", attr, item.isChecked(), - def.isChecked(), boolean.class); + def.isChecked(), boolean.class, context); DesignAttributeHandler.writeAttribute("description", attr, - item.getDescription(), def.getDescription(), String.class); + item.getDescription(), def.getDescription(), String.class, + context); DesignAttributeHandler.writeAttribute("style-name", attr, - item.getStyleName(), def.getStyleName(), String.class); + item.getStyleName(), def.getStyleName(), String.class, context); menuElement.append(item.getText()); if (item.hasChildren()) { for (MenuItem subMenu : item.getChildren()) { - menuElement.appendChild(createMenuElement(subMenu)); + menuElement.appendChild(createMenuElement(subMenu, context)); } } diff --git a/server/src/main/java/com/vaadin/ui/PasswordField.java b/server/src/main/java/com/vaadin/ui/PasswordField.java index 231236c01f..7a154b2b29 100644 --- a/server/src/main/java/com/vaadin/ui/PasswordField.java +++ b/server/src/main/java/com/vaadin/ui/PasswordField.java @@ -76,6 +76,6 @@ public class PasswordField extends TextField { .getDefaultInstance(this); Attributes attr = design.attributes(); DesignAttributeHandler.writeAttribute("value", attr, getValue(), - def.getValue(), String.class); + def.getValue(), String.class, designContext); } } diff --git a/server/src/main/java/com/vaadin/ui/ProgressBar.java b/server/src/main/java/com/vaadin/ui/ProgressBar.java index 7df0fc5f83..e5d82a62b0 100644 --- a/server/src/main/java/com/vaadin/ui/ProgressBar.java +++ b/server/src/main/java/com/vaadin/ui/ProgressBar.java @@ -126,7 +126,7 @@ public class ProgressBar extends AbstractComponent { Float defaultValue = ((ProgressBar) designContext .getDefaultInstance(this)).getValue(); DesignAttributeHandler.writeAttribute("value", design.attributes(), - getValue(), defaultValue, Float.class); + getValue(), defaultValue, Float.class, designContext); } /** diff --git a/server/src/main/java/com/vaadin/ui/Slider.java b/server/src/main/java/com/vaadin/ui/Slider.java index 397e33e7c3..3a1d77e38f 100644 --- a/server/src/main/java/com/vaadin/ui/Slider.java +++ b/server/src/main/java/com/vaadin/ui/Slider.java @@ -372,7 +372,7 @@ public class Slider extends AbstractField<Double> { } Slider defaultSlider = context.getDefaultInstance(this); DesignAttributeHandler.writeAttribute(this, "value", - design.attributes(), defaultSlider); + design.attributes(), defaultSlider, context); } @Override diff --git a/server/src/main/java/com/vaadin/ui/TabSheet.java b/server/src/main/java/com/vaadin/ui/TabSheet.java index 7a154dd752..b19acf8ddb 100644 --- a/server/src/main/java/com/vaadin/ui/TabSheet.java +++ b/server/src/main/java/com/vaadin/ui/TabSheet.java @@ -1556,29 +1556,32 @@ public class TabSheet extends AbstractComponentContainer Attributes attr = tabElement.attributes(); // write attributes DesignAttributeHandler.writeAttribute("visible", attr, tab.isVisible(), - def.isVisible(), Boolean.class); + def.isVisible(), Boolean.class, designContext); DesignAttributeHandler.writeAttribute("closable", attr, - tab.isClosable(), def.isClosable(), Boolean.class); + tab.isClosable(), def.isClosable(), Boolean.class, + designContext); DesignAttributeHandler.writeAttribute("caption", attr, tab.getCaption(), - def.getCaption(), String.class); + def.getCaption(), String.class, designContext); DesignAttributeHandler.writeAttribute("enabled", attr, tab.isEnabled(), - def.isEnabled(), Boolean.class); + def.isEnabled(), Boolean.class, designContext); DesignAttributeHandler.writeAttribute("icon", attr, tab.getIcon(), - def.getIcon(), Resource.class); + def.getIcon(), Resource.class, designContext); DesignAttributeHandler.writeAttribute("icon-alt", attr, tab.getIconAlternateText(), def.getIconAlternateText(), - String.class); + String.class, designContext); DesignAttributeHandler.writeAttribute("description", attr, - tab.getDescription(), def.getDescription(), String.class); + tab.getDescription(), def.getDescription(), String.class, + designContext); DesignAttributeHandler.writeAttribute("style-name", attr, - tab.getStyleName(), def.getStyleName(), String.class); + tab.getStyleName(), def.getStyleName(), String.class, + designContext); DesignAttributeHandler.writeAttribute("id", attr, tab.getId(), - def.getId(), String.class); + def.getId(), String.class, designContext); if (getSelectedTab() != null && getSelectedTab().equals(tab.getComponent())) { // use write attribute to get consistent handling for boolean DesignAttributeHandler.writeAttribute("selected", attr, true, false, - boolean.class); + boolean.class, designContext); } } diff --git a/server/src/main/java/com/vaadin/ui/TextField.java b/server/src/main/java/com/vaadin/ui/TextField.java index c01650fb60..b6eec11795 100644 --- a/server/src/main/java/com/vaadin/ui/TextField.java +++ b/server/src/main/java/com/vaadin/ui/TextField.java @@ -87,6 +87,6 @@ public class TextField extends AbstractTextField { .getDefaultInstance(this); Attributes attr = design.attributes(); DesignAttributeHandler.writeAttribute("value", attr, getValue(), - def.getValue(), String.class); + def.getValue(), String.class, designContext); } } diff --git a/server/src/main/java/com/vaadin/ui/Video.java b/server/src/main/java/com/vaadin/ui/Video.java index a9c3f3e729..af0ef618a4 100644 --- a/server/src/main/java/com/vaadin/ui/Video.java +++ b/server/src/main/java/com/vaadin/ui/Video.java @@ -118,7 +118,7 @@ public class Video extends AbstractMedia { if (getPoster() != null) { Attributes attr = design.appendElement("poster").attributes(); DesignAttributeHandler.writeAttribute("href", attr, getPoster(), - null, Resource.class); + null, Resource.class, designContext); } } diff --git a/server/src/main/java/com/vaadin/ui/Window.java b/server/src/main/java/com/vaadin/ui/Window.java index 82acf0ee6f..df06e54457 100644 --- a/server/src/main/java/com/vaadin/ui/Window.java +++ b/server/src/main/java/com/vaadin/ui/Window.java @@ -1406,7 +1406,7 @@ public class Window extends Panel } DesignAttributeHandler.writeAttribute("position", design.attributes(), - getPosition(), def.getPosition(), String.class); + getPosition(), def.getPosition(), String.class, context); // Process keyboard shortcuts if (closeShortcuts.size() == 1 && hasCloseShortcut(KeyCode.ESCAPE)) { @@ -1425,7 +1425,8 @@ public class Window extends Panel // Write everything except the last "," to the design DesignAttributeHandler.writeAttribute("close-shortcut", - design.attributes(), attrString.trim(), null, String.class); + design.attributes(), attrString.trim(), null, String.class, + context); } for (Component c : getAssistiveDescription()) { diff --git a/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java b/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java index 4d655e0cbf..2eea59a046 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java +++ b/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java @@ -56,25 +56,10 @@ public class DesignAttributeHandler implements Serializable { return Logger.getLogger(DesignAttributeHandler.class.getName()); } - private static Map<Class<?>, AttributeCacheEntry> cache = new ConcurrentHashMap<>(); + private final static Map<Class<?>, AttributeCacheEntry> cache = new ConcurrentHashMap<>(); // translates string <-> object - private static DesignFormatter FORMATTER = new DesignFormatter(); - - private static boolean writeDefaultValues = false; - - /** - * Set whether default attribute values should be written by the - * {@code DesignAttributeHandler#writeAttribute(String, Attributes, Object, Object, Class)} - * method. Default is {@code false}. - * - * @param value - * {@code true} to write default values of attributes, - * {@code false} to disable writing of default values - */ - public static void setWriteDefaultValues(boolean value) { - writeDefaultValues = value; - } + private final static DesignFormatter FORMATTER = new DesignFormatter(); /** * Returns the currently used formatter. All primitive types and all types @@ -212,7 +197,7 @@ public class DesignAttributeHandler implements Serializable { */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static void writeAttribute(Object component, String attribute, - Attributes attr, Object defaultInstance) { + Attributes attr, Object defaultInstance, DesignContext context) { Method getter = findGetterForAttribute(component.getClass(), attribute); if (getter == null) { getLogger().warning( @@ -223,7 +208,7 @@ public class DesignAttributeHandler implements Serializable { Object value = getter.invoke(component); Object defaultValue = getter.invoke(defaultInstance); writeAttribute(attribute, attr, value, defaultValue, - (Class) getter.getReturnType()); + (Class) getter.getReturnType(), context); } catch (Exception e) { getLogger().log(Level.SEVERE, "Failed to invoke getter for attribute " + attribute, @@ -248,13 +233,14 @@ public class DesignAttributeHandler implements Serializable { * the type of the input value */ public static <T> void writeAttribute(String attribute, - Attributes attributes, T value, T defaultValue, - Class<T> inputType) { + Attributes attributes, T value, T defaultValue, Class<T> inputType, + DesignContext context) { if (!getFormatter().canConvert(inputType)) { throw new IllegalArgumentException( "input type: " + inputType.getName() + " not supported"); } - if (writeDefaultValues || !SharedUtil.equals(value, defaultValue)) { + if (context.shouldWriteDefaultValues() + || !SharedUtil.equals(value, defaultValue)) { String attributeValue = toAttributeValue(inputType, value); if ("".equals(attributeValue) && (inputType == boolean.class || inputType == Boolean.class)) { diff --git a/server/src/main/java/com/vaadin/ui/declarative/DesignContext.java b/server/src/main/java/com/vaadin/ui/declarative/DesignContext.java index ecbd5e638c..c8e794c46a 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/DesignContext.java +++ b/server/src/main/java/com/vaadin/ui/declarative/DesignContext.java @@ -94,6 +94,8 @@ public class DesignContext implements Serializable { // this cannot be static because of testability issues private Boolean legacyDesignPrefix = null; + private boolean shouldWriteDefaultValues = false; + public DesignContext(Document doc) { this.doc = doc; // Initialize the mapping between prefixes and package names. @@ -831,4 +833,31 @@ public class DesignContext implements Serializable { } map.put(attribute, value); } + + /** + * Set whether default attribute values should be written by the + * {@code DesignAttributeHandler#writeAttribute(String, Attributes, Object, Object, Class, DesignContext)} + * method. Default is {@code false}. + * + * @since 8.0 + * @param value + * {@code true} to write default values of attributes, + * {@code false} to disable writing of default values + */ + public void setShouldWriteDefaultValues(boolean value) { + shouldWriteDefaultValues = value; + } + + /** + * Determines whether default attribute values should be written by the + * {@code DesignAttributeHandler#writeAttribute(String, Attributes, Object, Object, Class, DesignContext)} + * method. Default is {@code false}. + * + * @since 8.0 + * @return {@code true} if default values of attributes should be written, + * otherwise {@code false} + */ + public boolean shouldWriteDefaultValues() { + return shouldWriteDefaultValues; + } } |