01: /*******************************************************************************
02: * Copyright (c) 2007 MyGWT and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Darrell Meyer <darrell@mygwt.net> - initial API and implementation
10: *******************************************************************************/package net.mygwt.ui.client.viewer;
11:
12: import net.mygwt.ui.client.data.ChangeEvent;
13: import net.mygwt.ui.client.data.ChangeListener;
14: import net.mygwt.ui.client.data.Model;
15:
16: /**
17: * A <code>TreeContentProvider</code> implementation for <code>Model</code>
18: * instances.
19: *
20: * @see Model
21: */
22: public class ModelTreeContentProvider implements ITreeContentProvider,
23: ChangeListener {
24:
25: /**
26: * Specifies if model changes should be monitored. Default value is
27: * <code>true</code>.
28: */
29: public boolean monitorChanges = true;
30:
31: protected ITreeViewer viewer;
32:
33: public void getChildren(Object parent,
34: IAsyncContentCallback callback) {
35: callback.setElements(((Model) parent).getChildren().toArray());
36: }
37:
38: public Object getParent(Object element) {
39: return ((Model) element).getParent();
40: }
41:
42: public boolean hasChildren(Object element) {
43: return ((Model) element).getChildCount() > 0;
44: }
45:
46: public void getElements(Object input, IAsyncContentCallback callback) {
47: callback.setElements(((Model) input).getChildren().toArray());
48: }
49:
50: public void inputChanged(Viewer viewer, Object oldInput,
51: Object newInput) {
52: this .viewer = (ITreeViewer) viewer;
53: if (monitorChanges) {
54: if (oldInput != null) {
55: Model m = (Model) oldInput;
56: m.removeChangeListener(this );
57: }
58: Model m = (Model) newInput;
59: m.addChangeListener(this );
60: }
61: }
62:
63: public void modelChanged(ChangeEvent event) {
64: int type = event.type;
65: switch (type) {
66: case Model.Add: {
67: Model parent = event.source;
68: Model add = event.item;
69: viewer.add(parent, add);
70: break;
71: }
72: case Model.Insert: {
73: Model parent = event.source;
74: Model insert = event.item;
75: viewer.insert(parent, insert, event.index);
76: break;
77: }
78: case Model.Remove: {
79: viewer.remove(event.item);
80: break;
81: }
82: case Model.Update: {
83: viewer.update(event.item);
84: break;
85: }
86:
87: }
88: }
89:
90: }
|