diff options
author | Johannes Dahlström <johannesd@vaadin.com> | 2015-03-31 15:41:46 +0300 |
---|---|---|
committer | Johannes Dahlström <johannesd@vaadin.com> | 2015-04-07 15:21:50 +0300 |
commit | 8664c97c7bb6fb36b2ebbe3849b51ec00e052e24 (patch) | |
tree | 31451d8bf956cb2bfb686857f27f12e54703f67d /server/src/com/vaadin/ui/Window.java | |
parent | 0f1dcd23a0bffae76fc312d95d7b64bf7861df88 (diff) | |
download | vaadin-framework-8664c97c7bb6fb36b2ebbe3849b51ec00e052e24.tar.gz vaadin-framework-8664c97c7bb6fb36b2ebbe3849b51ec00e052e24.zip |
Fix declarative support for Window (#17314)
Change-Id: If89a46a4c08ec1491eb00a2f2b8580fb3ef785fc
Diffstat (limited to 'server/src/com/vaadin/ui/Window.java')
-rw-r--r-- | server/src/com/vaadin/ui/Window.java | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/server/src/com/vaadin/ui/Window.java b/server/src/com/vaadin/ui/Window.java index 653b620746..e7764ffd8d 100644 --- a/server/src/com/vaadin/ui/Window.java +++ b/server/src/com/vaadin/ui/Window.java @@ -18,11 +18,18 @@ package com.vaadin.ui; import java.io.Serializable; import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Iterator; import java.util.LinkedHashSet; +import java.util.List; import java.util.Map; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + import com.vaadin.event.FieldEvents.BlurEvent; import com.vaadin.event.FieldEvents.BlurListener; import com.vaadin.event.FieldEvents.BlurNotifier; @@ -42,6 +49,9 @@ import com.vaadin.shared.ui.window.WindowMode; import com.vaadin.shared.ui.window.WindowRole; import com.vaadin.shared.ui.window.WindowServerRpc; import com.vaadin.shared.ui.window.WindowState; +import com.vaadin.ui.declarative.DesignAttributeHandler; +import com.vaadin.ui.declarative.DesignContext; +import com.vaadin.ui.declarative.DesignException; import com.vaadin.util.ReflectTools; /** @@ -1283,4 +1293,116 @@ public class Window extends Panel implements FocusNotifier, BlurNotifier, public String getTabStopBottomAssistiveText() { return getState(false).assistiveTabStopBottomText; } + + @Override + public void readDesign(Element design, DesignContext context) { + super.readDesign(design, context); + + if (design.hasAttr("center")) { + center(); + } + if (design.hasAttr("position")) { + String[] position = design.attr("position").split(","); + setPositionX(Integer.parseInt(position[0])); + setPositionY(Integer.parseInt(position[1])); + } + if (design.hasAttr("close-shortcut")) { + ShortcutAction shortcut = DesignAttributeHandler + .readAttribute("close-shortcut", design.attributes(), + ShortcutAction.class); + setCloseShortcut(shortcut.getKeyCode(), shortcut.getModifiers()); + } + } + + /** + * Reads the content and possible assistive descriptions from the list of + * child elements of a design. If an element has an + * {@code :assistive-description} attribute, adds the parsed component to + * the list of components used as the assistive description of this Window. + * Otherwise, sets the component as the content of this Window. If there are + * multiple non-description elements, throws a DesignException. + * + * @param children + * child elements in a design + * @param context + * the DesignContext instance used to parse the design + * + * @throws DesignException + * if there are multiple non-description child elements + * @throws DesignException + * if a child element could not be parsed as a Component + * + * @see #setContent(Component) + * @see #setAssistiveDescription(Component...) + */ + @Override + protected void readDesignChildren(Elements children, DesignContext context) { + List<Component> descriptions = new ArrayList<Component>(); + Elements content = new Elements(); + + for (Element child : children) { + if (child.hasAttr(":assistive-description")) { + descriptions.add(context.readDesign(child)); + } else { + content.add(child); + } + } + super.readDesignChildren(content, context); + setAssistiveDescription(descriptions.toArray(new Component[0])); + } + + @Override + public void writeDesign(Element design, DesignContext context) { + super.writeDesign(design, context); + + Window def = context.getDefaultInstance(this); + + if (getState().centered) { + design.attr("center", ""); + } + + DesignAttributeHandler.writeAttribute("position", design.attributes(), + getPosition(), def.getPosition(), String.class); + + CloseShortcut shortcut = getCloseShortcut(); + if (shortcut != null) { + // TODO What if several close shortcuts?? + + CloseShortcut defShortcut = def.getCloseShortcut(); + if (defShortcut == null + || shortcut.getKeyCode() != defShortcut.getKeyCode() + || !Arrays.equals(shortcut.getModifiers(), + defShortcut.getModifiers())) { + DesignAttributeHandler.writeAttribute("close-shortcut", + design.attributes(), shortcut, null, + CloseShortcut.class); + } + } + + for (Component c : getAssistiveDescription()) { + Element child = context.createElement(c).attr( + ":assistive-description", ""); + design.appendChild(child); + } + } + + private String getPosition() { + return getPositionX() + "," + getPositionY(); + } + + private CloseShortcut getCloseShortcut() { + Iterator<CloseShortcut> i = getCloseShortcuts().iterator(); + return i.hasNext() ? i.next() : null; + } + + @Override + protected Collection<String> getCustomAttributes() { + Collection<String> result = super.getCustomAttributes(); + result.add("center"); + result.add("position"); + result.add("position-y"); + result.add("position-x"); + result.add("close-shortcut"); + return result; + } } |