Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ece92d3a3ea0c57e669233cf2a179fe0a576c063 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*******************************************************************************
 * Copyright (c) 2009, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 ******************************************************************************/
package org.eclipse.ui.internal.statushandlers;

import com.ibm.icu.text.DateFormat;
import java.net.URL;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import org.eclipse.core.runtime.Adapters;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.resource.LocalResourceManager;
import org.eclipse.jface.resource.ResourceManager;
import org.eclipse.jface.viewers.ILabelDecorator;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.internal.WorkbenchMessages;
import org.eclipse.ui.internal.progress.ProgressManager;
import org.eclipse.ui.internal.progress.ProgressMessages;
import org.eclipse.ui.progress.IProgressConstants;
import org.eclipse.ui.statushandlers.IStatusAdapterConstants;
import org.eclipse.ui.statushandlers.StatusAdapter;

/**
 * This is an utility class which is responsible for text and icon decorators in
 * the StatusDialog.
 *
 * @since 3.6
 */
public class LabelProviderWrapper extends ViewerComparator implements ITableLabelProvider {
	/**
	 * The default status label provider.
	 */
	private class DefaultLabelProvider implements ITableLabelProvider {
		ResourceManager manager = new LocalResourceManager(JFaceResources.getResources());

		@Override
		public void addListener(ILabelProviderListener listener) {
			// Do nothing
		}

		@Override
		public void dispose() {
			manager.dispose();
		}

		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			Image result = null;
			if (element != null) {
				StatusAdapter statusAdapter = ((StatusAdapter) element);
				Job job = Adapters.adapt(statusAdapter, Job.class);
				if (job != null) {
					result = getIcon(job);
				}
			}
			// if somehow disposed image was received (should not happen)
			if (result != null && result.isDisposed()) {
				result = null;
			}
			return result;
		}

