diff options
author | Johannes Dahlström <johannesd@vaadin.com> | 2015-04-09 17:34:04 +0300 |
---|---|---|
committer | Teemu Suo-Anttila <teemusa@vaadin.com> | 2015-04-13 08:23:36 +0000 |
commit | 68c7963f085fc299ac9ebded9394b638c7392bb1 (patch) | |
tree | 0593ab527b14b0deac8407a9e064d5325fdf416b /server/src/com/vaadin/ui/AbstractSelect.java | |
parent | b3b50eb368a8b6200200b6b1f1324d0fc63f4409 (diff) | |
download | vaadin-framework-68c7963f085fc299ac9ebded9394b638c7392bb1.tar.gz vaadin-framework-68c7963f085fc299ac9ebded9394b638c7392bb1.zip |
Declarative support for Tree inline data (#16321)
<v-tree> elements may now contain a hierarchy of <node> elements.
Change-Id: I6b1703aa4ec598cf0b3a6221f615727efbcbf0ad
Diffstat (limited to 'server/src/com/vaadin/ui/AbstractSelect.java')
-rw-r--r-- | server/src/com/vaadin/ui/AbstractSelect.java | 125 |
1 files changed, 91 insertions, 34 deletions
diff --git a/server/src/com/vaadin/ui/AbstractSelect.java b/server/src/com/vaadin/ui/AbstractSelect.java index 4c5e6b6ea3..629d2fb504 100644 --- a/server/src/com/vaadin/ui/AbstractSelect.java +++ b/server/src/com/vaadin/ui/AbstractSelect.java @@ -2188,28 +2188,13 @@ public abstract class AbstractSelect extends AbstractField<Object> implements } @Override - public void readDesign(Element design, DesignContext designContext) { + public void readDesign(Element design, DesignContext context) { // handle default attributes - super.readDesign(design, designContext); + super.readDesign(design, context); // handle children specifying selectable items (<option>) Set<String> selected = new HashSet<String>(); for (Element child : design.children()) { - if (!"option".equals(child.nodeName())) { - throw new DesignException( - "Unsupported child element in a select: " - + child.nodeName() + "."); - } - String itemId = child.html(); - addItem(itemId); - if (child.hasAttr("icon")) { - setItemIcon( - itemId, - DesignAttributeHandler.readAttribute("icon", - child.attributes(), Resource.class)); - } - if (child.hasAttr("selected")) { - selected.add(itemId); - } + readItem(child, selected, context); } if (!selected.isEmpty()) { if (isMultiSelect()) { @@ -2223,29 +2208,101 @@ public abstract class AbstractSelect extends AbstractField<Object> implements } } + /** + * Reads an Item from a design and inserts it into the data source. + * Hierarchical select components should override this method to recursively + * recursively read any child items as well. + * + * @since + * @param child + * a child element representing the item + * @param selected + * A set accumulating selected items. If the item that is read is + * marked as selected, its item id should be added to this set. + * @param context + * the DesignContext instance used in parsing + * @return the item id of the new item + * + * @throws DesignException + * if the tag name of the {@code child} element is not + * {@code option}. + */ + protected String readItem(Element child, Set<String> selected, + DesignContext context) { + if (!"option".equals(child.tagName())) { + throw new DesignException("Unrecognized child element in " + + getClass().getSimpleName() + ": " + child.tagName()); + } + String itemId = child.html(); + addItem(itemId); + if (child.hasAttr("icon")) { + setItemIcon( + itemId, + DesignAttributeHandler.readAttribute("icon", + child.attributes(), Resource.class)); + } + if (child.hasAttr("selected")) { + selected.add(itemId); + } + return itemId; + } + @Override - public void writeDesign(Element design, DesignContext designContext) { + public void writeDesign(Element design, DesignContext context) { // Write default attributes - super.writeDesign(design, designContext); + super.writeDesign(design, context); // Write options if warranted - if (designContext.shouldWriteData(this)) { - for (Object itemId : getItemIds()) { - Element optionElement = design.appendElement("option"); + if (context.shouldWriteData(this)) { + writeItems(design, context); + } + } + + /** + * Writes the data source items to a design. Hierarchical select components + * should override this method to only write the root items. + * + * @since + * @param design + * the element into which to insert the items + * @param context + * the DesignContext instance used in writing + */ + protected void writeItems(Element design, DesignContext context) { + for (Object itemId : getItemIds()) { + writeItem(design, itemId, context); + } + } - optionElement.html(getItemCaption(itemId)); + /** + * Writes a data source Item to a design. Hierarchical select components + * should override this method to recursively write any child items as well. + * + * @since + * @param design + * the element into which to insert the item + * @param itemId + * the id of the item to write + * @param context + * the DesignContext instance used in writing + * @return + */ + protected Element writeItem(Element design, Object itemId, + DesignContext context) { + Element element = design.appendElement("option"); - Resource icon = getItemIcon(itemId); - if (icon != null) { - DesignAttributeHandler.writeAttribute("icon", - optionElement.attributes(), icon, null, - Resource.class); - } + element.html(itemId.toString()); - if (isSelected(itemId)) { - optionElement.attr("selected", ""); - } - } + Resource icon = getItemIcon(itemId); + if (icon != null) { + DesignAttributeHandler.writeAttribute("icon", element.attributes(), + icon, null, Resource.class); } + + if (isSelected(itemId)) { + element.attr("selected", ""); + } + + return element; } }
\ No newline at end of file |