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.Music;
012: import net.mygwt.samples.resources.client.TestData;
013: import net.mygwt.ui.client.Style;
014: import net.mygwt.ui.client.data.Model;
015: import net.mygwt.ui.client.event.BaseEvent;
016: import net.mygwt.ui.client.event.SelectionListener;
017: import net.mygwt.ui.client.viewer.ModelComparator;
018: import net.mygwt.ui.client.viewer.ModelLabelProvider;
019: import net.mygwt.ui.client.viewer.ModelTreeContentProvider;
020: import net.mygwt.ui.client.viewer.ISelectionChangedListener;
021: import net.mygwt.ui.client.viewer.SelectionChangedEvent;
022: import net.mygwt.ui.client.viewer.TreeViewer;
023: import net.mygwt.ui.client.viewer.ViewerSorter;
024: import net.mygwt.ui.client.widget.Button;
025: import net.mygwt.ui.client.widget.ButtonBar;
026: import net.mygwt.ui.client.widget.Info;
027: import net.mygwt.ui.client.widget.WidgetContainer;
028: import net.mygwt.ui.client.widget.layout.FlowLayout;
029: import net.mygwt.ui.client.widget.tree.Tree;
030:
031: import com.google.gwt.user.client.ui.VerticalPanel;
032:
033: public class TreeViewerPage extends Page {
034:
035: private int count = 1;
036:
037: protected void createWidget(WidgetContainer container) {
038: final Folder folder = TestData.getTreeModel();
039:
040: Tree tree = new Tree();
041: tree.setItemImageStyle("icon-music");
042:
043: final TreeViewer viewer = new TreeViewer(tree);
044: viewer.setContentProvider(new ModelTreeContentProvider());
045: viewer.setLabelProvider(new ModelLabelProvider());
046: viewer.setSorter(new ViewerSorter(new ModelComparator()));
047:
048: viewer.addSelectionListener(new ISelectionChangedListener() {
049: public void selectionChanged(SelectionChangedEvent se) {
050: Model m = (Model) se.getSelection().getFirstElement();
051: if (m != null) {
052: Info.show("Selection Changed", "{0} was selected",
053: m.getAsString("name"));
054: }
055: }
056: });
057:
058: viewer.setInput(folder);
059:
060: ButtonBar buttonBar = new ButtonBar(Style.LEFT);
061: buttonBar.add(new Button("Remove Selected",
062: new SelectionListener() {
063: public void widgetSelected(BaseEvent be) {
064: if (!viewer.getSelection().isEmpty()) {
065: Model m = (Model) viewer.getSelection()
066: .getFirstElement();
067: Model p = m.getParent();
068: if (p != null) {
069: p.remove(m);
070: }
071: }
072:
073: }
074: }));
075: buttonBar.add(new Button("Add", new SelectionListener() {
076: public void widgetSelected(BaseEvent be) {
077: Music music = new Music("New Music " + count++,
078: "Unknown", "Unknown");
079: Model m = folder.getChild(0);
080:
081: // m will fire a add change event. The ModelContentProvider is
082: // listening and will then instruct the viewer to add the new object.
083: m.add(music);
084:
085: viewer.setExpanded(m, true);
086: }
087: }));
088:
089: buttonBar.add(new Button("Update", new SelectionListener() {
090: public void widgetSelected(BaseEvent be) {
091: Model m = folder.getChild(0);
092: m.set("name", "I have been updated");
093: }
094: }));
095:
096: VerticalPanel vp = new VerticalPanel();
097: vp.setSpacing(10);
098: vp.add(buttonBar);
099: vp.add(tree);
100:
101: container.setLayout(new FlowLayout());
102: container.add(vp);
103: }
104:
105: }
|