01: /*
02: * MyGWT Widget Library
03: * Copyright(c) 2007, MyGWT.
04: * licensing@mygwt.net
05: *
06: * http://mygwt.net/license
07: */
08: package net.mygwt.samples.explorer.client;
09:
10: import net.mygwt.samples.resources.client.TestData;
11: import net.mygwt.ui.client.Style;
12: import net.mygwt.ui.client.event.BaseEvent;
13: import net.mygwt.ui.client.event.SelectionListener;
14: import net.mygwt.ui.client.util.TreeBuilder;
15: import net.mygwt.ui.client.widget.WidgetContainer;
16: import net.mygwt.ui.client.widget.layout.FlowLayout;
17: import net.mygwt.ui.client.widget.menu.Menu;
18: import net.mygwt.ui.client.widget.menu.MenuItem;
19: import net.mygwt.ui.client.widget.tree.Tree;
20: import net.mygwt.ui.client.widget.tree.TreeItem;
21:
22: public class TreeContextMenuPage extends Page {
23:
24: private int count = 1;
25:
26: protected void createWidget(WidgetContainer container) {
27: final Tree tree = new Tree(Style.SINGLE);
28: tree.setItemImageStyle("icon-music");
29:
30: // quick way to build tree from Model instances.
31: TreeBuilder.buildTree(tree, TestData.getTreeModel());
32:
33: Menu contextMenu = new Menu();
34:
35: MenuItem insert = new MenuItem(Style.PUSH);
36: insert.setText("Insert Item");
37: insert.setIconStyle("icon-add");
38: insert.addSelectionListener(new SelectionListener() {
39: public void widgetSelected(BaseEvent be) {
40: TreeItem item = tree.getSelectedItem();
41: if (item != null) {
42: TreeItem newItem = new TreeItem();
43: newItem.setText("Add Child " + count++);
44: item.add(newItem);
45: item.setExpanded(true);
46: }
47: }
48: });
49: contextMenu.add(insert);
50:
51: MenuItem remove = new MenuItem(Style.PUSH);
52: remove.setText("Remove Selected");
53: remove.setIconStyle("icon-delete");
54: remove.addSelectionListener(new SelectionListener() {
55: public void widgetSelected(BaseEvent be) {
56: TreeItem item = tree.getSelectedItem();
57: if (item != null) {
58: item.getParentItem().remove(item);
59: }
60: }
61: });
62: contextMenu.add(remove);
63:
64: tree.setContextMenu(contextMenu);
65:
66: container.setLayout(new FlowLayout(8));
67: container.add(tree);
68: }
69:
70: }
|