KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > databinding > viewers > ObservableMapLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Brad Reynolds - bug 164247
11  * Brad Reynolds - bug 164134
12  ******************************************************************************/

13
14 package org.eclipse.jface.databinding.viewers;
15
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.databinding.observable.map.IMapChangeListener;
19 import org.eclipse.core.databinding.observable.map.IObservableMap;
20 import org.eclipse.core.databinding.observable.map.MapChangeEvent;
21 import org.eclipse.jface.viewers.ILabelProvider;
22 import org.eclipse.jface.viewers.ITableLabelProvider;
23 import org.eclipse.jface.viewers.LabelProvider;
24 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
25 import org.eclipse.swt.graphics.Image;
26
27 /**
28  * @since 1.1
29  *
30  */

31 public class ObservableMapLabelProvider extends LabelProvider
32         implements ILabelProvider, ITableLabelProvider {
33
34     private final IObservableMap[] attributeMaps;
35
36     private IMapChangeListener mapChangeListener = new IMapChangeListener() {
37         public void handleMapChange(MapChangeEvent event) {
38             Set JavaDoc affectedElements = event.diff.getChangedKeys();
39             LabelProviderChangedEvent newEvent = new LabelProviderChangedEvent(
40                     ObservableMapLabelProvider.this, affectedElements
41                             .toArray());
42             fireLabelProviderChanged(newEvent);
43         }
44     };
45
46     /**
47      * @param attributeMap
48      */

49     public ObservableMapLabelProvider(IObservableMap attributeMap) {
50         this(new IObservableMap[] { attributeMap });
51     }
52
53     /**
54      * @param attributeMaps
55      */

56     public ObservableMapLabelProvider(IObservableMap[] attributeMaps) {
57         this.attributeMaps = attributeMaps;
58         for (int i = 0; i < attributeMaps.length; i++) {
59             attributeMaps[i].addMapChangeListener(mapChangeListener);
60         }
61     }
62
63     public void dispose() {
64         for (int i = 0; i < attributeMaps.length; i++) {
65             attributeMaps[i].removeMapChangeListener(mapChangeListener);
66         }
67         super.dispose();
68     }
69
70     public Image getImage(Object JavaDoc element) {
71         return null;
72     }
73
74     public String JavaDoc getText(Object JavaDoc element) {
75         return getColumnText(element, 0);
76     }
77
78     public Image getColumnImage(Object JavaDoc element, int columnIndex) {
79         return null;
80     }
81
82     public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
83         if (columnIndex < attributeMaps.length) {
84             Object JavaDoc result = attributeMaps[columnIndex].get(element);
85             return result == null ? "" : result.toString(); //$NON-NLS-1$
86
}
87         return null;
88     }
89
90 }
91
Popular Tags