diff options
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/com/vaadin/ui/Button.java | 42 | ||||
-rw-r--r-- | server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java | 171 |
2 files changed, 210 insertions, 3 deletions
diff --git a/server/src/com/vaadin/ui/Button.java b/server/src/com/vaadin/ui/Button.java index 809fbd5e55..46366eda8c 100644 --- a/server/src/com/vaadin/ui/Button.java +++ b/server/src/com/vaadin/ui/Button.java @@ -20,6 +20,7 @@ import java.io.Serializable; import java.lang.reflect.Method; import java.util.Collection; +import org.jsoup.nodes.Attributes; import org.jsoup.nodes.Element; import com.vaadin.event.Action; @@ -38,6 +39,7 @@ import com.vaadin.shared.MouseEventDetails; import com.vaadin.shared.ui.button.ButtonServerRpc; import com.vaadin.shared.ui.button.ButtonState; import com.vaadin.ui.Component.Focusable; +import com.vaadin.ui.declarative.DesignAttributeHandler; import com.vaadin.ui.declarative.DesignContext; import com.vaadin.util.ReflectTools; @@ -675,8 +677,25 @@ public class Button extends AbstractComponent implements public void synchronizeFromDesign(Element design, DesignContext designContext) { super.synchronizeFromDesign(design, designContext); + Button def = designContext.getDefaultInstance(this.getClass()); + Attributes attr = design.attributes(); String content = design.html(); setCaption(content); + // tabindex + setTabIndex(DesignAttributeHandler.readAttribute("tabindex", attr, + def.getTabIndex(), Integer.class)); + // plain-text (default is html) + setHtmlContentAllowed(!DesignAttributeHandler.readAttribute( + "plain-text", attr, false, Boolean.class)); + setIconAlternateText(DesignAttributeHandler.readAttribute("icon-alt", + attr, def.getIconAlternateText(), String.class)); + // click-shortcut + removeClickShortcut(); + ShortcutAction action = DesignAttributeHandler.readAttribute( + "click-shortcut", attr, null, ShortcutAction.class); + if (action != null) { + setClickShortcut(action.getKeyCode(), action.getModifiers()); + } } /* @@ -687,7 +706,12 @@ public class Button extends AbstractComponent implements @Override protected Collection<String> getCustomAttributes() { Collection<String> result = super.getCustomAttributes(); + result.add("tabindex"); + result.add("plain-text"); result.add("caption"); + result.add("icon-alt"); + result.add("click-shortcut"); + result.add("html-content-allowed"); return result; } @@ -701,9 +725,27 @@ public class Button extends AbstractComponent implements @Override public void synchronizeToDesign(Element design, DesignContext designContext) { super.synchronizeToDesign(design, designContext); + Attributes attr = design.attributes(); + Button def = designContext.getDefaultInstance(this.getClass()); String content = getCaption(); if (content != null) { design.html(content); } + // tabindex + DesignAttributeHandler.writeAttribute("tabindex", attr, getTabIndex(), + def.getTabIndex(), Integer.class); + // plain-text (default is html) + if (!isHtmlContentAllowed()) { + design.attr("plain-text", ""); + } + // icon-alt + DesignAttributeHandler.writeAttribute("icon-alt", attr, + getIconAlternateText(), def.getIconAlternateText(), + String.class); + // click-shortcut + if (clickShortcut != null) { + DesignAttributeHandler.writeAttribute("click-shortcut", attr, + clickShortcut, null, ShortcutAction.class); + } } } diff --git a/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java b/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java index 0c370b8da7..f70a79447f 100644 --- a/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java +++ b/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java @@ -40,6 +40,9 @@ import org.jsoup.nodes.Attributes; import org.jsoup.nodes.Element; import org.jsoup.nodes.Node; +import com.vaadin.event.ShortcutAction; +import com.vaadin.event.ShortcutAction.KeyCode; +import com.vaadin.event.ShortcutAction.ModifierKey; import com.vaadin.server.ExternalResource; import com.vaadin.server.FileResource; import com.vaadin.server.FontAwesome; @@ -316,7 +319,6 @@ public class DesignAttributeHandler implements Serializable { * Formats the given design attribute value. The method is provided to * ensure consistent number formatting for design attribute values * - * @since 7.4 * @param number * the number to be formatted * @return the formatted number @@ -326,6 +328,64 @@ public class DesignAttributeHandler implements Serializable { } /** + * Convert ShortcutAction to attribute string presentation + * + * @param shortcut + * the shortcut action + * @return the action as attribute string presentation + */ + private static String formatShortcutAction(ShortcutAction shortcut) { + StringBuilder sb = new StringBuilder(); + // handle modifiers + if (shortcut.getModifiers() != null) { + for (int modifier : shortcut.getModifiers()) { + sb.append(ShortcutKeyMapper.getStringForKeycode(modifier)) + .append("-"); + } + } + // handle keycode + sb.append(ShortcutKeyMapper.getStringForKeycode(shortcut.getKeyCode())); + return sb.toString(); + } + + /** + * Reads shortcut action from attribute presentation + * + * @param attributeValue + * attribute presentation of shortcut action + * @return shortcut action with keycode and modifier keys from attribute + * value + */ + private static ShortcutAction readShortcutAction(String attributeValue) { + if (attributeValue.length() == 0) { + return null; + } + String[] parts = attributeValue.split("-"); + // handle keycode + String keyCodePart = parts[parts.length - 1]; + int keyCode = ShortcutKeyMapper.getKeycodeForString(keyCodePart); + if (keyCode < 0) { + throw new IllegalArgumentException("Invalid shortcut definition " + + attributeValue); + } + // handle modifiers + int[] modifiers = null; + if (parts.length > 1) { + modifiers = new int[parts.length - 1]; + } + for (int i = 0; i < parts.length - 1; i++) { + int modifier = ShortcutKeyMapper.getKeycodeForString(parts[i]); + if (modifier > 0) { + modifiers[i] = modifier; + } else { + throw new IllegalArgumentException( + "Invalid shortcut definition " + attributeValue); + } + } + return new ShortcutAction(null, keyCode, modifiers); + } + + /** * Creates the decimal format used when writing attributes to the design * * @since 7.4 @@ -407,6 +467,9 @@ public class DesignAttributeHandler implements Serializable { return Enum.valueOf((Class<? extends Enum>) targetType, value.toUpperCase()); } + if (targetType == ShortcutAction.class) { + return readShortcutAction(value); + } return null; } @@ -449,9 +512,10 @@ public class DesignAttributeHandler implements Serializable { return formatFloat(((Float) value).floatValue()); } else if (sourceType == Double.class || sourceType == Double.TYPE) { return formatDouble(((Double) value).doubleValue()); + } else if (sourceType == ShortcutAction.class) { + return formatShortcutAction((ShortcutAction) value); } else { return value.toString(); - } } @@ -510,7 +574,8 @@ public class DesignAttributeHandler implements Serializable { private static final List<Class<?>> supportedClasses = Arrays .asList(new Class<?>[] { String.class, Boolean.class, Integer.class, Byte.class, Short.class, Long.class, - Character.class, Float.class, Double.class, Resource.class }); + Character.class, Float.class, Double.class, Resource.class, + ShortcutAction.class }); /** * Returns true if the specified value type is supported by this class. @@ -562,4 +627,104 @@ public class DesignAttributeHandler implements Serializable { } } + /** + * Provides mappings between shortcut keycodes and their representation in + * design attributes + * + * @since 7.4 + * @author Vaadin Ltd + */ + private static class ShortcutKeyMapper implements Serializable { + + private static Map<Integer, String> keyCodeMap = Collections + .synchronizedMap(new HashMap<Integer, String>()); + private static Map<String, Integer> presentationMap = Collections + .synchronizedMap(new HashMap<String, Integer>()); + + static { + // map modifiers + mapKey(ModifierKey.ALT, "alt"); + mapKey(ModifierKey.CTRL, "ctrl"); + mapKey(ModifierKey.META, "meta"); + mapKey(ModifierKey.SHIFT, "shift"); + // map keys + mapKey(KeyCode.ENTER, "enter"); + mapKey(KeyCode.ESCAPE, "escape"); + mapKey(KeyCode.PAGE_UP, "pageup"); + mapKey(KeyCode.PAGE_DOWN, "pagedown"); + mapKey(KeyCode.TAB, "tab"); + mapKey(KeyCode.ARROW_LEFT, "left"); + mapKey(KeyCode.ARROW_UP, "up"); + mapKey(KeyCode.ARROW_RIGHT, "right"); + mapKey(KeyCode.ARROW_DOWN, "down"); + mapKey(KeyCode.BACKSPACE, "backspace"); + mapKey(KeyCode.DELETE, "delete"); + mapKey(KeyCode.INSERT, "insert"); + mapKey(KeyCode.END, "end"); + mapKey(KeyCode.HOME, "home"); + mapKey(KeyCode.F1, "f1"); + mapKey(KeyCode.F2, "f2"); + mapKey(KeyCode.F3, "f3"); + mapKey(KeyCode.F4, "f4"); + mapKey(KeyCode.F5, "f5"); + mapKey(KeyCode.F6, "f6"); + mapKey(KeyCode.F7, "f7"); + mapKey(KeyCode.F8, "f8"); + mapKey(KeyCode.F9, "f9"); + mapKey(KeyCode.F10, "f10"); + mapKey(KeyCode.F11, "f11"); + mapKey(KeyCode.F12, "f12"); + mapKey(KeyCode.NUM0, "0"); + mapKey(KeyCode.NUM1, "1"); + mapKey(KeyCode.NUM2, "2"); + mapKey(KeyCode.NUM3, "3"); + mapKey(KeyCode.NUM4, "4"); + mapKey(KeyCode.NUM5, "5"); + mapKey(KeyCode.NUM6, "6"); + mapKey(KeyCode.NUM7, "7"); + mapKey(KeyCode.NUM8, "8"); + mapKey(KeyCode.NUM9, "9"); + mapKey(KeyCode.SPACEBAR, "spacebar"); + mapKey(KeyCode.A, "a"); + mapKey(KeyCode.B, "b"); + mapKey(KeyCode.C, "c"); + mapKey(KeyCode.D, "d"); + mapKey(KeyCode.E, "e"); + mapKey(KeyCode.F, "f"); + mapKey(KeyCode.G, "g"); + mapKey(KeyCode.H, "h"); + mapKey(KeyCode.I, "i"); + mapKey(KeyCode.J, "j"); + mapKey(KeyCode.K, "k"); + mapKey(KeyCode.L, "l"); + mapKey(KeyCode.M, "m"); + mapKey(KeyCode.N, "n"); + mapKey(KeyCode.O, "o"); + mapKey(KeyCode.P, "p"); + mapKey(KeyCode.Q, "q"); + mapKey(KeyCode.R, "r"); + mapKey(KeyCode.S, "s"); + mapKey(KeyCode.T, "t"); + mapKey(KeyCode.U, "u"); + mapKey(KeyCode.V, "v"); + mapKey(KeyCode.X, "x"); + mapKey(KeyCode.Y, "y"); + mapKey(KeyCode.Z, "z"); + } + + private static void mapKey(int keyCode, String presentation) { + keyCodeMap.put(keyCode, presentation); + presentationMap.put(presentation, keyCode); + } + + private static int getKeycodeForString(String attributePresentation) { + Integer code = presentationMap.get(attributePresentation); + return code != null ? code.intValue() : -1; + } + + private static String getStringForKeycode(int keyCode) { + return keyCodeMap.get(keyCode); + } + } + } |