]> source.dussan.org Git - vaadin-framework.git/commitdiff
Updated book examples.
authorMarko Grönroos <magi@iki.fi>
Thu, 3 Jul 2008 13:12:12 +0000 (13:12 +0000)
committerMarko Grönroos <magi@iki.fi>
Thu, 3 Jul 2008 13:12:12 +0000 (13:12 +0000)
svn changeset:5032/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 [new file with mode: 0644]
src/com/itmill/toolkit/tests/book/SSNField.java
src/com/itmill/toolkit/tests/book/WindowTestApplication.java

index 1db09d55982b2b135ee7faab3b5483c9655314bf..b75c95f7470534dc01668916fce14e89c467b6e7 100644 (file)
@@ -110,7 +110,7 @@ public class BookTestApplication extends com.itmill.toolkit.Application {
                     "select/select", "select/native", "select/optiongroup",
                     "select/twincol", "filterselect", "validator", "table", "table/select",
                     "upload", "link", "gridlayout", "orderedlayout",
-                    "formlayout", "form", "panel", "expandlayout", "tabsheet",
+                    "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",
@@ -500,13 +500,37 @@ public class BookTestApplication extends com.itmill.toolkit.Application {
     }
 
     void example_Validator(Window main, String param) {
+        if (param != null && param.equals("required")) {
+            Form form = new Form();
+            form.setCaption("My Form");
+            form.setRequired(true);
+            main.addComponent(form);
+            
+            TextField text = new TextField("This is a required text field");
+            text.setRequired(true);
+            text.setImmediate(true);
+            form.getLayout().addComponent(text);
+            return;
+        }
         main.addComponent(new SSNField());
     }
 
+    class PagingTable extends Table {
+        public String getTag() {
+            return "pagingtable";
+        }
+    }
+    
     void example_Table(Window main, String param) {
-        if (param.equals("select")) {
+        if (param != null && param.equals("select")) {
             main.addComponent(new TableExample2());
-        } else
+        } else if (param != null && param.equals("paging")) {
+            PagingTable table = new PagingTable();
+            table.addContainerProperty("Column 1", String.class, null);
+            for (int i=0; i<100; i++)
+                table.addItem(new Object[]{"Item "+i}, new Integer(i));
+            main.addComponent(table);
+        }else
             main.addComponent(new TableExample1());
     }
 
@@ -607,6 +631,16 @@ public class BookTestApplication extends com.itmill.toolkit.Application {
     }
 
     void example_GridLayout(Window main, String param) {
+        if (param.equals("embedded")) {
+            final GridLayout grid = new GridLayout(3,3);
+            for (int i=0; i<3*3; i++) {
+                ClassResource img = new ClassResource("smiley.jpg", main.getApplication());
+                Embedded embedded = new Embedded("", img);
+                grid.addComponent(embedded);
+            }
+            main.addComponent(grid);
+            return;
+        }
         /* Create a 4 by 4 grid layout. */
         final GridLayout grid = new GridLayout(4, 4);
         grid.addStyleName("example-gridlayout");
@@ -648,8 +682,8 @@ public class BookTestApplication extends com.itmill.toolkit.Application {
             
              Object cells[][] = {
                      {new Button("Top Left"),      new Integer(OrderedLayout.ALIGNMENT_LEFT),              new Integer(OrderedLayout.ALIGNMENT_TOP)},
-                     {new Label("Top Center"),    new Integer(OrderedLayout.ALIGNMENT_HORIZONTAL_CENTER), new Integer(OrderedLayout.ALIGNMENT_TOP)},
-                     {new Label("Top Right"),     new Integer(OrderedLayout.ALIGNMENT_RIGHT),             new Integer(OrderedLayout.ALIGNMENT_TOP)},
+                     {new Label ("Top Center"),    new Integer(OrderedLayout.ALIGNMENT_HORIZONTAL_CENTER), new Integer(OrderedLayout.ALIGNMENT_TOP)},
+                     {new Label ("Top Right"),     new Integer(OrderedLayout.ALIGNMENT_RIGHT),             new Integer(OrderedLayout.ALIGNMENT_TOP)},
                      {new Button("Center Left"),   new Integer(OrderedLayout.ALIGNMENT_LEFT),              new Integer(OrderedLayout.ALIGNMENT_VERTICAL_CENTER)},
                      {new Button("Center Center"), new Integer(OrderedLayout.ALIGNMENT_HORIZONTAL_CENTER), new Integer(OrderedLayout.ALIGNMENT_VERTICAL_CENTER)},
                      {new Button("Center Right"),  new Integer(OrderedLayout.ALIGNMENT_RIGHT),             new Integer(OrderedLayout.ALIGNMENT_VERTICAL_CENTER)},
@@ -683,14 +717,49 @@ public class BookTestApplication extends com.itmill.toolkit.Application {
 
     void example_FormLayout(Window main, String param) {
         final FormLayout layout = new FormLayout();
-        layout.addComponent(new TextField("Name"));
-        layout.addComponent(new TextField("Street address"));
-        layout.addComponent(new TextField("Postal code"));
+        layout.addComponent(new TextField("Text Field"));
+        layout.addComponent(new CheckBox("Check Box"));
+        layout.addComponent(new Select("Select"));
         main.addComponent(layout);
     }
 
     void example_Form(Window main, String param) {
-        main.addComponent(new FormExample());
+        if (param != null && param.equals("simple")) {
+            main.addComponent(new FormExample2());
+        } else if (param != null && param.equals("layout")) {
+            Form form = new Form();
+            form.setCaption("Form Caption");
+            form.setDescription("This is a description of the Form that is " +
+                    "displayed in the upper part of the form. You normally enter some " +
+                    "descriptive text about the form and its use here.");
+            
+            // Add a field directly to the layout. This field will not be bound to
+            // the data source Item of the form. 
+            form.getLayout().addComponent(new TextField("A Field"));
+            
+            // Add a field and bind it to an named item property.
+            form.addField("another", new TextField("Another Field"));
+            
+            form.setComponentError(new UserError("This is the error indicator of the Form."));
+
+            // Set the footer layout and add some text.
+            form.setFooter(new OrderedLayout());
+            form.getFooter().addComponent(new Label("This is the footer area of the Form. "+
+                                        "You can use any layout here. This is nice for buttons."));
+            
+            // Add an Ok (commit), Reset (discard), and Cancel buttons for the form.
+            ExpandLayout okbar = new ExpandLayout(OrderedLayout.ORIENTATION_HORIZONTAL);
+            okbar.setHeight("25px");
+            Button okbutton = new Button("OK", form, "commit");
+            okbar.addComponent(okbutton);
+            okbar.setComponentAlignment(okbutton, ExpandLayout.ALIGNMENT_RIGHT, ExpandLayout.ALIGNMENT_TOP);
+            okbar.addComponent(new Button("Reset", form, "discard"));
+            okbar.addComponent(new Button("Cancel"));
+            form.getFooter().addComponent(okbar);
+            
+            main.addComponent(form);
+        } else 
+            main.addComponent(new FormExample());
     }
 
     void example_ExpandLayout(Window main, String param) {
@@ -724,6 +793,26 @@ public class BookTestApplication extends com.itmill.toolkit.Application {
             window.setLayout(layout);
             window.addComponent(progress);
             
+            return;
+        } else if (param != null && param.equals("root")) {
+            final Window mainwin = main;
+            
+            // Layout to switch to
+            final OrderedLayout expand2 = new OrderedLayout();
+            expand2.addComponent(new Label("I am layout too."));
+            
+            // Original layout
+            final OrderedLayout expand1 = new OrderedLayout();
+            Button switchButton = new Button("Switch to other layout");
+            switchButton.addListener(new Button.ClickListener() {
+                public void buttonClick(ClickEvent event) {
+                    mainwin.setLayout(null);
+                    mainwin.setLayout(expand2);
+                }
+            });
+            expand1.addComponent(switchButton);
+            main.setLayout(expand1);
+            
             return;
         } else if (param != null && param.equals("size")) {
             ExpandLayout layout = new ExpandLayout();
@@ -1062,7 +1151,7 @@ public class BookTestApplication extends com.itmill.toolkit.Application {
         
         // Create the custom layout and set it as the root layout of
         // the containing window.
-        CustomLayout custom = new CustomLayout("layoutname");
+        final CustomLayout custom = new CustomLayout("layoutname");
         sub.setLayout(custom);
         
         // Create components and bind them to the location tags
@@ -1073,8 +1162,26 @@ public class BookTestApplication extends com.itmill.toolkit.Application {
         TextField password = new TextField();
         custom.addComponent(password, "password");
         
-        Button ok = new Button("Login");
+        final Button ok = new Button("Login");
         custom.addComponent(ok, "okbutton");
+        
+        final Button deny = new Button("No can do!");
+        
+        Button.ClickListener listener = new Button.ClickListener() {
+            public void buttonClick(ClickEvent event) {
+                // Switch between ok and deny
+                if (custom.getComponent("okbutton") == ok) {
+                    System.out.println("Changing to deny button.");
+                    custom.addComponent(deny, "okbutton");
+                } else {
+                    System.out.println("Changing to ok button.");
+                    custom.addComponent(ok, "okbutton");
+                }
+             } 
+         };
+        
+        ok.addListener(listener);
+        deny.addListener(listener);
     }
 
     void example_Spacing(final Window main, String param) {
index 22a64af9dca6b719ea657d01b19c3008e73ddb8f..86ec3f5e8564d77b6b1aa7ddb78f1299c5ea46a5 100644 (file)
@@ -7,6 +7,7 @@ import com.itmill.toolkit.data.Item;
 import com.itmill.toolkit.data.Property;
 import com.itmill.toolkit.data.Validator;
 import com.itmill.toolkit.data.util.BeanItem;
+import com.itmill.toolkit.terminal.UserError;
 import com.itmill.toolkit.ui.Button;
 import com.itmill.toolkit.ui.Component;
 import com.itmill.toolkit.ui.CustomComponent;
@@ -18,6 +19,7 @@ import com.itmill.toolkit.ui.FormLayout;
 import com.itmill.toolkit.ui.OrderedLayout;
 import com.itmill.toolkit.ui.Select;
 import com.itmill.toolkit.ui.TextField;
+import com.itmill.toolkit.ui.Button.ClickEvent;
 
 /**
  * This example demonstrates the most important features of the Form component:
@@ -117,8 +119,8 @@ public class FormExample extends CustomComponent {
                     public boolean isValid(Object value) {
                         if (value == null || !(value instanceof String))
                             return false;
-                        int val = Integer.parseInt((String) value);
-                        return val >= 10000 && val < 100000;
+                        
+                        return ((String)value).matches("[0-9]{5}");
                     }
 
                     public void validate(Object value)
@@ -151,12 +153,10 @@ public class FormExample extends CustomComponent {
     }
 
     public FormExample() {
-        // Create a form and use FormLayout as its layout.
+        // Create a form. It will use FormLayout as its layout by default.
         final Form form = new Form();
-        final FormLayout layout = new FormLayout();
-        form.setLayout(layout);
 
-        // Set form caption and description texts
+        // Set form caption and description texts.
         form.setCaption("Contact Information");
         form.setDescription("Please enter valid name and address. Fields marked with * are required.");
 
@@ -173,7 +173,7 @@ public class FormExample extends CustomComponent {
         form.setItemDataSource(item);
 
         // Set the order of the items in the form.
-        Vector order = new Vector();
+        Vector<String> order = new Vector<String>();
         order.add("name");
         order.add("address");
         order.add("postalCode");
@@ -182,31 +182,43 @@ public class FormExample extends CustomComponent {
 
         // Set required fields.
         form.getField("name").setRequired(true);
+        form.getField("name").setRequiredError("Name is missing");
+        form.getField("address").setRequired(true);
+        form.getField("address").setRequiredError("Address is missing");
 
         // Set the form to act immediately on user input. This is
         // Necessary for the validation to occur immediately when the
         // input focus changes.
         form.setImmediate(true);
+        form.setValidationVisible(false);
+        form.setRequired(true);
 
-        // Have the validation error indicator area visible.
-        form.setValidationVisible(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).
         form.setWriteThrough(false);
         form.setReadThrough(false);
 
-        // Add Ok and Reset controls to the form.
+        // Add Commit and Discard controls to the form.
         ExpandLayout footer = new ExpandLayout(
                 OrderedLayout.ORIENTATION_HORIZONTAL);
-        Button ok = new Button("Ok", form, "commit");
-        Button reset = new Button("Reset", form, "discard");
-        footer.addComponent(ok);
-        footer.setComponentAlignment(ok, ExpandLayout.ALIGNMENT_RIGHT,
+        
+        // The Commit button calls form.commit().
+        Button commit = new Button("Commit");
+        commit.addListener(new Button.ClickListener() {
+            public void buttonClick(ClickEvent event) {
+                form.setValidationVisible(true);
+                form.commit();
+            }
+        });
+        
+        // The Discard button calls form.discard().
+        Button discard = new Button("Discard", form, "discard");
+        footer.addComponent(commit);
+        footer.setComponentAlignment(commit, ExpandLayout.ALIGNMENT_RIGHT,
                 ExpandLayout.ALIGNMENT_TOP);
-        footer.setHeight("30px");
-        footer.addComponent(reset);
+        footer.setHeight("25px");
+        footer.addComponent(discard);
         form.setFooter(footer);
 
         OrderedLayout root = new OrderedLayout();
diff --git a/src/com/itmill/toolkit/tests/book/FormExample2.java b/src/com/itmill/toolkit/tests/book/FormExample2.java
new file mode 100644 (file)
index 0000000..27d095b
--- /dev/null
@@ -0,0 +1,119 @@
+package com.itmill.toolkit.tests.book;
+
+import java.util.Vector;
+
+import com.itmill.toolkit.data.Container;
+import com.itmill.toolkit.data.Item;
+import com.itmill.toolkit.data.Property;
+import com.itmill.toolkit.data.util.BeanItem;
+import com.itmill.toolkit.ui.Component;
+import com.itmill.toolkit.ui.CustomComponent;
+import com.itmill.toolkit.ui.Field;
+import com.itmill.toolkit.ui.FieldFactory;
+import com.itmill.toolkit.ui.Form;
+import com.itmill.toolkit.ui.OrderedLayout;
+import com.itmill.toolkit.ui.Select;
+import com.itmill.toolkit.ui.TextField;
+
+/**
+ * This example demonstrates the most important features of the Form component:
+ * binding Form to a JavaBean so that form fields are automatically generated
+ * from the bean properties, creation of fields with proper types for each bean
+ * properly using a FieldFactory, buffering (commit/discard), and validation.
+ * 
+ * The Form is used with a FormLayout, which automatically lays the components
+ * out in a format typical for forms.
+ */
+public class FormExample2 extends CustomComponent {
+    /** A simple JavaBean. */
+    public class PersonBean {
+        String name = "";
+        String city = "";
+        
+        public void setName(String name) {
+            this.name = name;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public void setCity(String city) {
+            this.city = city;
+        }
+
+        public String getCity() {
+            return city;
+        }
+    }
+
+    /**
+     * Factory to create the proper type of field for each property type. We
+     * need to implement just one of the factory methods.
+     */
+    class MyFieldFactory implements FieldFactory {
+
+        public Field createField(Class type, Component uiContext) {
+            return null;
+        }
+
+        public Field createField(Property property, Component uiContext) {
+            return null;
+        }
+
+        public Field createField(Item item, Object propertyId,
+                Component uiContext) {
+            String pid = (String) propertyId;
+            if (pid.equals("name"))
+                return new TextField("Name");
+            else if (pid.equals("city")) {
+                Select select = new Select("City");
+                select.addItem("Berlin");
+                select.addItem("Helsinki");
+                select.addItem("London");
+                select.addItem("New York");
+                select.addItem("Turku");
+                select.setNewItemsAllowed(true);
+                return select;
+            }
+            return null;
+        }
+
+        public Field createField(Container container, Object itemId,
+                Object propertyId, Component uiContext) {
+            return null;
+        }
+    }
+
+    public FormExample2() {
+        // Create a form and use FormLayout as its layout.
+        final Form form = new Form();
+
+        // Set form caption and description texts
+        form.setCaption("Contact Information");
+        form.setDescription("Please specify name of the person and the city where the person lives in.");
+
+        // Use the custom field factory to create the fields in the form.
+        form.setFieldFactory(new MyFieldFactory());
+
+        // Create the custom bean.
+        PersonBean bean = new PersonBean();
+
+        // Create a bean item that is bound to the bean.
+        BeanItem item = new BeanItem(bean);
+
+        // Bind the bean item as the data source for the form.
+        form.setItemDataSource(item);
+
+        // Set the order of the items in the form.
+        Vector order = new Vector();
+        order.add("name");
+        order.add("city");
+        form.setVisibleItemProperties(order);
+
+        OrderedLayout root = new OrderedLayout();
+        root.setWidth(300, OrderedLayout.UNITS_PIXELS);
+        root.addComponent(form);
+        this.setCompositionRoot(root);
+    }
+}
index aa5deee44705aea18c348f6497b917fe7022a308..0659ebb91045a8bed04b4cecf39e5885ec10cdac 100644 (file)
@@ -64,8 +64,7 @@ public class SSNField extends CustomComponent implements
         myfield.setColumns(11);
 
         /* Create and set the validator object for the field. */
-        final SSNValidator ssnvalidator = new SSNValidator();
-        myfield.addValidator(ssnvalidator);
+        myfield.addValidator(new SSNValidator());
 
         /* ValueChageEvent will be generated immediately when the component
            loses focus. */
index c553c17073bd719fa15b19e669f4502747735c63..baa149f79d9ba74224cadc9faa018530b29b4b6d 100644 (file)
@@ -1,5 +1,7 @@
 package com.itmill.toolkit.tests.book;
 
+import java.util.HashMap;
+
 import com.itmill.toolkit.Application;
 import com.itmill.toolkit.ui.*;
 import com.itmill.toolkit.ui.Button.ClickEvent;
@@ -7,11 +9,12 @@ import com.itmill.toolkit.terminal.*;
 
 public class WindowTestApplication extends Application {
     Window anotherpage = null;
+    HashMap<String, Window> windows = new HashMap<String, Window>();
 
     public void init() {
         final Window main = new Window ("Window Test Application");
         setMainWindow(main);
-        setTheme("tests-magi");
+        setTheme("tests-book");
         
         /* Create a new window. */
         final Window mywindow = new Window("Second Window");
@@ -38,35 +41,45 @@ public class WindowTestApplication extends Application {
         Link link = new Link("Click to open second window",
                              new ExternalResource(mywindow.getURL()));
         link.setTargetName("_new");
-        //link.setTargetHeight(300);
-        //link.setTargetWidth(300);
-        //link.setTargetBorder(Link.TARGET_BORDER_DEFAULT);
         main.addComponent(link);
         
-        /* Add the link manually inside a Label. */
+        // Add the link manually inside a Label.
         main.addComponent(new Label("Second window: <a href='"
                                     + mywindow.getURL() + "' target='_new'>click to open</a>",
                           Label.CONTENT_XHTML));
         main.addComponent(new Label("The second window can be accessed through URL: "
                                     + mywindow.getURL()));
 
-        /* Add link to the yet another window that does not yet exist. */
-        main.addComponent(new Label("Yet another window: <a href='"
-                                    + getURL() + "anotherpage/' target='_new'>click to open</a>",
-                          Label.CONTENT_XHTML));
-        main.addComponent(new Label("The yet another window can be accessed through URL: "
-                                    + getURL()+"anotherpage/"));
+        // Add links to windows that do not yet exist, but are created dynamically
+        // when the URL is called.
+        main.addComponent(new Label("URLs to open item windows:"));
+        final String[] items = new String[] {"mercury", "venus", "earth", "mars",
+                "jupiter", "saturn", "uranus", "neptune"};
+        for (String item : items) {
+            // We do not create window objects here, but just links to the windows
+            String windowUrl = getURL() + "planet-" + item;
+            main.addComponent(new Label("A window about '"+item+"': <a href='" +
+                    windowUrl + "' target='_new'>"+ windowUrl +"</a>",
+                    Label.CONTENT_XHTML));
+        }
     }
     
     public Window getWindow(String name) {
-        if (name.equals("anotherpage")) {
-            if (anotherpage == null) {
-                anotherpage = new Window("Yet Another Page");
-                anotherpage.addComponent(new Label("This is a yet another window."));
+        if (name.startsWith("planet-")) {
+            String planetName = name.substring("planet-".length());
+            if (! windows.containsKey(planetName)) {
+                // Create the window object on the fly.
+                Window newWindow = new Window("Yet Another Page");
+                newWindow.addComponent(new Label("This window contains details about "+planetName+"."));
+                windows.put(planetName, newWindow);
+                
+                // We must add the window to the application, it is not done
+                // automatically
+                addWindow(newWindow);
             }
-            return anotherpage;
+            return windows.get(planetName);
         }
+        
         return super.getWindow(name);
     }
-    
 }