]> source.dussan.org Git - vaadin-framework.git/commitdiff
Pre-refactor commit.
authorMarc Englund <marc.englund@itmill.com>
Thu, 29 Nov 2007 09:02:35 +0000 (09:02 +0000)
committerMarc Englund <marc.englund@itmill.com>
Thu, 29 Nov 2007 09:02:35 +0000 (09:02 +0000)
svn changeset:3033/svn branch:trunk

src/com/itmill/toolkit/demo/ApplicationDemo.java
src/com/itmill/toolkit/demo/featurebrowser/ClientCachingExample.java [new file with mode: 0644]
src/com/itmill/toolkit/demo/featurebrowser/EmbeddedBrowserExample.java [new file with mode: 0644]
src/com/itmill/toolkit/demo/featurebrowser/NotificationExample.java [new file with mode: 0644]
src/com/itmill/toolkit/demo/featurebrowser/WindowingExample.java [new file with mode: 0644]

index ccfda10eca7a2639b685de11589ef1f00583db2e..0ac486eed25d10f17712e0e235b738cd30db63ef 100644 (file)
@@ -1,22 +1,30 @@
 package com.itmill.toolkit.demo;
 
 import java.util.HashMap;
-import java.util.Iterator;
 
 import com.itmill.toolkit.data.Item;
 import com.itmill.toolkit.data.Property;
 import com.itmill.toolkit.data.Property.ValueChangeEvent;
 import com.itmill.toolkit.data.util.HierarchicalContainer;
 import com.itmill.toolkit.data.util.IndexedContainer;
+import com.itmill.toolkit.demo.featurebrowser.ClientCachingExample;
+import com.itmill.toolkit.demo.featurebrowser.EmbeddedBrowserExample;
+import com.itmill.toolkit.demo.featurebrowser.NotificationExample;
+import com.itmill.toolkit.terminal.ExternalResource;
 import com.itmill.toolkit.terminal.Sizeable;
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.Component;
 import com.itmill.toolkit.ui.ExpandLayout;
 import com.itmill.toolkit.ui.Label;
+import com.itmill.toolkit.ui.Layout;
+import com.itmill.toolkit.ui.OrderedLayout;
 import com.itmill.toolkit.ui.Select;
 import com.itmill.toolkit.ui.SplitPanel;
 import com.itmill.toolkit.ui.TabSheet;
 import com.itmill.toolkit.ui.Table;
 import com.itmill.toolkit.ui.Tree;
 import com.itmill.toolkit.ui.Window;
