]> source.dussan.org Git - vaadin-framework.git/commitdiff
Updated Form example. New parameter and URI handler example.
authorMarko Grönroos <magi@iki.fi>
Tue, 8 Jul 2008 16:27:04 +0000 (16:27 +0000)
committerMarko Grönroos <magi@iki.fi>
Tue, 8 Jul 2008 16:27:04 +0000 (16:27 +0000)
svn changeset:5072/svn branch:trunk

src/com/itmill/toolkit/tests/book/BookTestApplication.java
src/com/itmill/toolkit/tests/book/FormExample.java
src/com/itmill/toolkit/tests/book/FormExample2.java
src/com/itmill/toolkit/tests/book/MyDynamicResource.java [new file with mode: 0644]

index b75c95f7470534dc01668916fce14e89c467b6e7..59764ccadd1faba3718239508227d1f4db15711f 100644 (file)
@@ -7,6 +7,7 @@ package com.itmill.toolkit.tests.book;
 import java.net.URL;
 import java.util.Iterator;
 import java.util.Locale;
+import java.util.Map;
 import java.util.Set;
 import java.util.Vector;
 
@@ -21,8 +22,10 @@ import com.itmill.toolkit.data.validator.StringLengthValidator;
 import com.itmill.toolkit.terminal.ClassResource;
 import com.itmill.toolkit.terminal.DownloadStream;
 import com.itmill.toolkit.terminal.ExternalResource;
+import com.itmill.toolkit.terminal.ParameterHandler;
 import com.itmill.toolkit.terminal.Sizeable;
 import com.itmill.toolkit.terminal.StreamResource;
+import com.itmill.toolkit.terminal.URIHandler;
 import com.itmill.toolkit.terminal.UserError;
 import com.itmill.toolkit.terminal.gwt.server.WebApplicationContext;
 import com.itmill.toolkit.ui.AbstractSelect;