		@Override
		public String getColumnText(Object element, int columnIndex) {
			StatusAdapter statusAdapter = (StatusAdapter) element;
			String text = WorkbenchMessages.WorkbenchStatusDialog_ProblemOccurred;
			if (!isMulti()) {
				Job job = Adapters.adapt(statusAdapter, Job.class);
				if (job != null) {
					text = getPrimaryMessage(statusAdapter);
				} else {
					text = getSecondaryMessage(statusAdapter);
				}
			} else {
				Job job = Adapters.adapt(statusAdapter, Job.class);
				if (job != null) {
					text = job.getName();
				} else {
					text = getPrimaryMessage(statusAdapter);
				}
			}
			Long timestamp = (Long) statusAdapter.getProperty(IStatusAdapterConstants.TIMESTAMP_PROPERTY);

			if (timestamp != null && isMulti()) {
				String date = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG)
						.format(new Date(timestamp.longValue()));
				return NLS.bind(ProgressMessages.JobInfo_Error, (new Object[] { text, date }));
			}
			return text;
		}

		/*
		 * Get the icon for the job.
		 */
		private Image getIcon(Job job) {
			if (job != null) {
				Object property = job.getProperty(IProgressConstants.ICON_PROPERTY);

				// Create an image from the job's icon property or family
				if (property instanceof ImageDescriptor) {
					return manager.createImage((ImageDescriptor) property);
				} else if (property instanceof URL) {
					return manager.createImage(ImageDescriptor.createFromURL((URL) property));
				} else {
					// Let the progress manager handle the resource management
					return ProgressManager.getInstance().getIconFor(job);
				}
			}
			return null;
		}

		@Override
		public boolean isLabelProperty(Object element, String property) {
			return false;
		}

		@Override
		public void removeListener(ILabelProviderListener listener) {
			// Do nothing
		}
	}

	private ITableLabelProvider labelProvider;

	/**
	 * This field stores the decorator which can override various texts produced by
	 * this class.
	 */
	private ILabelDecorator messageDecorator;

	private Map dialogState;

	/**
	 * @param dialogState
	 */
	public LabelProviderWrapper(Map dialogState) {
		this.dialogState = dialogState;
	}

	@Override
	public Image getColumnImage(Object element, int columnIndex) {
		return labelProvider.getColumnImage(element, columnIndex);
	}

	@Override
	public String getColumnText(Object element, int columnIndex) {
		return getLabelProvider().getColumnText(element, columnIndex);
	}

	@Override
	public void addListener(ILabelProviderListener listener) {
		getLabelProvider().addListener(listener);
	}

	/**
	 * This method disposes the label provider if and only if the dialog is not
	 * changing its state.
	 *
	 * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
	 */
	@Override
	public void dispose() {
		boolean modalitySwitch = ((Boolean) dialogState.get(IStatusDialogConstants.MODALITY_SWITCH)).booleanValue();
		if (!modalitySwitch) {
			getLabelProvider().dispose();
		}
	}

	@Override
	public boolean isLabelProperty(Object element, String property) {
		return getLabelProvider().isLabelProperty(element, property);
	}

	@Override
	public void removeListener(ILabelProviderListener listener) {
		getLabelProvider().removeListener(listener);
	}

	/**
	 * Gets {@link Image} associated with current {@link StatusAdapter} severity.
	 *
	 * @param statusAdapter
	 *
	 * @return {@link Image} associated with current {@link StatusAdapter} severity.
	 */
	public Image getImage(StatusAdapter statusAdapter) {
		if (statusAdapter != null) {
			int severity = statusAdapter.getStatus().getSeverity();
			switch (severity) {
			case IStatus.OK:
			case IStatus.INFO:
			case IStatus.CANCEL:
				return getSWTImage(SWT.ICON_INFORMATION);
			case IStatus.WARNING:
				return getSWTImage(SWT.ICON_WARNING);
			default: /* IStatus.ERROR */
				return getSWTImage(SWT.ICON_ERROR);
			}
		}
		// should not never happen but if we do not know what is going on, then
		// return error image.
		return getSWTImage(SWT.ICON_ERROR);
	}

	/**
	 * Get an <code>Image</code> from the provide SWT image constant.
	 *
	 * @param imageID the SWT image constant
	 * @return image the image
	 */
	public Image getSWTImage(final int imageID) {
		return Display.getCurrent().getSystemImage(imageID);
	}

	/**
	 * This method computes the dialog main message.
	 *
	 * If there is only one reported status adapter, main message should be:
	 * <ul>
	 * <li>information about job that reported an error.</li>
	 * <li>primary message, if the statusAdapter was not reported by job</li>
	 * </ul>
	 *
	 * If there is more reported statusAdapters, main message should be:
	 * <ul>
	 * <li>primary message for job reported statusAdapters</li>
	 * <li>secondary message for statuses not reported by jobs</li>
	 * </ul>
	 *
	 * If nothing can be found, some general information should be displayed.
	 *
	 * @param statusAdapter A status adapter which is used as the base for
	 *                      computation.
	 * @return main message of the dialog.
	 *
	 * @see #getPrimaryMessage(StatusAdapter)
	 * @see #getSecondaryMessage(StatusAdapter)
	 */
	public String getMainMessage(StatusAdapter statusAdapter) {
		if (!isMulti()) {
			Job job = Adapters.adapt(statusAdapter, Job.class);
			// job
			if (job != null) {
				return NLS.bind(WorkbenchMessages.WorkbenchStatusDialog_ProblemOccurredInJob, job.getName());
			}
			// we are not handling job
			return getPrimaryMessage(statusAdapter);
		}
		// we have a list. primary message or job name or on the list name (both
		// with timestamp if available).
		// we display secondary message or status
		if (isMulti()) {
			Job job = Adapters.adapt(statusAdapter, Job.class);
			// job
			if (job != null) {
				return getPrimaryMessage(statusAdapter);
			}

			// plain status
			return getSecondaryMessage(statusAdapter);
		}
		return WorkbenchMessages.WorkbenchStatusDialog_ProblemOccurred;
	}

	/**
	 * Retrieves primary message from passed statusAdapter. Primary message should
	 * be (from the most important):
	 * <ul>
	 * <li>statusAdapter title</li>
	 * <li>IStatus message</li>
	 * <li>pointing to child statuses if IStatus has them.</li>
	 * <li>exception message</li>
	 * <li>exception class</li>
	 * <li>general message informing about error (no details at all)</li>
	 * </ul>
	 *
	 * @param statusAdapter an status adapter to retrieve primary message from
	 * @return String containing primary message
	 *
	 * @see #getMainMessage(StatusAdapter)
	 * @see #getSecondaryMessage(StatusAdapter)
	 */
	public String getPrimaryMessage(StatusAdapter statusAdapter) {
		// if there was nonempty title set, display the title
		Object property = statusAdapter.getProperty(IStatusAdapterConstants.TITLE_PROPERTY);
		if (property instanceof String) {
			String header = (String) property;
			if (header.trim().length() > 0) {
				return decorate(header, statusAdapter);
			}
		}
		// if there was message set in the status
		IStatus status = statusAdapter.getStatus();
		if (status.getMessage() != null && status.getMessage().trim().length() > 0) {
			return decorate(status.getMessage(), statusAdapter);
		}

		// if status has children
		if (status.getChildren().length > 0) {
			return WorkbenchMessages.WorkbenchStatusDialog_StatusWithChildren;
		}

		// check the exception
		Throwable t = status.getException();
		if (t != null) {
			if (t.getMessage() != null && t.getMessage().trim().length() > 0) {
				return decorate(t.getMessage(), statusAdapter);
			}
			return t.getClass().getName();
		}
		return WorkbenchMessages.WorkbenchStatusDialog_ProblemOccurred;
	}

	/**
	 * Retrieves secondary message from the passed statusAdapter. Secondary message
	 * is one level lower than primary. Secondary message should be (from the most
	 * important):
	 * <ul>
	 * <li>IStatus message</li>
	 * <li>pointing to child statuses if IStatus has them.</li>
	 * <li>exception message</li>
	 * <li>exception class</li>
	 * </ul>
	 * Secondary message should not be the same as primary one. If no secondary
	 * message can be extracted, details should be pointed.
	 *
	 * @param statusAdapter an status adapter to retrieve secondary message from
	 * @return String containing secondary message
	 *
	 * @see #getMainMessage(StatusAdapter)
	 * @see #getPrimaryMessage(StatusAdapter)
	 */
	public String getSecondaryMessage(StatusAdapter statusAdapter) {
		String primary = getPrimaryMessage(statusAdapter);
		// we can skip the title, it is always displayed as primary message

		// if there was message set in the status
		IStatus status = statusAdapter.getStatus();
		String message = status.getMessage();
		String decoratedMessage = message == null ? null : decorate(message, statusAdapter);
		if (message != null && message.trim().length() > 0 && !primary.equals(decoratedMessage)) {
			/* we have not displayed it yet */
			return decoratedMessage;
		}
		// if status has children
		if (status.getChildren().length > 0 && !primary.equals(decoratedMessage)) {
			return WorkbenchMessages.WorkbenchStatusDialog_StatusWithChildren;
		}

		// check the exception
		Throwable t = status.getException();
		if (t != null) {
			if (t.getMessage() != null) {
				String decoratedThrowable = decorate(t.getMessage(), statusAdapter);
				if (t.getMessage().trim().length() > 0 && !primary.equals(decoratedThrowable)) {
					return decoratedThrowable;
				}
			}
			String throwableName = t.getClass().getName();
			if (!primary.equals(throwableName)) {
				return throwableName;
			}
		}
		return WorkbenchMessages.WorkbenchStatusDialog_SeeDetails;
	}

	private String decorate(String string, StatusAdapter adapter) {
		messageDecorator = (ILabelDecorator) dialogState.get(IStatusDialogConstants.DECORATOR);
		if (messageDecorator != null) {
			string = messageDecorator.decorateText(string, adapter);
		}
		return string;
	}

	private int compare(StatusAdapter s1, StatusAdapter s2) {
		Long timestamp1 = ((Long) s1.getProperty(IStatusAdapterConstants.TIMESTAMP_PROPERTY));
		Long timestamp2 = ((Long) s2.getProperty(IStatusAdapterConstants.TIMESTAMP_PROPERTY));
		if (timestamp1 == null || timestamp2 == null || (timestamp1.equals(timestamp2))) {
			String text1 = getColumnText(s1, 0);
			String text2 = getColumnText(s2, 0);
			return text1.compareTo(text2);
		}

		if (timestamp1.longValue() < timestamp2.longValue()) {
			return -1;
		}
		if (timestamp1.longValue() > timestamp2.longValue()) {
			return 1;
		}
		// should be never called
		return 0;
	}

	@Override
	public int compare(Viewer testViewer, Object o1, Object o2) {
		if (o1 instanceof StatusAdapter && o2 instanceof StatusAdapter) {
			return compare((StatusAdapter) o1, (StatusAdapter) o2);
		}
		// should not happen
		if (o1.hashCode() < o2.hashCode()) {
			return -1;
		}
		if (o2.hashCode() > o2.hashCode()) {
			return 1;
		}
		return 0;
	}

	private boolean isMulti() {
		return ((Collection) dialogState.get(IStatusDialogConstants.STATUS_ADAPTERS)).size() > 1;
	}

	/**
	 * @return Returns the labelProvider.
	 */
	public ITableLabelProvider getLabelProvider() {
		ITableLabelProvider temp = (ITableLabelProvider) dialogState.get(IStatusDialogConstants.CUSTOM_LABEL_PROVIDER);
		if (temp != null) {
			labelProvider = temp;
		}
		if (labelProvider == null) {
			labelProvider = new DefaultLabelProvider();
		}
		return labelProvider;
	}
}

Back to the top