diff options
Diffstat (limited to 'server/src/com/vaadin/ui/TabSheet.java')
-rw-r--r-- | server/src/com/vaadin/ui/TabSheet.java | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/server/src/com/vaadin/ui/TabSheet.java b/server/src/com/vaadin/ui/TabSheet.java index d3410464a2..dd9beb07f0 100644 --- a/server/src/com/vaadin/ui/TabSheet.java +++ b/server/src/com/vaadin/ui/TabSheet.java @@ -19,11 +19,15 @@ package com.vaadin.ui; import java.io.Serializable; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; + import com.vaadin.event.FieldEvents.BlurEvent; import com.vaadin.event.FieldEvents.BlurListener; import com.vaadin.event.FieldEvents.BlurNotifier; @@ -40,6 +44,9 @@ import com.vaadin.shared.ui.tabsheet.TabsheetClientRpc; import com.vaadin.shared.ui.tabsheet.TabsheetServerRpc; import com.vaadin.shared.ui.tabsheet.TabsheetState; import com.vaadin.ui.Component.Focusable; +import com.vaadin.ui.declarative.DesignAttributeHandler; +import com.vaadin.ui.declarative.DesignContext; +import com.vaadin.ui.declarative.DesignException; import com.vaadin.ui.themes.Reindeer; import com.vaadin.ui.themes.Runo; @@ -1447,4 +1454,164 @@ public class TabSheet extends AbstractComponentContainer implements Focusable, protected TabsheetState getState() { return (TabsheetState) super.getState(); } + + /* + * (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) { + super.synchronizeFromDesign(design, designContext); + Attributes attr = design.attributes(); + TabSheet def = designContext.getDefaultInstance(this.getClass()); + // handle tab index + int tabIndex = DesignAttributeHandler.readAttribute("tabindex", attr, + def.getTabIndex(), Integer.class); + setTabIndex(tabIndex); + // clear old tabs + removeAllComponents(); + // create new tabs + for (Element tab : design.children()) { + if (!tab.tagName().equals("tab")) { + throw new DesignException("Invalid tag name for tabsheet tab " + + tab.tagName()); + } + readTabFromDesign(tab, designContext); + } + } + + /** + * Reads the given tab element from design + * + * @since 7.4 + * + * @param tabElement + * the element to be read + * @param designContext + * the design context + */ + private void readTabFromDesign(Element tabElement, + DesignContext designContext) { + Attributes attr = tabElement.attributes(); + if (tabElement.children().size() != 1) { + throw new DesignException( + "A tab must have exactly one child element"); + } + // create the component that is in tab content + Element content = tabElement.child(0); + DesignSynchronizable child = designContext.createChild(content); + Tab tab = this.addTab(child); + tab.setVisible(DesignAttributeHandler.readAttribute("visible", attr, + tab.isVisible(), Boolean.class)); + tab.setClosable(DesignAttributeHandler.readAttribute("closable", attr, + tab.isClosable(), Boolean.class)); + tab.setCaption(DesignAttributeHandler.readAttribute("caption", attr, + tab.getCaption(), String.class)); + tab.setEnabled(DesignAttributeHandler.readAttribute("enabled", attr, + tab.isEnabled(), Boolean.class)); + tab.setIcon(DesignAttributeHandler.readAttribute("icon", attr, + tab.getIcon(), Resource.class)); + tab.setIconAlternateText(DesignAttributeHandler.readAttribute( + "icon-alt", attr, tab.getIconAlternateText(), String.class)); + tab.setDescription(DesignAttributeHandler.readAttribute("description", + attr, tab.getDescription(), String.class)); + tab.setStyleName(DesignAttributeHandler.readAttribute("style-name", + attr, tab.getStyleName(), String.class)); + tab.setId(DesignAttributeHandler.readAttribute("id", attr, tab.getId(), + String.class)); + boolean selected = DesignAttributeHandler.readAttribute("selected", + attr, false, Boolean.class); + if (selected) { + this.setSelectedTab(tab.getComponent()); + } + } + + /** + * Writes the given tab to design + * + * @since 7.4 + * @param design + * the design node for tabsheet + * @param designContext + * the design context + * @param tab + * the tab to be written + */ + private void writeTabToDesign(Element design, DesignContext designContext, + Tab tab) { + // get default tab instance + Tab def = new TabSheetTabImpl(null, null, null); + // create element for tab + Element tabElement = design.appendElement("tab"); + // add tab content + tabElement.appendChild(designContext + .createNode((DesignSynchronizable) tab.getComponent())); + Attributes attr = tabElement.attributes(); + // write attributes + DesignAttributeHandler.writeAttribute("visible", attr, tab.isVisible(), + def.isVisible(), Boolean.class); + DesignAttributeHandler.writeAttribute("closable", attr, + tab.isClosable(), def.isClosable(), Boolean.class); + DesignAttributeHandler.writeAttribute("caption", attr, + tab.getCaption(), def.getCaption(), String.class); + DesignAttributeHandler.writeAttribute("enabled", attr, tab.isEnabled(), + def.isEnabled(), Boolean.class); + DesignAttributeHandler.writeAttribute("icon", attr, tab.getIcon(), + def.getIcon(), Resource.class); + DesignAttributeHandler.writeAttribute("icon-alt", attr, + tab.getIconAlternateText(), def.getIconAlternateText(), + String.class); + DesignAttributeHandler.writeAttribute("description", attr, + tab.getDescription(), def.getDescription(), String.class); + DesignAttributeHandler.writeAttribute("style-name", attr, + tab.getStyleName(), def.getStyleName(), String.class); + DesignAttributeHandler.writeAttribute("id", attr, tab.getId(), + def.getId(), String.class); + if (getSelectedTab() != null + && getSelectedTab().equals(tab.getComponent())) { + // use write attribute to get consistent handling for boolean + DesignAttributeHandler.writeAttribute("selected", attr, true, + false, boolean.class); + } + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.AbstractComponent#getCustomAttributes() + */ + @Override + protected Collection<String> getCustomAttributes() { + Collection<String> attributes = super.getCustomAttributes(); + attributes.add("tabindex"); + // no need to list tab attributes since they are considered internal + return attributes; + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.ui.AbstractComponent#synchronizeToDesign(org.jsoup.nodes.Element + * , com.vaadin.ui.declarative.DesignContext) + */ + @Override + public void synchronizeToDesign(Element design, DesignContext designContext) { + super.synchronizeToDesign(design, designContext); + TabSheet def = designContext.getDefaultInstance(this.getClass()); + Attributes attr = design.attributes(); + // handle tab index + DesignAttributeHandler.writeAttribute("tabindex", attr, getTabIndex(), + def.getTabIndex(), Integer.class); + // write tabs + for (Component component : this) { + Tab tab = this.getTab(component); + writeTabToDesign(design, designContext, tab); + } + } + } |