From: Jani Laakso Date: Wed, 18 Apr 2007 06:19:48 +0000 (+0000) Subject: Added new demo FilterSelect, changed it's layout and texts. X-Git-Tag: 6.7.0.beta1~6432 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=78e180871a5f15d2bda91b6549b034fc8a2c7a53;p=vaadin-framework.git Added new demo FilterSelect, changed it's layout and texts. svn changeset:1264/svn branch:trunk --- diff --git a/WebContent/WEB-INF/web.xml b/WebContent/WEB-INF/web.xml index e649cafceb..5a11c3cd99 100644 --- a/WebContent/WEB-INF/web.xml +++ b/WebContent/WEB-INF/web.xml @@ -54,6 +54,19 @@ /SelectDemo/* + + FilterSelect + com.itmill.toolkit.terminal.web.ApplicationServlet + + application + com.itmill.toolkit.demo.FilterSelect + + + + FilterSelect + /FilterSelect/* + + features com.itmill.toolkit.terminal.web.ApplicationServlet diff --git a/WebContent/index.html b/WebContent/index.html index 7c798c8058..b486a56fc8 100644 --- a/WebContent/index.html +++ b/WebContent/index.html @@ -206,6 +206,19 @@ SelectDemo.java +
+

FilterSelect demo

+
+

+ This example shows three select components with lazy loading functionality enabled. + First select works like Google Suggest, second uses "Contains" filter and third uses custom "Ends with" filter. + Click to select components and type few letters using your keyboard to see it in action. +

+
+ Source code: + FilterSelect.java +
+

Modal window

diff --git a/src/com/itmill/toolkit/demo/FilterSelect.java b/src/com/itmill/toolkit/demo/FilterSelect.java new file mode 100644 index 0000000000..97d7ee20da --- /dev/null +++ b/src/com/itmill/toolkit/demo/FilterSelect.java @@ -0,0 +1,141 @@ +package com.itmill.toolkit.demo; + +import java.util.ArrayList; +import java.util.Iterator; + +import com.itmill.toolkit.data.Item; +import com.itmill.toolkit.ui.*; +import com.itmill.toolkit.ui.select.ContainsFilter; +import com.itmill.toolkit.ui.select.OptionFilter; + +/** + * The classic "hello, world!" example for IT Mill Toolkit. The class simply + * implements the abstract {@link com.itmill.toolkit.Application#init() init()} + * method in which it creates a Window and adds a Label to it. + * + * @author IT Mill Ltd. + * @see com.itmill.toolkit.Application + * @see com.itmill.toolkit.ui.Window + * @see com.itmill.toolkit.ui.Label + */ +public class FilterSelect extends com.itmill.toolkit.Application { + + private static final String[] firstnames = new String[] { "John", "Mary", + "Joe", "Sarah", "Jeff", "Jane", "Peter", "Marc", "Robert", "Paula", + "Lenny", "Kenny", "Nathan", "Nicole", "Laura", "Jos", "Josie", + "Linus" }; + + private static final String[] lastnames = new String[] { "Torvalds", + "Smith", "Adams", "Black", "Wilson", "Richards", "Thompson", + "McGoff", "Halas", "Jones", "Beck", "Sheridan", "Picard", "Hill", + "Fielding", "Einstein" }; + + /** + * The initialization method that is the only requirement for inheriting the + * com.itmill.toolkit.service.Application class. It will be automatically + * called by the framework when a user accesses the application. + */ + 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 + */ + Window main = new Window("Filter select examples"); + setMainWindow(main); + + // default filter + Select s1 = new Select(); + for (int i = 0; i < 500; i++) + s1 + .addItem(firstnames[(int) (Math.random() * (firstnames.length - 1))] + + " " + + lastnames[(int) (Math.random() * (lastnames.length - 1))]); + s1.setLazyLoading(true); + + // contains filter + Select s2 = new Select(); + for (int i = 0; i < 500; i++) + s2 + .addItem(firstnames[(int) (Math.random() * (firstnames.length - 1))] + + " " + + lastnames[(int) (Math.random() * (lastnames.length - 1))]); + s2.setLazyLoading(true); + s2.setOptionFilter(new ContainsFilter(s2)); + + // custom filter + Select s3 = new Select(); + for (int i = 0; i < 500; i++) + s3 + .addItem(firstnames[(int) (Math.random() * (firstnames.length - 1))] + + " " + + lastnames[(int) (Math.random() * (lastnames.length - 1))]); + s3.setLazyLoading(true); + s3.setOptionFilter(new FilterSelect.EndsWithFilter(s3)); + + // Add selects to UI using ordered layout and panels + OrderedLayout orderedLayout = new OrderedLayout( + OrderedLayout.ORIENTATION_HORIZONTAL); + + Panel panel1 = new Panel("Select with default filter"); + Panel panel2 = new Panel("Select with contains filter"); + Panel panel3 = new Panel("Select with custom 'EndsWith' filter"); + + panel1.addComponent(s1); + panel2.addComponent(s2); + panel3.addComponent(s3); + + orderedLayout.addComponent(panel1); + orderedLayout.addComponent(panel2); + orderedLayout.addComponent(panel3); + main.addComponent(orderedLayout); + + } + + /** + * Custom filter that implements "ends with" functionality. + * + * @author IT Mill Ltd. + * + */ + public class EndsWithFilter implements OptionFilter { + private Select s; + + private ArrayList filteredItemsBuffer; + + public EndsWithFilter(Select s) { + this.s = s; + } + + public ArrayList filter(String filterstring) { + // prefix MUST be in lowercase + if ("".equals(filterstring)) { + this.filteredItemsBuffer = new ArrayList(s.getItemIds()); + return this.filteredItemsBuffer; + + } else if (s.getContainerDataSource() != null) { + // all items will be iterated and tested. + // SLOW when there are lot of items. + this.filteredItemsBuffer = new ArrayList(); + for (Iterator iter = s.getItemIds().iterator(); iter.hasNext();) { + Object id = iter.next(); + + Item item = s.getItem(id); + String test = ""; + if (s.getItemCaptionMode() == Select.ITEM_CAPTION_MODE_PROPERTY) + test = item.getItemProperty( + s.getItemCaptionPropertyId()).getValue() + .toString().trim(); + else + test = String.valueOf(id); + + if (test.toLowerCase().endsWith(filterstring)) { + this.filteredItemsBuffer.add(id); + } + } + } + return this.filteredItemsBuffer; + } + } + +}