Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 77c5c7de6cc65fb7af742e25407d52ffd96f8ee2 (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
/*******************************************************************************
 * Copyright (c) 2007, 2016 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.statushandlers;

import java.util.HashMap;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.QualifiedName;

/**
 * <p>
 * The StatusAdapter wraps an instance of IStatus subclass and can hold
 * additional information either by using properties or by adding a new adapter. Used during
 * status handling process.
 * </p>
 *
 * @since 3.3
 */
public class StatusAdapter implements IAdaptable {

	/**
	 * This property is used to add title to the adapter. If the adapter is
	 * shown in a dialog, this property is used to create title of the dialog.
	 *
	 * @deprecated use {@link IStatusAdapterConstants#TITLE_PROPERTY} instead
	 */
	@Deprecated
	public static final QualifiedName TITLE_PROPERTY = IStatusAdapterConstants.TITLE_PROPERTY;

	/**
	 * This property is used to add a timestamp to the adapter. If the adapter
	 * is shown in the UI, this property can be used for sorting and showing
	 * information about the status creation time.
	 *
	 * <p>
	 * The property must be of type <code>Long</code>.
	 * </p>
	 *
	 * @deprecated use {@link IStatusAdapterConstants#TIMESTAMP_PROPERTY}
	 *             instead
	 */
	@Deprecated
	public static final QualifiedName TIMESTAMP_PROPERTY = IStatusAdapterConstants.TIMESTAMP_PROPERTY;

	private IStatus status;

	private HashMap properties;

	private HashMap adapters;

	/**
	 * Creates an instance of this class.
	 *
	 * @param status
	 *            the status to wrap. May not be <code>null</code>.
	 */
	public StatusAdapter(IStatus status) {
		this.status = status;
	}

	/**
	 * Associates new object which is an instance of the given class with this
	 * adapter. object will be returned when {@link IAdaptable#getAdapter(Class)}
	 * is called on the receiver with {@link Class} adapter as a parameter.
	 *
	 * @param adapter
	 *            the adapter class
	 * @param object
	 *            the adapter instance
	 */
	public void addAdapter(Class adapter, Object object) {
		if (adapters == null) {
			adapters = new HashMap();
		}
		adapters.put(adapter, object);
	}

	@Override
	public <T> T getAdapter(Class<T> adapter) {
		if (adapters == null) {
			return null;
		}
		return adapter.cast(adapters.get(adapter));
	}

	/**
	 * Returns the wrapped status.
	 *
	 * @return the wrapped status set in the constructor or in
	 *         <code>setStatus(IStatus)</code>. Will not be <code>null</code>.
	 */
	public IStatus getStatus() {
		return status;
	}

	/**
	 * Sets a new status for this adapter.
	 *
	 * @param status
	 *            the status to set. May not be <code>null</code>.
	 */
	public void setStatus(IStatus status) {
		this.status = status;
	}

	/**
	 * Returns the value of the adapter's property identified by the given key,
	 * or <code>null</code> if this adapter has no such property.
	 *
	 * @param key
	 *            the qualified name of the property
	 * @return the value of the property, or <code>null</code> if this adapter
	 *         has no such property
	 */
	public Object getProperty(QualifiedName key) {
		if (properties == null) {
			return null;
		}
		return properties.get(key);
	}

	/**
	 * Sets the value of the receiver's property identified by the given key.
	 *
	 * @param key
	 *            the qualified name of the property
	 * @param value
	 *            the value of the property
	 */
	public void setProperty(QualifiedName key, Object value) {
		if (properties == null) {
			properties = new HashMap();
		}
		properties.put(key, value);
	}
}

Back to the top