From: Artur Signell Date: Thu, 22 Dec 2011 21:52:15 +0000 (+0200) Subject: Minitutorial applications X-Git-Tag: 7.0.0.alpha1^2~4 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0b1a5772265fa510bcda25b6193a0938f6ebcd97;p=vaadin-framework.git Minitutorial applications --- diff --git a/tests/testbench/com/vaadin/tests/minitutorials/CustomConverterFactoryRoot.java b/tests/testbench/com/vaadin/tests/minitutorials/CustomConverterFactoryRoot.java new file mode 100644 index 0000000000..1e6e10c8d4 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/CustomConverterFactoryRoot.java @@ -0,0 +1,33 @@ +package com.vaadin.tests.minitutorials; + +import com.vaadin.terminal.WrappedRequest; +import com.vaadin.tests.components.AbstractTestRoot; +import com.vaadin.ui.TextField; + +public class CustomConverterFactoryRoot extends AbstractTestRoot { + @Override + public void setup(WrappedRequest request) { + getApplication().setConverterFactory(new MyConverterFactory()); + + TextField tf = new TextField("This is my double field"); + tf.setImmediate(true); + tf.setConverter(Double.class); + addComponent(tf); + + // As we do not set the locale explicitly for the field we set the value + // after the field has been attached so it uses the application locale + // for conversion + tf.setConvertedValue(50.1); + + } + + @Override + protected String getTestDescription() { + return "Mini tutorial for https://vaadin.com/wiki/-/wiki/Main/Changing%20the%20default%20converters%20for%20an%20application"; + } + + @Override + protected Integer getTicketNumber() { + return null; + } +} diff --git a/tests/testbench/com/vaadin/tests/minitutorials/DynamicImageRoot.java b/tests/testbench/com/vaadin/tests/minitutorials/DynamicImageRoot.java new file mode 100644 index 0000000000..a0e6a54c41 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/DynamicImageRoot.java @@ -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 index 0000000000..0c346bfb50 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/MyConverterFactory.java @@ -0,0 +1,17 @@ +package com.vaadin.tests.minitutorials; + +import com.vaadin.data.util.converter.Converter; +import com.vaadin.data.util.converter.DefaultConverterFactory; + +public class MyConverterFactory extends DefaultConverterFactory { + @Override + protected Converter findConverter( + Class presentationType, Class modelType) { + // Handle String <-> Double + if (presentationType == String.class && modelType == Double.class) { + return (Converter) new MyStringToDoubleConverter(); + } + // Let default factory handle the rest + return super.findConverter(presentationType, modelType); + } +} diff --git a/tests/testbench/com/vaadin/tests/minitutorials/MyStringToDoubleConverter.java b/tests/testbench/com/vaadin/tests/minitutorials/MyStringToDoubleConverter.java new file mode 100644 index 0000000000..77fddc7684 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/MyStringToDoubleConverter.java @@ -0,0 +1,18 @@ +package com.vaadin.tests.minitutorials; + +import java.text.NumberFormat; +import java.util.Locale; + +import com.vaadin.data.util.converter.StringToDoubleConverter; + +public class MyStringToDoubleConverter extends StringToDoubleConverter { + + @Override + protected NumberFormat getFormat(Locale locale) { + NumberFormat format = super.getFormat(locale); + format.setGroupingUsed(false); + format.setMaximumFractionDigits(3); + format.setMinimumFractionDigits(3); + return format; + } +}