+import com.itmill.toolkit.ui.Button.ClickEvent;
 
 /**
  * 
@@ -34,48 +42,50 @@ public class ApplicationDemo extends com.itmill.toolkit.Application implements
 
     private Tree tree;
     private Table table;
+    private TabSheet ts;
+
+    private HashMap components = new HashMap();
 
     // category, name, desc, class, viewed
     private static final Object[][] demos = new Object[][] {
     // START
             // Intro
-            { "Intro", "About", "About this demo", HelloWorld.class,
-                    Boolean.FALSE },
+            { "Intro", "About", "About this demo", Button.class, Boolean.FALSE },
             // Windowing
-            { "Intro", "Windowing", "About windowing", HelloWorld.class,
+            { "Intro", "Windowing", "About windowing", Button.class,
                     Boolean.FALSE },
             // Basic: Labels
-            { "Basic", "Labels", "Some variations of Labels", HelloWorld.class,
+            { "Basic", "Labels", "Some variations of Labels", Button.class,
                     Boolean.FALSE },
             // Basic: Buttons
             { "Basic", "Buttons and links",
-                    "Some variations of Buttons and Links", HelloWorld.class,
+                    "Some variations of Buttons and Links", Button.class,
                     Boolean.FALSE },
             // Basic: Fields
             { "Basic", "User input", "TextFields, DateFields, and such",
-                    HelloWorld.class, Boolean.FALSE },
+                    Button.class, Boolean.FALSE },
             // Basic: Selects
             { "Basic", "Choices, choices", "Some variations of simple selects",
-                    HelloWorld.class, Boolean.FALSE },
+                    Button.class, Boolean.FALSE },
             // Organizing: ComboBox
             { "Organizing", "ComboBox", "ComboBox - the swiss army select",
-                    HelloWorld.class, Boolean.FALSE },
+                    Button.class, Boolean.FALSE },
             // Organizing: Table
             { "Organizing", "Table", "A dynamic Table with bells and whistles",
-                    HelloWorld.class, Boolean.FALSE },
+                    Button.class, Boolean.FALSE },
             // Organizing: Tree
             { "Organizing", "Tree", "Some variations of Buttons and Links",
-                    HelloWorld.class, Boolean.FALSE },
+                    Button.class, Boolean.FALSE },
             // Misc: Notifications
             { "Misc", "Notifications", "Notifications can improve usability",
-                    HelloWorld.class, Boolean.FALSE },
+                    NotificationExample.class, Boolean.FALSE },
             // Misc: Caching
             { "Misc", "Client caching", "A simple demo of client-side caching",
-                    HelloWorld.class, Boolean.FALSE },
+                    ClientCachingExample.class, Boolean.FALSE },
             // Misc: Embedded
             { "Misc", "Embedding",
                     "You can embed resources - another site in this case",
-                    HelloWorld.class, Boolean.FALSE },
+                    EmbeddedBrowserExample.class, Boolean.FALSE },
     // END
     };
 
@@ -92,6 +102,10 @@ public class ApplicationDemo extends com.itmill.toolkit.Application implements
 
         HashMap sectionIds = new HashMap();
         HierarchicalContainer container = createContainer();
+        Object rootId = container.addItem();
+        Item item = container.getItem(rootId);
+        Property p = item.getItemProperty(PROPERTY_ID_NAME);
+        p.setValue("All");
         for (int i = 0; i < demos.length; i++) {
             Object[] demo = demos[i];
             String section = (String) demo[0];
@@ -101,8 +115,9 @@ public class ApplicationDemo extends com.itmill.toolkit.Application implements
             } else {
                 sectionId = container.addItem();
                 sectionIds.put(section, sectionId);
-                Item item = container.getItem(sectionId);
-                Property p = item.getItemProperty(PROPERTY_ID_NAME);
+                container.setParent(sectionId, rootId);
+                item = container.getItem(sectionId);
+                p = item.getItemProperty(PROPERTY_ID_NAME);
                 p.setValue(section);
             }
             Object id = container.addItem();
@@ -120,10 +135,8 @@ public class ApplicationDemo extends com.itmill.toolkit.Application implements
         tree.setItemCaptionPropertyId(PROPERTY_ID_NAME);
         tree.addListener(this);
         tree.setImmediate(true);
-        for (Iterator it = sectionIds.values().iterator(); it.hasNext();) {
-            // expand all sections
-            tree.expandItemsRecursively(it.next());
-        }
+        tree.expandItemsRecursively(rootId);
+
         split.addComponent(tree);
 
         SplitPanel split2 = new SplitPanel();
@@ -149,12 +162,72 @@ public class ApplicationDemo extends com.itmill.toolkit.Application implements
         ExpandLayout exp = new ExpandLayout();
         exp.setMargin(true);
         split2.addComponent(exp);
-        exp.addComponent(new Label("Short desc + open in window (+native)"));
+        OrderedLayout wbLayout = new OrderedLayout(
+                OrderedLayout.ORIENTATION_HORIZONTAL);
 
-        TabSheet ts = new TabSheet();
+        wbLayout.addComponent(new Button("Open in popup window",
+                new Button.ClickListener() {
+                    public void buttonClick(ClickEvent event) {
+                        Component component = (Component) ts
+                                .getComponentIterator().next();
+                        String caption = ts.getTabCaption(component);
+                        try {
+                            component = (Component) component.getClass()
+                                    .newInstance();
+                        } catch (Exception e) {
+                            // Could not create
+                            return;
+                        }
+                        Window w = new Window(caption);
+                        if (Layout.class.isAssignableFrom(component.getClass())) {
+                            w.setLayout((Layout) component);
+                        } else {
+                            w.getLayout().setSizeFull();
+                            w.addComponent(component);
+                        }
+                        getMainWindow().addWindow(w);
+                    }
+                }));
+        wbLayout.addComponent(new Button("Open in native window",
+                new Button.ClickListener() {
+                    public void buttonClick(ClickEvent event) {
+                        Component component = (Component) ts
+                                .getComponentIterator().next();
+                        String caption = ts.getTabCaption(component);
+                        Window w = getWindow(caption);
+                        if (w == null) {
+                            try {
+                                component = (Component) component.getClass()
+                                        .newInstance();
+                            } catch (Exception e) {
+                                // Could not create
+                                return;
+                            }
+                            w = new Window(caption);
+                            w.setName(caption);
+                            if (Layout.class.isAssignableFrom(component
+                                    .getClass())) {
+                                w.setLayout((Layout) component);
+                            } else {
+                                w.getLayout().setSizeFull();
+                                w.addComponent(component);
+                            }
+                            addWindow(w);
+                        }
+                        getMainWindow().open(new ExternalResource(w.getURL()),
+                                caption);
+                    }
+                }));
+
+        exp.addComponent(wbLayout);
+        exp.setComponentAlignment(wbLayout, exp.ALIGNMENT_RIGHT,
+                exp.ALIGNMENT_TOP);
+
+        ts = new TabSheet();
         ts.setSizeFull();
-        ts.addTab(new Label("asd"), "Demo", null);
-        ts.addTab(new Label("asd"), "Source", null);
+        ts.addTab(new Label(
+                "Choose demo (Only Notification and CliensSideCaching yet"),
+                "Demo", null);
         exp.addComponent(ts);
         exp.expand(ts);
 
@@ -163,6 +236,8 @@ public class ApplicationDemo extends com.itmill.toolkit.Application implements
         exp.setComponentAlignment(status, exp.ALIGNMENT_RIGHT,
                 exp.ALIGNMENT_VERTICAL_CENTER);
 
+        // select initial section ("All")
+        tree.setValue(rootId);
     }
 
     private void initItem(Item item, Object[] data) {
@@ -184,9 +259,7 @@ public class ApplicationDemo extends com.itmill.toolkit.Application implements
         c.addContainerProperty(PROPERTY_ID_CATEGORY, String.class, null);
         c.addContainerProperty(PROPERTY_ID_NAME, String.class, "");
         c.addContainerProperty(PROPERTY_ID_DESC, String.class, "");
-        c
-                .addContainerProperty(PROPERTY_ID_CLASS, Class.class,
-                        HelloWorld.class);
+        c.addContainerProperty(PROPERTY_ID_CLASS, Class.class, Button.class);
         c
                 .addContainerProperty(PROPERTY_ID_VIEWED, Boolean.class,
                         Boolean.FALSE);
@@ -198,7 +271,9 @@ public class ApplicationDemo extends com.itmill.toolkit.Application implements
             Object id = tree.getValue();
             Item item = tree.getItem(id);
             String section;
-            if (tree.hasChildren(id)) {
+            if (tree.isRoot(id)) {
+                section = ""; // show all sections
+            } else if (tree.hasChildren(id)) {
                 section = (String) item.getItemProperty(PROPERTY_ID_NAME)
                         .getValue();
             } else {
@@ -210,7 +285,11 @@ public class ApplicationDemo extends com.itmill.toolkit.Application implements
             IndexedContainer c = (IndexedContainer) table
                     .getContainerDataSource();
             c.removeAllContainerFilters();
-            c.addContainerFilter(PROPERTY_ID_CATEGORY, section, false, true);
+            if (section != null) {
+                c
+                        .addContainerFilter(PROPERTY_ID_CATEGORY, section,
+                                false, true);
+            }
             if (!tree.hasChildren(id)) {
                 table.setValue(id);
             }
@@ -220,9 +299,36 @@ public class ApplicationDemo extends com.itmill.toolkit.Application implements
                 table.removeListener(this);
                 tree.setValue(table.getValue());
                 table.addListener(this);
+                Item item = table.getItem(table.getValue());
+                Class c = (Class) item.getItemProperty(PROPERTY_ID_CLASS)
+                        .getValue();
+                Component component = getComponent(c);
+                if (component != null) {
+                    String caption = (String) item.getItemProperty(
+                            PROPERTY_ID_NAME).getValue();
+                    ts.removeAllComponents();
+                    ts.addTab(component, caption, null);
+                }
             }
         }
 
     }
 
+    private Component getComponent(Class componentClass) {
+        if (!components.containsKey(componentClass)) {
+            try {
+                Component c = (Component) componentClass.newInstance();
+                components.put(componentClass, c);
+            } catch (Exception e) {
+                return null;
+            }
+        }
+        return (Component) components.get(componentClass);
+    }
+
+    public class Dummy extends Label {
+        public Dummy() {
+            super("Dummy component");
+        }
+    }
 }
diff --git a/src/com/itmill/toolkit/demo/featurebrowser/ClientCachingExample.java b/src/com/itmill/toolkit/demo/featurebrowser/ClientCachingExample.java
new file mode 100644 (file)
index 0000000..235fe9c
--- /dev/null
@@ -0,0 +1,66 @@
+package com.itmill.toolkit.demo.featurebrowser;\r
+\r
+import com.itmill.toolkit.terminal.PaintException;\r
+import com.itmill.toolkit.terminal.PaintTarget;\r
+import com.itmill.toolkit.ui.CustomComponent;\r
+import com.itmill.toolkit.ui.Label;\r
+import com.itmill.toolkit.ui.Layout;\r
+import com.itmill.toolkit.ui.OrderedLayout;\r
+import com.itmill.toolkit.ui.TabSheet;\r
+\r
+/**\r
+ * This example is a (simple) demonstration of client-side caching. The content\r
+ * in one tab is intentionally made very slow to produce server-side. When the\r
+ * user changes to this tab for the first time, there will be a 3 second wait\r
+ * before the content shows up, but the second time it shows up immediately\r
+ * since the content has not changed and is cached client-side.\r
+ * \r
+ * @author IT Mill Ltd.\r
+ */\r
+public class ClientCachingExample extends CustomComponent {\r
+\r
+    private static final String msg = "This example is a (simple) demonstration of client-side caching."\r
+            + " The content in one tab is intentionally made very slow to"\r
+            + " 'produce' server-side. When you changes to this tab for the"\r
+            + " first time, there will be a 3 second wait before the content"\r
+            + " shows up, but the second time it shows up immediately since the"\r
+            + " content has not changed and is cached client-side.";\r
+\r
+    public ClientCachingExample() {\r
+\r
+        OrderedLayout main = new OrderedLayout();\r
+        main.setMargin(true);\r
+        setCompositionRoot(main);\r
+\r
+        main.addComponent(new Label(msg));\r
+\r
+        TabSheet ts = new TabSheet();\r
+        main.addComponent(ts);\r
+\r
+        Layout layout = new OrderedLayout();\r
+        layout.setMargin(true);\r
+        Label l = new Label("This is a normal label, quick to render.");\r
+        l.setCaption("A normal label");\r
+        layout.addComponent(l);\r
+\r
+        ts.addTab(layout, "Normal", null);\r
+\r
+        layout = new OrderedLayout();\r
+        layout.setMargin(true);\r
+        l = new Label("Slow label - until cached client side.") {\r
+            public void paintContent(PaintTarget target) throws PaintException {\r
+                try {\r
+                    Thread.sleep(3000);\r
+                } catch (Exception e) {\r
+                    // IGNORED\r
+                }\r
+                super.paintContent(target);\r
+            }\r
+\r
+        };\r
+        l.setCaption("A slow label");\r
+        layout.addComponent(l);\r
+        ts.addTab(layout, "Slow", null);\r
+\r
+    }\r
+}\r
diff --git a/src/com/itmill/toolkit/demo/featurebrowser/EmbeddedBrowserExample.java b/src/com/itmill/toolkit/demo/featurebrowser/EmbeddedBrowserExample.java
new file mode 100644 (file)
index 0000000..ebbbbce
--- /dev/null
@@ -0,0 +1,63 @@
+package com.itmill.toolkit.demo.featurebrowser;
+
+import com.itmill.toolkit.data.Property.ValueChangeEvent;
+import com.itmill.toolkit.terminal.ExternalResource;
+import com.itmill.toolkit.ui.Embedded;
+import com.itmill.toolkit.ui.ExpandLayout;
+import com.itmill.toolkit.ui.Select;
+
+/**
+ * Demonstrates the use of Embedded and "suggesting" Select by creating a simple
+ * web-browser. Note: does not check for recursion.
+ * 
+ * @author IT Mill Ltd.
+ * @see com.itmill.toolkit.ui.Window
+ */
+public class EmbeddedBrowserExample extends ExpandLayout implements
+        Select.ValueChangeListener {
+
+    // Default URL to open.
+    private static final String DEFAULT_URL = "http://www.itmill.com/index_itmill_toolkit.htm";
+
+    // The embedded page
+    Embedded emb = new Embedded();
+
+    public EmbeddedBrowserExample() {
+        setSizeFull();
+
+        // create the address combobox
+        Select select = new Select();
+        // allow input
+        select.setNewItemsAllowed(true);
+        // no empty selection
+        select.setNullSelectionAllowed(false);
+        // no 'go' -button clicking necessary
+        select.setImmediate(true);
+        // add some pre-configured URLs
+        select.addItem(DEFAULT_URL);
+        select.addItem("http://www.google.com");
+        select.addItem("http://toolkit.itmill.com/demo");
+        // add to layout
+        addComponent(select);
+        // add listener and select initial URL
+        select.addListener(this);
+        select.setValue(DEFAULT_URL);
+
+        // configure the embedded and add to layout
+        emb.setType(Embedded.TYPE_BROWSER);
+        addComponent(emb);
+        // make the embedded as large as possible
+        expand(emb);
+
+    }
+
+    public void valueChange(ValueChangeEvent event) {
+        String url = (String) event.getProperty().getValue();
+        if (url != null) {
+            // the selected url has changed, let's go there
+            emb.setSource(new ExternalResource(url));
+        }
+
+    }
+
+}
diff --git a/src/com/itmill/toolkit/demo/featurebrowser/NotificationExample.java b/src/com/itmill/toolkit/demo/featurebrowser/NotificationExample.java
new file mode 100644 (file)
index 0000000..c007df8
--- /dev/null
@@ -0,0 +1,90 @@
+package com.itmill.toolkit.demo.featurebrowser;
+
+import java.util.Date;
+
+import com.itmill.toolkit.data.Item;
+import com.itmill.toolkit.ui.AbstractSelect;
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.CustomComponent;
+import com.itmill.toolkit.ui.NativeSelect;
+import com.itmill.toolkit.ui.OrderedLayout;
+import com.itmill.toolkit.ui.RichTextArea;
+import com.itmill.toolkit.ui.TextField;
+import com.itmill.toolkit.ui.Window;
+import com.itmill.toolkit.ui.Button.ClickEvent;
+import com.itmill.toolkit.ui.Button.ClickListener;
+
+/**
+ * Demonstrates the use of Notifications.
+ * 
+ * @author IT Mill Ltd.
+ * @see com.itmill.toolkit.ui.Window
+ */
+public class NotificationExample extends CustomComponent {
+
+    // Dropdown select for notification type, using the native dropdown
+    NativeSelect type;
+    // Textfield for the notification caption
+    TextField caption;
+    // Textfield for the notification content
+    TextField message;
+
+    /**
+     * Default constructor; We're subclassing CustomComponent, so we need to
+     * choose a root component and set it as composition root.
+     */
+    public NotificationExample() {
+        // Main layout
+        OrderedLayout main = new OrderedLayout();
+        main.setMargin(true); // use theme-specific margin
+        setCompositionRoot(main);
+
+        // Create the 'type' dropdown select.
+        type = new NativeSelect("Notification type");
+        main.addComponent(type);
+        // no empty selection allowed
+        type.setNullSelectionAllowed(false);
+        // we want a different caption than the value
+        type.addContainerProperty("caption", String.class, null);
+        type.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
+        type.setItemCaptionPropertyId("caption");
+        // add some content (items) using the Container API
+        Item i = type.addItem(new Integer(
+                Window.Notification.TYPE_HUMANIZED_MESSAGE));
+        i.getItemProperty("caption").setValue("Humanized message");
+        i = type.addItem(new Integer(Window.Notification.TYPE_WARNING_MESSAGE));
+        i.getItemProperty("caption").setValue("Warning message");
+        i = type.addItem(new Integer(Window.Notification.TYPE_ERROR_MESSAGE));
+        i.getItemProperty("caption").setValue("Error message");
+        i = type
+                .addItem(new Integer(Window.Notification.TYPE_TRAY_NOTIFICATION));
+        i.getItemProperty("caption").setValue("Tray notification");
+        // set the initially selected item
+        type.setValue(new Integer(Window.Notification.TYPE_HUMANIZED_MESSAGE));
+
+        // Notification caption
+        caption = new TextField("Caption");
+        main.addComponent(caption);
+        caption.setColumns(20);
+        caption.setValue("Brown Fox!");
+
+        // Notification message
+        message = new RichTextArea();
+        main.addComponent(message);
+        message.setCaption("Message");
+        message.setValue("A quick one jumped over the lazy dog.");
+
+        // Button to show the notification
+        Button b = new Button("Show notification", new ClickListener() {
+            // this is an inline ClickListener
+            public void buttonClick(ClickEvent event) {
+                // show the notification
+                getWindow().showNotification((String) caption.getValue(),
+                        (String) message.getValue(),
+                        ((Integer) type.getValue()).intValue());
+                getWindow().setCaption(new Date().toString());
+            }
+        });
+        main.addComponent(b);
+    }
+}
diff --git a/src/com/itmill/toolkit/demo/featurebrowser/WindowingExample.java b/src/com/itmill/toolkit/demo/featurebrowser/WindowingExample.java
new file mode 100644 (file)
index 0000000..2871aef
--- /dev/null
@@ -0,0 +1,28 @@
+/**\r
+ * \r
+ */\r
+package com.itmill.toolkit.demo.featurebrowser;\r
+\r
+import com.itmill.toolkit.ui.CustomComponent;\r
+import com.itmill.toolkit.ui.OrderedLayout;\r
+\r
+/**\r
+ * @author marc\r
+ * \r
+ */\r
+public class WindowingExample extends CustomComponent {\r
+\r
+    public static final String txt = "There are two main types of windows:";\r
+\r
+    /*\r
+     * application-level windows, and\r
+     * \r
+     */\r
+\r
+    public WindowingExample() {\r
+        OrderedLayout main = new OrderedLayout();\r
+        setCompositionRoot(main);\r
+\r
+    }\r
+\r
+}\r