@@ -68,135 +71,157 @@ public class BookTestApplication extends com.itmill.toolkit.Application {
     StreamResource strres;
     OrderedLayout ol;
     int getwincount = 0;
-
+    
     public void init() {
         setTheme("tests-book");
         
         setMainWindow(main);
-    }
 
-    public DownloadStream handleURI(URL context, String relativeUri) {
-        // Let default implementation handle requests for
-        // application resources.
-        if (relativeUri.startsWith("APP"))
-            return super.handleURI(context, relativeUri);
-        if (relativeUri.startsWith("win-"))
-            return super.handleURI(context, relativeUri);
+        // Demo the use of parameter and URI handlers
+        main.addParameterHandler(new MyParameterHandler());
+        main.addURIHandler(new MyURIHandler());
         
-        String example;
-        String param = null;
+        MyDynamicResource myresource = new MyDynamicResource();
+        main.addParameterHandler(myresource);
+        main.addURIHandler(myresource);
 
-        final int slashPos = relativeUri.indexOf("/");
-        if (slashPos > 0) {
-            example = relativeUri.substring(0, slashPos);
-            param = relativeUri.substring(slashPos + 1);
-        } else {
-            example = relativeUri;
+        main.addURIHandler(new BookTestURIHandler());
+    }
+    
+    class MyParameterHandler implements ParameterHandler {
+        public void handleParameters(Map parameters) {
+            // Print out the parameters to standard output
+            for (Iterator it = parameters.keySet().iterator(); it.hasNext();) {
+                String key   = (String) it.next();
+                String value = ((String[]) parameters.get(key))[0];
+                System.out.println("Key: "+key+", value: "+value);
+            }
         }
-
-        /* Remove existing components and windows. */
-        main.removeAllComponents();
-        final Set childwindows = main.getChildWindows();
-        for (final Iterator cwi = childwindows.iterator(); cwi.hasNext();) {
-            final Window child = (Window) cwi.next();
-            main.removeWindow(child);
+    }
+    class MyURIHandler implements URIHandler {
+        public DownloadStream handleURI(URL context, String relativeUri) {
+            System.out.println("Context: "+context.toString()+", relative: "+relativeUri);
+            return null; // Let the Application provide the response
         }
-        main.setLayout(new OrderedLayout());
-
-        if (example.equals("index")) {
-            final String examples[] = { "defaultbutton", "label",
-                    "labelcontent", "tree", "embedded", "textfield",
-                    "textfieldvalidation", "datefield", "button",
-                    "select/select", "select/native", "select/optiongroup",
-                    "select/twincol", "filterselect", "validator", "table", "table/select",
-                    "upload", "link", "gridlayout", "orderedlayout",
-                    "formlayout", "form", "form/simple", "form/layout", "panel", "expandlayout", "expandlayout/root", "tabsheet",
-                    "alignment", "alignment/grid", "window", "window/opener",
-                    "window/multiple", "classresource", "usererror",
-                    "progress/window", "progress/thread", "progress",
-                    "customlayout", "spacing", "margin", "clientinfo",
-                    "fillinform/templates"};
-            for (int i = 0; i < examples.length; i++) {
-                main.addComponent(new Label("<a href='" + context.toString() +
-                        examples[i] + "'>" + examples[i] + "</a>",
-                        Label.CONTENT_XHTML));
+    }
+
+    class BookTestURIHandler implements URIHandler {
+        public DownloadStream handleURI(URL context, String relativeUri) {
+            String example;
+            String param = null;
+
+            final int slashPos = relativeUri.indexOf("/");
+            if (slashPos > 0) {
+                example = relativeUri.substring(0, slashPos);
+                param = relativeUri.substring(slashPos + 1);
+            } else {
+                example = relativeUri;
             }
-            return null;
-        }
 
-        if (example.equals("defaultbutton")) {
-            example_defaultButton(main, param);
-        } else if (example.equals("label")) {
-            example_Label(main, param);
-        } else if (example.equals("labelcontent")) {
-            example_LabelContent(main, param);
-        } else if (example.equals("tree")) {
-            example_Tree(main, param);
-        } else if (example.equals("embedded")) {
-            example_Embedded(main, param);
-        } else if (example.equals("textfield")) {
-            example_TextField(main, param);
-        } else if (example.equals("textfieldvalidation")) {
-            example_TextFieldValidation(main, param);
-        } else if (example.equals("usererror")) {
-            example_UserError(main, param);
-        } else if (example.equals("datefield")) {
-            example_DateField(main, param);
-        } else if (example.equals("button")) {
-            example_Button(main, param);
-        } else if (example.equals("checkbox")) {
-            example_CheckBox(main, param);
-        } else if (example.equals("select")) {
-            example_Select(main, param);
-        } else if (example.equals("filterselect")) {
-            example_FilterSelect(main, param);
-        } else if (example.equals("validator")) {
-            example_Validator(main, param);
-        } else if (example.equals("table")) {
-            example_Table(main, param);
-        } else if (example.equals("upload")) {
-            example_Upload(main, param);
-        } else if (example.equals("link")) {
-            example_Link(main, param);
-        } else if (example.equals("gridlayout")) {
-            example_GridLayout(main, param);
-        } else if (example.equals("orderedlayout")) {
-            example_OrderedLayout(main, param);
-        } else if (example.equals("formlayout")) {
-            example_FormLayout(main, param);
-        } else if (example.equals("form")) {
-            example_Form(main, param);
-        } else if (example.equals("tabsheet")) {
-            example_TabSheet(main, param);
-        } else if (example.equals("panel")) {
-            example_Panel(main, param);
-        } else if (example.equals("expandlayout")) {
-            example_ExpandLayout(main, param);
-        } else if (example.equals("alignment")) {
-            example_Alignment(main, param);
-        } else if (example.equals("window")) {
-            example_Window(main, param);
-        } else if (example.equals("classresource")) {
-            example_ClassResource(main, param);
-        } else if (example.equals("progress")) {
-            example_ProgressIndicator(main, param);
-        } else if (example.equals("customlayout")) {
-            example_CustomLayout(main, param);
-        } else if (example.equals("spacing")) {
-            example_Spacing(main, param);
-        } else if (example.equals("margin")) {
-            example_Margin(main, param);
-        } else if (example.equals("clientinfo")) {
-            example_ClientInfo(main, param);
-        } else if (example.equals("fillinform")) {
-            example_FillInForm(main, param);
-        } else {
-            ; // main.addComponent(new Label("Unknown test '"+example+"'."));
-        }
+            /* Remove existing components and windows. */
+            main.removeAllComponents();
+            final Set childwindows = main.getChildWindows();
+            for (final Iterator cwi = childwindows.iterator(); cwi.hasNext();) {
+                final Window child = (Window) cwi.next();
+                main.removeWindow(child);
+            }
+            main.setLayout(new OrderedLayout());
+
+            if (example.equals("index")) {
+                final String examples[] = { "defaultbutton", "label",
+                        "labelcontent", "tree", "embedded", "textfield",
+                        "textfieldvalidation", "datefield", "button",
+                        "select/select", "select/native", "select/optiongroup",
+                        "select/twincol", "filterselect", "validator", "table", "table/select",
+                        "upload", "link", "gridlayout", "orderedlayout",
+                        "formlayout", "form", "form/simple", "form/layout", "panel", "expandlayout", "expandlayout/root", "tabsheet",
+                        "alignment", "alignment/grid", "window", "window/opener",
+                        "window/multiple", "classresource", "usererror",
+                        "progress/window", "progress/thread", "progress",
+                        "customlayout", "spacing", "margin", "clientinfo",
+                        "fillinform/templates"};
+                for (int i = 0; i < examples.length; i++) {
+                    main.addComponent(new Label("<a href='" + context.toString() +
+                            examples[i] + "'>" + examples[i] + "</a>",
+                            Label.CONTENT_XHTML));
+                }
+                return null;
+            }
 
-        return null;
-    }
+            if (example.equals("defaultbutton")) {
+                example_defaultButton(main, param);
+            } else if (example.equals("label")) {
+                example_Label(main, param);
+            } else if (example.equals("labelcontent")) {
+                example_LabelContent(main, param);
+            } else if (example.equals("tree")) {
+                example_Tree(main, param);
+            } else if (example.equals("embedded")) {
+                example_Embedded(main, param);
+            } else if (example.equals("textfield")) {
+                example_TextField(main, param);
+            } else if (example.equals("textfieldvalidation")) {
+                example_TextFieldValidation(main, param);
+            } else if (example.equals("usererror")) {
+                example_UserError(main, param);
+            } else if (example.equals("datefield")) {
+                example_DateField(main, param);
+            } else if (example.equals("button")) {
+                example_Button(main, param);
+            } else if (example.equals("checkbox")) {
+                example_CheckBox(main, param);
+            } else if (example.equals("select")) {
+                example_Select(main, param);
+            } else if (example.equals("filterselect")) {
+                example_FilterSelect(main, param);
+            } else if (example.equals("validator")) {
+                example_Validator(main, param);
+            } else if (example.equals("table")) {
+                example_Table(main, param);
+            } else if (example.equals("upload")) {
+                example_Upload(main, param);
+            } else if (example.equals("link")) {
+                example_Link(main, param);
+            } else if (example.equals("gridlayout")) {
+                example_GridLayout(main, param);
+            } else if (example.equals("orderedlayout")) {
+                example_OrderedLayout(main, param);
+            } else if (example.equals("formlayout")) {
+                example_FormLayout(main, param);
+            } else if (example.equals("form")) {
+                example_Form(main, param);
+            } else if (example.equals("tabsheet")) {
+                example_TabSheet(main, param);
+            } else if (example.equals("panel")) {
+                example_Panel(main, param);
+            } else if (example.equals("expandlayout")) {
+                example_ExpandLayout(main, param);
+            } else if (example.equals("alignment")) {
+                example_Alignment(main, param);
+            } else if (example.equals("window")) {
+                example_Window(main, param);
+            } else if (example.equals("classresource")) {
+                example_ClassResource(main, param);
+            } else if (example.equals("progress")) {
+                example_ProgressIndicator(main, param);
+            } else if (example.equals("customlayout")) {
+                example_CustomLayout(main, param);
+            } else if (example.equals("spacing")) {
+                example_Spacing(main, param);
+            } else if (example.equals("margin")) {
+                example_Margin(main, param);
+            } else if (example.equals("clientinfo")) {
+                example_ClientInfo(main, param);
+            } else if (example.equals("fillinform")) {
+                example_FillInForm(main, param);
+            } else {
+                ; // main.addComponent(new Label("Unknown test '"+example+"'."));
+            }
 
+            return null;
+        }
+    }
+    
     /*
      * public Window getWindow(String name) { Window superwin =
      * super.getWindow(name); if (superwin != null) return superwin;
index 786471d6c6a57e1357660a9a09c56909a3990b26..8b2fe6ef80f60407437dba920ed16e243aee5ec6 100644 (file)
@@ -186,19 +186,19 @@ public class FormExample extends CustomComponent {
         order.add("city");
         form.setVisibleItemProperties(order);
 
-        // Set required fields.
+        // Set required fields. The required error is displayed in
+        // the error indication are of the Form if a required
+        // field is empty. If it is not set, no error is displayed
+        // about an empty required field.
         form.getField("name").setRequired(true);
         form.getField("name").setRequiredError("Name is missing");
-        form.getField("address").setRequired(true);
-        form.getField("address").setRequiredError("Address is missing");
+        form.getField("address").setRequired(true); // No error message
 
         // Set the form to act immediately on user input. This is
-        // Necessary for the validation to occur immediately when the
-        // input focus changes.
+        // necessary for the validation of the fields to occur immediately when
+        // the input focus changes and not just on commit.
         form.setImmediate(true);
-        form.setValidationVisible(false);
-        form.setRequired(true);
-
+        
         // Set buffering so that commit() must be called for the form
         // before input is written to the data. (Input is not written
         // immediately through).
index 27d095bc01fedce306b2963b959dff4da61898af..8d9c0e86e28baf14680924bbd4341d75bac203d3 100644 (file)
@@ -110,6 +110,10 @@ public class FormExample2 extends CustomComponent {
         order.add("name");
         order.add("city");
         form.setVisibleItemProperties(order);
+        
+        form.getField("name").setRequired(true);
+        form.getField("name").setRequiredError("You must enter a name.");
+        form.getField("city").setRequired(true);
 
         OrderedLayout root = new OrderedLayout();
         root.setWidth(300, OrderedLayout.UNITS_PIXELS);
diff --git a/src/com/itmill/toolkit/tests/book/MyDynamicResource.java b/src/com/itmill/toolkit/tests/book/MyDynamicResource.java
new file mode 100644 (file)
index 0000000..34ca740
--- /dev/null
@@ -0,0 +1,66 @@
+package com.itmill.toolkit.tests.book;
+
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.*;
+import java.net.URL;
+import java.util.Map;
+import javax.imageio.ImageIO;
+import com.itmill.toolkit.terminal.*;
+
+/**
+ * Demonstrates handling URI parameters and the URI itself to create a dynamic
+ * resource.
+ */
+public class MyDynamicResource implements URIHandler, ParameterHandler {
+    String textToDisplay = "- no text given -";
+
+    /**
+     * Handle the URL parameters and store them for the URI handler to use.
+     */
+    public void handleParameters(Map parameters) {
+        // Get and store the passed HTTP parameter.
+        if (parameters.containsKey("text"))
+            textToDisplay = ((String[])parameters.get("text"))[0]; 
+    }
+
+    /**
+     * Provides the dynamic resource if the URI matches the resource URI. The
+     * matching URI is "/myresource" under the application URI context.
+     * 
+     * Returns null if the URI does not match. Otherwise returns a download
+     * stream that contains the response from the server.
+     */
+    public DownloadStream handleURI(URL context, String relativeUri) {
+        // Catch the given URI that identifies the resource, otherwise let other
+        // URI handlers or the Application to handle the response.
+        if (!relativeUri.startsWith("myresource"))
+            return null;
+        
+        // Create an image and draw some background on it.
+        BufferedImage image = new BufferedImage (200, 200, BufferedImage.TYPE_INT_RGB);
+        Graphics drawable = image.getGraphics();
+        drawable.setColor(Color.lightGray);
+        drawable.fillRect(0,0,200,200);
+        drawable.setColor(Color.yellow);
+        drawable.fillOval(25,25,150,150);
+        drawable.setColor(Color.blue);
+        drawable.drawRect(0,0,199,199);
+        
+        // Use the parameter to create dynamic content.
+        drawable.setColor(Color.black);
+        drawable.drawString("Text: "+textToDisplay, 75, 100);
+
+        try {
+            // Write the image to a buffer.
+            ByteArrayOutputStream imagebuffer = new ByteArrayOutputStream();
+            ImageIO.write(image, "png", imagebuffer);
+
+            // Return a stream from the buffer.
+            ByteArrayInputStream istream = new ByteArrayInputStream(imagebuffer.toByteArray());
+            return new DownloadStream (istream,null,null);
+        } catch (IOException e) {
+            return null;
+        }
+    }
+}