001: /*******************************************************************************
002: * Copyright (c) 2007 MyGWT.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Darrell Meyer <darrell@mygwt.net> - initial API and implementation
010: *******************************************************************************/package net.mygwt.ui.client.viewer;
011:
012: import java.util.List;
013:
014: import net.mygwt.ui.client.data.ChangeEvent;
015: import net.mygwt.ui.client.data.ChangeListener;
016: import net.mygwt.ui.client.data.Model;
017:
018: /**
019: * A <code>ContentProvider</code> implementation for <code>Models</code>.
020: */
021: public class ModelContentProvider implements
022: IStructuredContentProvider, ChangeListener {
023:
024: /**
025: * monitorChanges specifies if model changes should be monitored. Default
026: * value is <code>true</code>.
027: */
028: public boolean monitorChanges = true;
029:
030: /**
031: * ensureChild specifies if model changes should be tested to ensure the
032: * updated model instance is a direct child of the viewer's input. Default
033: * value is <code>true</code>.
034: */
035: public boolean ensureChild = true;
036:
037: protected Viewer viewer;
038:
039: public void getElements(Object input, IAsyncContentCallback callback) {
040: if (input instanceof Model) {
041: callback.setElements(((Model) input).getChildren()
042: .toArray());
043: return;
044: } else if (input instanceof List) {
045: callback.setElements(((List) input).toArray(new Model[0]));
046: return;
047: }
048: callback.setElements(null);
049: }
050:
051: public void inputChanged(Viewer viewer, Object oldInput,
052: Object newInput) {
053: this .viewer = viewer;
054: if (monitorChanges) {
055: if (oldInput != null) {
056: if (oldInput instanceof List) {
057: List list = (List) oldInput;
058: int count = list.size();
059: for (int i = 0; i < count; i++) {
060: Model m = (Model) list.get(i);
061: m.removeChangeListener(this );
062: }
063:
064: } else if (oldInput instanceof Model) {
065: Model m = (Model) oldInput;
066: m.removeChangeListener(this );
067: }
068: }
069: if (newInput instanceof List) {
070: List list = (List) newInput;
071: int count = list.size();
072: for (int i = 0; i < count; i++) {
073: Model m = (Model) list.get(i);
074: m.addChangeListener(this );
075: }
076: } else if (newInput instanceof Model) {
077: Model m = (Model) newInput;
078: m.addChangeListener(this );
079: }
080:
081: }
082: }
083:
084: public void modelChanged(ChangeEvent event) {
085: int type = event.type;
086: switch (type) {
087: case Model.Add: {
088: if (!ensureChild
089: || (ensureChild && event.source == viewer
090: .getInput())) {
091: viewer.add(event.item);
092: }
093: break;
094: }
095: case Model.Insert: {
096: if (!ensureChild
097: || (ensureChild && event.source == viewer
098: .getInput())) {
099: viewer.insert(event.item, event.index);
100: }
101: break;
102: }
103: case Model.Remove: {
104: if (!ensureChild
105: || (ensureChild && event.source == viewer
106: .getInput())) {
107: viewer.remove(event.item);
108: }
109: break;
110: }
111: case Model.Update: {
112: viewer.update(event.item);
113: break;
114: }
115: }
116: }
117:
118: }
|