01: /*******************************************************************************
02: * Copyright (c) 2006 IBM Corporation 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: * IBM Corporation - initial API and implementation
10: * Brad Reynolds - bug 164247
11: * Brad Reynolds - bug 164134
12: ******************************************************************************/package org.eclipse.jface.databinding.viewers;
13:
14: import java.util.Set;
15:
16: import org.eclipse.core.databinding.observable.map.IMapChangeListener;
17: import org.eclipse.core.databinding.observable.map.IObservableMap;
18: import org.eclipse.core.databinding.observable.map.MapChangeEvent;
19: import org.eclipse.jface.viewers.ILabelProvider;
20: import org.eclipse.jface.viewers.ITableLabelProvider;
21: import org.eclipse.jface.viewers.LabelProvider;
22: import org.eclipse.jface.viewers.LabelProviderChangedEvent;
23: import org.eclipse.swt.graphics.Image;
24:
25: /**
26: * @since 1.1
27: *
28: */
29: public class ObservableMapLabelProvider extends LabelProvider implements
30: ILabelProvider, ITableLabelProvider {
31:
32: private final IObservableMap[] attributeMaps;
33:
34: private IMapChangeListener mapChangeListener = new IMapChangeListener() {
35: public void handleMapChange(MapChangeEvent event) {
36: Set affectedElements = event.diff.getChangedKeys();
37: LabelProviderChangedEvent newEvent = new LabelProviderChangedEvent(
38: ObservableMapLabelProvider.this , affectedElements
39: .toArray());
40: fireLabelProviderChanged(newEvent);
41: }
42: };
43:
44: /**
45: * @param attributeMap
46: */
47: public ObservableMapLabelProvider(IObservableMap attributeMap) {
48: this (new IObservableMap[] { attributeMap });
49: }
50:
51: /**
52: * @param attributeMaps
53: */
54: public ObservableMapLabelProvider(IObservableMap[] attributeMaps) {
55: this .attributeMaps = attributeMaps;
56: for (int i = 0; i < attributeMaps.length; i++) {
57: attributeMaps[i].addMapChangeListener(mapChangeListener);
58: }
59: }
60:
61: public void dispose() {
62: for (int i = 0; i < attributeMaps.length; i++) {
63: attributeMaps[i].removeMapChangeListener(mapChangeListener);
64: }
65: super .dispose();
66: }
67:
68: public Image getImage(Object element) {
69: return null;
70: }
71:
72: public String getText(Object element) {
73: return getColumnText(element, 0);
74: }
75:
76: public Image getColumnImage(Object element, int columnIndex) {
77: return null;
78: }
79:
80: public String getColumnText(Object element, int columnIndex) {
81: if (columnIndex < attributeMaps.length) {
82: Object result = attributeMaps[columnIndex].get(element);
83: return result == null ? "" : result.toString(); //$NON-NLS-1$
84: }
85: return null;
86: }
87:
88: }
|