From: Jani Laakso Date: Mon, 2 Apr 2007 14:27:33 +0000 (+0000) Subject: Added new demos. X-Git-Tag: 6.7.0.beta1~6489 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7397520086aec1cff86d6da93577c9c8d68aeb3f;p=vaadin-framework.git Added new demos. svn changeset:1097/svn branch:trunk --- diff --git a/src/com/itmill/toolkit/demo/Calc.java b/src/com/itmill/toolkit/demo/Calc.java index fb6f7272d5..50a03d7479 100644 --- a/src/com/itmill/toolkit/demo/Calc.java +++ b/src/com/itmill/toolkit/demo/Calc.java @@ -56,7 +56,9 @@ public class Calc // Create the buttons and place them in the grid for (int i = 0; i < captions.length; i++) { - layout.addComponent(new Button(captions[i], this)); + Button button = new Button(captions[i], this); + button.setStaticPid("_Button"+captions[i]); + layout.addComponent(button); } // Create the main window with a caption and add it to the application. diff --git a/src/com/itmill/toolkit/demo/Login.java b/src/com/itmill/toolkit/demo/Login.java new file mode 100644 index 0000000000..32dac25c82 --- /dev/null +++ b/src/com/itmill/toolkit/demo/Login.java @@ -0,0 +1,143 @@ +package com.itmill.toolkit.demo; + +import com.itmill.toolkit.Application; +import com.itmill.toolkit.data.Property; +import com.itmill.toolkit.ui.*; + +/** + *

+ * 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. + *

