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.CellLabelProvider;
018: import net.mygwt.ui.client.viewer.ISelectionChangedListener;
019: import net.mygwt.ui.client.viewer.ModelLabelProvider;
020: import net.mygwt.ui.client.viewer.ModelTreeContentProvider;
021: import net.mygwt.ui.client.viewer.SelectionChangedEvent;
022: import net.mygwt.ui.client.viewer.TreeTableViewer;
023: import net.mygwt.ui.client.viewer.TreeTableViewerColumn;
024: import net.mygwt.ui.client.viewer.ViewerCell;
025: import net.mygwt.ui.client.widget.Info;
026: import net.mygwt.ui.client.widget.WidgetContainer;
027: import net.mygwt.ui.client.widget.layout.FillLayout;
028: import net.mygwt.ui.client.widget.menu.Menu;
029: import net.mygwt.ui.client.widget.menu.MenuItem;
030: import net.mygwt.ui.client.widget.treetable.TreeTable;
031: import net.mygwt.ui.client.widget.treetable.TreeTableColumn;
032: import net.mygwt.ui.client.widget.treetable.TreeTableColumnModel;
033: import net.mygwt.ui.client.widget.treetable.TreeTableItem;
034:
035: public class TreeTableViewerPage extends Page {
036: private int count = 1;
037:
038: protected void createWidget(WidgetContainer container) {
039: final Folder folder = TestData.getTreeModel();
040:
041: TreeTableColumn[] columns = new TreeTableColumn[3];
042:
043: columns[0] = new TreeTableColumn("Name", 0.40f);
044: columns[0].setMinWidth(75);
045:
046: columns[1] = new TreeTableColumn("Author", 0.30f);
047:
048: columns[2] = new TreeTableColumn("Genre", 0.30f);
049: columns[2].setAlignment(Style.RIGHT);
050:
051: TreeTableColumnModel cm = new TreeTableColumnModel(columns);
052:
053: final TreeTable table = new TreeTable(Style.SINGLE, cm);
054: table.setItemImageStyle("icon-music");
055:
056: final TreeTableViewer viewer = new TreeTableViewer(table);
057: viewer.setContentProvider(new ModelTreeContentProvider());
058: viewer.setLabelProvider(new ModelLabelProvider());
059:
060: TreeTableViewerColumn col = viewer.getViewerColumn(1);
061: col.setLabelProvider(new CellLabelProvider() {
062: public void update(ViewerCell cell) {
063: if (cell.getElement() instanceof Music) {
064: cell.setText(((Music) cell.getElement())
065: .getAuthor());
066: }
067: cell.setToolTipText(cell.getText());
068: }
069: });
070:
071: col = viewer.getViewerColumn(2);
072: col.setLabelProvider(new CellLabelProvider() {
073: public void update(ViewerCell cell) {
074: if (cell.getElement() instanceof Music) {
075: cell
076: .setText(((Music) cell.getElement())
077: .getGenre());
078: }
079: cell.setToolTipText("<b>This is content:</b> "
080: + cell.getText());
081: }
082: });
083:
084: viewer.addSelectionListener(new ISelectionChangedListener() {
085: public void selectionChanged(SelectionChangedEvent se) {
086: Model m = (Model) se.getSelection().getFirstElement();
087: Info.show("Selection Changed", "{0} was selected", m
088: .getAsString("name"));
089: }
090: });
091:
092: Menu contextMenu = new Menu();
093:
094: MenuItem insert = new MenuItem(Style.PUSH);
095: insert.setText("Insert Item");
096: insert.setIconStyle("icon-add");
097: insert.addSelectionListener(new SelectionListener() {
098: public void widgetSelected(BaseEvent be) {
099: Music music = new Music("Add Child: " + count++,
100: "Unknown", "Unknown");
101: Model m = (Model) viewer.getSelection()
102: .getFirstElement();
103: m.add(music);
104: }
105: });
106: contextMenu.add(insert);
107:
108: MenuItem remove = new MenuItem(Style.PUSH);
109: remove.setText("Remove Selected");
110: remove.setIconStyle("icon-delete");
111: remove.addSelectionListener(new SelectionListener() {
112: public void widgetSelected(BaseEvent be) {
113: TreeTableItem item = (TreeTableItem) table
114: .getSelectedItem();
115: if (item != null) {
116: item.getParentItem().remove(item);
117: }
118: }
119: });
120: contextMenu.add(remove);
121:
122: table.setContextMenu(contextMenu);
123: viewer.setInput(folder);
124:
125: container.setLayout(new FillLayout(8));
126: container.add(table);
127: }
128:
129: }
|