Mercurial > projects > dwt-addons
comparison dwtx/core/runtime/Status.d @ 3:6518c18a01f7
eclipse.core package without osgi dependencies
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 26 Mar 2008 00:57:19 +0100 |
parents | |
children | ea8ff534f622 |
comparison
equal
deleted
inserted
replaced
2:a012107a911c | 3:6518c18a01f7 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.core.runtime.Status; | |
14 | |
15 import dwtx.core.internal.runtime.IRuntimeConstants; | |
16 import dwtx.core.internal.runtime.LocalizationUtils; | |
17 import dwtx.core.runtime.IStatus; | |
18 import dwtx.core.runtime.Assert; | |
19 | |
20 import dwt.dwthelper.utils; | |
21 import tango.text.convert.Format; | |
22 | |
23 /** | |
24 * A concrete status implementation, suitable either for | |
25 * instantiating or subclassing. | |
26 * <p> | |
27 * This class can be used without OSGi running. | |
28 * </p> | |
29 */ | |
30 public class Status : IStatus { | |
31 | |
32 /** | |
33 * A standard OK status with an "ok" message. | |
34 * | |
35 * @since 3.0 | |
36 */ | |
37 public static const IStatus OK_STATUS; | |
38 /** | |
39 * A standard CANCEL status with no message. | |
40 * | |
41 * @since 3.0 | |
42 */ | |
43 public static const IStatus CANCEL_STATUS; | |
44 | |
45 static this(){ | |
46 OK_STATUS = new Status(OK, IRuntimeConstants.PI_RUNTIME, OK, LocalizationUtils.safeLocalize("ok"), null); //$NON-NLS-1$ | |
47 CANCEL_STATUS = new Status(CANCEL, IRuntimeConstants.PI_RUNTIME, 1, "", null); //$NON-NLS-1$ | |
48 } | |
49 | |
50 /** | |
51 * The severity. One of | |
52 * <ul> | |
53 * <li><code>CANCEL</code></li> | |
54 * <li><code>ERROR</code></li> | |
55 * <li><code>WARNING</code></li> | |
56 * <li><code>INFO</code></li> | |
57 * <li>or <code>OK</code> (0)</li> | |
58 * </ul> | |
59 */ | |
60 private int severity = OK; | |
61 | |
62 /** Unique identifier of plug-in. | |
63 */ | |
64 private String pluginId; | |
65 | |
66 /** Plug-in-specific status code. | |
67 */ | |
68 private int code; | |
69 | |
70 /** Message, localized to the current locale. | |
71 */ | |
72 private String message; | |
73 | |
74 /** Wrapped exception, or <code>null</code> if none. | |
75 */ | |
76 private Exception exception = null; | |
77 | |
78 /** Constant to avoid generating garbage. | |
79 */ | |
80 private static const IStatus[] theEmptyStatusArray = null; | |
81 | |
82 /** | |
83 * Creates a new status object. The created status has no children. | |
84 * | |
85 * @param severity the severity; one of <code>OK</code>, <code>ERROR</code>, | |
86 * <code>INFO</code>, <code>WARNING</code>, or <code>CANCEL</code> | |
87 * @param pluginId the unique identifier of the relevant plug-in | |
88 * @param code the plug-in-specific status code, or <code>OK</code> | |
89 * @param message a human-readable message, localized to the | |
90 * current locale | |
91 * @param exception a low-level exception, or <code>null</code> if not | |
92 * applicable | |
93 */ | |
94 public this(int severity, String pluginId, int code, String message, Exception exception) { | |
95 setSeverity(severity); | |
96 setPlugin(pluginId); | |
97 setCode(code); | |
98 setMessage(message); | |
99 setException(exception); | |
100 } | |
101 | |
102 /** | |
103 * Simplified constructor of a new status object; assumes that code is <code>OK</code>. | |
104 * The created status has no children. | |
105 * | |
106 * @param severity the severity; one of <code>OK</code>, <code>ERROR</code>, | |
107 * <code>INFO</code>, <code>WARNING</code>, or <code>CANCEL</code> | |
108 * @param pluginId the unique identifier of the relevant plug-in | |
109 * @param message a human-readable message, localized to the | |
110 * current locale | |
111 * @param exception a low-level exception, or <code>null</code> if not | |
112 * applicable | |
113 * | |
114 * @since dwtx.equinox.common 3.3 | |
115 */ | |
116 public this(int severity, String pluginId, String message, Exception exception) { | |
117 setSeverity(severity); | |
118 setPlugin(pluginId); | |
119 setMessage(message); | |
120 setException(exception); | |
121 setCode(OK); | |
122 } | |
123 | |
124 /** | |
125 * Simplified constructor of a new status object; assumes that code is <code>OK</code> and | |
126 * exception is <code>null</code>. The created status has no children. | |
127 * | |
128 * @param severity the severity; one of <code>OK</code>, <code>ERROR</code>, | |
129 * <code>INFO</code>, <code>WARNING</code>, or <code>CANCEL</code> | |
130 * @param pluginId the unique identifier of the relevant plug-in | |
131 * @param message a human-readable message, localized to the | |
132 * current locale | |
133 * | |
134 * @since dwtx.equinox.common 3.3 | |
135 */ | |
136 public this(int severity, String pluginId, String message) { | |
137 setSeverity(severity); | |
138 setPlugin(pluginId); | |
139 setMessage(message); | |
140 setCode(OK); | |
141 setException(null); | |
142 } | |
143 | |
144 /* (Intentionally not javadoc'd) | |
145 * Implements the corresponding method on <code>IStatus</code>. | |
146 */ | |
147 public IStatus[] getChildren() { | |
148 return theEmptyStatusArray; | |
149 } | |
150 | |
151 /* (Intentionally not javadoc'd) | |
152 * Implements the corresponding method on <code>IStatus</code>. | |
153 */ | |
154 public int getCode() { | |
155 return code; | |
156 } | |
157 | |
158 /* (Intentionally not javadoc'd) | |
159 * Implements the corresponding method on <code>IStatus</code>. | |
160 */ | |
161 public Exception getException() { | |
162 return exception; | |
163 } | |
164 | |
165 /* (Intentionally not javadoc'd) | |
166 * Implements the corresponding method on <code>IStatus</code>. | |
167 */ | |
168 public String getMessage() { | |
169 return message; | |
170 } | |
171 | |
172 /* (Intentionally not javadoc'd) | |
173 * Implements the corresponding method on <code>IStatus</code>. | |
174 */ | |
175 public String getPlugin() { | |
176 return pluginId; | |
177 } | |
178 | |
179 /* (Intentionally not javadoc'd) | |
180 * Implements the corresponding method on <code>IStatus</code>. | |
181 */ | |
182 public int getSeverity() { | |
183 return severity; | |
184 } | |
185 | |
186 /* (Intentionally not javadoc'd) | |
187 * Implements the corresponding method on <code>IStatus</code>. | |
188 */ | |
189 public bool isMultiStatus() { | |
190 return false; | |
191 } | |
192 | |
193 /* (Intentionally not javadoc'd) | |
194 * Implements the corresponding method on <code>IStatus</code>. | |
195 */ | |
196 public bool isOK() { | |
197 return severity is OK; | |
198 } | |
199 | |
200 /* (Intentionally not javadoc'd) | |
201 * Implements the corresponding method on <code>IStatus</code>. | |
202 */ | |
203 public bool matches(int severityMask) { | |
204 return (severity & severityMask) !is 0; | |
205 } | |
206 | |
207 /** | |
208 * Sets the status code. | |
209 * | |
210 * @param code the plug-in-specific status code, or <code>OK</code> | |
211 */ | |
212 protected void setCode(int code) { | |
213 this.code = code; | |
214 } | |
215 | |
216 /** | |
217 * Sets the exception. | |
218 * | |
219 * @param exception a low-level exception, or <code>null</code> if not | |
220 * applicable | |
221 */ | |
222 protected void setException(Exception exception) { | |
223 this.exception = exception; | |
224 } | |
225 | |
226 /** | |
227 * Sets the message. If null is passed, message is set to an empty | |
228 * string. | |
229 * | |
230 * @param message a human-readable message, localized to the | |
231 * current locale | |
232 */ | |
233 protected void setMessage(String message) { | |
234 if (message is null) | |
235 this.message = ""; //$NON-NLS-1$ | |
236 else | |
237 this.message = message; | |
238 } | |
239 | |
240 /** | |
241 * Sets the plug-in id. | |
242 * | |
243 * @param pluginId the unique identifier of the relevant plug-in | |
244 */ | |
245 protected void setPlugin(String pluginId) { | |
246 Assert.isLegal(pluginId !is null && pluginId.length > 0); | |
247 this.pluginId = pluginId; | |
248 } | |
249 | |
250 /** | |
251 * Sets the severity. | |
252 * | |
253 * @param severity the severity; one of <code>OK</code>, <code>ERROR</code>, | |
254 * <code>INFO</code>, <code>WARNING</code>, or <code>CANCEL</code> | |
255 */ | |
256 protected void setSeverity(int severity) { | |
257 Assert.isLegal(severity is OK || severity is ERROR || severity is WARNING || severity is INFO || severity is CANCEL); | |
258 this.severity = severity; | |
259 } | |
260 | |
261 /** | |
262 * Returns a string representation of the status, suitable | |
263 * for debugging purposes only. | |
264 */ | |
265 public String toString() { | |
266 String sev; | |
267 if (severity is OK) { | |
268 sev="OK"; //$NON-NLS-1$ | |
269 } else if (severity is ERROR) { | |
270 sev="ERROR"; //$NON-NLS-1$ | |
271 } else if (severity is WARNING) { | |
272 sev="WARNING"; //$NON-NLS-1$ | |
273 } else if (severity is INFO) { | |
274 sev="INFO"; //$NON-NLS-1$ | |
275 } else if (severity is CANCEL) { | |
276 sev="CANCEL"; //$NON-NLS-1$ | |
277 } else { | |
278 sev=Format( "severity={}", severity); | |
279 } | |
280 return Format("Status {}: {} code={} {} {}", sev, pluginId, code, message, exception.toString ); | |
281 } | |
282 } |