From: Marc Englund Date: Thu, 29 Nov 2007 09:02:35 +0000 (+0000) Subject: Pre-refactor commit. X-Git-Tag: 6.7.0.beta1~5396 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=350b6b063eb86122787ee6582d9a0bc92fde80d6;p=vaadin-framework.git Pre-refactor commit. svn changeset:3033/svn branch:trunk --- diff --git a/src/com/itmill/toolkit/demo/ApplicationDemo.java b/src/com/itmill/toolkit/demo/ApplicationDemo.java index ccfda10eca..0ac486eed2 100644 --- a/src/com/itmill/toolkit/demo/ApplicationDemo.java +++ b/src/com/itmill/toolkit/demo/ApplicationDemo.java @@ -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 index 0000000000..235fe9c466 --- /dev/null +++ b/src/com/itmill/toolkit/demo/featurebrowser/ClientCachingExample.java @@ -0,0 +1,66 @@ +package com.itmill.toolkit.demo.featurebrowser; + +import com.itmill.toolkit.terminal.PaintException; +import com.itmill.toolkit.terminal.PaintTarget; +import com.itmill.toolkit.ui.CustomComponent; +import com.itmill.toolkit.ui.Label; +import com.itmill.toolkit.ui.Layout; +import com.itmill.toolkit.ui.OrderedLayout; +import com.itmill.toolkit.ui.TabSheet; + +/** + * This example is a (simple) demonstration of client-side caching. The content + * in one tab is intentionally made very slow to produce server-side. When the + * user changes to this tab for the first time, there will be a 3 second wait + * before the content shows up, but the second time it shows up immediately + * since the content has not changed and is cached client-side. + * + * @author IT Mill Ltd. + */ +public class ClientCachingExample extends CustomComponent { + + private static final String msg = "This example is a (simple) demonstration of client-side caching." + + " The content in one tab is intentionally made very slow to" + + " 'produce' server-side. When you changes to this tab for the" + + " first time, there will be a 3 second wait before the content" + + " shows up, but the second time it shows up immediately since the" + + " content has not changed and is cached client-side."; + + public ClientCachingExample() { + + OrderedLayout main = new OrderedLayout(); + main.setMargin(true); + setCompositionRoot(main); + + main.addComponent(new Label(msg)); + + TabSheet ts = new TabSheet(); + main.addComponent(ts); + + Layout layout = new OrderedLayout(); + layout.setMargin(true); + Label l = new Label("This is a normal label, quick to render."); + l.setCaption("A normal label"); + layout.addComponent(l); + + ts.addTab(layout, "Normal", null); + + layout = new OrderedLayout(); + layout.setMargin(true); + l = new Label("Slow label - until cached client side.") { + public void paintContent(PaintTarget target) throws PaintException { + try { + Thread.sleep(3000); + } catch (Exception e) { + // IGNORED + } + super.paintContent(target); + } + + }; + l.setCaption("A slow label"); + layout.addComponent(l); + ts.addTab(layout, "Slow", null); + + } +} diff --git a/src/com/itmill/toolkit/demo/featurebrowser/EmbeddedBrowserExample.java b/src/com/itmill/toolkit/demo/featurebrowser/EmbeddedBrowserExample.java new file mode 100644 index 0000000000..ebbbbce16b --- /dev/null +++ b/src/com/itmill/toolkit/demo/featurebrowser/EmbeddedBrowserExample.java @@ -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 index 0000000000..c007df8e74 --- /dev/null +++ b/src/com/itmill/toolkit/demo/featurebrowser/NotificationExample.java @@ -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 index 0000000000..2871aef769 --- /dev/null +++ b/src/com/itmill/toolkit/demo/featurebrowser/WindowingExample.java @@ -0,0 +1,28 @@ +/** + * + */ +package com.itmill.toolkit.demo.featurebrowser; + +import com.itmill.toolkit.ui.CustomComponent; +import com.itmill.toolkit.ui.OrderedLayout; + +/** + * @author marc + * + */ +public class WindowingExample extends CustomComponent { + + public static final String txt = "There are two main types of windows:"; + + /* + * application-level windows, and + * + */ + + public WindowingExample() { + OrderedLayout main = new OrderedLayout(); + setCompositionRoot(main); + + } + +}