summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/tests
diff options
context:
space:
mode:
authorMatti Tahvonen <matti.tahvonen@itmill.com>2008-12-18 14:26:48 +0000
committerMatti Tahvonen <matti.tahvonen@itmill.com>2008-12-18 14:26:48 +0000
commit05f564ec22e0d715853d340ecef2909cf2227886 (patch)
tree7dc5205c8b90bdf65e23abdacacda2f9c62fb228 /src/com/itmill/toolkit/tests
parent2b34ce5f6b901dd801def5fc9fb345caa198db78 (diff)
downloadvaadin-framework-05f564ec22e0d715853d340ecef2909cf2227886.tar.gz
vaadin-framework-05f564ec22e0d715853d340ecef2909cf2227886.zip
moved old demos to tests package
svn changeset:6283/svn branch:trunk
Diffstat (limited to 'src/com/itmill/toolkit/tests')
-rw-r--r--src/com/itmill/toolkit/tests/CustomLayoutDemo.java139
-rw-r--r--src/com/itmill/toolkit/tests/LayoutDemo.java148
-rw-r--r--src/com/itmill/toolkit/tests/ModalWindow.java83
-rw-r--r--src/com/itmill/toolkit/tests/NativeWindowing.java128
-rw-r--r--src/com/itmill/toolkit/tests/Parameters.java123
-rw-r--r--src/com/itmill/toolkit/tests/QueryContainerDemo.java193
-rw-r--r--src/com/itmill/toolkit/tests/TreeFilesystem.java105
-rw-r--r--src/com/itmill/toolkit/tests/TreeFilesystemContainer.java103
-rw-r--r--src/com/itmill/toolkit/tests/UpgradingSample.java169
9 files changed, 1191 insertions, 0 deletions
diff --git a/src/com/itmill/toolkit/tests/CustomLayoutDemo.java b/src/com/itmill/toolkit/tests/CustomLayoutDemo.java
new file mode 100644
index 0000000000..0409a81e66
--- /dev/null
+++ b/src/com/itmill/toolkit/tests/CustomLayoutDemo.java
@@ -0,0 +1,139 @@
+/*
+@ITMillApache2LicenseForJavaFiles@
+ */
+
+package com.itmill.toolkit.tests;
+
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.CustomLayout;
+import com.itmill.toolkit.ui.Field;
+import com.itmill.toolkit.ui.Label;
+import com.itmill.toolkit.ui.Panel;
+import com.itmill.toolkit.ui.TextField;
+import com.itmill.toolkit.ui.Tree;
+import com.itmill.toolkit.ui.Window;
+import com.itmill.toolkit.ui.Component.Event;
+import com.itmill.toolkit.ui.Component.Listener;
+
+/**
+ * This example demonstrates custom layout. All components created here are
+ * placed using custom.html file. Custom layouts may be created with any web
+ * designer tool such as Dreamweaver. To place Toolkit components into html
+ * page, use divs with location tag as an identifier for Toolkit components, see
+ * html page (themes/example/layout/custom.html) and source code below. Body
+ * panel contents are changed when menu items are clicked. Contents are HTML
+ * pages located at themes/example/layout directory.
+ *
+ * @author IT Mill Ltd.
+ * @since 4.0.0
+ *
+ */
+public class CustomLayoutDemo extends com.itmill.toolkit.Application implements
+ Listener {
+
+ private CustomLayout mainLayout = null;
+
+ private final Panel bodyPanel = new Panel();
+
+ private final TextField username = new TextField("Username");
+
+ private final TextField loginPwd = new TextField("Password");
+
+ private final Button loginButton = new Button("Login", this, "loginClicked");
+
+ private final Tree menu = new Tree();
+
+ /**
+ * Initialize Application. Demo components are added to main window.
+ */
+ public void init() {
+ final Window mainWindow = new Window("CustomLayout demo");
+ setMainWindow(mainWindow);
+
+ // set the application to use example -theme
+ setTheme("example");
+
+ // Create custom layout, themes/example/layout/mainLayout.html
+ mainLayout = new CustomLayout("mainLayout");
+ // wrap custom layout inside a panel
+ final Panel customLayoutPanel = new Panel(
+ "Panel containing custom layout (mainLayout.html)");
+ customLayoutPanel.addComponent(mainLayout);
+
+ // Login components
+ loginPwd.setSecret(true);
+ mainLayout.addComponent(username, "loginUser");
+ mainLayout.addComponent(loginPwd, "loginPassword");
+ mainLayout.addComponent(loginButton, "loginButton");
+
+ // Menu component, when clicked bodyPanel is updated
+ menu.addItem("Welcome");
+ menu.addItem("Products");
+ menu.addItem("Support");
+ menu.addItem("News");
+ menu.addItem("Developers");
+ menu.addItem("Contact");
+ // "this" handles all menu events, e.g. node clicked event
+ menu.addListener(this);
+ // Value changes are immediate
+ menu.setImmediate(true);
+ menu.setNullSelectionAllowed(false);
+ mainLayout.addComponent(menu, "menu");
+
+ // Body component
+ mainLayout.addComponent(bodyPanel, "body");
+
+ // Initial body are comes from Welcome.html
+ setBody("Welcome");
+
+ // Add heading label and custom layout panel to main window
+ mainWindow.addComponent(new Label("<h3>Custom layout demo</h3>",
+ Label.CONTENT_XHTML));
+ mainWindow.addComponent(customLayoutPanel);
+ }
+
+ /**
+ * Login button clicked. Hide login components and replace username
+ * component with "Welcome user Username" message.
+ *
+ */
+ public void loginClicked() {
+ username.setVisible(false);
+ loginPwd.setVisible(false);
+ if (username.getValue().toString().length() < 1) {
+ username.setValue("Anonymous");
+ }
+ mainLayout.replaceComponent(loginButton, new Label("Welcome user <em>"
+ + username.getValue() + "</em>", Label.CONTENT_XHTML));
+ }
+
+ /**
+ * Set body panel caption, remove all existing components and add given
+ * custom layout in it.
+ *
+ */
+ public void setBody(String customLayout) {
+ bodyPanel.setCaption(customLayout + ".html");
+ bodyPanel.removeAllComponents();
+ bodyPanel.addComponent(new CustomLayout(customLayout));
+ }
+
+ /**
+ * Handle all menu events. Updates body panel contents if menu item is
+ * clicked.
+ */
+ public void componentEvent(Event event) {
+ // Check if event occured at fsTree component
+ if (event.getSource() == menu) {
+ // Check if event is about changing value
+ if (event.getClass() == Field.ValueChangeEvent.class) {
+ // Update body area with selected item
+ setBody(menu.getValue().toString());
+ }
+ // here we could check for other type of events for tree
+ // component
+ }
+ // here we could check for other component's events
+ }
+
+}
diff --git a/src/com/itmill/toolkit/tests/LayoutDemo.java b/src/com/itmill/toolkit/tests/LayoutDemo.java
new file mode 100644
index 0000000000..132c1a8548
--- /dev/null
+++ b/src/com/itmill/toolkit/tests/LayoutDemo.java
@@ -0,0 +1,148 @@
+/*
+@ITMillApache2LicenseForJavaFiles@
+ */
+
+package com.itmill.toolkit.tests;
+
+import com.itmill.toolkit.terminal.ClassResource;
+import com.itmill.toolkit.ui.Component;
+import com.itmill.toolkit.ui.Embedded;
+import com.itmill.toolkit.ui.GridLayout;
+import com.itmill.toolkit.ui.Label;
+import com.itmill.toolkit.ui.Layout;
+import com.itmill.toolkit.ui.OrderedLayout;
+import com.itmill.toolkit.ui.Panel;
+import com.itmill.toolkit.ui.TabSheet;
+import com.itmill.toolkit.ui.Window;
+
+/**
+ * This example demonstrates layouts. Layouts are populated with sample Toolkit
+ * UI components.
+ *
+ * @author IT Mill Ltd.
+ * @since 4.0.0
+ *
+ */
+public class LayoutDemo extends com.itmill.toolkit.Application {
+
+ /**
+ * Initialize Application. Demo components are added to main window.
+ */
+ public void init() {
+ final Window mainWindow = new Window("Layout demo");
+ setMainWindow(mainWindow);
+
+ //
+ // Create horizontal ordered layout
+ //
+ final OrderedLayout layoutA = new OrderedLayout(
+ OrderedLayout.ORIENTATION_HORIZONTAL);
+ // Add 4 panels
+ fillLayout(layoutA, 4);
+
+ //
+ // Create vertical ordered layout
+ //
+ final OrderedLayout layoutB = new OrderedLayout(
+ OrderedLayout.ORIENTATION_VERTICAL);
+ // Add 4 panels
+ fillLayout(layoutB, 4);
+
+ //
+ // Create grid layout
+ //
+ final GridLayout layoutG = new GridLayout(4, 4);
+ // Add 16 panels components
+ fillLayout(layoutG, 16);
+
+ //
+ // Create grid layout
+ //
+ final GridLayout layoutG2 = new GridLayout(4, 4);
+ // Add 4 panels with absolute coordinates (diagonally)
+ layoutG2.addComponent(getExampleComponent("x=0, y=0"), 0, 0);
+ layoutG2.addComponent(getExampleComponent("x=1, y=1"), 1, 1);
+ layoutG2.addComponent(getExampleComponent("x=2, y=2"), 2, 2);
+ layoutG2.addComponent(getExampleComponent("x=3, y=3"), 3, 3);
+ // Add 4 pictures with absolute coordinates (diagonally)
+ layoutG2.addComponent(getExamplePicture("x=3, y=0"), 3, 0);
+ layoutG2.addComponent(getExamplePicture("x=2, y=1"), 2, 1);
+ layoutG2.addComponent(getExamplePicture("x=1, y=2"), 1, 2);
+ layoutG2.addComponent(getExamplePicture("x=0, y=3"), 0, 3);
+
+ //
+ // Create TabSheet
+ //
+ final TabSheet tabsheet = new TabSheet();
+ tabsheet
+ .setCaption("Tabsheet, above layouts are added to this component");
+ tabsheet.addTab(layoutA, "Horizontal ordered layout", null);
+ tabsheet.addTab(layoutB, "Vertical ordered layout", null);
+ tabsheet.addTab(layoutG, "First grid layout", null);
+ tabsheet.addTab(layoutG2, "Second grid layout", null);
+
+ //
+ // Add demo layouts to main window
+ //
+ mainWindow.addComponent(new Label(
+ "<h3>Horizontal ordered layout</h3>Added four components.",
+ Label.CONTENT_XHTML));
+ mainWindow.addComponent(layoutA);
+ mainWindow.addComponent(new Label(
+ "<br /><h3>Vertical ordered layout</h3>Added four components.",
+ Label.CONTENT_XHTML));
+ mainWindow.addComponent(layoutB);
+ mainWindow.addComponent(new Label(
+ "<br /><h3>Grid Layout (4 x 4)</h3>Added 16 components.",
+ Label.CONTENT_XHTML));
+ mainWindow.addComponent(layoutG);
+ mainWindow
+ .addComponent(new Label("<br /><h3>Grid Layout (4 x 4)</h3>"
+ + "Added four panels and four embedded components "
+ + "diagonally with absolute coordinates.",
+ Label.CONTENT_XHTML));
+ mainWindow.addComponent(layoutG2);
+ mainWindow.addComponent(new Label(
+ "<br /><h3>TabSheet</h3>Added above layouts as tabs.",
+ Label.CONTENT_XHTML));
+ mainWindow.addComponent(tabsheet);
+
+ }
+
+ private Component getExamplePicture(String caption) {
+ // loads image from package com.itmill-toolkit.demo
+ final ClassResource cr = new ClassResource("m-bullet-blue.gif", this);
+ final Embedded em = new Embedded("Embedded " + caption, cr);
+ em.setWidth(170);
+ return em;
+ }
+
+ private Component getExampleComponent(String caption) {
+ final Panel panel = new Panel();
+ panel.setCaption("Panel component " + caption);
+ panel
+ .addComponent(new Label(
+ "Panel is a container for other components, by default it draws a frame around it's "
+ + "extremities and may have a caption to clarify the nature of the contained components' purpose."
+ + " Panel contains an layout where the actual contained components are added, "
+ + "this layout may be switched on the fly.",
+ Label.CONTENT_XHTML));
+ panel.setWidth(222);
+ return panel;
+ }
+
+ /**
+ * Add multiple demo component to given layout.
+ *
+ * @param layout
+ * where components are added
+ * @param numberOfComponents
+ * to add
+ */
+ private void fillLayout(Layout layout, int numberOfComponents) {
+ for (int i = 1; i <= numberOfComponents; i++) {
+ layout.addComponent(getExampleComponent(Integer.toString(i)));
+ }
+ }
+
+}
diff --git a/src/com/itmill/toolkit/tests/ModalWindow.java b/src/com/itmill/toolkit/tests/ModalWindow.java
new file mode 100644
index 0000000000..70edf029f6
--- /dev/null
+++ b/src/com/itmill/toolkit/tests/ModalWindow.java
@@ -0,0 +1,83 @@
+/*
+@ITMillApache2LicenseForJavaFiles@
+ */
+
+package com.itmill.toolkit.tests;
+
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.Label;
+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;
+
+/**
+ * Simple program that demonstrates "modal windows" that block all access other
+ * windows.
+ *
+ * @author IT Mill Ltd.
+ * @since 4.0.1
+ * @see com.itmill.toolkit.Application
+ * @see com.itmill.toolkit.ui.Window
+ * @see com.itmill.toolkit.ui.Label
+ */
+public class ModalWindow extends com.itmill.toolkit.Application implements
+ ClickListener {
+
+ private Window test;
+ private Button reopen;
+
+ public void init() {
+
+ // Create main window
+ final Window main = new Window("ModalWindow demo");
+ setMainWindow(main);
+ main.addComponent(new Label("ModalWindow demo"));
+
+ // Main window textfield
+ final TextField f = new TextField();
+ f.setTabIndex(1);
+ main.addComponent(f);
+
+ // Main window button
+ final Button b = new Button("Test Button in main window");
+ b.addListener(this);
+ b.setTabIndex(2);
+ main.addComponent(b);
+
+ reopen = new Button("Open modal subwindow");
+ reopen.addListener(this);
+ reopen.setTabIndex(3);
+ main.addComponent(reopen);
+
+ }
+
+ public void buttonClick(ClickEvent event) {
+ if (event.getButton() == reopen) {
+ openSubWindow();
+ }
+ getMainWindow().addComponent(
+ new Label("Button click: " + event.getButton().getCaption()));
+ }
+
+ private void openSubWindow() {
+ // Modal window
+ test = new Window("Modal window");
+ test.setModal(true);
+ getMainWindow().addWindow(test);
+ test.addComponent(new Label(
+ "You have to close this window before accessing others."));
+
+ // Textfield for modal window
+ final TextField f = new TextField();
+ f.setTabIndex(4);
+ test.addComponent(f);
+ f.focus();
+
+ // Modal window button
+ final Button b = new Button("Test Button in modal window");
+ b.setTabIndex(5);
+ b.addListener(this);
+ test.addComponent(b);
+ }
+}
diff --git a/src/com/itmill/toolkit/tests/NativeWindowing.java b/src/com/itmill/toolkit/tests/NativeWindowing.java
new file mode 100644
index 0000000000..04f54194b1
--- /dev/null
+++ b/src/com/itmill/toolkit/tests/NativeWindowing.java
@@ -0,0 +1,128 @@
+/*
+@ITMillApache2LicenseForJavaFiles@
+ */
+
+package com.itmill.toolkit.tests;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import com.itmill.toolkit.Application;
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.Label;
+import com.itmill.toolkit.ui.Window;
+import com.itmill.toolkit.ui.Button.ClickEvent;
+
+public class NativeWindowing extends Application {
+
+ Window main = new Window("Windowing test");
+
+ public void init() {
+
+ setMainWindow(main);
+
+ main.addComponent(new Button("Add new subwindow",
+ new Button.ClickListener() {
+ public void buttonClick(ClickEvent event) {
+ final Window w = new Window("sw "
+ + System.currentTimeMillis());
+ main.addWindow(w);
+ w.setPositionX(100);
+ w.setPositionY(100);
+ w.setWidth(200);
+ w.setHeight(200);
+
+ w.setWidth(100);
+ w.setHeight(400);
+
+ final Button closebutton = new Button("Close "
+ + w.getCaption(), new Button.ClickListener() {
+ public void buttonClick(ClickEvent event) {
+ main.removeWindow(w);
+ }
+
+ });
+ w.addComponent(closebutton);
+
+ w.addComponent(new Label(
+ "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>"
+ + "<p>Lorem ipsum dolor sit amet.</p>",
+ Label.CONTENT_XHTML));
+
+ }
+ }));
+
+ main.addComponent(new Button(
+ "Open a currently uncreated application level window",
+ new Button.ClickListener() {
+ public void buttonClick(ClickEvent event) {
+ try {
+ main
+ .open(
+ new com.itmill.toolkit.terminal.ExternalResource(
+ new URL(
+ getURL(),
+ "mainwin-"
+ + System
+ .currentTimeMillis()
+ + "/")),
+ null);
+ } catch (final MalformedURLException e) {
+ }
+ }
+ }));
+
+ main.addComponent(new Button(
+ "Commit (saves window state: size, place, scrollpos)"));
+ }
+
+ public Window getWindow(String name) {
+
+ final Window w = super.getWindow(name);
+ if (w != null) {
+ return w;
+ }
+
+ if (name != null && name.startsWith("mainwin-")) {
+ final String postfix = name.substring("mainwin-".length());
+ final Window ww = new Window("Window: " + postfix);
+ ww.setName(name);
+ ww.addComponent(new Label(
+ "This is a application-level window opened with name: "
+ + name));
+ ww.addComponent(new Button("Click me", new Button.ClickListener() {
+ int state = 0;
+
+ public void buttonClick(ClickEvent event) {
+ ww.addComponent(new Label("Button clicked " + (++state)
+ + " times"));
+ }
+ }));
+ addWindow(ww);
+ return ww;
+ }
+
+ return null;
+ }
+
+}
diff --git a/src/com/itmill/toolkit/tests/Parameters.java b/src/com/itmill/toolkit/tests/Parameters.java
new file mode 100644
index 0000000000..e0ff6759f2
--- /dev/null
+++ b/src/com/itmill/toolkit/tests/Parameters.java
@@ -0,0 +1,123 @@
+/*
+@ITMillApache2LicenseForJavaFiles@
+ */
+
+package com.itmill.toolkit.tests;
+
+import java.net.URL;
+import java.util.Iterator;
+import java.util.Map;
+
+import com.itmill.toolkit.terminal.DownloadStream;
+import com.itmill.toolkit.terminal.ExternalResource;
+import com.itmill.toolkit.terminal.ParameterHandler;
+import com.itmill.toolkit.terminal.URIHandler;
+import com.itmill.toolkit.ui.ExpandLayout;
+import com.itmill.toolkit.ui.Label;
+import com.itmill.toolkit.ui.Link;
+import com.itmill.toolkit.ui.Panel;
+import com.itmill.toolkit.ui.Table;
+import com.itmill.toolkit.ui.Window;
+
+/**
+ * This is a demonstration of how URL parameters can be recieved and handled.
+ * Parameters and URL:s can be received trough the windows by registering
+ * URIHandler and ParameterHandler classes window.
+ *
+ * @since 3.1.1
+ */
+public class Parameters extends com.itmill.toolkit.Application implements
+ URIHandler, ParameterHandler {
+
+ private final Label context = new Label();
+
+ private final Label relative = new Label();
+
+ private final Table params = new Table();
+
+ public void init() {
+ final Window main = new Window("Parameters demo");
+ setMainWindow(main);
+
+ // This class acts both as URI handler and parameter handler
+ main.addURIHandler(this);
+ main.addParameterHandler(this);
+
+ final ExpandLayout layout = new ExpandLayout();
+ final Label info = new Label("To test URI and Parameter Handlers, "
+ + "add get parameters to URL. For example try examples below: ");
+ info.setCaption("Usage info");
+ layout.addComponent(info);
+ try {
+ final URL u1 = new URL(getURL(), "test/uri?test=1&test=2");
+ final URL u2 = new URL(getURL(), "foo/bar?mary=john&count=3");
+ layout.addComponent(new Link(u1.toString(),
+ new ExternalResource(u1)));
+ layout.addComponent(new Label("Or this: "));
+ layout.addComponent(new Link(u2.toString(),
+ new ExternalResource(u2)));
+ } catch (final Exception e) {
+ System.out.println("Couldn't get hostname for this machine: "
+ + e.toString());
+ e.printStackTrace();
+ }
+
+ // URI
+ final Panel panel1 = new Panel("URI Handler");
+ context.setCaption("Last URI handler context");
+ panel1.addComponent(context);
+ relative.setCaption("Last relative URI");
+ panel1.addComponent(relative);
+ layout.addComponent(panel1);
+
+ params.addContainerProperty("Key", String.class, "");
+ params.addContainerProperty("Value", String.class, "");
+ final Panel panel2 = new Panel("Parameter Handler");
+ params.setSizeFull();
+ panel2.setLayout(new ExpandLayout());
+ panel2.getLayout().setMargin(true);
+
+ params.setColumnHeaderMode(Table.COLUMN_HEADER_MODE_ID);
+ panel2.addComponent(params);
+ layout.addComponent(panel2);
+
+ // expand parameter panel and its table
+ layout.expand(panel2);
+
+ layout.setMargin(true);
+ layout.setSpacing(true);
+
+ main.setLayout(layout);
+ }
+
+ /**
+ * Update URI
+ *
+ * @see com.itmill.toolkit.terminal.URIHandler#handleURI(URL, String)
+ */
+ public DownloadStream handleURI(URL context, String relativeUri) {
+ this.context.setValue(context.toString());
+ relative.setValue(relativeUri);
+ return null;
+ }
+
+ /**
+ * Handles GET parameters, in this demo GET parameteres are used to
+ * communicate with EmbeddedToolkit.jsp
+ */
+ public void handleParameters(Map parameters) {
+ params.removeAllItems();
+ for (final Iterator i = parameters.keySet().iterator(); i.hasNext();) {
+ final String name = (String) i.next();
+ final String[] values = (String[]) parameters.get(name);
+ String v = "";
+ for (int j = 0; j < values.length; j++) {
+ if (v.length() > 0) {
+ v += ", ";
+ }
+ v += "'" + values[j] + "'";
+ }
+ params.addItem(new Object[] { name, v }, name);
+ }
+ }
+}
diff --git a/src/com/itmill/toolkit/tests/QueryContainerDemo.java b/src/com/itmill/toolkit/tests/QueryContainerDemo.java
new file mode 100644
index 0000000000..1ace2988f8
--- /dev/null
+++ b/src/com/itmill/toolkit/tests/QueryContainerDemo.java
@@ -0,0 +1,193 @@
+/*
+@ITMillApache2LicenseForJavaFiles@
+ */
+
+package com.itmill.toolkit.tests;
+
+import java.sql.SQLException;
+
+import com.itmill.toolkit.data.util.QueryContainer;
+import com.itmill.toolkit.demo.util.SampleDatabase;
+import com.itmill.toolkit.event.Action;
+import com.itmill.toolkit.ui.Label;
+import com.itmill.toolkit.ui.Select;
+import com.itmill.toolkit.ui.Table;
+import com.itmill.toolkit.ui.Tree;
+import com.itmill.toolkit.ui.Window;
+
+/**
+ * This example shows how Table, Select and Tree UI components can use
+ * Containers. QueryContainer is used to bind SQL table rows into Toolkit UI
+ * components. Table has few example actions added. Also embedding XHTML through
+ * Label components is used. Demonstrates: how to create
+ * <code>com.itmill.toolkit.data.Container</code> and set it as datasource to UI
+ * components <code>com.itmill.toolkit.ui.Component.Tree</code>, how to receive
+ * ExpandEvent and implement
+ * <code>com.itmill.toolkit.ui.Tree.ExpandListener</code>, how to use
+ * <code>com.itmill.toolkit.event.Action</code>.
+ *
+ * @author IT Mill Ltd.
+ * @since 4.0.0
+ *
+ */
+public class QueryContainerDemo extends com.itmill.toolkit.Application
+ implements Action.Handler {
+
+ private static final String ACTION_DESCRIPTION = "Try right mouse button to initiate "
+ + "actions menu.<br />Note: on Opera you use meta key "
+ + "and left mouse button.";
+
+ private static final String TABLE_CAPTION = SampleDatabase.ROWCOUNT
+ + " dynamically loaded rows from example SQL table";
+
+ // Table component where SQL rows are attached (using QueryContainer)
+ private final Table table = new Table();
+
+ private final Label tableLastAction = new Label(
+ "No action selected for table.");
+
+ // Select component where SQL rows are attached (using QueryContainer)
+ private final Select select = new Select();
+
+ // Tree component that uses select as datasource
+ private final Tree tree = new Tree();
+
+ private final Label treeLastAction = new Label(
+ "No action selected for tree.");
+
+ // Database provided with sample data
+ private SampleDatabase sampleDatabase;
+
+ // Example Actions for table
+ private final Action ACTION1 = new Action("Upload");
+
+ private final Action ACTION2 = new Action("Download");
+
+ private final Action ACTION3 = new Action("Show history");
+
+ private final Action[] actions = new Action[] { ACTION1, ACTION2, ACTION3 };
+
+ /**
+ * Initialize Application. Demo components are added to main window.
+ */
+ public void init() {
+ final Window main = new Window("QueryContainer demo");
+ setMainWindow(main);
+
+ // Main window contains heading, table, select and tree
+ main
+ .addComponent(new Label(
+ "<h2>QueryContainer demo</h2>"
+ + "<b>Rows are loaded from the server as they are needed.<br />"
+ + "Try scrolling the table to see it in action.</b><br />"
+ + ACTION_DESCRIPTION, Label.CONTENT_XHTML));
+ main.addComponent(table);
+ main.addComponent(tableLastAction);
+ main.addComponent(new Label("<hr />", Label.CONTENT_XHTML));
+ main.addComponent(select);
+ main.addComponent(new Label("<hr />", Label.CONTENT_XHTML));
+ main.addComponent(tree);
+ main.addComponent(treeLastAction);
+
+ // create demo database
+ sampleDatabase = new SampleDatabase();
+
+ // initialize demo components
+ initTable();
+ initSelect();
+ initTree();
+ }
+
+ /**
+ * Populates table component with all rows from employee table.
+ *
+ */
+ private void initTable() {
+ // init table
+ table.setCaption(TABLE_CAPTION);
+ table.setPageLength(10);
+ table.setSelectable(true);
+ table.setRowHeaderMode(Table.ROW_HEADER_MODE_INDEX);
+ table.setColumnCollapsingAllowed(true);
+ table.setColumnReorderingAllowed(true);
+ table.setSelectable(true);
+ // this class handles table actions (see handleActions method below)
+ table.addActionHandler(this);
+ table.setDescription(ACTION_DESCRIPTION);
+
+ // populate Toolkit table component with test SQL table rows
+ try {
+ final QueryContainer qc = new QueryContainer(
+ "SELECT * FROM employee", sampleDatabase.getConnection());
+ table.setContainerDataSource(qc);
+ } catch (final SQLException e) {
+ e.printStackTrace();
+ }
+ // define which columns should be visible on Table component
+ table.setVisibleColumns(new Object[] { "FIRSTNAME", "LASTNAME",
+ "TITLE", "UNIT" });
+ table.setItemCaptionPropertyId("ID");
+ }
+
+ /**
+ * Populates select component with distinct unit values from employee table.
+ *
+ */
+ private void initSelect() {
+ // init select
+ select.setCaption("All distinct units from employee table.");
+ select.setItemCaptionPropertyId("UNIT");
+
+ // populate Toolkit select component with test SQL table rows
+ try {
+ final QueryContainer qc = new QueryContainer(
+ "SELECT DISTINCT UNIT FROM employee", sampleDatabase
+ .getConnection());
+ select.setContainerDataSource(qc);
+ } catch (final SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Populates tree component using select component as data source for root
+ * nodes, child nodes are queried from database. Implementation is done for
+ * example purposes only.
+ *
+ */
+ private void initTree() {
+ // init tree
+ tree.setCaption("All distinct units from employee table.");
+ tree.setItemCaptionPropertyId("UNIT");
+ tree.setSelectable(true);
+ // this class handles tree actions (see handleActions method below)
+ tree.addActionHandler(this);
+ tree.setDescription("Try right mouse button to initiate "
+ + "actions menu. Note: on Opera you use meta key "
+ + "and left mouse button.");
+
+ // Populate Toolkit Tree using select component as data source
+ tree.setContainerDataSource(select.getContainerDataSource());
+ }
+
+ /**
+ * Return example actions
+ */
+ public Action[] getActions(Object target, Object sender) {
+ return actions;
+ }
+
+ /**
+ * Executed by right mouse button on table or tree component.
+ */
+ public void handleAction(Action action, Object sender, Object target) {
+ if (sender == table) {
+ tableLastAction.setValue("Last action clicked was '"
+ + action.getCaption() + "' on item " + target);
+ } else if (sender == tree) {
+ treeLastAction.setValue("Last action clicked was '"
+ + action.getCaption() + "' on item " + target);
+ }
+ }
+
+}
diff --git a/src/com/itmill/toolkit/tests/TreeFilesystem.java b/src/com/itmill/toolkit/tests/TreeFilesystem.java
new file mode 100644
index 0000000000..e22634ef91
--- /dev/null
+++ b/src/com/itmill/toolkit/tests/TreeFilesystem.java
@@ -0,0 +1,105 @@
+/*
+@ITMillApache2LicenseForJavaFiles@
+ */
+
+package com.itmill.toolkit.tests;
+
+import java.io.File;
+
+import com.itmill.toolkit.data.Item;
+import com.itmill.toolkit.demo.util.SampleDirectory;
+import com.itmill.toolkit.ui.Label;
+import com.itmill.toolkit.ui.Panel;
+import com.itmill.toolkit.ui.Tree;
+import com.itmill.toolkit.ui.Window;
+import com.itmill.toolkit.ui.Tree.ExpandEvent;
+
+/**
+ * Browsable file explorer using Toolkit Tree component. Demonstrates: how to
+ * add items hierarchially into
+ * <code>com.itmill.toolkit.ui.Component.Tree</code>, how to receive ExpandEvent
+ * and implement <code>com.itmill.toolkit.ui.Tree.ExpandListener</code>.
+ *
+ * @since 4.0.0
+ *
+ */
+public class TreeFilesystem extends com.itmill.toolkit.Application implements
+ Tree.ExpandListener {
+
+ // Filesystem explorer panel and it's components
+ private final Panel explorerPanel = new Panel("Filesystem explorer");
+
+ private final Tree tree = new Tree();
+
+ public void init() {
+ final Window main = new Window("Tree filesystem demo");
+ setMainWindow(main);
+
+ // Main window contains heading and panel
+ main.addComponent(new Label("<h2>Tree demo</h2>", Label.CONTENT_XHTML));
+
+ // configure file structure panel
+ main.addComponent(explorerPanel);
+ explorerPanel.addComponent(tree);
+ explorerPanel.setHeight(400);
+
+ // "this" handles tree's expand event
+ tree.addListener(this);
+
+ // Get sample directory
+ final File sampleDir = SampleDirectory.getDirectory(this);
+ // populate tree's root node with example directory
+ if (sampleDir != null) {
+ populateNode(sampleDir.getAbsolutePath(), null);
+ }
+ }
+
+ /**
+ * Handle tree expand event, populate expanded node's childs with new files
+ * and directories.
+ */
+ public void nodeExpand(ExpandEvent event) {
+ final Item i = tree.getItem(event.getItemId());
+ if (!tree.hasChildren(i)) {
+ // populate tree's node which was expanded
+ populateNode(event.getItemId().toString(), event.getItemId());
+ }
+ }
+
+ /**
+ * Populates files to tree as items. In this example items are of String
+ * type that consist of file path. New items are added to tree and item's
+ * parent and children properties are updated.
+ *
+ * @param file
+ * path which contents are added to tree
+ * @param parent
+ * for added nodes, if null then new nodes are added to root node
+ */
+ private void populateNode(String file, Object parent) {
+ final File subdir = new File(file);
+ final File[] files = subdir.listFiles();
+ for (int x = 0; x < files.length; x++) {
+ try {
+ // add new item (String) to tree
+ final String path = files[x].getCanonicalPath().toString();
+ tree.addItem(path);
+ // set parent if this item has one
+ if (parent != null) {
+ tree.setParent(path, parent);
+ }
+ // check if item is a directory and read access exists
+ if (files[x].isDirectory() && files[x].canRead()) {
+ // yes, childrens therefore exists
+ tree.setChildrenAllowed(path, true);
+ } else {
+ // no, childrens therefore do not exists
+ tree.setChildrenAllowed(path, false);
+ }
+ } catch (final Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+}
diff --git a/src/com/itmill/toolkit/tests/TreeFilesystemContainer.java b/src/com/itmill/toolkit/tests/TreeFilesystemContainer.java
new file mode 100644
index 0000000000..8a0e364d5e
--- /dev/null
+++ b/src/com/itmill/toolkit/tests/TreeFilesystemContainer.java
@@ -0,0 +1,103 @@
+/*
+@ITMillApache2LicenseForJavaFiles@
+ */
+
+package com.itmill.toolkit.tests;
+
+import java.io.File;
+
+import com.itmill.toolkit.data.util.FilesystemContainer;
+import com.itmill.toolkit.data.util.FilesystemContainer.FileItem;
+import com.itmill.toolkit.demo.util.SampleDirectory;
+import com.itmill.toolkit.ui.ExpandLayout;
+import com.itmill.toolkit.ui.Field;
+import com.itmill.toolkit.ui.Label;
+import com.itmill.toolkit.ui.Panel;
+import com.itmill.toolkit.ui.Tree;
+import com.itmill.toolkit.ui.Window;
+import com.itmill.toolkit.ui.Component.Event;
+import com.itmill.toolkit.ui.Component.Listener;
+
+/**
+ * Browsable file explorer using Toolkit Tree component. Demonstrates: how to
+ * use <code>com.itmill.toolkit.ui.Component.Tree</code> datasource container,
+ * how to create <code>com.itmill.toolkit.data.util.FilesystemContainer</code>,
+ * how to read <code>com.itmill.toolkit.ui.Component.Event</code> objects, how
+ * to receive and handle any event by implementing
+ * <code>com.itmill.toolkit.ui.Component.Listener</code>.
+ *
+ * @since 4.0.0
+ *
+ */
+public class TreeFilesystemContainer extends com.itmill.toolkit.Application
+ implements Listener {
+
+ // Filesystem explorer panel and it's components
+ private final Panel explorerPanel = new Panel("Filesystem explorer");
+
+ private final Tree filesystem = new Tree();
+
+ // File properties panel and it's components
+ private final Panel propertyPanel = new Panel("File properties");
+
+ private final Label fileProperties = new Label();
+
+ public void init() {
+ final Window w = new Window("Tree FilesystemContainer demo");
+ setMainWindow(w);
+ final ExpandLayout main = new ExpandLayout();
+ w.setLayout(main);
+ main.setMargin(true);
+ main.setSpacing(true);
+
+ propertyPanel.setHeight(120);
+ main.addComponent(propertyPanel);
+ explorerPanel.setHeight(100);
+ explorerPanel.setHeightUnits(Panel.UNITS_PERCENTAGE);
+ main.addComponent(explorerPanel);
+ main.expand(explorerPanel);
+
+ // Explorer panel contains tree
+ explorerPanel.addComponent(filesystem);
+
+ // Property panel contains label
+ propertyPanel.addComponent(fileProperties);
+ fileProperties.setCaption("No file selected.");
+ propertyPanel.setEnabled(false);
+
+ // Get sample directory
+ final File sampleDir = SampleDirectory.getDirectory(this);
+ // Populate tree with FilesystemContainer
+ final FilesystemContainer fsc = new FilesystemContainer(sampleDir, true);
+ filesystem.setContainerDataSource(fsc);
+ // "this" handles all filesystem events
+ // e.g. node clicked, expanded etc.
+ filesystem.addListener(this);
+ // Value changes are immediate
+ filesystem.setImmediate(true);
+ }
+
+ /**
+ * Listener for any component events. This class has been registered as an
+ * listener for component fsTree.
+ */
+ public void componentEvent(Event event) {
+ // Check if event occured at fsTree component
+ if (event.getSource() == filesystem) {
+ // Check if event is about changing value
+ if (event.getClass() == Field.ValueChangeEvent.class) {
+ // Update property panel contents
+ final FileItem fileItem = (FileItem) filesystem
+ .getItem(filesystem.getValue());
+ fileProperties.setIcon(fileItem.getIcon());
+ fileProperties.setCaption(fileItem.getName() + ", size "
+ + fileItem.getSize() + " bytes.");
+ propertyPanel.setEnabled(true);
+ }
+ // here we could check for other type of events for filesystem
+ // component
+ }
+ // here we could check for other component's events
+ }
+
+}
diff --git a/src/com/itmill/toolkit/tests/UpgradingSample.java b/src/com/itmill/toolkit/tests/UpgradingSample.java
new file mode 100644
index 0000000000..50f6e177a9
--- /dev/null
+++ b/src/com/itmill/toolkit/tests/UpgradingSample.java
@@ -0,0 +1,169 @@
+/*
+@ITMillApache2LicenseForJavaFiles@
+ */
+
+package com.itmill.toolkit.tests;
+
+//
+// Millstone imports were replaced
+//
+// import org.millstone.base.Application;
+// import org.millstone.base.ui.*;
+// import org.millstone.base.data.*;
+//
+import com.itmill.toolkit.Application;
+import com.itmill.toolkit.data.Property;
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.CustomComponent;
+import com.itmill.toolkit.ui.GridLayout;
+import com.itmill.toolkit.ui.Label;
+import com.itmill.toolkit.ui.OrderedLayout;
+import com.itmill.toolkit.ui.Panel;
+import com.itmill.toolkit.ui.TextField;
+import com.itmill.toolkit.ui.Tree;
+import com.itmill.toolkit.ui.Window;
+
+/**
+ * <p>
+ * Example application demonstrating simple user login. This example is from
+ * MillStone 3.1.1 examples section. Upgrading from 3.1.1 to 4.0.0 was done by
+ * updating imports, also setTheme("corporate") call was added to application
+ * init method.
+ * </p>
+ *
+ * @since 3.1.1
+ * @author IT Mill Ltd.
+ */
+public class UpgradingSample extends Application implements
+ Property.ValueChangeListener {
+
+ /* Menu for navigating inside the application. */
+ private final Tree menu = new Tree();
+
+ /* Contents of the website */
+ private final String[][] pages = {
+ { "Welcome", "Welcome to our website..." },
+ { "Products", "Public product information." },
+ { "Contact", "Public contact information." },
+ { "CRM", "CRM Database requiring login." },
+ { "Intranet", "Internal information database." } };
+
+ /* Application layout */
+ private final GridLayout layout = new GridLayout(2, 1);
+
+ /* Initialize the application */
+ public void init() {
+
+ // Create the main window of the application
+ final Window main = new Window("Login example", layout);
+ setMainWindow(main);
+
+ // Add menu and loginbox to the application
+ final OrderedLayout l = new OrderedLayout();
+ layout.addComponent(l, 0, 0);
+ l.addComponent(menu);
+ l.addComponent(new LoginBox());
+
+ // Setup menu
+ menu.setStyleName("menu");
+ menu.addListener(this);
+ menu.setImmediate(true);
+ addToMenu(new String[] { "Welcome", "Products", "Contact" });
+ }
+
+ // Overriding usetUser method is a simple way of updating application
+ // privileges when the user is changed
+ public void setUser(Object user) {
+ super.setUser(user);
+ if (user != null) {
+ addToMenu(new String[] { "CRM", "Intranet" });
+ }
+ }
+
+ public void addToMenu(String[] items) {
+ for (int i = 0; i < items.length; i++) {
+ menu.addItem(items[i]);
+ menu.setChildrenAllowed(items[i], false);
+ }
+ if (menu.getValue() == null) {
+ menu.setValue(items[0]);
+ }
+ }
+
+ // Handle menu selection and update visible page
+ public void valueChange(Property.ValueChangeEvent event) {
+ layout.removeComponent(1, 0);
+ final String title = (String) menu.getValue();
+ for (int i = 0; i < pages.length; i++) {
+ if (pages[i][0].equals(title)) {
+ final Panel p = new Panel(pages[i][0]);
+ p.addComponent(new Label(pages[i][1]));
+ p.setStyleName("strong");
+ layout.addComponent(p, 1, 0);
+ }
+ }
+ }
+
+ // Simple loginbox component for the application
+ public class LoginBox extends CustomComponent implements
+ Application.UserChangeListener {
+
+ // The components this loginbox is composed of
+ private final TextField loginName = new TextField("Name");
+
+ private final Button loginButton = new Button("Enter", this, "login");
+
+ private final Panel loginPanel = new Panel("Login");
+
+ private final Panel statusPanel = new Panel();
+
+ private final Button logoutButton = new Button("Logout",
+ UpgradingSample.this, "close");
+
+ private final Label statusLabel = new Label();
+
+ // Initialize login component
+ public LoginBox() {
+
+ // Initialize the component
+ loginPanel.addComponent(loginName);
+ loginPanel.addComponent(loginButton);
+ loginPanel.setStyleName("strong");
+ loginName.setColumns(8);
+ statusPanel.addComponent(statusLabel);
+ statusPanel.addComponent(logoutButton);
+
+ // Set the status of the loginbox and show correct
+ // components
+ updateStatus();
+
+ // Listen application user change events
+ UpgradingSample.this.addListener(this);
+ }
+
+ // Login into application
+ public void login() {
+ final String name = (String) loginName.getValue();
+ if (name != null && name.length() > 0) {
+ setUser(name);
+ }
+ loginName.setValue("");
+ }
+
+ // Update login status on application user change events
+ public void applicationUserChanged(Application.UserChangeEvent event) {
+ updateStatus();
+ }
+
+ // Update login status of the component by exposing correct
+ // components
+ private void updateStatus() {
+ statusLabel.setValue("User: " + getUser());
+ if (getUser() != null) {
+ setCompositionRoot(statusPanel);
+ } else {
+ setCompositionRoot(loginPanel);
+ }
+ }
+ }
+}