From edabf251a53762d5bf45a70be757e5b0bd48d25a Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Wed, 4 Feb 2015 15:52:42 +0200 Subject: Simplified declarative converters * Removed shortcut interface * Moved TimeZone converter to a separate class * Moved Enum converter to a separate class Change-Id: I2b959e3a002e6319912212df4a62a3bd05677f80 --- .../com/vaadin/ui/declarative/DesignFormatter.java | 34 +----- .../converters/DesignEnumConverter.java | 63 +++++++++++ .../converters/DesignShortcutActionConverter.java | 124 ++++++++++++++++----- .../converters/DesignTimeZoneConverter.java | 65 +++++++++++ .../abstracttextfield/WriteDesignTest.java | 2 +- 5 files changed, 234 insertions(+), 54 deletions(-) create mode 100644 server/src/com/vaadin/ui/declarative/converters/DesignEnumConverter.java create mode 100644 server/src/com/vaadin/ui/declarative/converters/DesignTimeZoneConverter.java diff --git a/server/src/com/vaadin/ui/declarative/DesignFormatter.java b/server/src/com/vaadin/ui/declarative/DesignFormatter.java index 103142a11b..25cf05cc9b 100644 --- a/server/src/com/vaadin/ui/declarative/DesignFormatter.java +++ b/server/src/com/vaadin/ui/declarative/DesignFormatter.java @@ -31,11 +31,12 @@ import com.vaadin.data.util.converter.Converter; import com.vaadin.event.ShortcutAction; import com.vaadin.server.Resource; import com.vaadin.ui.declarative.converters.DesignDateConverter; +import com.vaadin.ui.declarative.converters.DesignEnumConverter; import com.vaadin.ui.declarative.converters.DesignFormatConverter; import com.vaadin.ui.declarative.converters.DesignResourceConverter; import com.vaadin.ui.declarative.converters.DesignShortcutActionConverter; +import com.vaadin.ui.declarative.converters.DesignTimeZoneConverter; import com.vaadin.ui.declarative.converters.DesignToStringConverter; -import com.vaadin.ui.declarative.converters.ShortcutKeyMapper; /** * Class focused on flexible and consistent formatting and parsing of different @@ -48,6 +49,7 @@ import com.vaadin.ui.declarative.converters.ShortcutKeyMapper; public class DesignFormatter implements Serializable { private final Map, Converter> converterMap = new ConcurrentHashMap, Converter>(); + private final Converter stringEnumConverter = new DesignEnumConverter(); /** * Creates the formatter with default types already mapped. @@ -162,25 +164,11 @@ public class DesignFormatter implements Serializable { converterMap.put(Character.class, charConverter); converterMap.put(Character.TYPE, charConverter); - // date conversion has its own class converterMap.put(Date.class, new DesignDateConverter()); - - // as shortcut action and resource converterMap.put(ShortcutAction.class, - new DesignShortcutActionConverter(ShortcutKeyMapper.DEFAULT)); - + new DesignShortcutActionConverter()); converterMap.put(Resource.class, new DesignResourceConverter()); - - // timezones use different static method and do not use toString() - converterMap.put(TimeZone.class, new DesignToStringConverter( - TimeZone.class, "getTimeZone") { - @Override - public String convertToPresentation(TimeZone value, - Class targetType, Locale locale) - throws Converter.ConversionException { - return value.getID(); - } - }); + converterMap.put(TimeZone.class, new DesignTimeZoneConverter()); } /** @@ -307,17 +295,7 @@ public class DesignFormatter implements Serializable { protected Converter findConverterFor( Class sourceType, boolean strict) { if (sourceType.isEnum()) { - // enums can be read in lowercase - return new DesignToStringConverter(sourceType) { - - @Override - public T convertToModel(String value, - Class targetType, Locale locale) - throws Converter.ConversionException { - return super.convertToModel(value.toUpperCase(), - targetType, locale); - } - }; + return (Converter) stringEnumConverter; } else if (converterMap.containsKey(sourceType)) { return ((Converter) converterMap.get(sourceType)); } else if (!strict) { diff --git a/server/src/com/vaadin/ui/declarative/converters/DesignEnumConverter.java b/server/src/com/vaadin/ui/declarative/converters/DesignEnumConverter.java new file mode 100644 index 0000000000..9f07ff9560 --- /dev/null +++ b/server/src/com/vaadin/ui/declarative/converters/DesignEnumConverter.java @@ -0,0 +1,63 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.ui.declarative.converters; + +import java.util.Locale; + +import com.vaadin.data.util.converter.Converter; +import com.vaadin.ui.declarative.DesignAttributeHandler; + +/** + * An converter for Enum to/from String for {@link DesignAttributeHandler} to + * use internally. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class DesignEnumConverter implements Converter { + + @Override + public Enum convertToModel(String value, Class targetType, + Locale locale) + throws com.vaadin.data.util.converter.Converter.ConversionException { + if (value == null || value.trim().equals("")) { + return null; + } + return Enum.valueOf(targetType, value.toUpperCase(Locale.ENGLISH)); + } + + @Override + public String convertToPresentation(Enum value, + Class targetType, Locale locale) + throws com.vaadin.data.util.converter.Converter.ConversionException { + if (value == null) { + return null; + } + + return value.name().toLowerCase(Locale.ENGLISH); + } + + @Override + public Class getModelType() { + return Enum.class; + } + + @Override + public Class getPresentationType() { + return String.class; + } + +} diff --git a/server/src/com/vaadin/ui/declarative/converters/DesignShortcutActionConverter.java b/server/src/com/vaadin/ui/declarative/converters/DesignShortcutActionConverter.java index d9d84a1263..e2b6ed8e14 100644 --- a/server/src/com/vaadin/ui/declarative/converters/DesignShortcutActionConverter.java +++ b/server/src/com/vaadin/ui/declarative/converters/DesignShortcutActionConverter.java @@ -15,10 +15,16 @@ */ package com.vaadin.ui.declarative.converters; +import java.util.Collections; +import java.util.HashMap; import java.util.Locale; +import java.util.Map; +import java.util.Map.Entry; import com.vaadin.data.util.converter.Converter; import com.vaadin.event.ShortcutAction; +import com.vaadin.event.ShortcutAction.KeyCode; +import com.vaadin.event.ShortcutAction.ModifierKey; /** * Converter for {@link ShortcutActions}. @@ -29,19 +35,88 @@ import com.vaadin.event.ShortcutAction; public class DesignShortcutActionConverter implements Converter { - /** - * Default instance of the shortcut key mapper. - */ - private final ShortcutKeyMapper keyMapper; - - /** - * Constructs the converter with given key mapper. - * - * @param mapper - * Key mapper to use. - */ - public DesignShortcutActionConverter(ShortcutKeyMapper mapper) { - keyMapper = mapper; + private final Map keyCodeMap; + private final Map presentationMap; + + public DesignShortcutActionConverter() { + HashMap codes = new HashMap(); + // map modifiers + codes.put(ModifierKey.ALT, "alt"); + codes.put(ModifierKey.CTRL, "ctrl"); + codes.put(ModifierKey.META, "meta"); + codes.put(ModifierKey.SHIFT, "shift"); + // map keys + codes.put(KeyCode.ENTER, "enter"); + codes.put(KeyCode.ESCAPE, "escape"); + codes.put(KeyCode.PAGE_UP, "pageup"); + codes.put(KeyCode.PAGE_DOWN, "pagedown"); + codes.put(KeyCode.TAB, "tab"); + codes.put(KeyCode.ARROW_LEFT, "left"); + codes.put(KeyCode.ARROW_UP, "up"); + codes.put(KeyCode.ARROW_RIGHT, "right"); + codes.put(KeyCode.ARROW_DOWN, "down"); + codes.put(KeyCode.BACKSPACE, "backspace"); + codes.put(KeyCode.DELETE, "delete"); + codes.put(KeyCode.INSERT, "insert"); + codes.put(KeyCode.END, "end"); + codes.put(KeyCode.HOME, "home"); + codes.put(KeyCode.F1, "f1"); + codes.put(KeyCode.F2, "f2"); + codes.put(KeyCode.F3, "f3"); + codes.put(KeyCode.F4, "f4"); + codes.put(KeyCode.F5, "f5"); + codes.put(KeyCode.F6, "f6"); + codes.put(KeyCode.F7, "f7"); + codes.put(KeyCode.F8, "f8"); + codes.put(KeyCode.F9, "f9"); + codes.put(KeyCode.F10, "f10"); + codes.put(KeyCode.F11, "f11"); + codes.put(KeyCode.F12, "f12"); + codes.put(KeyCode.NUM0, "0"); + codes.put(KeyCode.NUM1, "1"); + codes.put(KeyCode.NUM2, "2"); + codes.put(KeyCode.NUM3, "3"); + codes.put(KeyCode.NUM4, "4"); + codes.put(KeyCode.NUM5, "5"); + codes.put(KeyCode.NUM6, "6"); + codes.put(KeyCode.NUM7, "7"); + codes.put(KeyCode.NUM8, "8"); + codes.put(KeyCode.NUM9, "9"); + codes.put(KeyCode.SPACEBAR, "spacebar"); + codes.put(KeyCode.A, "a"); + codes.put(KeyCode.B, "b"); + codes.put(KeyCode.C, "c"); + codes.put(KeyCode.D, "d"); + codes.put(KeyCode.E, "e"); + codes.put(KeyCode.F, "f"); + codes.put(KeyCode.G, "g"); + codes.put(KeyCode.H, "h"); + codes.put(KeyCode.I, "i"); + codes.put(KeyCode.J, "j"); + codes.put(KeyCode.K, "k"); + codes.put(KeyCode.L, "l"); + codes.put(KeyCode.M, "m"); + codes.put(KeyCode.N, "n"); + codes.put(KeyCode.O, "o"); + codes.put(KeyCode.P, "p"); + codes.put(KeyCode.Q, "q"); + codes.put(KeyCode.R, "r"); + codes.put(KeyCode.S, "s"); + codes.put(KeyCode.T, "t"); + codes.put(KeyCode.U, "u"); + codes.put(KeyCode.V, "v"); + codes.put(KeyCode.X, "x"); + codes.put(KeyCode.Y, "y"); + codes.put(KeyCode.Z, "z"); + + keyCodeMap = Collections.unmodifiableMap(codes); + + HashMap presentations = new HashMap(); + for (Entry entry : keyCodeMap.entrySet()) { + presentations.put(entry.getValue(), entry.getKey()); + } + + presentationMap = Collections.unmodifiableMap(presentations); } @Override @@ -56,7 +131,7 @@ public class DesignShortcutActionConverter implements String[] parts = data[0].split("-"); // handle keycode String keyCodePart = parts[parts.length - 1]; - int keyCode = getKeyMapper().getKeycodeForString(keyCodePart); + int keyCode = getKeycodeForString(keyCodePart); if (keyCode < 0) { throw new IllegalArgumentException("Invalid shortcut definition " + value); @@ -67,7 +142,7 @@ public class DesignShortcutActionConverter implements modifiers = new int[parts.length - 1]; } for (int i = 0; i < parts.length - 1; i++) { - int modifier = getKeyMapper().getKeycodeForString(parts[i]); + int modifier = getKeycodeForString(parts[i]); if (modifier > 0) { modifiers[i] = modifier; } else { @@ -87,12 +162,11 @@ public class DesignShortcutActionConverter implements // handle modifiers if (value.getModifiers() != null) { for (int modifier : value.getModifiers()) { - sb.append(getKeyMapper().getStringForKeycode(modifier)).append( - "-"); + sb.append(getStringForKeycode(modifier)).append("-"); } } // handle keycode - sb.append(getKeyMapper().getStringForKeycode(value.getKeyCode())); + sb.append(getStringForKeycode(value.getKeyCode())); if (value.getCaption() != null) { sb.append(" ").append(value.getCaption()); } @@ -109,13 +183,13 @@ public class DesignShortcutActionConverter implements return String.class; } - /** - * Returns the currently used key mapper. - * - * @return Key mapper. - */ - public ShortcutKeyMapper getKeyMapper() { - return keyMapper; + public int getKeycodeForString(String attributePresentation) { + Integer code = presentationMap.get(attributePresentation); + return code != null ? code.intValue() : -1; + } + + public String getStringForKeycode(int keyCode) { + return keyCodeMap.get(keyCode); } } diff --git a/server/src/com/vaadin/ui/declarative/converters/DesignTimeZoneConverter.java b/server/src/com/vaadin/ui/declarative/converters/DesignTimeZoneConverter.java new file mode 100644 index 0000000000..51f47edac9 --- /dev/null +++ b/server/src/com/vaadin/ui/declarative/converters/DesignTimeZoneConverter.java @@ -0,0 +1,65 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.ui.declarative.converters; + +import java.util.Locale; +import java.util.TimeZone; + +import com.vaadin.data.util.converter.Converter; +import com.vaadin.ui.declarative.DesignAttributeHandler; + +/** + * Utility class for {@link DesignAttributeHandler} that deals with converting + * various TimeZones to string. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class DesignTimeZoneConverter implements Converter { + + @Override + public TimeZone convertToModel(String value, + Class targetTimeZone, Locale locale) + throws Converter.ConversionException { + if (value == null || value.isEmpty()) { + return null; + } + + return TimeZone.getTimeZone(value); + } + + @Override + public String convertToPresentation(TimeZone value, + Class targetTimeZone, Locale locale) + throws Converter.ConversionException { + if (value == null) { + return ""; + } else { + return value.getID(); + } + } + + @Override + public Class getModelType() { + return TimeZone.class; + } + + @Override + public Class getPresentationType() { + return String.class; + } + +} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/WriteDesignTest.java b/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/WriteDesignTest.java index 9b1129ce14..840de9e819 100644 --- a/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/WriteDesignTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/WriteDesignTest.java @@ -57,7 +57,7 @@ public class WriteDesignTest extends TestCase { assertEquals("5", design.attr("maxlength")); assertEquals("3", design.attr("columns")); assertEquals("input", design.attr("input-prompt")); - assertEquals("EAGER", design.attr("text-change-event-mode")); + assertEquals("eager", design.attr("text-change-event-mode")); assertEquals("100", design.attr("text-change-timeout")); } -- cgit v1.2.3