Skip to main content
summaryrefslogtreecommitdiffstats
blob: 23b682cde228098c898f1dfa077b7970d06b4a9f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*******************************************************************************
 * Copyright (c) 2007, 2015 Wind River Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.dsf.ui.viewmodel.properties;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.cdt.dsf.concurrent.ImmediateExecutor;
import org.eclipse.cdt.dsf.concurrent.ThreadSafe;
import org.eclipse.cdt.dsf.internal.ui.DsfUIPlugin;
import org.eclipse.cdt.dsf.ui.concurrent.ViewerDataRequestMonitor;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementLabelProvider;
import org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate;

/**
 * A configurable label provider which uses element's property label provider 
 * to set element's label attributes.
 * <p>
 * When this provider is registered for an element it calculates the properties
 * that need to be retrieved based on view's active columns, and then it calls the
 * element's property provider to retrieve those properties.  After the property
 * values are retrieved, they are processed in order to produce correct label text, 
 * images, fonts, and colors, for the given element.
 * 
 * @since 2.0 - Renamed from PropertyBasedLabelProvider 
 */
@ThreadSafe
public class PropertiesBasedLabelProvider 
    implements IElementLabelProvider
{
    public static final String ID_COLUMN_NO_COLUMNS = "ID_COLUMN_NO_COLUMNS"; //$NON-NLS-1$
    
    /**
     * Attribute information for each column by column ID.
     */
    private Map<String, LabelColumnInfo> fColumnInfos = Collections.synchronizedMap(new HashMap<String,LabelColumnInfo>());
    
    private IPropertiesUpdateListener[] fListeners = new IPropertiesUpdateListener[0];
    
    /**
     * Standard constructor.  A property based label constructor does not 
     * initialize column attribute information {@link #setColumnInfo(String, LabelColumnInfo)} 
     * must be called to configure each column.
     */
    public PropertiesBasedLabelProvider() {
    }
    
    /**
     * Sets the given column info object for the given column ID.  This column
     * info will be used to generate the label when the given column is visibile.
     * 
     * @param columnId Column ID that the given column info is being registered for.
     * @param info Column 'info' object containing column attributes.
     * @return The previous column info object configured for this ID.
     */
    public LabelColumnInfo setColumnInfo(String columnId, LabelColumnInfo info) {
        LabelColumnInfo oldInfo = fColumnInfos.put(columnId, info);
        return oldInfo;
    }

    /**
     * Returns the given column info object for the given column ID.  
     * @param columnId Column ID to retrieve the column info for.
     * 
     * @param columnId Column ID that the given column info is being registered for.
     * @@return Column 'info' object containing column attributes.
     */
    public LabelColumnInfo getColumnInfo(String columnId) {
        return fColumnInfos.get(columnId);
    }

    /**
     * Adds a listener for properties updates generated by this label provider.
     * 
     * @since 2.2
     */
    public void addPropertiesUpdateListener(IPropertiesUpdateListener listener) {
        synchronized(this) {
            if (!Arrays.asList(fListeners).contains(listener)) {
                IPropertiesUpdateListener[] newListeners = new IPropertiesUpdateListener[fListeners.length + 1];
                System.arraycopy(fListeners, 0, newListeners, 0, fListeners.length);
                newListeners[fListeners.length] = listener;
                fListeners = newListeners;
            }
        }
    }

    /**
     * Removes a listener for properties updates generated by this label provider.
     * 
     * @since 2.2
     */
    public void removePropertiesUpdateListener(IPropertiesUpdateListener listener) {
        synchronized(this) {
            int listenerIdx = Arrays.asList(fListeners).indexOf(listener);
            
            if (listenerIdx != -1) {
                IPropertiesUpdateListener[] newListeners = new IPropertiesUpdateListener[fListeners.length - 1];
                System.arraycopy(fListeners, 0, newListeners, 0, listenerIdx);
                System.arraycopy(fListeners, listenerIdx + 1, newListeners, listenerIdx, newListeners.length - listenerIdx);
                fListeners = newListeners;
            }
        }
    }

	/**
	 * In addition to guarantees on [labelUpdates] declared by
	 * {@link IElementLabelProvider}, we further require/assume that all the
	 * model elements referenced by [labelUpdates] adapt to the same
	 * {@link IElementPropertiesProvider}.
	 * 
	 * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IElementLabelProvider#update(org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate[])
	 */
    @Override
	public void update(final ILabelUpdate[] labelUpdates) {
        IElementPropertiesProvider propertiesProvider = getElementPropertiesProvider(labelUpdates[0].getElement());
        if (propertiesProvider == null) {
            for (ILabelUpdate update : labelUpdates) {
                update.setStatus(new Status(IStatus.ERROR, DsfUIPlugin.PLUGIN_ID, "Properties-based label provider " + this + " failed to generate a label, no properties provider registered for element: " + labelUpdates[0].getElement()));  //$NON-NLS-1$ //$NON-NLS-2$
                update.done();
            }
            return;
        }
        
		// We are guaranteed that all the provided updates are for the same
		// presentation context. Thus we can safely assume they request the same
		// columns
        String[] columnIds = labelUpdates[0].getColumnIds();
        
        Set<String> propertyNames = calcPropertyNamesForColumns(columnIds);
        
        // Call the properties provider.  Create a request monitor for each label update.
        // We can use an immediate executor for the request monitor because the label provider
        // is thread safe.
        final IPropertiesUpdate[] propertiesUpdates = new IPropertiesUpdate[labelUpdates.length];
        for (int i = 0; i < labelUpdates.length; i++) {
            final int idx = i;
            propertiesUpdates[idx] = new VMPropertiesUpdate(
                propertyNames, labelUpdates[idx],  
                new ViewerDataRequestMonitor<Map<String, Object>>(ImmediateExecutor.getInstance(), labelUpdates[idx]) {
                    @Override
                    protected void handleCompleted() {
                        notifyPropertiesUpdateCompleted(propertiesUpdates[idx]);
                        updateLabel(labelUpdates[idx], getStatus(), getData());
                    }
                });
        }
        notifyPropertiesUpdatesStarted(propertiesUpdates);
        propertiesProvider.update(propertiesUpdates);
    }

    private void notifyPropertiesUpdatesStarted(IPropertiesUpdate[] updates) {
        IPropertiesUpdateListener[] listeners = null;
        synchronized(this) {
            listeners = fListeners;
        }
        for (IPropertiesUpdateListener listener : listeners) {
            listener.propertiesUpdatesStarted(updates);
        }
    }

    private void notifyPropertiesUpdateCompleted(IPropertiesUpdate update) {
        IPropertiesUpdateListener[] listeners = null;
        synchronized(this) {
            listeners = fListeners;
        }
        for (IPropertiesUpdateListener listener : listeners) {
            listener.propertiesUpdateCompleted(update);
        }
    }

    /**
     * Calculates the names of properties that have to be retrieved from the property
     * provider to generate the labels for given columns.
     * @param columnIds Column IDs to check.
     * @return Array of property names.
     */
    private Set<String> calcPropertyNamesForColumns(String[] columnIds) {
        Set<String> propertyNames = new HashSet<String>();
        if (columnIds == null) {
            LabelColumnInfo columnInfo = getColumnInfo(ID_COLUMN_NO_COLUMNS);
            if (columnInfo != null) {
                for (String propertyName : columnInfo.getPropertyNames()) {
                    propertyNames.add(propertyName);
                }
            } 
        } else {
            for (String columnId : columnIds) {
                LabelColumnInfo info = getColumnInfo(columnId);
                if (info != null) {
                    String[] infoPropertyNames = info.getPropertyNames();
                    for (int i = 0; i < infoPropertyNames.length; i++) {
                        propertyNames.add(infoPropertyNames[i]);
                    }
                }
            }
        }
        return propertyNames;
    }
    
    /**
     * Updates the label information based on given map of properties.
     * 
     * @param update Label update to write to.
     * @param status Result of the properties update
     * @param properties Properties retrieved from the element properties provider.
     * 
     * @since 2.0
     */
    protected void updateLabel(ILabelUpdate update, IStatus status, Map<String, Object> properties) {
        if (update.getColumnIds() == null) {
            LabelColumnInfo info = getColumnInfo(ID_COLUMN_NO_COLUMNS);
            if (info != null) {
                info.updateColumn(update, 0, status, properties);
            }
        } else {
            String[] columnIds = update.getColumnIds();
            
            for (int i = 0; i < columnIds.length; i++) {
                LabelColumnInfo info = getColumnInfo(columnIds[i]);
                if (info != null) {
                    info.updateColumn(update, i, status, properties);
                }
            }       
        }
        
        update.done();
    }

    private IElementPropertiesProvider getElementPropertiesProvider(Object element) {
        if (element instanceof IAdaptable) {
            return ((IAdaptable)element).getAdapter(IElementPropertiesProvider.class);
        }
        return null;
    }
}

Back to the top