aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/demo/featurebrowser/TreeExample.java
blob: 4f2137644f6720a08ac92af59190135d9120a1d9 (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
package com.itmill.toolkit.demo.featurebrowser;

import com.itmill.toolkit.data.Item;
import com.itmill.toolkit.data.Property;
import com.itmill.toolkit.data.Property.ValueChangeEvent;
import com.itmill.toolkit.event.Action;
import com.itmill.toolkit.ui.CustomComponent;
import com.itmill.toolkit.ui.Label;
import com.itmill.toolkit.ui.OrderedLayout;
import com.itmill.toolkit.ui.TextField;
import com.itmill.toolkit.ui.Tree;

/**
 * 
 */
public class TreeExample extends CustomComponent implements Action.Handler,
        Tree.ValueChangeListener {

    private static final Action ADD = new Action("Add item");
    private static final Action DELETE = new Action("Delete item");
    private static final Action[] actions = new Action[] { ADD, DELETE };

    private static final Object CAPTION_PROPERTY = "caption";

    private static final String desc = "Select an item in the tree to edit it's"
            + " caption. Right-click to add or remove items.";

    Tree tree;
    TextField tf;

    public TreeExample() {
        OrderedLayout main = new OrderedLayout();
        main.setMargin(true);
        setCompositionRoot(main);
        // Description
        main.addComponent(new Label(desc));
        // Caption editor
        tf = new TextField("Edit item caption");
        tf.setImmediate(true);
        tf.setEnabled(false);
        tf.setColumns(15);
        main.addComponent(tf);

        // Add tree with a few items
        tree = new Tree();
        tree.setImmediate(true);
        tree.addContainerProperty(CAPTION_PROPERTY, String.class, "");
        tree.setItemCaptionMode(Tree.ITEM_CAPTION_MODE_PROPERTY);
        tree.setItemCaptionPropertyId(CAPTION_PROPERTY);
        for (int i = 1; i <= 3; i++) {
            Object id = addCaptionedItem("Section " + i, null);
            tree.expandItem(id);
            addCaptionedItem("Team A", id);
            addCaptionedItem("Team B", id);
        }
        tree.addListener(this);
        tree.addActionHandler(this);
        main.addComponent(tree);

    }

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

    public void handleAction(Action action, Object sender, Object target) {
        if (action == DELETE) {
            tree.removeItem(target);
        } else {
            // Add
            Object id = addCaptionedItem("New Item", target);
            tree.expandItem(target);
            tree.setValue(id);
            tf.focus();
        }
    }

    public void valueChange(ValueChangeEvent event) {
        Object id = tree.getValue();
        if (event.getProperty() == tree) {
            if (id == null) {
                tf.removeListener(this);
                tf.setValue("");
                tf.setEnabled(false);

            } else {
                tf.setEnabled(true);
                Item item = tree.getItem(id);
                tf.setValue(item.getItemProperty(CAPTION_PROPERTY).getValue());
                tf.addListener(this);
            }
        } else {
            // TextField
            if (id != null) {
                Item item = tree.getItem(id);
                Property p = item.getItemProperty(CAPTION_PROPERTY);
                p.setValue(tf.getValue());
                tree.requestRepaint();
            }

        }
    }

    private Object addCaptionedItem(String caption, Object parent) {
        Object id = tree.addItem();
        Item item = tree.getItem(id);
        Property p = item.getItemProperty("caption");
        p.setValue(caption);
        if (parent != null) {
            tree.setParent(id, parent);
        }
        return id;
    }

}