]> source.dussan.org Git - vaadin-framework.git/commitdiff
Minitutorial applications
authorArtur Signell <artur@vaadin.com>
Thu, 22 Dec 2011 21:52:15 +0000 (23:52 +0200)
committerArtur Signell <artur@vaadin.com>
Thu, 22 Dec 2011 21:52:15 +0000 (23:52 +0200)
tests/testbench/com/vaadin/tests/minitutorials/CustomConverterFactoryRoot.java [new file with mode: 0644]
tests/testbench/com/vaadin/tests/minitutorials/DynamicImageRoot.java [new file with mode: 0644]
tests/testbench/com/vaadin/tests/minitutorials/MyConverterFactory.java [new file with mode: 0644]
tests/testbench/com/vaadin/tests/minitutorials/MyStringToDoubleConverter.java [new file with mode: 0644]

diff --git a/tests/testbench/com/vaadin/tests/minitutorials/CustomConverterFactoryRoot.java b/tests/testbench/com/vaadin/tests/minitutorials/CustomConverterFactoryRoot.java
new file mode 100644 (file)
index 0000000..1e6e10c
--- /dev/null
@@ -0,0 +1,33 @@
+package com.vaadin.tests.minitutorials;\r
+\r
+import com.vaadin.terminal.WrappedRequest;\r
+import com.vaadin.tests.components.AbstractTestRoot;\r
+import com.vaadin.ui.TextField;\r
+\r
+public class CustomConverterFactoryRoot extends AbstractTestRoot {\r
+    @Override\r
+    public void setup(WrappedRequest request) {\r
+        getApplication().setConverterFactory(new MyConverterFactory());\r
+\r
+        TextField tf = new TextField("This is my double field");\r
+        tf.setImmediate(true);\r
+        tf.setConverter(Double.class);\r
+        addComponent(tf);\r
+\r
+        // As we do not set the locale explicitly for the field we set the value\r
+        // after the field has been attached so it uses the application locale\r
+        // for conversion\r
+        tf.setConvertedValue(50.1);\r
+\r
+    }\r
+\r
+    @Override\r
+    protected String getTestDescription() {\r
+        return "Mini tutorial for https://vaadin.com/wiki/-/wiki/Main/Changing%20the%20default%20converters%20for%20an%20application";\r
+    }\r
+\r
+    @Override\r
+    protected Integer getTicketNumber() {\r
+        return null;\r
+    }\r
+}\r
diff --git a/tests/testbench/com/vaadin/tests/minitutorials/DynamicImageRoot.java b/tests/testbench/com/vaadin/tests/minitutorials/DynamicImageRoot.java
new file mode 100644 (file)
index 0000000..a0e6a54
--- /dev/null
@@ -0,0 +1,79 @@
+package com.vaadin.tests.minitutorials;
+
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.imageio.ImageIO;
+
+import com.vaadin.Application;
+import com.vaadin.terminal.ExternalResource;
+import com.vaadin.terminal.RequestHandler;
+import com.vaadin.terminal.WrappedRequest;
+import com.vaadin.terminal.WrappedResponse;
+import com.vaadin.tests.components.AbstractTestRoot;
+import com.vaadin.ui.Embedded;
+
+public class DynamicImageRoot extends AbstractTestRoot {
+
+    @Override
+    public void setup(WrappedRequest request) {
+        // Add the request handler that handles our dynamic image
+        getApplication().addRequestHandler(new DynamicImageRequestHandler());
+
+        // Create a URL that we can handle in DynamicImageRequestHandler
+        URL imageUrl;
+        try {
+            imageUrl = new URL(getApplication().getURL(),
+                    DynamicImageRequestHandler.IMAGE_URL + "?text=Hello!");
+        } catch (MalformedURLException e) {
+            // This should never happen
+            throw new RuntimeException(e);
+        }
+
+        // Add an embedded using the created URL
+        Embedded embedded = new Embedded("A dynamically generated image",
+                new ExternalResource(imageUrl));
+        embedded.setType(Embedded.TYPE_IMAGE);
+        getContent().addComponent(embedded);
+
+    }
+
+    @Override
+    protected String getTestDescription() {
+        return "Mini tutorial for https://vaadin.com/wiki/-/wiki/Main/Generating%20dynamic%20resources%20based%20on%20URI%20or%20parameters";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return null;
+    }
+}
+
+class DynamicImageRequestHandler implements RequestHandler {
+
+    public static final String IMAGE_URL = "myimage.png";
+
+    public boolean handleRequest(Application application,
+            WrappedRequest request, WrappedResponse response)
+            throws IOException {
+        String pathInfo = request.getRequestPathInfo();
+        if (("/" + IMAGE_URL).equals(pathInfo)) {
+            // Create an image, draw the "text" parameter to it and output it to
+            // the browser.
+            String text = request.getParameter("text");
+            BufferedImage bi = new BufferedImage(100, 30,
+                    BufferedImage.TYPE_3BYTE_BGR);
+            bi.getGraphics().drawChars(text.toCharArray(), 0, text.length(),
+                    10, 20);
+            response.setContentType("image/png");
+            ImageIO.write(bi, "png", response.getOutputStream());
+
+            return true;
+        }
+        // If the URL did not match our image URL, let the other request
+        // handlers handle it
+        return false;
+    }
+}
diff --git a/tests/testbench/com/vaadin/tests/minitutorials/MyConverterFactory.java b/tests/testbench/com/vaadin/tests/minitutorials/MyConverterFactory.java
new file mode 100644 (file)
index 0000000..0c346bf
--- /dev/null
@@ -0,0 +1,17 @@
+package com.vaadin.tests.minitutorials;\r
+\r
+import com.vaadin.data.util.converter.Converter;\r
+import com.vaadin.data.util.converter.DefaultConverterFactory;\r
+\r
+public class MyConverterFactory extends DefaultConverterFactory {\r
+    @Override\r
+    protected <PRESENTATION, MODEL> Converter<PRESENTATION, MODEL> findConverter(\r
+            Class<PRESENTATION> presentationType, Class<MODEL> modelType) {\r
+        // Handle String <-> Double\r
+        if (presentationType == String.class && modelType == Double.class) {\r
+            return (Converter<PRESENTATION, MODEL>) new MyStringToDoubleConverter();\r
+        }\r
+        // Let default factory handle the rest\r
+        return super.findConverter(presentationType, modelType);\r
+    }\r
+}\r
diff --git a/tests/testbench/com/vaadin/tests/minitutorials/MyStringToDoubleConverter.java b/tests/testbench/com/vaadin/tests/minitutorials/MyStringToDoubleConverter.java
new file mode 100644 (file)
index 0000000..77fddc7
--- /dev/null
@@ -0,0 +1,18 @@
+package com.vaadin.tests.minitutorials;\r
+\r
+import java.text.NumberFormat;\r
+import java.util.Locale;\r
+\r
+import com.vaadin.data.util.converter.StringToDoubleConverter;\r
+\r
+public class MyStringToDoubleConverter extends StringToDoubleConverter {\r
+\r
+    @Override\r
+    protected NumberFormat getFormat(Locale locale) {\r
+        NumberFormat format = super.getFormat(locale);\r
+        format.setGroupingUsed(false);\r
+        format.setMaximumFractionDigits(3);\r
+        format.setMinimumFractionDigits(3);\r
+        return format;\r
+    }\r
+}\r