]> source.dussan.org Git - vaadin-framework.git/commitdiff
Declarative support for ProgressBar (#16316)
authorArtur Signell <artur@vaadin.com>
Wed, 4 Feb 2015 22:23:25 +0000 (00:23 +0200)
committerArtur Signell <artur@vaadin.com>
Thu, 5 Feb 2015 14:45:56 +0000 (16:45 +0200)
Change-Id: I8c13b19218e0da936abdb0860d492a3d5a096aa7

server/src/com/vaadin/ui/ProgressBar.java
server/src/com/vaadin/ui/declarative/DesignFormatter.java
server/src/com/vaadin/ui/declarative/converters/DesignFormatConverter.java [deleted file]
server/tests/src/com/vaadin/tests/server/component/progressbar/ProgressBarDeclarativeTest.java [new file with mode: 0644]

index bf52cefefe17f5bd0096fd78aa5982ef70e5bf77..c288695ae121344885dc75fa315d30fec3757291 100644 (file)
 
 package com.vaadin.ui;
 
+import org.jsoup.nodes.Element;
+
 import com.vaadin.data.Property;
 import com.vaadin.shared.ui.progressindicator.ProgressBarState;
+import com.vaadin.ui.declarative.DesignAttributeHandler;
+import com.vaadin.ui.declarative.DesignContext;
 
 /**
  * Shows the current progress of a long running task.
@@ -149,4 +153,21 @@ public class ProgressBar extends AbstractField<Float> implements
         getState().state = newValue;
     }
 
+    @Override
+    public void readDesign(Element design, DesignContext designContext) {
+        super.readDesign(design, designContext);
+        if (design.hasAttr("value") && !design.attr("value").isEmpty()) {
+            setValue(DesignAttributeHandler.readAttribute("value",
+                    design.attributes(), Float.class));
+        }
+    }
+
+    @Override
+    public void writeDesign(Element design, DesignContext designContext) {
+        super.writeDesign(design, designContext);
+        Float defaultValue = ((ProgressBar) designContext
+                .getDefaultInstance(this)).getValue();
+        DesignAttributeHandler.writeAttribute("value", design.attributes(),
+                getValue(), defaultValue, Float.class);
+    }
 }
index 985b9235f3d844a028e277ae11fc9acf8b8c6ae1..d2fbf2c765626efbdda7e51e8a025616e5fb3e58 100644 (file)
@@ -28,11 +28,12 @@ import java.util.TimeZone;
 import java.util.concurrent.ConcurrentHashMap;
 
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.StringToDoubleConverter;
+import com.vaadin.data.util.converter.StringToFloatConverter;
 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.DesignObjectConverter;
 import com.vaadin.ui.declarative.converters.DesignResourceConverter;
 import com.vaadin.ui.declarative.converters.DesignShortcutActionConverter;
@@ -111,16 +112,20 @@ public class DesignFormatter implements Serializable {
         // floats and doubles use formatters
         DecimalFormatSymbols symbols = new DecimalFormatSymbols(new Locale(
                 "en_US"));
-        DecimalFormat fmt = new DecimalFormat("0.###", symbols);
+        final DecimalFormat fmt = new DecimalFormat("0.###", symbols);
         fmt.setGroupingUsed(false);
-        converterMap.put(Float.class, new DesignFormatConverter<Float>(
-                Float.class, fmt));
-        converterMap.put(Float.TYPE, new DesignFormatConverter<Float>(
-                Float.class, fmt));
-        converterMap.put(Double.class, new DesignFormatConverter<Double>(
-                Double.class, fmt));
-        converterMap.put(Double.TYPE, new DesignFormatConverter<Double>(
-                Double.class, fmt));
+        converterMap.put(Float.class, new StringToFloatConverter() {
+            @Override
+            protected java.text.NumberFormat getFormat(Locale locale) {
+                return fmt;
+            };
+        });
+        converterMap.put(Double.class, new StringToDoubleConverter() {
+            @Override
+            protected java.text.NumberFormat getFormat(Locale locale) {
+                return fmt;
+            };
+        });
 
         // strings do nothing
         converterMap.put(String.class, new Converter<String, String>() {
diff --git a/server/src/com/vaadin/ui/declarative/converters/DesignFormatConverter.java b/server/src/com/vaadin/ui/declarative/converters/DesignFormatConverter.java
deleted file mode 100644 (file)
index e9b26fc..0000000
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * 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.text.Format;
-import java.text.ParseException;
-import java.util.Locale;
-
-import com.vaadin.data.util.converter.Converter;
-
-/**
- * Converter based on Java Formats rather than static methods.
- * 
- * @since 7.4
- * @author Vaadin Ltd
- * @param <TYPE>
- *            Type of the object to format.
- */
-public class DesignFormatConverter<TYPE> implements Converter<String, TYPE> {
-
-    private final Format format;
-    private final Class<? extends TYPE> type;
-
-    /**
-     * Constructs an instance of the converter.
-     */
-    public DesignFormatConverter(Class<? extends TYPE> type, Format format) {
-        this.type = type;
-        this.format = format;
-    }
-
-    @Override
-    public TYPE convertToModel(String value, Class<? extends TYPE> targetType,
-            Locale locale) throws Converter.ConversionException {
-        try {
-            return targetType.cast(this.format.parseObject(value));
-        } catch (ParseException e) {
-            throw new Converter.ConversionException(e);
-        }
-    }
-
-    @Override
-    public String convertToPresentation(TYPE value,
-            Class<? extends String> targetType, Locale locale)
-            throws Converter.ConversionException {
-        return this.format.format(value);
-    }
-
-    @Override
-    public Class<TYPE> getModelType() {
-        return (Class<TYPE>) this.type;
-    }
-
-    @Override
-    public Class<String> getPresentationType() {
-        return String.class;
-    }
-
-}
diff --git a/server/tests/src/com/vaadin/tests/server/component/progressbar/ProgressBarDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/progressbar/ProgressBarDeclarativeTest.java
new file mode 100644 (file)
index 0000000..c98883a
--- /dev/null
@@ -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.tests.server.component.progressbar;
+
+import org.junit.Test;
+
+import com.vaadin.tests.design.DeclarativeTestBase;
+import com.vaadin.ui.ProgressBar;
+
+/**
+ * Test cases for reading the properties of selection components.
+ * 
+ * @author Vaadin Ltd
+ */
+public class ProgressBarDeclarativeTest extends
+        DeclarativeTestBase<ProgressBar> {
+
+    public String getBasicDesign() {
+        return "<v-progress-bar value=0.5 indeterminate='true'>";
+
+    }
+
+    public ProgressBar getBasicExpected() {
+        ProgressBar ns = new ProgressBar();
+        ns.setIndeterminate(true);
+        ns.setValue(0.5f);
+        return ns;
+    }
+
+    @Test
+    public void testReadBasic() {
+        testRead(getBasicDesign(), getBasicExpected());
+    }
+
+    @Test
+    public void testWriteBasic() {
+        testWrite(stripOptionTags(getBasicDesign()), getBasicExpected());
+    }
+
+    @Test
+    public void testReadEmpty() {
+        testRead("<v-progress-bar>", new ProgressBar());
+    }
+
+    @Test
+    public void testWriteEmpty() {
+        testWrite("<v-progress-bar>", new ProgressBar());
+    }
+
+}
\ No newline at end of file