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.TestData;
011: import net.mygwt.ui.client.Events;
012: import net.mygwt.ui.client.Style;
013: import net.mygwt.ui.client.event.BaseEvent;
014: import net.mygwt.ui.client.event.Listener;
015: import net.mygwt.ui.client.event.SelectionListener;
016: import net.mygwt.ui.client.util.TreeBuilder;
017: import net.mygwt.ui.client.widget.Button;
018: import net.mygwt.ui.client.widget.ButtonBar;
019: import net.mygwt.ui.client.widget.Info;
020: import net.mygwt.ui.client.widget.WidgetContainer;
021: import net.mygwt.ui.client.widget.layout.FlowLayout;
022: import net.mygwt.ui.client.widget.tree.Tree;
023: import net.mygwt.ui.client.widget.tree.TreeItem;
024:
025: import com.google.gwt.user.client.ui.ChangeListener;
026: import com.google.gwt.user.client.ui.HorizontalPanel;
027: import com.google.gwt.user.client.ui.ListBox;
028: import com.google.gwt.user.client.ui.VerticalPanel;
029: import com.google.gwt.user.client.ui.Widget;
030:
031: public class CheckBoxTreePage extends Page {
032:
033: private String path;
034: private Tree tree;
035:
036: protected void createWidget(WidgetContainer container) {
037: final ListBox listbox = new ListBox();
038: listbox.addItem("Cascade Parents", "0");
039: listbox.addItem("Cascade Children", "1");
040: listbox.addItem("Cascade None", "2");
041: listbox.addChangeListener(new ChangeListener() {
042: public void onChange(Widget sender) {
043: String val = listbox.getValue(listbox
044: .getSelectedIndex());
045: if (val.equals("0")) {
046: tree.setCheckStyle(Style.PARENT);
047: } else if (val.equals("1")) {
048: tree.setCheckStyle(Style.CHILDREN);
049: } else if (val.equals("2")) {
050: tree.setCheckStyle(Style.SINGLE);
051: }
052: }
053:
054: });
055:
056: tree = new Tree(Style.SINGLE | Style.CHECK);
057: tree.addListener(Events.SelectionChange, new Listener() {
058: public void handleEvent(BaseEvent be) {
059: TreeItem item = (TreeItem) be.item;
060: Info.show("Selection Changed",
061: "The '{0}' item was selected", item.getText());
062: }
063: });
064: tree.setItemImageStyle("icon-music");
065:
066: // quick way to build tree from Model instances.
067: TreeBuilder.buildTree(tree, TestData.getTreeModel());
068:
069: ButtonBar buttonBar = new ButtonBar(Style.LEFT);
070: buttonBar.add(new Button("Copy Selected Path",
071: new SelectionListener() {
072: public void widgetSelected(BaseEvent be) {
073: TreeItem item = tree.getSelectedItem();
074: if (item == null)
075: return;
076: path = item.getPath();
077: String[] ids = path.split(",");
078: StringBuffer sb = new StringBuffer();
079: for (int i = 0; i < ids.length; i++) {
080: TreeItem ti = tree.getItemById(ids[i]);
081: sb.append("/" + ti.getText());
082: }
083: Info.show("Get Path",
084: "The current path is {0}", sb
085: .toString().substring(1));
086: }
087: }));
088: buttonBar.add(new Button("Restore Path",
089: new SelectionListener() {
090: public void widgetSelected(BaseEvent be) {
091: tree.expandPath(path);
092: }
093: }));
094:
095: buttonBar.add(new Button("Get Checked",
096: new SelectionListener() {
097: public void widgetSelected(BaseEvent be) {
098: TreeItem[] items = tree.getChecked();
099: StringBuffer sb = new StringBuffer();
100: for (int i = 0; i < items.length; i++) {
101: TreeItem item = items[i];
102: sb.append(", " + item.getText());
103: }
104: String s = sb.toString();
105: if (s.length() > 1)
106: s = s.substring(2);
107: if (s.length() > 100)
108: s = s.substring(0, 100) + "...";
109: Info.show("Checked Items", s, "");
110: }
111: }));
112:
113: VerticalPanel vp = new VerticalPanel();
114: vp.setSpacing(10);
115:
116: HorizontalPanel hp = new HorizontalPanel();
117: hp.setSpacing(4);
118: hp.add(buttonBar);
119: hp.add(listbox);
120: hp.setCellVerticalAlignment(listbox,
121: HorizontalPanel.ALIGN_MIDDLE);
122:
123: vp.add(hp);
124: vp.add(tree);
125:
126: container.setLayout(new FlowLayout());
127: container.add(vp);
128: }
129:
130: }
|