001: /* *************************************************************************
002:
003: Millstone(TM)
004: Open Sourced User Interface Library for
005: Internet Development with Java
006:
007: Millstone is a registered trademark of IT Mill Ltd
008: Copyright (C) 2000-2005 IT Mill Ltd
009:
010: *************************************************************************
011:
012: This library is free software; you can redistribute it and/or
013: modify it under the terms of the GNU Lesser General Public
014: license version 2.1 as published by the Free Software Foundation.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: *************************************************************************
026:
027: For more information, contact:
028:
029: IT Mill Ltd phone: +358 2 4802 7180
030: Ruukinkatu 2-4 fax: +358 2 4802 7181
031: 20540, Turku email: info@itmill.com
032: Finland company www: www.itmill.com
033:
034: Primary source for MillStone information and releases: www.millstone.org
035:
036: ********************************************************************** */
037:
038: package org.millstone.examples.features;
039:
040: import java.util.Iterator;
041:
042: import org.millstone.base.ui.*;
043: import org.millstone.base.event.Action;
044:
045: public class FeatureTree extends Feature implements Action.Handler {
046:
047: private static final String[] firstnames = new String[] { "John",
048: "Mary", "Joe", "Sarah", "Jeff", "Jane", "Peter", "Marc",
049: "Josie", "Linus" };
050: private static final String[] lastnames = new String[] {
051: "Torvalds", "Smith", "Jones", "Beck", "Sheridan", "Picard",
052: "Hill", "Fielding", "Einstein" };
053:
054: private Tree t;
055:
056: private boolean actionsActive = false;
057: private Button actionHandlerSwitch = new Button("Activate actions",
058: this , "toggleActions");
059:
060: public FeatureTree() {
061: super ();
062: }
063:
064: public void toggleActions() {
065: if (actionsActive) {
066: t.removeActionHandler(this );
067: actionsActive = false;
068: actionHandlerSwitch.setCaption("Activate Actions");
069: } else {
070: t.addActionHandler(this );
071: actionsActive = true;
072: actionHandlerSwitch.setCaption("Deactivate Actions");
073: }
074: }
075:
076: public void expandAll() {
077: for (Iterator i = t.rootItemIds().iterator(); i.hasNext();) {
078: t.expandItemsRecursively(i.next());
079: }
080: }
081:
082: public void collapseAll() {
083: for (Iterator i = t.rootItemIds().iterator(); i.hasNext();) {
084: t.collapseItemsRecursively(i.next());
085: }
086: }
087:
088: protected Component getDemoComponent() {
089:
090: OrderedLayout l = new OrderedLayout();
091:
092: // Create names
093: Panel show = new Panel("Tree component");
094: String[] names = new String[100];
095: for (int i = 0; i < names.length; i++)
096: names[i] = firstnames[(int) (Math.random() * (firstnames.length - 1))]
097: + " "
098: + lastnames[(int) (Math.random() * (lastnames.length - 1))];
099:
100: // Create tree
101: t = new Tree("Family Tree");
102: for (int i = 0; i < 100; i++) {
103: t.addItem(names[i]);
104: String parent = names[(int) (Math.random() * (names.length - 1))];
105: if (t.containsId(parent))
106: t.setParent(names[i], parent);
107: }
108:
109: // Forbid childless people to have children (makes them leaves)
110: for (int i = 0; i < 100; i++)
111: if (!t.hasChildren(names[i]))
112: t.setChildrenAllowed(names[i], false);
113:
114: show.addComponent(t);
115: l.addComponent(show);
116:
117: // Actions
118: l.addComponent(this .actionHandlerSwitch);
119:
120: // Expand and Collapse buttons
121: l.addComponent(new Button("Expand All", this , "expandAll"));
122: l.addComponent(new Button("Collapse All", this , "collapseAll"));
123:
124: // Properties
125: PropertyPanel p = new PropertyPanel(t);
126: Form ap = p
127: .createBeanPropertySet(new String[] { "selectable" });
128: Select themes = (Select) p.getField("style");
129: themes.addItem("menu").getItemProperty(
130: themes.getItemCaptionPropertyId()).setValue("menu");
131: p.addProperties("Tree Properties", ap);
132: l.addComponent(p);
133:
134: return l;
135: }
136:
137: protected String getExampleSrc() {
138: return "// Create tree\n"
139: + "t = new Tree(\"Family Tree\");\n"
140: + "for (int i = 0; i < 100; i++) {\n"
141: + " t.addItem(names[i]);\n"
142: + " String parent = names[(int) (Math.random() * (names.length - 1))];\n"
143: + " if (t.containsId(parent)) \n"
144: + " t.setParent(names[i],parent);\n"
145: + "}\n\n"
146: + "// Forbid childless people to have children (makes them leaves)\n"
147: + "for (int i = 0; i < 100; i++)\n"
148: + " if (!t.hasChildren(names[i]))\n"
149: + " t.setChildrenAllowed(names[i], false);\n";
150: }
151:
152: protected String getDescriptionXHTML() {
153: return "<p>A tree is a natural way to represent datasets that have"
154: + " hierarchical relationships, such as filesystems, message "
155: + "threads or... family trees. Millstone features a versatile "
156: + "and powerful Tree component that works much like the tree components "
157: + "of most modern operating systems. </p>"
158: + "<p>The most prominent use of the Tree component is to "
159: + "use it for displaying a hierachical menu, like the "
160: + "menu on the left side of the screen for instance "
161: + "or to display filesystems or other hierarchical datasets.</p>"
162: + "<p>The tree component uses <code>Container</code> "
163: + "datasources much like the Table component, "
164: + "with the addition that it also utilizes the hierarchy "
165: + "information maintained by the container. </p><p>On "
166: + "the demo tab you can try out how the different properties "
167: + "affect the presentation of the tree component.</p>";
168: }
169:
170: protected String getImage() {
171: return "tree.jpg";
172: }
173:
174: protected String getTitle() {
175: return "Tree";
176: }
177:
178: private Action ACTION1 = new Action("Action 1");
179: private Action ACTION2 = new Action("Action 2");
180: private Action ACTION3 = new Action("Action 3");
181:
182: private Action[] actions = new Action[] { ACTION1, ACTION2, ACTION3 };
183:
184: public Action[] getActions(Object target, Object sender) {
185: return actions;
186: }
187:
188: public void handleAction(Action action, Object sender, Object target) {
189: t.setDescription("Last action clicked was '"
190: + action.getCaption() + "' on item '" + target + "'");
191: }
192: }
|