You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FeatureBrowser.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.demo.featurebrowser;
  5. import java.util.HashMap;
  6. import com.itmill.toolkit.data.Item;
  7. import com.itmill.toolkit.data.Property;
  8. import com.itmill.toolkit.data.Property.ValueChangeEvent;
  9. import com.itmill.toolkit.data.util.HierarchicalContainer;
  10. import com.itmill.toolkit.data.util.IndexedContainer;
  11. import com.itmill.toolkit.terminal.ExternalResource;
  12. import com.itmill.toolkit.terminal.Sizeable;
  13. import com.itmill.toolkit.terminal.ThemeResource;
  14. import com.itmill.toolkit.ui.AbstractSelect;
  15. import com.itmill.toolkit.ui.Button;
  16. import com.itmill.toolkit.ui.Component;
  17. import com.itmill.toolkit.ui.Embedded;
  18. import com.itmill.toolkit.ui.ExpandLayout;
  19. import com.itmill.toolkit.ui.Label;
  20. import com.itmill.toolkit.ui.Layout;
  21. import com.itmill.toolkit.ui.OrderedLayout;
  22. import com.itmill.toolkit.ui.Select;
  23. import com.itmill.toolkit.ui.SplitPanel;
  24. import com.itmill.toolkit.ui.TabSheet;
  25. import com.itmill.toolkit.ui.Table;
  26. import com.itmill.toolkit.ui.Tree;
  27. import com.itmill.toolkit.ui.Window;
  28. import com.itmill.toolkit.ui.Button.ClickEvent;
  29. /**
  30. *
  31. * @author IT Mill Ltd.
  32. * @see com.itmill.toolkit.ui.Window
  33. */
  34. public class FeatureBrowser extends com.itmill.toolkit.Application implements
  35. Select.ValueChangeListener {
  36. // Property IDs
  37. private static final Object PROPERTY_ID_CATEGORY = "Category";
  38. private static final Object PROPERTY_ID_NAME = "Name";
  39. private static final Object PROPERTY_ID_DESC = "Description";
  40. private static final Object PROPERTY_ID_CLASS = "Class";
  41. private static final Object PROPERTY_ID_VIEWED = "Viewed";
  42. // Global components
  43. private Tree tree;
  44. private Table table;
  45. private TabSheet ts;
  46. // Example "cache"
  47. private final HashMap exampleInstances = new HashMap();
  48. // List of examples
  49. private static final Object[][] demos = new Object[][] {
  50. // Category, Name, Desc, Class, Viewed
  51. // Getting started: Labels
  52. { "Getting started", "Labels", "Some variations of Labels",
  53. LabelExample.class },
  54. // Getting started: Buttons
  55. { "Getting started", "Buttons and links",
  56. "Various Buttons and Links", ButtonExample.class },
  57. // Getting started: Fields
  58. { "Getting started", "Basic value input",
  59. "TextFields, DateFields, and such", ValueInputExample.class },
  60. //
  61. { "Getting started", "RichText", "Rich text editing",
  62. RichTextExample.class },
  63. // Getting started: Selects
  64. { "Getting started", "Choices, choices",
  65. "Some variations of simple selects", SelectExample.class },
  66. // Layouts
  67. { "Getting started", "Layouts", "Laying out components",
  68. LayoutExample.class },
  69. // Wrangling data: ComboBox
  70. { "Wrangling data", "ComboBox", "ComboBox - the swiss army select",
  71. ComboBoxExample.class },
  72. // Wrangling data: Table
  73. { "Wrangling data", "Table",
  74. "A dynamic Table with bells, whistles and actions",
  75. TableExample.class },
  76. // Wrangling data: Tree
  77. { "Wrangling data", "Tree", "A hierarchy of things",
  78. TreeExample.class },
  79. // Misc: Notifications
  80. { "Misc", "Notifications", "Notifications can improve usability",
  81. NotificationExample.class },
  82. // Misc: Caching
  83. { "Misc", "Client caching", "Demonstrating of client-side caching",
  84. ClientCachingExample.class },
  85. // Misc: Embedded
  86. { "Misc", "Embedding",
  87. "Embedding resources - another site in this case",
  88. EmbeddedBrowserExample.class },
  89. // Windowing
  90. { "Misc", "Windowing", "About windowing", WindowingExample.class },
  91. // END
  92. };
  93. public void init() {
  94. // Need to set a theme for ThemeResources to work
  95. setTheme("example");
  96. // Create new window for the application and give the window a visible.
  97. final Window main = new Window("IT Mill Toolkit 5");
  98. // set as main window
  99. setMainWindow(main);
  100. final SplitPanel split = new SplitPanel(
  101. SplitPanel.ORIENTATION_HORIZONTAL);
  102. split.setSplitPosition(200, Sizeable.UNITS_PIXELS);
  103. main.setLayout(split);
  104. final HashMap sectionIds = new HashMap();
  105. final HierarchicalContainer container = createContainer();
  106. final Object rootId = container.addItem();
  107. Item item = container.getItem(rootId);
  108. Property p = item.getItemProperty(PROPERTY_ID_NAME);
  109. p.setValue("All examples");
  110. for (int i = 0; i < demos.length; i++) {
  111. final Object[] demo = demos[i];
  112. final String section = (String) demo[0];
  113. Object sectionId;
  114. if (sectionIds.containsKey(section)) {
  115. sectionId = sectionIds.get(section);
  116. } else {
  117. sectionId = container.addItem();
  118. sectionIds.put(section, sectionId);
  119. container.setParent(sectionId, rootId);
  120. item = container.getItem(sectionId);
  121. p = item.getItemProperty(PROPERTY_ID_NAME);
  122. p.setValue(section);
  123. }
  124. final Object id = container.addItem();
  125. container.setParent(id, sectionId);
  126. initItem(container.getItem(id), demo);
  127. }
  128. tree = new Tree();
  129. tree.setSelectable(true);
  130. tree.setMultiSelect(false);
  131. tree.setNullSelectionAllowed(false);
  132. tree.setContainerDataSource(container);
  133. tree.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
  134. tree.setItemCaptionPropertyId(PROPERTY_ID_NAME);
  135. tree.addListener(this);
  136. tree.setImmediate(true);
  137. tree.expandItemsRecursively(rootId);
  138. split.addComponent(tree);
  139. final SplitPanel split2 = new SplitPanel();
  140. split2.setSplitPosition(200, Sizeable.UNITS_PIXELS);
  141. split.addComponent(split2);
  142. table = new Table();
  143. table.setSizeFull();
  144. table.setColumnReorderingAllowed(true);
  145. table.setColumnCollapsingAllowed(true);
  146. table.setSelectable(true);
  147. table.setMultiSelect(false);
  148. table.setNullSelectionAllowed(false);
  149. try {
  150. table.setContainerDataSource((IndexedContainer) container.clone());
  151. } catch (final Exception e) {
  152. e.printStackTrace(System.err);
  153. }
  154. // Hide some columns
  155. table.setVisibleColumns(new Object[] { PROPERTY_ID_CATEGORY,
  156. PROPERTY_ID_NAME, PROPERTY_ID_DESC, PROPERTY_ID_VIEWED });
  157. table.addListener(this);
  158. table.setImmediate(true);
  159. split2.addComponent(table);
  160. final ExpandLayout exp = new ExpandLayout();
  161. exp.setMargin(true);
  162. split2.addComponent(exp);
  163. final OrderedLayout wbLayout = new OrderedLayout(
  164. OrderedLayout.ORIENTATION_HORIZONTAL);
  165. Button b = new Button("Open in sub-window", new Button.ClickListener() {
  166. public void buttonClick(ClickEvent event) {
  167. Component component = (Component) ts.getComponentIterator()
  168. .next();
  169. String caption = ts.getTabCaption(component);
  170. try {
  171. component = (Component) component.getClass().newInstance();
  172. } catch (Exception e) {
  173. // Could not create
  174. return;
  175. }
  176. Window w = new Window(caption);
  177. w.setWidth(640);
  178. if (Layout.class.isAssignableFrom(component.getClass())) {
  179. w.setLayout((Layout) component);
  180. } else {
  181. w.getLayout().setSizeFull();
  182. w.addComponent(component);
  183. }
  184. getMainWindow().addWindow(w);
  185. }
  186. });
  187. b.setStyleName(Button.STYLE_LINK);
  188. wbLayout.addComponent(b);
  189. b = new Button("Open in native window", new Button.ClickListener() {
  190. public void buttonClick(ClickEvent event) {
  191. Component component = (Component) ts.getComponentIterator()
  192. .next();
  193. final String caption = ts.getTabCaption(component);
  194. Window w = getWindow(caption);
  195. if (w == null) {
  196. try {
  197. component = (Component) component.getClass()
  198. .newInstance();
  199. } catch (final Exception e) {
  200. // Could not create
  201. return;
  202. }
  203. w = new Window(caption);
  204. w.setName(caption);
  205. if (Layout.class.isAssignableFrom(component.getClass())) {
  206. w.setLayout((Layout) component);
  207. } else {
  208. w.getLayout().setSizeFull();
  209. w.addComponent(component);
  210. }
  211. addWindow(w);
  212. }
  213. getMainWindow().open(new ExternalResource(w.getURL()), caption);
  214. }
  215. });
  216. b.setStyleName(Button.STYLE_LINK);
  217. wbLayout.addComponent(b);
  218. exp.addComponent(wbLayout);
  219. exp.setComponentAlignment(wbLayout, OrderedLayout.ALIGNMENT_RIGHT,
  220. OrderedLayout.ALIGNMENT_TOP);
  221. ts = new TabSheet();
  222. ts.setSizeFull();
  223. ts.addTab(new Label(""), "Choose example", null);
  224. exp.addComponent(ts);
  225. exp.expand(ts);
  226. final Label status = new Label(
  227. "<a href=\"http://www.itmill.com/index_developers.htm\">Developer Area</a>"
  228. + " | <a href=\"http://www.itmill.com/developers_documentation.htm\">Documentation</a>");
  229. status.setContentMode(Label.CONTENT_XHTML);
  230. exp.addComponent(status);
  231. exp.setComponentAlignment(status, OrderedLayout.ALIGNMENT_RIGHT,
  232. OrderedLayout.ALIGNMENT_VERTICAL_CENTER);
  233. // select initial section ("All")
  234. tree.setValue(rootId);
  235. getMainWindow()
  236. .showNotification(
  237. "Welcome",
  238. "Choose an example to begin.<br/><br/>And remember to experiment!",
  239. Window.Notification.TYPE_TRAY_NOTIFICATION);
  240. }
  241. private void initItem(Item item, Object[] data) {
  242. int p = 0;
  243. Property prop = item.getItemProperty(PROPERTY_ID_CATEGORY);
  244. prop.setValue(data[p++]);
  245. prop = item.getItemProperty(PROPERTY_ID_NAME);
  246. prop.setValue(data[p++]);
  247. prop = item.getItemProperty(PROPERTY_ID_DESC);
  248. prop.setValue(data[p++]);
  249. prop = item.getItemProperty(PROPERTY_ID_CLASS);
  250. prop.setValue(data[p++]);
  251. }
  252. private HierarchicalContainer createContainer() {
  253. final HierarchicalContainer c = new HierarchicalContainer();
  254. c.addContainerProperty(PROPERTY_ID_CATEGORY, String.class, null);
  255. c.addContainerProperty(PROPERTY_ID_NAME, String.class, "");
  256. c.addContainerProperty(PROPERTY_ID_DESC, String.class, "");
  257. c.addContainerProperty(PROPERTY_ID_CLASS, Class.class, null);
  258. c.addContainerProperty(PROPERTY_ID_VIEWED, Embedded.class, null);
  259. return c;
  260. }
  261. public void valueChange(ValueChangeEvent event) {
  262. if (event.getProperty() == tree) {
  263. final Object id = tree.getValue();
  264. final Item item = tree.getItem(id);
  265. //
  266. String section;
  267. if (tree.isRoot(id)) {
  268. section = ""; // show all sections
  269. } else if (tree.hasChildren(id)) {
  270. section = (String) item.getItemProperty(PROPERTY_ID_NAME)
  271. .getValue();
  272. } else {
  273. section = (String) item.getItemProperty(PROPERTY_ID_CATEGORY)
  274. .getValue();
  275. }
  276. table.setValue(null);
  277. final IndexedContainer c = (IndexedContainer) table
  278. .getContainerDataSource();
  279. c.removeAllContainerFilters();
  280. if (section != null) {
  281. c
  282. .addContainerFilter(PROPERTY_ID_CATEGORY, section,
  283. false, true);
  284. }
  285. if (!tree.hasChildren(id)) {
  286. // Example, not section
  287. // update table selection
  288. table.setValue(id);
  289. }
  290. } else if (event.getProperty() == table) {
  291. if (table.getValue() != null) {
  292. table.removeListener(this);
  293. tree.setValue(table.getValue());
  294. table.addListener(this);
  295. final Item item = table.getItem(table.getValue());
  296. final Class c = (Class) item.getItemProperty(PROPERTY_ID_CLASS)
  297. .getValue();
  298. final Component component = getComponent(c);
  299. if (component != null) {
  300. final String caption = (String) item.getItemProperty(
  301. PROPERTY_ID_NAME).getValue();
  302. ts.removeAllComponents();
  303. ts.addTab(component, caption, null);
  304. }
  305. // update "viewed" state
  306. final Property p = item.getItemProperty(PROPERTY_ID_VIEWED);
  307. if (p.getValue() == null) {
  308. p.setValue(new Embedded("", new ThemeResource(
  309. "icons/ok.png")));
  310. }
  311. table.requestRepaint();
  312. }
  313. }
  314. }
  315. private Component getComponent(Class componentClass) {
  316. if (!exampleInstances.containsKey(componentClass)) {
  317. try {
  318. final Component c = (Component) componentClass.newInstance();
  319. exampleInstances.put(componentClass, c);
  320. } catch (final Exception e) {
  321. return null;
  322. }
  323. }
  324. return (Component) exampleInstances.get(componentClass);
  325. }
  326. }