summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/tests/featurebrowser/FeatureTree.java
blob: 51c58e49a9dcbfc1d33d01a2ccda01b37b6b94be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.itmill.toolkit.tests.featurebrowser;

import java.util.Iterator;

import com.itmill.toolkit.event.Action;
import com.itmill.toolkit.ui.Button;
import com.itmill.toolkit.ui.Component;
import com.itmill.toolkit.ui.Form;
import com.itmill.toolkit.ui.OrderedLayout;
import com.itmill.toolkit.ui.Select;
import com.itmill.toolkit.ui.Tree;

public class FeatureTree extends Feature implements Action.Handler {

    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 Tree t;

    private boolean actionsActive = false;

    private final Button actionHandlerSwitch = new Button("Activate actions",
            this, "toggleActions");

    public FeatureTree() {
        super();
    }

    public void toggleActions() {
        if (actionsActive) {
            t.removeActionHandler(this);
            actionsActive = false;
            actionHandlerSwitch.setCaption("Activate Actions");
        } else {
            t.addActionHandler(this);
            actionsActive = true;
            actionHandlerSwitch.setCaption("Deactivate Actions");
        }
    }

    public void expandAll() {
        for (final Iterator i = t.rootItemIds().iterator(); i.hasNext();) {
            t.expandItemsRecursively(i.next());
        }
    }

    public void collapseAll() {
        for (final Iterator i = t.rootItemIds().iterator(); i.hasNext();) {
            t.collapseItemsRecursively(i.next());
        }
    }

    @Override
    protected Component getDemoComponent() {

        final OrderedLayout l = new OrderedLayout();

        final String[] names = new String[100];
        for (int i = 0; i < names.length; i++) {
            names[i] = firstnames[(int) (Math.random() * (firstnames.length - 1))]
                    + " "
                    + lastnames[(int) (Math.random() * (lastnames.length - 1))];
        }

        // Create tree
        t = new Tree("Organization Structure");
        for (int i = 0; i < 100; i++) {
            t.addItem(names[i]);
            final String parent = names[(int) (Math.random() * (names.length - 1))];
            if (t.containsId(parent)) {
                t.setParent(names[i], parent);
            }
        }

        // Forbid childless people to have children (makes them leaves)
        for (int i = 0; i < 100; i++) {
            if (!t.hasChildren(names[i])) {
                t.setChildrenAllowed(names[i], false);
            }
        }

        l.addComponent(t);

        // Actions
        l.addComponent(actionHandlerSwitch);

        // Expand and Collapse buttons
        l.addComponent(new Button("Expand All", this, "expandAll"));
        l.addComponent(new Button("Collapse All", this, "collapseAll"));

        // Properties
        propertyPanel = new PropertyPanel(t);
        final Form ap = propertyPanel
                .createBeanPropertySet(new String[] { "selectable" });
        final Select themes = (Select) propertyPanel.getField("style");
        themes.addItem("menu").getItemProperty(
                themes.getItemCaptionPropertyId()).setValue("menu");
        propertyPanel.addProperties("Tree Properties", ap);

        setJavadocURL("ui/Tree.html");

        return l;
    }

    @Override
    protected String getExampleSrc() {
        return "// Create tree\n"
                + "t = new Tree(\"Organization Structure\");\n"
                + "for (int i = 0; i < 100; i++) {\n"
                + "	t.addItem(names[i]);\n"
                + "	String parent = names[(int) (Math.random() * (names.length - 1))];\n"
                + "	if (t.containsId(parent)) \n"
                + "		t.setParent(names[i],parent);\n"
                + "}\n\n"
                + "// Forbid childless people to have children (makes them leaves)\n"
                + "for (int i = 0; i < 100; i++)\n"
                + "	if (!t.hasChildren(names[i]))\n"
                + "		t.setChildrenAllowed(names[i], false);\n";
    }

    @Override
    protected String getDescriptionXHTML() {
        return "A tree is a natural way to represent datasets that have"
                + " hierarchical relationships, such as filesystems, message "
                + "threads or, as in this example, organization structure. IT Mill Toolkit features a versatile "
                + "and powerful Tree component that works much like the tree components "
                + "of most modern operating systems."
                + "<br /><br />The most prominent use of the Tree component is to "
                + "use it for displaying a hierachical menu, like the "
                + "menu on the left side of the screen for instance "
                + "or to display filesystems or other hierarchical datasets."
                + "<br /><br />The tree component uses <code>Container</code> "
                + "datasources much like the Table component, "
                + "with the addition that it also utilizes the hierarchy "
                + "information maintained by the container."
                + "<br /><br />On the demo tab you can try out how the different properties "
                + "affect the presentation of the tree component.";
    }

    @Override
    protected String getImage() {
        return "icon_demo.png";
    }

    @Override
    protected String getTitle() {
        return "Tree";
    }

    private final Action ACTION1 = new Action("Action 1");

    private final Action ACTION2 = new Action("Action 2");

    private final Action ACTION3 = new Action("Action 3");

    private final Action[] actions = new Action[] { ACTION1, ACTION2, ACTION3 };

    public Action[] getActions(Object target, Object sender) {
        return actions;
    }

    public void handleAction(Action action, Object sender, Object target) {
        t.setDescription("Last action clicked was '" + action.getCaption()
                + "' on item '" + target + "'");
    }
}