+ * + * @since 3.1.1 + * @author IT Mill Ltd. + */ +public class Login extends Application implements Property.ValueChangeListener { + + /* Menu for navigating inside the application. */ + private Tree menu = new Tree(); + + /* Contents of the website */ + private 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 GridLayout layout = new GridLayout(2, 1); + + /* Initialize the application */ + public void init() { + + // set the application to use Corporate -theme + setTheme("corporate"); + + // Create the main window of the application + Window main = new Window("Login example", layout); + setMainWindow(main); + + // Add menu and loginbox to the application + OrderedLayout l = new OrderedLayout(); + layout.addComponent(l, 0, 0); + l.addComponent(menu); + l.addComponent(new LoginBox()); + + // Setup menu + menu.setStyle("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); + String title = (String) menu.getValue(); + for (int i = 0; i < pages.length; i++) + if (pages[i][0].equals(title)) { + Panel p = new Panel(pages[i][0]); + p.addComponent(new Label(pages[i][1])); + p.setStyle("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 TextField loginName = new TextField("Name"); + + private Button loginButton = new Button("Enter", this, "login"); + + private Panel loginPanel = new Panel("Login"); + + private Panel statusPanel = new Panel(); + + private Button logoutButton = new Button("Logout", Login.this, "close"); + + private Label statusLabel = new Label(); + + // Initialize login component + public LoginBox() { + + // Initialize the component + loginPanel.addComponent(loginName); + loginPanel.addComponent(loginButton); + loginPanel.setStyle("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 + Login.this.addListener(this); + } + + // Login into application + public void login() { + 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); + } + } +} diff --git a/src/com/itmill/toolkit/demo/ModalWindow.java b/src/com/itmill/toolkit/demo/ModalWindow.java new file mode 100644 index 0000000000..0906dd2f6b --- /dev/null +++ b/src/com/itmill/toolkit/demo/ModalWindow.java @@ -0,0 +1,82 @@ +package com.itmill.toolkit.demo; + +import com.itmill.toolkit.event.Action; +import com.itmill.toolkit.ui.*; +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 + Action.Handler, ClickListener { + + private Window test; + + public void init() { + + // set the application to use Corporate -theme + setTheme("corporate"); + + // Create main window + Window main = new Window("ModalWindow demo"); + setMainWindow(main); + main.addComponent(new Label("ModalWindow demo")); + + // Main window textfield + TextField f = new TextField(); + f.setTabIndex(1); + main.addComponent(f); + + // Main window button + Button b = new Button("Button on main window"); + b.addListener(this); + b.setTabIndex(2); + main.addComponent(b); + + // Modal window + test = new Window("Modal window"); + test.setStyle("modal"); + this.addWindow(test); + test.addComponent(new Label( + "You have to close this window before accessing others.")); + + // Textfield for modal window + f = new TextField(); + f.setTabIndex(4); + test.addComponent(f); + f.focus(); + + // Modal window button + b = new Button("Button on modal window"); + b.setTabIndex(3); + b.addListener(this); + test.addComponent(b); + + } + + public Action[] getActions(Object target, Object sender) { + Action actionA = new Action("Action A for " + target.toString()); + Action actionB = new Action("Action B for " + target.toString()); + Action[] actions = new Action[] { actionA, actionB }; + return actions; + } + + public void handleAction(Action action, Object sender, Object target) { + this.test.addComponent(new Label(action.getCaption() + " clicked on " + + target)); + + } + + public void buttonClick(ClickEvent event) { + this.test.addComponent(new Label("Clicked " + event)); + + } +} diff --git a/src/com/itmill/toolkit/demo/Parameters.java b/src/com/itmill/toolkit/demo/Parameters.java new file mode 100644 index 0000000000..1c6b66457a --- /dev/null +++ b/src/com/itmill/toolkit/demo/Parameters.java @@ -0,0 +1,111 @@ +package com.itmill.toolkit.demo; + +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.*; + +/** + * 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 Label context = new Label(); + + private Label relative = new Label(); + + private Table params = new Table(); + + public void init() { + Window main = new Window("Table demo"); + setMainWindow(main); + + // set the application to use Corporate -theme + setTheme("corporate"); + + // This class acts both as URI handler and parameter handler + main.addURIHandler(this); + main.addParameterHandler(this); + + OrderedLayout layout = new OrderedLayout(); + 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 { + URL u1 = new URL(getURL(), "test/uri?test=1&test=2"); + 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 (Exception e) { + System.out.println("Couldn't get hostname for this machine: " + + e.toString()); + e.printStackTrace(); + } + + // URI + 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); + + // Parameters + params.addContainerProperty("Values", String.class, ""); + Panel panel2 = new Panel("Parameter Handler"); + params.setCaption("Last parameters"); + params.setColumnHeaderMode(Table.COLUMN_HEADER_MODE_ID); + params.setRowHeaderMode(Table.ROW_HEADER_MODE_ID); + panel2.addComponent(params); + layout.addComponent(panel2); + + main.addComponent(layout); + } + + /** + * Update URI + * + * @see com.itmill.toolkit.terminal.URIHandler#handleURI(URL, String) + */ + public DownloadStream handleURI(URL context, String relativeUri) { + this.context.setValue(context.toString()); + this.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) { + // disabled, see bug #550 + //params.removeAllItems(); + System.out.println("handleParameters()"); + for (Iterator i = parameters.keySet().iterator(); i.hasNext();) { + String name = (String) i.next(); + 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[] { v }, name); + System.out.println("parameter name="+name+", value="+v); + } + } +} diff --git a/src/com/itmill/toolkit/demo/QueryContainerDemo.java b/src/com/itmill/toolkit/demo/QueryContainerDemo.java new file mode 100644 index 0000000000..91026e33ac --- /dev/null +++ b/src/com/itmill/toolkit/demo/QueryContainerDemo.java @@ -0,0 +1,182 @@ +package com.itmill.toolkit.demo; + +import java.sql.SQLException; +import com.itmill.toolkit.data.util.QueryContainer; +import com.itmill.toolkit.event.Action; +import com.itmill.toolkit.demo.util.SampleDatabase; +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 + * com.itmill.toolkit.data.Container and set it as datasource to + * UI components com.itmill.toolkit.ui.Component.Tree, how to + * receive ExpandEvent and implement + * com.itmill.toolkit.ui.Tree.ExpandListener, how to use + * com.itmill.toolkit.event.Action. + * + * @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.
Note: on Opera you use meta key " + + "and left mouse button."; + + // Table component where SQL rows are attached (using QueryContainer) + private Table table = new Table(); + + private Label tableLastAction = new Label("No action selected for table."); + + // Select component where SQL rows are attached (using QueryContainer) + private Select select = new Select(); + + // Tree component that uses select as datasource + private Tree tree = new Tree(); + + private Label treeLastAction = new Label("No action selected for tree."); + + // Database provided with sample data + private SampleDatabase sampleDatabase; + + // Example Actions for table + private Action ACTION1 = new Action("Upload"); + + private Action ACTION2 = new Action("Download"); + + private Action ACTION3 = new Action("Show history"); + + private Action[] actions = new Action[] { ACTION1, ACTION2, ACTION3 }; + + /** + * Initialize Application. Demo components are added to main window. + */ + public void init() { + Window main = new Window("QueryContainer demo"); + setMainWindow(main); + + // set the application to use Corporate -theme + setTheme("corporate"); + + // Main window contains heading, table, select and tree + main.addComponent(new Label("

QueryContainer demo

" + + ACTION_DESCRIPTION, Label.CONTENT_XHTML)); + main.addComponent(table); + main.addComponent(tableLastAction); + main.addComponent(new Label("
", Label.CONTENT_XHTML)); + main.addComponent(select); + main.addComponent(new Label("
", 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("All rows from employee table"); + 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 { + QueryContainer qc = new QueryContainer("SELECT * FROM employee", + sampleDatabase.getConnection()); + table.setContainerDataSource(qc); + } catch (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 { + QueryContainer qc = new QueryContainer( + "SELECT DISTINCT UNIT FROM employee", sampleDatabase + .getConnection()); + select.setContainerDataSource(qc); + } catch (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/demo/Shortcut.java b/src/com/itmill/toolkit/demo/Shortcut.java new file mode 100644 index 0000000000..97f25ce549 --- /dev/null +++ b/src/com/itmill/toolkit/demo/Shortcut.java @@ -0,0 +1,131 @@ +package com.itmill.toolkit.demo; + +import com.itmill.toolkit.event.Action; +import com.itmill.toolkit.event.ShortcutAction; +import com.itmill.toolkit.event.Action.Handler; +import com.itmill.toolkit.ui.*; + +public class Shortcut extends com.itmill.toolkit.Application implements Handler { + Window main; + + Button a; + + Button b; + + Button c; + + Button close; + + Button d; + + private AbstractField f; + + public void init() { + + /* + * - Create new window for the application - Give the window a visible + * title - Set the window to be the main window of the application + */ + main = new Window("Hello window"); + setMainWindow(main); + + // set the application to use Corporate -theme + setTheme("corporate"); + + /* + * - Create a label with the classic text - Add the label to the main + * window + */ + main + .addComponent(new Label( + "This is a test program for shortcut actions
" + + "Note: if events do not work, set focus to Textfield first!", + Label.CONTENT_XHTML)); + main + .addComponent(new Label( + "ESC restarts program, alt-A hits A button, ctrl-B hits B button, ctrl-shift-C hits C")); + + // Restart button + close = new Button("restart", this, "close"); + close.addActionHandler(this); + main.addComponent(close); + + a = new Button("Button A", this, "buttonAHandler"); + a.addActionHandler(this); + + b = new Button("Button B", this, "buttonBHandler"); + b.addActionHandler(this); + + c = new Button("Button C", this, "buttonCHandler"); + c.addActionHandler(this); + + f = new TextField("Textfield"); + + main.addComponent(a); + main.addComponent(b); + main.addComponent(c); + main.addComponent(f); + + d = new Button("Click to focus button B", this, "setFocusB"); + main.addComponent(d); + d = new Button("Click to focus Textfield", this, "setFocusF"); + main.addComponent(d); + f.focus(); + } + + public void setFocusB() { + b.focus(); + } + + public void setFocusF() { + f.focus(); + } + + public Action[] getActions(Object target, Object sender) { + Action[] actions = new Action[1]; + if (sender == b) { + actions[0] = (Action) (new ShortcutAction("Button b action", + ShortcutAction.KeyCode.B, + new int[] { ShortcutAction.ModifierKey.CTRL })); + + } else if (sender == c) { + actions[0] = (Action) new ShortcutAction("Button c action", + ShortcutAction.KeyCode.C, new int[] { + ShortcutAction.ModifierKey.CTRL, + ShortcutAction.ModifierKey.SHIFT }); + } else if (sender == a) { + actions[0] = (Action) new ShortcutAction("Button a action", + ShortcutAction.KeyCode.A, + new int[] { ShortcutAction.ModifierKey.ALT }); + } else { + // restart button + actions[0] = new ShortcutAction("Restart ", + ShortcutAction.KeyCode.ESCAPE, null); + } + return actions; + } + + public void handleAction(Action action, Object sender, Object target) { + main.addComponent(new Label("ShortcutAction fired" + action)); + if (target == a) + this.buttonAHandler(); + if (target == b) + this.buttonBHandler(); + if (target == c) + this.buttonCHandler(); + if (target == close) + this.close(); + } + + public void buttonBHandler() { + main.addComponent(new Label("Button B handler fired")); + } + + public void buttonCHandler() { + main.addComponent(new Label("Button C handler fired")); + } + + public void buttonAHandler() { + main.addComponent(new Label("Button A handler fired")); + } +} diff --git a/src/com/itmill/toolkit/demo/TableDemo.java b/src/com/itmill/toolkit/demo/TableDemo.java new file mode 100644 index 0000000000..4a3ebcbc2c --- /dev/null +++ b/src/com/itmill/toolkit/demo/TableDemo.java @@ -0,0 +1,193 @@ +package com.itmill.toolkit.demo; + +import java.sql.SQLException; + +import com.itmill.toolkit.data.util.QueryContainer; +import com.itmill.toolkit.event.Action; +import com.itmill.toolkit.demo.util.SampleDatabase; +import com.itmill.toolkit.terminal.ExternalResource; +import com.itmill.toolkit.ui.Button; +import com.itmill.toolkit.ui.Label; +import com.itmill.toolkit.ui.Link; +import com.itmill.toolkit.ui.OrderedLayout; +import com.itmill.toolkit.ui.Table; +import com.itmill.toolkit.ui.Window; + +/** + * Similar to QueryContainerDemo + * + * @author IT Mill Ltd. + * @since 4.0.0 + * + */ +public class TableDemo extends com.itmill.toolkit.Application implements + Action.Handler { + + private static final String ACTION_DESCRIPTION = "Try right mouse button to initiate " + + "actions menu.
Note: on Opera you use meta key " + + "and left mouse button."; + + private static final String TABLE_CAPTION = "All rows from employee table"; + + private Link menuLink = new Link("Go back to menu", new ExternalResource( + "index.html")); + + // Table component where SQL rows are attached (using QueryContainer) + private Table table = new Table(); + + // Label which displays last performed action against table row + private Label tableLastAction = new Label("No action selected for table."); + + // Database provided with sample data + private SampleDatabase sampleDatabase; + + // Example Actions for table + private Action ACTION1 = new Action("Upload"); + + private Action ACTION2 = new Action("Download"); + + private Action ACTION3 = new Action("Show history"); + + private Action[] actions = new Action[] { ACTION1, ACTION2, ACTION3 }; + + // Button which is used to disable or enable table + // note: when button click event occurs, tableEnabler() method is called + private Button tableEnabler = new Button("Disable table", this, + "tableEnabler"); + + // Button which is used to hide or show table + // note: when button click event occurs, tableVisibility() method is called + private Button tableVisibility = new Button("Hide table", this, + "tableVisibility"); + + // Button which is used to hide or show table + // note: when button click event occurs, tableVisibility() method is called + private Button tableCaption = new Button("Hide caption", this, + "tableCaption"); + + // Links for switching between TableDemo and EmbeddedToolkit.jsp page + private Link embeddedToolkitLink = new Link( + "See same application instance through EmbeddedToolkit.jsp", + new ExternalResource("EmbeddedToolkit.jsp")); + + private Link tableDemoLink = new Link( + "See same application instance through TableDemo", + new ExternalResource("TableDemo")); + + /** + * Initialize Application. Demo components are added to main window. + */ + public void init() { + Window main = new Window("Table demo"); + setMainWindow(main); + + // set the application to use Corporate -theme + setTheme("corporate"); + + // Add link back to index.html + main.addComponent(menuLink); + + // create demo database + sampleDatabase = new SampleDatabase(); + + // Main window contains heading, two buttons, table and label + main.addComponent(new Label("

Table demo

" + ACTION_DESCRIPTION, + Label.CONTENT_XHTML)); + OrderedLayout layout = new OrderedLayout( + OrderedLayout.ORIENTATION_HORIZONTAL); + layout.addComponent(tableVisibility); + layout.addComponent(tableEnabler); + layout.addComponent(tableCaption); + main.addComponent(layout); + main.addComponent(table); + main.addComponent(tableLastAction); + main.addComponent(embeddedToolkitLink); + main.addComponent(tableDemoLink); + + // initialize demo components + initTable(); + } + + /** + * 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 { + QueryContainer qc = new QueryContainer("SELECT * FROM employee", + sampleDatabase.getConnection()); + table.setContainerDataSource(qc); + } catch (SQLException e) { + e.printStackTrace(); + } + // define which columns should be visible on Table component + table.setVisibleColumns(new Object[] { "FIRSTNAME", "LASTNAME", + "TITLE", "UNIT" }); + table.setItemCaptionPropertyId("ID"); + } + + public void tableVisibility() { + if (table.isVisible()) { + tableVisibility.setCaption("Show table"); + table.setVisible(false); + tableEnabler.setEnabled(false); + tableCaption.setEnabled(false); + } else { + tableVisibility.setCaption("Hide table"); + table.setVisible(true); + tableEnabler.setEnabled(true); + tableCaption.setEnabled(true); + } + } + + public void tableEnabler() { + if (table.isEnabled()) { + tableEnabler.setCaption("Enable table"); + table.setEnabled(false); + } else { + tableEnabler.setCaption("Disable table"); + table.setEnabled(true); + } + } + + public void tableCaption() { + if (table.getCaption().equals("")) { + table.setCaption(TABLE_CAPTION); + tableCaption.setCaption("Hide caption"); + } else { + table.setCaption(""); + tableCaption.setCaption("Show caption"); + } + } + + /** + * URIHandler 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); + } + } + +} diff --git a/src/com/itmill/toolkit/demo/TreeFilesystem.java b/src/com/itmill/toolkit/demo/TreeFilesystem.java new file mode 100644 index 0000000000..11c0bdf543 --- /dev/null +++ b/src/com/itmill/toolkit/demo/TreeFilesystem.java @@ -0,0 +1,96 @@ +package com.itmill.toolkit.demo; + +import java.io.File; +import com.itmill.toolkit.data.Item; +import com.itmill.toolkit.ui.*; +import com.itmill.toolkit.ui.Tree.ExpandEvent; + +/** + * Browsable file explorer using Toolkit Tree component. Demonstrates: how to + * add items hierarchially into + * com.itmill.toolkit.ui.Component.Tree, how to receive + * ExpandEvent and implement + * com.itmill.toolkit.ui.Tree.ExpandListener. + * + * @since 4.0.0 + * + */ +public class TreeFilesystem extends com.itmill.toolkit.Application implements + Tree.ExpandListener { + + // Default is root directory, e.g. / on unix or \ on windows + private static final String DIR_ROOT = "" + File.separatorChar; + + // Filesystem explorer panel and it's components + private Panel explorerPanel = new Panel("Filesystem explorer"); + + private Tree tree = new Tree(); + + public void init() { + Window main = new Window("Tree demo"); + setMainWindow(main); + + // set the application to use Corporate -theme + setTheme("corporate"); + + // Main window contains heading and panel + main.addComponent(new Label("

Tree demo

", Label.CONTENT_XHTML)); + + // configure file structure panel + main.addComponent(explorerPanel); + explorerPanel.addComponent(tree); + explorerPanel.setHeight(400); + + // "this" handles tree's expand event + tree.addListener((Tree.ExpandListener) this); + // populate tree's root node + populateNode(DIR_ROOT, null); + } + + /** + * Handle tree expand event, populate expanded node's childs with new files + * and directories. + */ + public void nodeExpand(ExpandEvent event) { + 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) { + File subdir = new File(file); + File[] files = subdir.listFiles(); + for (int x = 0; x < files.length; x++) { + try { + // add new item (String) to tree + 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 (Exception e) { + throw new RuntimeException(e); + } + } + } + +} diff --git a/src/com/itmill/toolkit/demo/TreeFilesystemContainer.java b/src/com/itmill/toolkit/demo/TreeFilesystemContainer.java new file mode 100644 index 0000000000..3297b45d3a --- /dev/null +++ b/src/com/itmill/toolkit/demo/TreeFilesystemContainer.java @@ -0,0 +1,94 @@ +package com.itmill.toolkit.demo; + +import java.io.File; + +import com.itmill.toolkit.data.util.FilesystemContainer; +import com.itmill.toolkit.data.util.FilesystemContainer.FileItem; +import com.itmill.toolkit.ui.*; +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 com.itmill.toolkit.ui.Component.Tree datasource container, + * how to create com.itmill.toolkit.data.util.FilesystemContainer, + * how to read com.itmill.toolkit.ui.Component.Event objects, how + * to receive and handle any event by implementing + * com.itmill.toolkit.ui.Component.Listener. + * + * @since 4.0.0 + * + */ +public class TreeFilesystemContainer extends com.itmill.toolkit.Application + implements Listener { + + // Default is root directory, e.g. / on unix or \ on windows + private static final String DIR_ROOT = "" + File.separatorChar; + + // Filesystem explorer panel and it's components + private Panel explorerPanel = new Panel("Filesystem explorer"); + + private Tree filesystem = new Tree(); + + // File properties panel and it's components + private Panel propertyPanel = new Panel("File properties"); + + private Label fileProperties = new Label(); + + public void init() { + Window main = new Window("Tree demo"); + setMainWindow(main); + + // set the application to use Corporate -theme + setTheme("corporate"); + + // Main window contains heading and two panels + main.addComponent(new Label("

TreeFilesystemContainer demo

", + Label.CONTENT_XHTML)); + main.addComponent(propertyPanel); + main.addComponent(explorerPanel); + + // Explorer panel contains tree + explorerPanel.addComponent(filesystem); + explorerPanel.setWidth(500); + + // Property panel contains label + propertyPanel.addComponent(fileProperties); + fileProperties.setCaption("No file selected."); + propertyPanel.setEnabled(false); + propertyPanel.setWidth(500); + + // Populate tree with FilesystemContainer + FilesystemContainer fsc = new FilesystemContainer(new File(DIR_ROOT), + true); + filesystem.setContainerDataSource(fsc); + // "this" handles all filesystem events + // e.g. node clicked, expanded etc. + filesystem.addListener((Listener) 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 + FileItem fileItem = (FileItem) filesystem.getItem(filesystem + .getValue()); + fileProperties.setIcon(fileItem.getIcon()); + fileProperties.setCaption(fileItem.getName() + ", size " + + fileItem.getSize() + " bytes."); + } + // 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/demo/features/FeatureBrowser.java b/src/com/itmill/toolkit/demo/features/FeatureBrowser.java index c79ca6f782..9a3d7487d3 100644 --- a/src/com/itmill/toolkit/demo/features/FeatureBrowser.java +++ b/src/com/itmill/toolkit/demo/features/FeatureBrowser.java @@ -141,8 +141,8 @@ public class FeatureBrowser extends CustomComponent implements registerFeature("/Data Model/Validators", new FeatureValidators()); registerFeature("/Data Model/Buffering", new FeatureBuffering()); // registerFeature("/Terminal", new IntroTerminal()); - // registerFeature("/Terminal/Parameters and URI Handling", - // new FeatureParameters()); + registerFeature("/Terminal/Parameters and URI Handling", + new FeatureParameters()); // Pre-open all menus for (Iterator i = features.getItemIds().iterator(); i.hasNext();) diff --git a/src/com/itmill/toolkit/demo/features/FeatureParameters.java b/src/com/itmill/toolkit/demo/features/FeatureParameters.java index f98de4ca17..b00621f5c4 100644 --- a/src/com/itmill/toolkit/demo/features/FeatureParameters.java +++ b/src/com/itmill/toolkit/demo/features/FeatureParameters.java @@ -92,6 +92,19 @@ public class FeatureParameters extends Feature implements URIHandler, p2.addComponent(params); l.addComponent(p2); + // Properties + propertyPanel = new PropertyPanel(p1); + Form ap = propertyPanel.createBeanPropertySet(new String[] { "width", + "height" }); + Select themes = (Select) propertyPanel.getField("style"); + themes.addItem("light").getItemProperty( + themes.getItemCaptionPropertyId()).setValue("light"); + themes.addItem("strong").getItemProperty( + themes.getItemCaptionPropertyId()).setValue("strong"); + propertyPanel.addProperties("Panel Properties", ap); + + setJavadocURL("ui/Panel.html"); + return l; } diff --git a/src/com/itmill/toolkit/demo/util/SampleDatabase.java b/src/com/itmill/toolkit/demo/util/SampleDatabase.java new file mode 100644 index 0000000000..36c8b26510 --- /dev/null +++ b/src/com/itmill/toolkit/demo/util/SampleDatabase.java @@ -0,0 +1,148 @@ +package com.itmill.toolkit.demo.util; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class SampleDatabase { + + public static final int ROWCOUNT = 1000; + + private Connection connection = null; + + private static final String[] firstnames = new String[] { "John", "Mary", + "Joe", "Sarah", "Jeff", "Jane", "Peter", "Marc", "Josie", "Linus" }; + + private static final String[] lastnames = new String[] { "Torvalds", + "Smith", "Jones", "Beck", "Sheridan", "Picard", "Hill", "Fielding", + "Einstein" }; + + private static final String[] titles = new String[] { "Project Manager", + "Marketing Manager", "Sales Manager", "Trainer", "IT Support", + "Account Manager", "Customer Support", "Testing Engineer", + "Software Designer", "Programmer", "Consultant" }; + + private static final String[] units = new String[] { "Tokyo", + "Mexico City", "Seoul", "New York", "Sao Paulo", "Bombay", "Delhi", + "Shanghai", "Los Angeles", "London", "Bangalore", "Hong Kong", + "Madrid", "Milano", "Beijing", "Paris", "Moscow", "Helsinki" }; + + /** + * Creates temporary database named toolkit with sample table named employee + * and populates it with data. By default we use HSQLDB. Ensure that you + * have hsqldb.jar under WEB-INF/lib directory. Database is will be created + * into memory. + * + */ + public SampleDatabase() { + // connect to SQL database + connect(); + + // initialize SQL database + createTables(); + + // test by executing sample JDBC query + testDatabase(); + } + + /** + * Creates sample table named employee and populates it with data.Use the + * specified database connection. + * + * @param connection + */ + public SampleDatabase(Connection connection) { + // initialize SQL database + createTables(); + + // test by executing sample JDBC query + testDatabase(); + } + + /** + * Connect to SQL database. In this sample we use HSQLDB and an toolkit + * named database in implicitly created into system memory. + * + */ + private void connect() { + // use memory-Only Database + String url = "jdbc:hsqldb:mem:toolkit"; + try { + Class.forName("org.hsqldb.jdbcDriver").newInstance(); + connection = DriverManager.getConnection(url, "sa", ""); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + // use for SQL commands CREATE, DROP, INSERT and UPDATE + public void update(String expression) throws SQLException { + Statement st = null; + st = connection.createStatement(); + int i = st.executeUpdate(expression); + if (i == -1) { + System.out.println("SampleDatabase error : " + expression); + } + st.close(); + } + + /** + * Create test table and few rows. Issue note: using capitalized column + * names as HSQLDB returns column names in capitalized form with this demo. + * + */ + private void createTables() { + try { + String stmt = null; + stmt = "CREATE TABLE employee ( ID INTEGER IDENTITY, FIRSTNAME VARCHAR(100), " + + "LASTNAME VARCHAR(100), TITLE VARCHAR(100), UNIT VARCHAR(100) )"; + update(stmt); + for (int j = 0; j < ROWCOUNT; j++) { + stmt = "INSERT INTO employee(FIRSTNAME, LASTNAME, TITLE, UNIT) VALUES (" + + "'" + + firstnames[(int) (Math.random() * (firstnames.length - 1))] + + "'," + + "'" + + lastnames[(int) (Math.random() * (lastnames.length - 1))] + + "'," + + "'" + + titles[(int) (Math.random() * (titles.length - 1))] + + "'," + + "'" + + units[(int) (Math.random() * (units.length - 1))] + + "'" + ")"; + update(stmt); + } + } catch (SQLException e) { + if (!e.toString().contains("Table already exists")) + throw new RuntimeException(e); + } + } + + /** + * Test database connection with simple SELECT command. + * + */ + private String testDatabase() { + String result = null; + try { + Statement stmt = connection.createStatement( + ResultSet.TYPE_SCROLL_INSENSITIVE, + ResultSet.CONCUR_UPDATABLE); + ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM employee"); + rs.next(); + result = "rowcount for table test is " + rs.getObject(1).toString(); + stmt.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return result; + } + + public Connection getConnection() { + return connection; + } + +}