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.Collections;
015: import java.util.Comparator;
016: import java.util.Iterator;
017: import java.util.List;
018:
019: import net.mygwt.ui.client.Events;
020: import net.mygwt.ui.client.Style;
021: import net.mygwt.ui.client.data.DataLoader;
022: import net.mygwt.ui.client.data.LoadEvent;
023: import net.mygwt.ui.client.data.Loader;
024: import net.mygwt.ui.client.event.BaseEvent;
025: import net.mygwt.ui.client.event.Listener;
026: import net.mygwt.ui.client.widget.Component;
027: import net.mygwt.ui.client.widget.LoadingPanel;
028: import net.mygwt.ui.client.widget.table.Table;
029: import net.mygwt.ui.client.widget.table.TableColumn;
030: import net.mygwt.ui.client.widget.table.TableItem;
031:
032: import com.google.gwt.user.client.ui.Widget;
033:
034: /**
035: * A concrete viewer based on a <code>Table</code> widget.
036: *
037: * @see Table
038: */
039: public class TableViewer extends ColumnViewer {
040:
041: private ViewerCell viewerCell = new ViewerCell();
042: private Table table;
043: private Loader loader;
044: private Listener loadListener;
045:
046: /**
047: * Creates a new table viewer.
048: *
049: * @param table the table
050: */
051: public TableViewer(Table table) {
052: this .table = table;
053: table.getElement();
054: hookWidget(table);
055: }
056:
057: public void add(Object elem) {
058: renderItem(elem, table.getItemCount());
059: }
060:
061: public Widget findItem(Object elem) {
062: int size = table.getItemCount();
063: for (int i = 0; i < size; i++) {
064: TableItem item = table.getItem(i);
065: Object data = item.getData();
066:
067: if (getComparer() != null) {
068: if (getComparer().equals(elem, data)) {
069: return item;
070: } else {
071: continue;
072: }
073: }
074:
075: if (elem == data || (elem != null && elem.equals(data))) {
076: return item;
077: }
078: }
079: return null;
080: }
081:
082: public Object[] getActiveElements() {
083: ArrayList list = new ArrayList();
084: int count = table.getItemCount();
085: for (int i = 0; i < count; i++) {
086: TableItem item = table.getItem(i);
087: if (item.isVisible()) {
088: list.add(item.getData());
089: }
090: }
091: return list.toArray();
092: }
093:
094: /**
095: * Returns the viewer's table widget.
096: *
097: * @return the table
098: */
099: public Table getTable() {
100: return table;
101: }
102:
103: /**
104: * Returns the table viewer column for the specified column.
105: *
106: * @param columnId the column id
107: * @return the table viewer column
108: */
109: public TableViewerColumn getViewerColumn(int columnId) {
110: TableColumn column = table.getColumnModel().getColumn(columnId);
111: TableViewerColumn vc = (TableViewerColumn) column
112: .getData(ViewerColumn.COLUMN_VIEWER_KEY);
113: if (vc == null) {
114: vc = new TableViewerColumn(this , column);
115: }
116: return vc;
117: }
118:
119: public Widget getWidget() {
120: return table;
121: }
122:
123: public void refresh() {
124: if (loader != null) {
125: loader.reload();
126: } else {
127: super .refresh();
128: }
129: }
130:
131: public void remove(Object elem) {
132: TableItem item = (TableItem) findItem(elem);
133: if (item != null) {
134: table.remove(item);
135: removeElement(elem);
136: item.setData(null);
137: }
138: }
139:
140: public void setContentProvider(IContentProvider contentProvider) {
141: super .setContentProvider(contentProvider);
142: if (contentProvider instanceof Loader) {
143: bind((Loader) contentProvider);
144: }
145: }
146:
147: public void setSelection(ISelection selection, boolean reveal) {
148: List selected = selection.toList();
149: int ct = table.getItemCount();
150:
151: for (int i = 0; i < ct; i++) {
152: TableItem item = table.getItem(i);
153: Object elem = item.getData();
154: if (selected.contains(elem)) {
155: table.select(i);
156: } else {
157: table.deselect(i);
158: }
159: }
160: }
161:
162: public void update() {
163: int ct = table.getItemCount();
164: for (int i = 0; i < ct; i++) {
165: updateInternal(table.getItem(i));
166: }
167: }
168:
169: public void update(Object elem) {
170: TableItem item = (TableItem) findItem(elem);
171: if (item != null) {
172: item.setData(elem);
173: updateInternal(item);
174: }
175: }
176:
177: protected void doLocalSort(int col) {
178: ViewerColumn vc = getViewerColumn(col);
179:
180: final ViewerSorter sorter = vc.getViewerSorter();
181: final TableViewer fViewer = this ;
182:
183: List items = table.getItems();
184:
185: Collections.sort(items, new Comparator() {
186: public int compare(Object arg0, Object arg1) {
187: TableItem item1 = (TableItem) arg0;
188: TableItem item2 = (TableItem) arg1;
189: Object o1 = item1.getData();
190: Object o2 = item2.getData();
191: return sorter.compare(fViewer, o1, o2);
192: }
193:
194: });
195: if (getTable().getColumn(col).getSortDir() == Style.DESC) {
196: Collections.reverse(items);
197: }
198:
199: }
200:
201: protected void doRemoteSort(BaseEvent be) {
202: int col = be.index;
203: TableColumn column = getTable().getColumn(col);
204: if (column.isSortable()) {
205: int dir = column.getSortDir();
206: switch (dir) {
207: case Style.NONE:
208: case Style.DESC:
209: dir = Style.ASC;
210: break;
211: case Style.ASC:
212: dir = Style.DESC;
213: break;
214: }
215: loader.sort(column.getID(), dir);
216: table.getTableHeader().sort(col, dir);
217: be.index = dir;
218: be.doit = false;
219: }
220: }
221:
222: protected void doSort(BaseEvent be) {
223: if (loader != null && loader.getRemoteSort()) {
224: doRemoteSort(be);
225: return;
226: } else if (loader != null) {
227: // update loader sort info
228: TableColumn column = getTable().getColumn(be.index);
229: loader.setSortDir(be.size);
230: loader.setSortField(column.getID());
231: }
232:
233: int col = be.index;
234: doLocalSort(col);
235:
236: table.getTableHeader().sort(col, be.size);
237: table.getView().reorderItems();
238: be.doit = false;
239:
240: }
241:
242: protected List getSelectedFromWidget() {
243: ArrayList elems = new ArrayList();
244: for (int i = 0; i < table.getSelection().length; i++) {
245: TableItem item = (TableItem) table.getSelection()[i];
246: elems.add(item.getData());
247: }
248: return elems;
249: }
250:
251: protected void hookWidget(Component widget) {
252: super .hookWidget(widget);
253: table.addListener(Events.SortChange, new Listener() {
254: public void handleEvent(BaseEvent be) {
255: doSort(be);
256: }
257: });
258: }
259:
260: protected void insert(Object elem, int index) {
261: renderItem(elem, index);
262: TableItem item = (TableItem) findItem(elem);
263: if (item != null) {
264: item.setVisible(isFiltered(null, elem) ? false : true);
265: }
266: }
267:
268: protected void onInputReceived(Object input) {
269: renderTable();
270: }
271:
272: protected void preserveSelections(ISelection selection) {
273: Iterator it = selection.iterator();
274: while (it.hasNext()) {
275: Object elem = it.next();
276: TableItem item = (TableItem) findItem(elem);
277: if (item != null) {
278: table.select(item);
279: }
280: }
281: }
282:
283: protected void renderItem(Object elem, int index) {
284: int cols = table.getColumnCount();
285: String[] values = new String[cols];
286: String[] toolTips = new String[cols];
287:
288: for (int j = 0; j < cols; j++) {
289: CellLabelProvider lp = (CellLabelProvider) getViewerColumn(
290: j).getLabelProvider();
291: viewerCell.reset(elem, null, j, table.getColumn(j).getID());
292: lp.update(viewerCell);
293: values[j] = viewerCell.getText();
294: toolTips[j] = viewerCell.getToolTipText();
295: }
296: TableItem item = new TableItem(values);
297:
298: item.setData(elem);
299: item.setCellToolTips(toolTips);
300:
301: table.insert(item, index);
302: update(elem);
303: }
304:
305: protected void renderTable() {
306: table.removeAll();
307: Object[] elems = elements;
308: elems = sortElements(elems);
309: for (int i = 0; i < elems.length; i++) {
310: Object element = elems[i];
311: renderItem(element, i);
312: }
313: applyFilters();
314: }
315:
316: private void bind(Loader loader) {
317: if (loadListener == null) {
318: loadListener = new Listener() {
319: public void handleEvent(BaseEvent be) {
320: LoadEvent le = (LoadEvent) be;
321: switch (be.type) {
322: case Loader.BeforeLoad:
323: onBeforeLoad(le);
324: break;
325: case DataLoader.Load:
326: onLoad(le);
327: break;
328: case DataLoader.LoadException:
329: onLoadException(le);
330: break;
331: }
332: }
333: };
334: }
335: if (loader != null) {
336: loader.removeListener(DataLoader.BeforeLoad, loadListener);
337: loader.removeListener(DataLoader.Load, loadListener);
338: loader.removeListener(DataLoader.LoadException,
339: loadListener);
340: }
341: this .loader = loader;
342: this .loader.addListener(DataLoader.BeforeLoad, loadListener);
343: this .loader.addListener(DataLoader.Load, loadListener);
344: this .loader.addListener(DataLoader.LoadException, loadListener);
345: }
346:
347: private void onBeforeLoad(LoadEvent de) {
348: LoadingPanel.get().show(table);
349: }
350:
351: private void onLoad(LoadEvent de) {
352: ISelection sel = getSelection();
353: // apply sort if sorting locally
354: if (!de.loader.getRemoteSort()) {
355: if (de.loader.getSortField() != null) {
356: String field = de.loader.getSortField();
357: TableColumn column = table.getColumn(field);
358: if (column != null) {
359: int index = table.getColumnModel().indexOf(column);
360: doLocalSort(index);
361: }
362: }
363: }
364: setInput(de.result.getData());
365: preserveSelections(sel);
366: LoadingPanel.get().hide();
367:
368: }
369:
370: private void onLoadException(LoadEvent le) {
371: LoadingPanel.get().hide();
372: }
373:
374: private void updateInternal(TableItem item) {
375: Object elem = item.getData();
376: int cols = table.getColumnCount();
377: ;
378: for (int j = 0; j < cols; j++) {
379: viewerCell.reset(elem, item, j, table.getColumn(j).getID());
380: CellLabelProvider lp = (CellLabelProvider) getViewerColumn(
381: j).getLabelProvider();
382: lp.update(viewerCell);
383: String text = viewerCell.getText();
384: item.setValue(j, text);
385: item.setCellToolTip(j, viewerCell.getToolTipText());
386:
387: String style = viewerCell.getTextStyle();
388: if (style != null) {
389: item.setCellStyle(j, style);
390: }
391: }
392: }
393:
394: }
|