001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
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: * IBM Corporation - initial API and implementation
010: * Darrell Meyer <darrell@mygwt.net> - derived implementation
011: *******************************************************************************/package net.mygwt.ui.client.viewer;
012:
013: import java.util.ArrayList;
014: import java.util.Iterator;
015:
016: import net.mygwt.ui.client.Events;
017: import net.mygwt.ui.client.event.BaseEvent;
018: import net.mygwt.ui.client.event.Listener;
019: import net.mygwt.ui.client.widget.Component;
020: import net.mygwt.ui.client.widget.List;
021: import net.mygwt.ui.client.widget.ListItem;
022:
023: import com.google.gwt.user.client.ui.Widget;
024:
025: /**
026: * A concrete viewer based for a <code>List</code>.
027: *
028: * @see List
029: */
030: public class ListViewer extends Viewer implements ICheckable {
031:
032: private List list;
033: private ArrayList checkChangeListener;
034:
035: /**
036: * Creates a new list intance.
037: *
038: * @param list the list
039: */
040: public ListViewer(List list) {
041: this .list = list;
042: list.getElement();
043: hookWidget(list);
044: }
045:
046: public void add(Object elem) {
047: renderItem(elem, list.getWidgetCount());
048: }
049:
050: public void addCheckStateListener(ICheckStateListener listener) {
051: if (checkChangeListener == null) {
052: checkChangeListener = new ArrayList();
053: }
054: if (!checkChangeListener.contains(listener)) {
055: checkChangeListener.add(listener);
056: }
057: }
058:
059: public Widget findItem(Object elem) {
060: int size = list.getWidgetCount();
061: for (int i = 0; i < size; i++) {
062: ListItem item = list.getItem(i);
063: Object data = item.getData();
064: if (getComparer() != null) {
065: if (getComparer().equals(elem, data)) {
066: return item;
067: } else {
068: continue;
069: }
070: }
071: if (elem == data || (elem != null && elem.equals(data))) {
072: return item;
073: }
074: }
075: return null;
076: }
077:
078: public Object[] getActiveElements() {
079: ArrayList temp = new ArrayList();
080: int count = list.getItemCount();
081: for (int i = 0; i < count; i++) {
082: ListItem item = list.getItem(i);
083: if (item.isVisible()) {
084: temp.add(item.getData());
085: }
086: }
087: return temp.toArray();
088: }
089:
090: public boolean getChecked(Object element) {
091: ListItem item = (ListItem) findItem(element);
092: if (item != null) {
093: return item.isChecked();
094: }
095: return false;
096: }
097:
098: /**
099: * Returns the current checked selection.
100: *
101: * @return the checked elements
102: */
103: public ISelection getCheckedSelection() {
104: ListItem[] items = list.getChecked();
105: if (items.length == 0) {
106: return DefaultSelection.EMPTY;
107: }
108: ArrayList checked = new ArrayList();
109: for (int i = 0; i < items.length; i++) {
110: checked.add(items[i].getData());
111: }
112: return new DefaultSelection(checked);
113: }
114:
115: /**
116: * Returns the viewer's list widget.
117: *
118: * @return the list
119: */
120: public List getList() {
121: return list;
122: }
123:
124: public Widget getWidget() {
125: return list;
126: }
127:
128: public void insert(Object elem, int index) {
129: renderItem(elem, index);
130: }
131:
132: public void remove(Object elem) {
133: ListItem item = (ListItem) findItem(elem);
134: if (item != null) {
135: removeElement(elem);
136: item.setData(null);
137: }
138: }
139:
140: public void removeCheckStateListener(ICheckStateListener listener) {
141: if (checkChangeListener != null) {
142: checkChangeListener.remove(listener);
143: }
144: }
145:
146: public boolean setChecked(Object element, boolean state) {
147: ListItem item = (ListItem) findItem(element);
148: if (item != null) {
149: item.setChecked(state);
150: return true;
151: }
152: return false;
153: }
154:
155: public void setSelection(ISelection selection, boolean reveal) {
156: java.util.List selected = selection.toList();
157: int ct = list.getWidgetCount();
158: for (int i = 0; i < ct; i++) {
159: ListItem item = list.getItem(i);
160: Object elem = item.getData();
161: if (selected.contains(elem)) {
162: list.select(i);
163: } else {
164: list.deselect(i);
165: }
166: }
167: }
168:
169: public void update() {
170: int ct = list.getWidgetCount();
171: for (int i = 0; i < ct; i++) {
172: updateInternal(list.getItem(i));
173: }
174: }
175:
176: public void update(Object elem) {
177: ListItem item = (ListItem) findItem(elem);
178: if (item != null) {
179: item.setData(elem);
180: updateInternal(item);
181: }
182:
183: }
184:
185: protected java.util.List getSelectedFromWidget() {
186: ArrayList elems = new ArrayList();
187: for (int i = 0; i < list.getSelection().length; i++) {
188: ListItem item = (ListItem) list.getSelection()[i];
189: elems.add(item.getData());
190: }
191: return elems;
192: }
193:
194: protected void hookWidget(Component widget) {
195: super .hookWidget(widget);
196: widget.addListener(Events.CheckChange, new Listener() {
197: public void handleEvent(BaseEvent be) {
198: fireCheckStateChanged(be);
199: }
200: });
201: }
202:
203: protected void onInputReceived(Object input) {
204: renderList();
205: }
206:
207: protected void renderItem(Object elem, int position) {
208: LabelProvider lp = (LabelProvider) getLabelProvider();
209: ListItem item = new ListItem();
210: item.setData(elem);
211: item.setText(lp.getText(elem));
212: String style = lp.getIconStyle(elem);
213: if (style != null) {
214: item.setIconStyle(lp.getIconStyle(elem));
215: }
216: list.insert(item, position);
217: }
218:
219: protected void renderList() {
220: list.removeAll();
221: Object[] elems = filter(elements);
222: elems = sortElements(elems);
223: for (int i = 0; i < elems.length; i++) {
224: Object element = elems[i];
225: renderItem(element, i);
226: }
227: }
228:
229: private void fireCheckStateChanged(BaseEvent be) {
230: if (checkChangeListener != null) {
231: ListItem item = (ListItem) be.item;
232: CheckStateChangedEvent evt = new CheckStateChangedEvent(
233: this , item.getData(), item.isChecked());
234: Iterator it = checkChangeListener.iterator();
235: while (it.hasNext()) {
236: ((ICheckStateListener) it.next())
237: .checkStateChanged(evt);
238: }
239: }
240: }
241:
242: private void updateInternal(ListItem item) {
243: LabelProvider lp = (LabelProvider) getLabelProvider();
244: Object elem = item.getData();
245: item.setText(lp.getText(elem));
246: String style = lp.getIconStyle(elem);
247: if (style != null) {
248: item.setIconStyle(lp.getIconStyle(elem));
249: }
250: }
251:
252: }
|