KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > Status


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  *******************************************************************************/

11 package org.eclipse.core.runtime;
12
13 import org.eclipse.core.internal.runtime.IRuntimeConstants;
14 import org.eclipse.core.internal.runtime.LocalizationUtils;
15
16 /**
17  * A concrete status implementation, suitable either for
18  * instantiating or subclassing.
19  * <p>
20  * This class can be used without OSGi running.
21  * </p>
22  */

23 public class Status implements IStatus {
24
25     /**
26      * A standard OK status with an "ok" message.
27      *
28      * @since 3.0
29      */

30     public static final IStatus OK_STATUS = new Status(OK, IRuntimeConstants.PI_RUNTIME, OK, LocalizationUtils.safeLocalize("ok"), null); //$NON-NLS-1$
31
/**
32      * A standard CANCEL status with no message.
33      *
34      * @since 3.0
35      */

36     public static final IStatus CANCEL_STATUS = new Status(CANCEL, IRuntimeConstants.PI_RUNTIME, 1, "", null); //$NON-NLS-1$
37
/**
38      * The severity. One of
39      * <ul>
40      * <li><code>CANCEL</code></li>
41      * <li><code>ERROR</code></li>
42      * <li><code>WARNING</code></li>
43      * <li><code>INFO</code></li>
44      * <li>or <code>OK</code> (0)</li>
45      * </ul>
46      */

47     private int severity = OK;
48
49     /** Unique identifier of plug-in.
50      */

51     private String JavaDoc pluginId;
52
53     /** Plug-in-specific status code.
54      */

55     private int code;
56
57     /** Message, localized to the current locale.
58      */

59     private String JavaDoc message;
60
61     /** Wrapped exception, or <code>null</code> if none.
62      */

63     private Throwable JavaDoc exception = null;
64
65     /** Constant to avoid generating garbage.
66      */

67     private static final IStatus[] theEmptyStatusArray = new IStatus[0];
68
69     /**
70      * Creates a new status object. The created status has no children.
71      *
72      * @param severity the severity; one of <code>OK</code>, <code>ERROR</code>,
73      * <code>INFO</code>, <code>WARNING</code>, or <code>CANCEL</code>
74      * @param pluginId the unique identifier of the relevant plug-in
75      * @param code the plug-in-specific status code, or <code>OK</code>
76      * @param message a human-readable message, localized to the
77      * current locale
78      * @param exception a low-level exception, or <code>null</code> if not
79      * applicable
80      */

81     public Status(int severity, String JavaDoc pluginId, int code, String JavaDoc message, Throwable JavaDoc exception) {
82         setSeverity(severity);
83         setPlugin(pluginId);
84         setCode(code);
85         setMessage(message);
86         setException(exception);
87     }
88
89     /**
90      * Simplified constructor of a new status object; assumes that code is <code>OK</code>.
91      * The created status has no children.
92      *
93      * @param severity the severity; one of <code>OK</code>, <code>ERROR</code>,
94      * <code>INFO</code>, <code>WARNING</code>, or <code>CANCEL</code>
95      * @param pluginId the unique identifier of the relevant plug-in
96      * @param message a human-readable message, localized to the
97      * current locale
98      * @param exception a low-level exception, or <code>null</code> if not
99      * applicable
100      *
101      * @since org.eclipse.equinox.common 3.3
102      */

103     public Status(int severity, String JavaDoc pluginId, String JavaDoc message, Throwable JavaDoc exception) {
104         setSeverity(severity);
105         setPlugin(pluginId);
106         setMessage(message);
107         setException(exception);
108         setCode(OK);
109     }
110
111     /**
112      * Simplified constructor of a new status object; assumes that code is <code>OK</code> and
113      * exception is <code>null</code>. The created status has no children.
114      *
115      * @param severity the severity; one of <code>OK</code>, <code>ERROR</code>,
116      * <code>INFO</code>, <code>WARNING</code>, or <code>CANCEL</code>
117      * @param pluginId the unique identifier of the relevant plug-in
118      * @param message a human-readable message, localized to the
119      * current locale
120      *
121      * @since org.eclipse.equinox.common 3.3
122      */

123     public Status(int severity, String JavaDoc pluginId, String JavaDoc message) {
124         setSeverity(severity);
125         setPlugin(pluginId);
126         setMessage(message);
127         setCode(OK);
128         setException(null);
129     }
130
131     /* (Intentionally not javadoc'd)
132      * Implements the corresponding method on <code>IStatus</code>.
133      */

134     public IStatus[] getChildren() {
135         return theEmptyStatusArray;
136     }
137
138     /* (Intentionally not javadoc'd)
139      * Implements the corresponding method on <code>IStatus</code>.
140      */

141     public int getCode() {
142         return code;
143     }
144
145     /* (Intentionally not javadoc'd)
146      * Implements the corresponding method on <code>IStatus</code>.
147      */

148     public Throwable JavaDoc getException() {
149         return exception;
150     }
151
152     /* (Intentionally not javadoc'd)
153      * Implements the corresponding method on <code>IStatus</code>.
154      */

155     public String JavaDoc getMessage() {
156         return message;
157     }
158
159     /* (Intentionally not javadoc'd)
160      * Implements the corresponding method on <code>IStatus</code>.
161      */

162     public String JavaDoc getPlugin() {
163         return pluginId;
164     }
165
166     /* (Intentionally not javadoc'd)
167      * Implements the corresponding method on <code>IStatus</code>.
168      */

169     public int getSeverity() {
170         return severity;
171     }
172
173     /* (Intentionally not javadoc'd)
174      * Implements the corresponding method on <code>IStatus</code>.
175      */

176     public boolean isMultiStatus() {
177         return false;
178     }
179
180     /* (Intentionally not javadoc'd)
181      * Implements the corresponding method on <code>IStatus</code>.
182      */

183     public boolean isOK() {
184         return severity == OK;
185     }
186
187     /* (Intentionally not javadoc'd)
188      * Implements the corresponding method on <code>IStatus</code>.
189      */

190     public boolean matches(int severityMask) {
191         return (severity & severityMask) != 0;
192     }
193
194     /**
195      * Sets the status code.
196      *
197      * @param code the plug-in-specific status code, or <code>OK</code>
198      */

199     protected void setCode(int code) {
200         this.code = code;
201     }
202
203     /**
204      * Sets the exception.
205      *
206      * @param exception a low-level exception, or <code>null</code> if not
207      * applicable
208      */

209     protected void setException(Throwable JavaDoc exception) {
210         this.exception = exception;
211     }
212
213     /**
214      * Sets the message. If null is passed, message is set to an empty
215      * string.
216      *
217      * @param message a human-readable message, localized to the
218      * current locale
219      */

220     protected void setMessage(String JavaDoc message) {
221         if (message == null)
222             this.message = ""; //$NON-NLS-1$
223
else
224             this.message = message;
225     }
226
227     /**
228      * Sets the plug-in id.
229      *
230      * @param pluginId the unique identifier of the relevant plug-in
231      */

232     protected void setPlugin(String JavaDoc pluginId) {
233         Assert.isLegal(pluginId != null && pluginId.length() > 0);
234         this.pluginId = pluginId;
235     }
236
237     /**
238      * Sets the severity.
239      *
240      * @param severity the severity; one of <code>OK</code>, <code>ERROR</code>,
241      * <code>INFO</code>, <code>WARNING</code>, or <code>CANCEL</code>
242      */

243     protected void setSeverity(int severity) {
244         Assert.isLegal(severity == OK || severity == ERROR || severity == WARNING || severity == INFO || severity == CANCEL);
245         this.severity = severity;
246     }
247
248     /**
249      * Returns a string representation of the status, suitable
250      * for debugging purposes only.
251      */

252     public String JavaDoc toString() {
253         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
254         buf.append("Status "); //$NON-NLS-1$
255
if (severity == OK) {
256             buf.append("OK"); //$NON-NLS-1$
257
} else if (severity == ERROR) {
258             buf.append("ERROR"); //$NON-NLS-1$
259
} else if (severity == WARNING) {
260             buf.append("WARNING"); //$NON-NLS-1$
261
} else if (severity == INFO) {
262             buf.append("INFO"); //$NON-NLS-1$
263
} else if (severity == CANCEL) {
264             buf.append("CANCEL"); //$NON-NLS-1$
265
} else {
266             buf.append("severity="); //$NON-NLS-1$
267
buf.append(severity);
268         }
269         buf.append(": "); //$NON-NLS-1$
270
buf.append(pluginId);
271         buf.append(" code="); //$NON-NLS-1$
272
buf.append(code);
273         buf.append(' ');
274         buf.append(message);
275         buf.append(' ');
276         buf.append(exception);
277         return buf.toString();
278     }
279 }
280
Popular Tags