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.Folder;
11: import net.mygwt.samples.resources.client.Stock;
12: import net.mygwt.samples.resources.client.TestData;
13: import net.mygwt.ui.client.Style;
14: import net.mygwt.ui.client.event.BaseEvent;
15: import net.mygwt.ui.client.event.SelectionListener;
16: import net.mygwt.ui.client.widget.List;
17: import net.mygwt.ui.client.widget.ListItem;
18: import net.mygwt.ui.client.widget.WidgetContainer;
19: import net.mygwt.ui.client.widget.layout.FlowLayout;
20: import net.mygwt.ui.client.widget.menu.Menu;
21: import net.mygwt.ui.client.widget.menu.MenuItem;
22:
23: public class ListContextMenuPage extends Page {
24:
25: private int count = 1;
26:
27: protected void createWidget(WidgetContainer container) {
28: final List list = new List(Style.SINGLE);
29: list.setBorders(false);
30: list.setSize(200, 300);
31:
32: Folder stocks = TestData.getStocks();
33: for (int i = 0; i < 10; i++) {
34: Stock stock = (Stock) stocks.getChild(i);
35: ListItem item = new ListItem(stock.getName());
36: item.setIconStyle("icon-chart");
37: list.add(item);
38: }
39:
40: Menu contextMenu = new Menu();
41:
42: MenuItem insert = new MenuItem(Style.PUSH);
43: insert.setText("Insert Item");
44: insert.setIconStyle("icon-add");
45: insert.addSelectionListener(new SelectionListener() {
46: public void widgetSelected(BaseEvent be) {
47: ListItem item = list.getSelectedItem();
48: if (item != null) {
49: int index = list.indexOf(item);
50: ListItem newItem = new ListItem();
51: newItem.setText("New Item " + count++);
52: newItem.setIconStyle(item.getIconStyle());
53: list.insert(newItem, ++index);
54: }
55: }
56: });
57: contextMenu.add(insert);
58:
59: MenuItem remove = new MenuItem(Style.PUSH);
60: remove.setText("Remove Selected");
61: remove.setIconStyle("icon-delete");
62: remove.addSelectionListener(new SelectionListener() {
63: public void widgetSelected(BaseEvent be) {
64: ListItem[] selected = list.getSelection();
65: for (int i = 0; i < selected.length; i++) {
66: list.remove(selected[i]);
67: }
68: }
69: });
70: contextMenu.add(remove);
71:
72: list.setContextMenu(contextMenu);
73:
74: container.setLayout(new FlowLayout(8));
75: container.add(list);
76:
77: }
78:
79: }
|