summaryrefslogtreecommitdiffstats
path: root/server/src
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2015-02-05 01:35:09 +0200
committerVaadin Code Review <review@vaadin.com>2015-02-05 13:28:06 +0000
commit620658687ec206bd3a631132e512b3e307890374 (patch)
tree393795d1be016129efcc223c6d1a79f218b7e8b8 /server/src
parent3c0a9961b0965b8cd0d88af8f3f4971c09ab271a (diff)
downloadvaadin-framework-620658687ec206bd3a631132e512b3e307890374.tar.gz
vaadin-framework-620658687ec206bd3a631132e512b3e307890374.zip
Declarative support for AbstractSelect (#15545)
Change-Id: Ie66ee3f2b02ce7b6aa2edb66176bfbf5bdcd6c33
Diffstat (limited to 'server/src')
-rw-r--r--server/src/com/vaadin/ui/AbstractField.java1
-rw-r--r--server/src/com/vaadin/ui/AbstractSelect.java43
-rw-r--r--server/src/com/vaadin/ui/declarative/DesignFormatter.java8
-rw-r--r--server/src/com/vaadin/ui/declarative/converters/DesignObjectConverter.java60
4 files changed, 111 insertions, 1 deletions
diff --git a/server/src/com/vaadin/ui/AbstractField.java b/server/src/com/vaadin/ui/AbstractField.java
index 9b9c7efd86..5c02c9e5fb 100644
--- a/server/src/com/vaadin/ui/AbstractField.java
+++ b/server/src/com/vaadin/ui/AbstractField.java
@@ -1772,6 +1772,7 @@ public abstract class AbstractField<T> extends AbstractComponent implements
attributes.add("readonly");
// must be handled by subclasses
attributes.add("value");
+ attributes.add("converted-value");
return attributes;
}
diff --git a/server/src/com/vaadin/ui/AbstractSelect.java b/server/src/com/vaadin/ui/AbstractSelect.java
index 423ebcb46a..06790ca78d 100644
--- a/server/src/com/vaadin/ui/AbstractSelect.java
+++ b/server/src/com/vaadin/ui/AbstractSelect.java
@@ -29,6 +29,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
+import org.jsoup.nodes.Element;
+
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
@@ -49,6 +51,9 @@ import com.vaadin.server.PaintTarget;
import com.vaadin.server.Resource;
import com.vaadin.shared.ui.combobox.FilteringMode;
import com.vaadin.shared.ui.dd.VerticalDropLocation;
+import com.vaadin.ui.declarative.DesignAttributeHandler;
+import com.vaadin.ui.declarative.DesignContext;
+import com.vaadin.ui.declarative.DesignException;
/**
* <p>
@@ -2181,4 +2186,40 @@ public abstract class AbstractSelect extends AbstractField<Object> implements
public String generateDescription(Component source, Object itemId,
Object propertyId);
}
-}
+
+ @Override
+ public void readDesign(Element design, DesignContext designContext) {
+ // handle default attributes
+ super.readDesign(design, designContext);
+ // 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);
+ }
+ }
+ if (!selected.isEmpty()) {
+ if (isMultiSelect()) {
+ setValue(selected);
+ } else if (selected.size() == 1) {
+ setValue(selected.iterator().next());
+ } else {
+ throw new DesignException(
+ "Multiple values selected for a single select component");
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/server/src/com/vaadin/ui/declarative/DesignFormatter.java b/server/src/com/vaadin/ui/declarative/DesignFormatter.java
index 25cf05cc9b..985b9235f3 100644
--- a/server/src/com/vaadin/ui/declarative/DesignFormatter.java
+++ b/server/src/com/vaadin/ui/declarative/DesignFormatter.java
@@ -33,6 +33,7 @@ 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.DesignObjectConverter;
import com.vaadin.ui.declarative.converters.DesignResourceConverter;
import com.vaadin.ui.declarative.converters.DesignShortcutActionConverter;
import com.vaadin.ui.declarative.converters.DesignTimeZoneConverter;
@@ -50,6 +51,7 @@ public class DesignFormatter implements Serializable {
private final Map<Class<?>, Converter<String, ?>> converterMap = new ConcurrentHashMap<Class<?>, Converter<String, ?>>();
private final Converter<String, Enum> stringEnumConverter = new DesignEnumConverter();
+ private final Converter<String, Object> stringObjectConverter = new DesignObjectConverter();
/**
* Creates the formatter with default types already mapped.
@@ -294,6 +296,12 @@ public class DesignFormatter implements Serializable {
@SuppressWarnings("unchecked")
protected <T> Converter<String, T> findConverterFor(
Class<? extends T> sourceType, boolean strict) {
+ if (sourceType == Object.class) {
+ // Use for propertyIds, itemIds and such. Only string type objects
+ // are really supported if no special logic is implemented in the
+ // component.
+ return (Converter<String, T>) stringObjectConverter;
+ }
if (sourceType.isEnum()) {
return (Converter<String, T>) stringEnumConverter;
} else if (converterMap.containsKey(sourceType)) {
diff --git a/server/src/com/vaadin/ui/declarative/converters/DesignObjectConverter.java b/server/src/com/vaadin/ui/declarative/converters/DesignObjectConverter.java
new file mode 100644
index 0000000000..f11585d6b8
--- /dev/null
+++ b/server/src/com/vaadin/ui/declarative/converters/DesignObjectConverter.java
@@ -0,0 +1,60 @@
+/*
+ * 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 Object to/from String for {@link DesignAttributeHandler} to
+ * use internally.
+ *
+ * @since 7.4
+ * @author Vaadin Ltd
+ */
+public class DesignObjectConverter implements Converter<String, Object> {
+
+ @Override
+ public Object convertToModel(String value,
+ Class<? extends Object> targetType, Locale locale)
+ throws com.vaadin.data.util.converter.Converter.ConversionException {
+ return value;
+ }
+
+ @Override
+ public String convertToPresentation(Object value,
+ Class<? extends String> targetType, Locale locale)
+ throws com.vaadin.data.util.converter.Converter.ConversionException {
+ if (value == null) {
+ return null;
+ }
+
+ return value.toString();
+ }
+
+ @Override
+ public Class<Object> getModelType() {
+ return Object.class;
+ }
+
+ @Override
+ public Class<String> getPresentationType() {
+ return String.class;
+ }
+
+}