Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

FeatureTree.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* *************************************************************************
  2. IT Mill Toolkit
  3. Development of Browser User Interfaces Made Easy
  4. Copyright (C) 2000-2006 IT Mill Ltd
  5. *************************************************************************
  6. This product is distributed under commercial license that can be found
  7. from the product package on license.pdf. Use of this product might
  8. require purchasing a commercial license from IT Mill Ltd. For guidelines
  9. on usage, see licensing-guidelines.html
  10. *************************************************************************
  11. For more information, contact:
  12. IT Mill Ltd phone: +358 2 4802 7180
  13. Ruukinkatu 2-4 fax: +358 2 4802 7181
  14. 20540, Turku email: info@itmill.com
  15. Finland company www: www.itmill.com
  16. Primary source for information and releases: www.itmill.com
  17. ********************************************************************** */
  18. package com.itmill.toolkit.demo.features;
  19. import java.util.Iterator;
  20. import com.itmill.toolkit.event.Action;
  21. import com.itmill.toolkit.ui.*;
  22. public class FeatureTree extends Feature implements Action.Handler {
  23. private static final String[] firstnames = new String[] { "John", "Mary",
  24. "Joe", "Sarah", "Jeff", "Jane", "Peter", "Marc", "Josie", "Linus" };
  25. private static final String[] lastnames = new String[] { "Torvalds",
  26. "Smith", "Jones", "Beck", "Sheridan", "Picard", "Hill", "Fielding",
  27. "Einstein" };
  28. private Tree t;
  29. private boolean actionsActive = false;
  30. private Button actionHandlerSwitch = new Button("Activate actions", this,
  31. "toggleActions");
  32. public FeatureTree() {
  33. super();
  34. }
  35. public void toggleActions() {
  36. if (actionsActive) {
  37. t.removeActionHandler(this);
  38. actionsActive = false;
  39. actionHandlerSwitch.setCaption("Activate Actions");
  40. } else {
  41. t.addActionHandler(this);
  42. actionsActive = true;
  43. actionHandlerSwitch.setCaption("Deactivate Actions");
  44. }
  45. }
  46. public void expandAll() {
  47. for (Iterator i = t.rootItemIds().iterator(); i.hasNext();) {
  48. t.expandItemsRecursively(i.next());
  49. }
  50. }
  51. public void collapseAll() {
  52. for (Iterator i = t.rootItemIds().iterator(); i.hasNext();) {
  53. t.collapseItemsRecursively(i.next());
  54. }
  55. }
  56. protected Component getDemoComponent() {
  57. OrderedLayout l = new OrderedLayout();
  58. String[] names = new String[100];
  59. for (int i = 0; i < names.length; i++)
  60. names[i] = firstnames[(int) (Math.random() * (firstnames.length - 1))]
  61. + " "
  62. + lastnames[(int) (Math.random() * (lastnames.length - 1))];
  63. // Create tree
  64. t = new Tree("Family Tree");
  65. for (int i = 0; i < 100; i++) {
  66. t.addItem(names[i]);
  67. String parent = names[(int) (Math.random() * (names.length - 1))];
  68. if (t.containsId(parent))
  69. t.setParent(names[i], parent);
  70. }
  71. // Forbid childless people to have children (makes them leaves)
  72. for (int i = 0; i < 100; i++)
  73. if (!t.hasChildren(names[i]))
  74. t.setChildrenAllowed(names[i], false);
  75. l.addComponent(t);
  76. // Actions
  77. l.addComponent(this.actionHandlerSwitch);
  78. // Expand and Collapse buttons
  79. l.addComponent(new Button("Expand All", this, "expandAll"));
  80. l.addComponent(new Button("Collapse All", this, "collapseAll"));
  81. // Properties
  82. propertyPanel = new PropertyPanel(t);
  83. Form ap = propertyPanel
  84. .createBeanPropertySet(new String[] { "selectable" });
  85. Select themes = (Select) propertyPanel.getField("style");
  86. themes.addItem("menu").getItemProperty(
  87. themes.getItemCaptionPropertyId()).setValue("menu");
  88. propertyPanel.addProperties("Tree Properties", ap);
  89. setJavadocURL("ui/Tree.html");
  90. return l;
  91. }
  92. protected String getExampleSrc() {
  93. return "// Create tree\n"
  94. + "t = new Tree(\"Family Tree\");\n"
  95. + "for (int i = 0; i < 100; i++) {\n"
  96. + " t.addItem(names[i]);\n"
  97. + " String parent = names[(int) (Math.random() * (names.length - 1))];\n"
  98. + " if (t.containsId(parent)) \n"
  99. + " t.setParent(names[i],parent);\n"
  100. + "}\n\n"
  101. + "// Forbid childless people to have children (makes them leaves)\n"
  102. + "for (int i = 0; i < 100; i++)\n"
  103. + " if (!t.hasChildren(names[i]))\n"
  104. + " t.setChildrenAllowed(names[i], false);\n";
  105. }
  106. protected String getDescriptionXHTML() {
  107. return "A tree is a natural way to represent datasets that have"
  108. + " hierarchical relationships, such as filesystems, message "
  109. + "threads or, as in this example, family trees. IT Mill Toolkit features a versatile "
  110. + "and powerful Tree component that works much like the tree components "
  111. + "of most modern operating systems."
  112. + "<br /><br />The most prominent use of the Tree component is to "
  113. + "use it for displaying a hierachical menu, like the "
  114. + "menu on the left side of the screen for instance "
  115. + "or to display filesystems or other hierarchical datasets."
  116. + "<br /><br />The tree component uses <code>Container</code> "
  117. + "datasources much like the Table component, "
  118. + "with the addition that it also utilizes the hierarchy "
  119. + "information maintained by the container."
  120. + "<br /><br />On the demo tab you can try out how the different properties "
  121. + "affect the presentation of the tree component.";
  122. }
  123. protected String getImage() {
  124. return "tree.jpg";
  125. }
  126. protected String getTitle() {
  127. return "Tree";
  128. }
  129. private Action ACTION1 = new Action("Action 1");
  130. private Action ACTION2 = new Action("Action 2");
  131. private Action ACTION3 = new Action("Action 3");
  132. private Action[] actions = new Action[] { ACTION1, ACTION2, ACTION3 };
  133. public Action[] getActions(Object target, Object sender) {
  134. return actions;
  135. }
  136. public void handleAction(Action action, Object sender, Object target) {
  137. t.setDescription("Last action clicked was '" + action.getCaption()
  138. + "' on item '" + target + "'");
  139. }
  140. }