001: /*
002: * MyGWT Widget Library
003: * Copyright(c) 2007, MyGWT.
004: * licensing@mygwt.net
005: *
006: * http://mygwt.net/license
007: */
008: package net.mygwt.samples.explorer.client;
009:
010: import net.mygwt.samples.resources.client.Folder;
011: import net.mygwt.samples.resources.client.Stock;
012: import net.mygwt.samples.resources.client.TestData;
013: import net.mygwt.ui.client.Events;
014: import net.mygwt.ui.client.Style;
015: import net.mygwt.ui.client.event.BaseEvent;
016: import net.mygwt.ui.client.event.Listener;
017: import net.mygwt.ui.client.event.SelectionListener;
018: import net.mygwt.ui.client.widget.Button;
019: import net.mygwt.ui.client.widget.ButtonBar;
020: import net.mygwt.ui.client.widget.Info;
021: import net.mygwt.ui.client.widget.List;
022: import net.mygwt.ui.client.widget.ListItem;
023: import net.mygwt.ui.client.widget.WidgetContainer;
024: import net.mygwt.ui.client.widget.layout.FlowLayout;
025: import net.mygwt.ui.client.widget.menu.Menu;
026: import net.mygwt.ui.client.widget.menu.MenuItem;
027:
028: import com.google.gwt.user.client.ui.HorizontalPanel;
029: import com.google.gwt.user.client.ui.VerticalPanel;
030:
031: public class ListPage extends Page {
032:
033: private int count = 1;
034:
035: protected void createWidget(WidgetContainer container) {
036: HorizontalPanel hp = new HorizontalPanel();
037: hp.setSpacing(8);
038:
039: Listener l = new Listener() {
040: public void handleEvent(BaseEvent be) {
041: List l = (List) be.widget;
042: int count = l.getSelection().length;
043: Info.show("Selection Changed",
044: "There are {0} items selected", "" + count);
045: }
046: };
047:
048: final List list = new List(Style.MULTI);
049: list.setBorders(false);
050: list.addListener(Events.SelectionChange, l);
051: list.setWidth(190);
052:
053: Menu contextMenu = new Menu();
054:
055: MenuItem insert = new MenuItem(Style.PUSH);
056: insert.setText("Insert Item");
057: insert.setIconStyle("icon-add");
058: insert.addSelectionListener(new SelectionListener() {
059: public void widgetSelected(BaseEvent be) {
060: ListItem item = list.getSelectedItem();
061: if (item != null) {
062: int index = list.indexOf(item);
063: ListItem newItem = new ListItem();
064: newItem.setText("New Item " + count++);
065: newItem.setIconStyle(item.getIconStyle());
066: list.insert(newItem, ++index);
067: }
068: }
069: });
070: contextMenu.add(insert);
071:
072: MenuItem remove = new MenuItem(Style.PUSH);
073: remove.setText("Remove Selected");
074: remove.setIconStyle("icon-delete");
075: remove.addSelectionListener(new SelectionListener() {
076: public void widgetSelected(BaseEvent be) {
077: ListItem[] selected = list.getSelection();
078: for (int i = 0; i < selected.length; i++) {
079: list.remove(selected[i]);
080: }
081: }
082: });
083: contextMenu.add(remove);
084:
085: list.setContextMenu(contextMenu);
086:
087: Folder stocks = TestData.getStocks();
088: for (int i = 0; i < 6; i++) {
089: Stock stock = (Stock) stocks.getChild(i);
090: ListItem item = new ListItem();
091:
092: item.setText(stock.getName());
093: item.setIconStyle("icon-chart");
094: list.add(item);
095: }
096: hp.add(list);
097:
098: final List list2 = new List(Style.SINGLE | Style.FLAT);
099: list2.setSize(210, 200);
100: list2.addListener(Events.SelectionChange, l);
101: stocks = TestData.getStocks();
102: for (int i = 0; i < stocks.getChildCount(); i++) {
103: Stock stock = (Stock) stocks.getChild(i);
104: ListItem item = new ListItem();
105: item.setText(stock.getName());
106: list2.add(item);
107: }
108:
109: hp.add(list2);
110:
111: VerticalPanel vp = new VerticalPanel();
112: vp.setSpacing(8);
113: ButtonBar buttonBar = new ButtonBar(Style.LEFT);
114: buttonBar.add(new Button("Select All", new SelectionListener() {
115: public void widgetSelected(BaseEvent be) {
116: list.selectAll();
117: }
118: }));
119: buttonBar.add(new Button("Select Last",
120: new SelectionListener() {
121: public void widgetSelected(BaseEvent be) {
122: list2.select(list2.getWidgetCount() - 1);
123: }
124: }));
125:
126: vp.add(buttonBar);
127:
128: container.setScrollEnabled(true);
129: container.setLayout(new FlowLayout());
130: container.add(vp);
131: container.add(hp);
132: list.setSize(210, 200);
133: }
134:
135: }
|