KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > core > refactoring > RefactoringStatusEntry


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.ltk.core.refactoring;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16
17 import org.eclipse.ltk.internal.core.refactoring.RefactoringCorePlugin;
18
19 /**
20  * An immutable object representing an entry in the list in <code>RefactoringStatus</code>.
21  * A refactoring status entry consists of a severity, a message, a problem code
22  * (represented by a tuple(plug-in identifier and code number)), a context object and a
23  * generic data pointer. The context object is used to provide context information for
24  * the problem itself. An example context is a tuple consisting of the resource that contains
25  * the problem and a corresponding line number.
26  * <p>
27  * Note: this class is not intended to be extended by clients.
28  * </p>
29  *
30  * @since 3.0
31  */

32 public class RefactoringStatusEntry {
33     
34     /**
35      * A special problem code indicating that no problem code is provided. If
36      * <code>NO_CODE</code> is used then the plug-in identifier can be <code>
37      * null</code>
38      */

39     public static final int NO_CODE= -1;
40
41     /** The severity */
42     private final int fSeverity;
43
44     /** The message */
45     private final String JavaDoc fMessage;
46
47     /** A plug-in specific problem code */
48     private final int fCode;
49     
50     /** A plug-in identifier to make the problem code unique */
51     private final String JavaDoc fPluginId;
52
53     /** A context providing detailed information of where the problem occurred */
54     private final RefactoringStatusContext fContext;
55
56     /** A generic data pointer */
57     private final Object JavaDoc fData;
58
59     /**
60      * Creates a new refactoring status entry. The context is set to <code>
61      * null</code> the problem code is set to <code>NO_CODE</code>, the
62      * plug-in identifier is set to <code>null</code> and the data pointer
63      * is set to <code>null</code> as well.
64      *
65      * @param severity the severity
66      * @param msg the message
67      */

68     public RefactoringStatusEntry(int severity, String JavaDoc msg) {
69         this(severity, msg, null);
70     }
71
72     /**
73      * Creates a new refactoring status entry. The problem code is set to <code>
74      * NO_CODE</code>, the plug-in identifier is set to <code>null</code> and
75      * the data pointer is set to <code>null</code> as well.
76      *
77      * @param severity the severity
78      * @param msg the message
79      * @param context the context. Can be <code>null</code>
80      */

81     public RefactoringStatusEntry(int severity, String JavaDoc msg, RefactoringStatusContext context) {
82         this(severity, msg, context, null, NO_CODE, null);
83     }
84
85     /**
86      * Creates a new refactoring status entry.
87      *
88      * @param severity the severity
89      * @param msg the message
90      * @param context the context. Can be <code>null</code>
91      * @param pluginId the plug-in identifier. Can be <code>null</code> if argument <code>
92      * code</code> equals <code>NO_CODE</code>
93      * @param code the problem code. Must be either <code>NO_CODE</code> or equals or greater
94      * than zero
95      */

96     public RefactoringStatusEntry(int severity, String JavaDoc msg, RefactoringStatusContext context, String JavaDoc pluginId, int code) {
97         this(severity, msg, context, pluginId, code, null);
98     }
99
100     /**
101      * Creates a new refactoring status entry.
102      *
103      * @param severity the severity
104      * @param msg the message
105      * @param context the context. Can be <code>null</code>
106      * @param pluginId the plug-in identifier. Can be <code>null</code> if argument <code>
107      * code</code> equals <code>NO_CODE</code>
108      * @param code the problem code. Must be either <code>NO_CODE</code> or a positive integer
109      * @param data application specific data
110      */

111     public RefactoringStatusEntry(int severity, String JavaDoc msg, RefactoringStatusContext context, String JavaDoc pluginId, int code, Object JavaDoc data) {
112         Assert.isTrue(severity == RefactoringStatus.INFO || severity == RefactoringStatus.WARNING
113             || severity == RefactoringStatus.ERROR || severity == RefactoringStatus.FATAL);
114         Assert.isNotNull(msg);
115         Assert.isTrue(code == NO_CODE || code >= 0);
116         if (code != NO_CODE) Assert.isTrue(pluginId != null);
117         fMessage= msg;
118         fSeverity= severity;
119         fContext= context;
120         fPluginId= pluginId;
121         fCode= code;
122         fData= data;
123     }
124
125     /**
126      * Returns the message of the status entry.
127      *
128      * @return the message
129      */

130     public String JavaDoc getMessage() {
131         return fMessage;
132     }
133
134     /**
135      * Returns the severity level.
136      *
137      * @return the severity level
138      *
139      * @see RefactoringStatus#INFO
140      * @see RefactoringStatus#WARNING
141      * @see RefactoringStatus#ERROR
142      * @see RefactoringStatus#FATAL
143      */

144     public int getSeverity() {
145         return fSeverity;
146     }
147
148     /**
149      * Returns the context which can be used to show more detailed information regarding
150      * this status entry in the UI. The method may return <code>null</code> indicating
151      * that no context is available.
152      *
153      * @return the status entry's context
154      */

155     public RefactoringStatusContext getContext() {
156         return fContext;
157     }
158
159     /**
160      * Returns the plug-in identifier associated with the
161      * problem code. Might return <code>null</code> if the
162      * problem code equals <code>NO_CODE</code>.
163      *
164      * @return the plug-in identifier
165      */

166     public String JavaDoc getPluginId() {
167         return fPluginId;
168     }
169     
170     /**
171      * Returns the problem code.
172      *
173      * @return the problem code
174      */

175     public int getCode() {
176         return fCode;
177     }
178     
179     /**
180      * Returns the application defined entry data associated
181      * with the receiver, or <code>null</code> if it has not
182      * been set.
183      *
184      * @return the entry data
185      */

186     public Object JavaDoc getData() {
187         return fData;
188     }
189
190     /**
191      * Returns whether the entry represents a fatal error or not.
192      *
193      * @return <code>true</code> if (severity ==<code>RefactoringStatus.FATAL</code>)
194      */

195     public boolean isFatalError() {
196         return fSeverity == RefactoringStatus.FATAL;
197     }
198
199     /**
200      * Returns whether the entry represents an error or not.
201      *
202      * @return <code>true</code> if (severity ==<code>RefactoringStatus.ERROR</code>).
203      */

204     public boolean isError() {
205         return fSeverity == RefactoringStatus.ERROR;
206     }
207
208     /**
209      * Returns whether the entry represents a warning or not.
210      *
211      * @return <code>true</code> if (severity ==<code>RefactoringStatus.WARNING</code>).
212      */

213     public boolean isWarning() {
214         return fSeverity == RefactoringStatus.WARNING;
215     }
216
217     /**
218      * Returns whether the entry represents an information or not.
219      *
220      * @return <code>true</code> if (severity ==<code>RefactoringStatus.INFO</code>).
221      */

222     public boolean isInfo() {
223         return fSeverity == RefactoringStatus.INFO;
224     }
225
226     /**
227      * Returns this refactoring status entry as an {@link IStatus}.
228      * <p>
229      * If this refactoring status entry has a severity of
230      * {@link RefactoringStatus#FATAL}, the returned status will have a
231      * severity of {@link IStatus#ERROR}, otherwise a status with severity
232      * corresponding to the refactoring status entry is returned. If the plugin
233      * id of this refactoring status entry is not defined, the plugin id
234      * <code>org.eclipse.ltk.core.refactoring</code> will be used in the
235      * returned status.
236      * </p>
237      *
238      * @return the corresponding status
239      *
240      * @since 3.2
241      */

242     public IStatus toStatus() {
243         int statusSeverity= IStatus.ERROR;
244         switch (getSeverity()) {
245             case RefactoringStatus.OK:
246                 statusSeverity= IStatus.OK;
247                 break;
248             case RefactoringStatus.INFO:
249                 statusSeverity= IStatus.INFO;
250                 break;
251             case RefactoringStatus.WARNING:
252             case RefactoringStatus.ERROR:
253                 statusSeverity= IStatus.WARNING;
254                 break;
255         }
256         String JavaDoc pluginId= getPluginId();
257         int code= getCode();
258         if (pluginId == null) {
259             pluginId= RefactoringCorePlugin.getPluginId();
260             code= IStatus.ERROR;
261         }
262         return new Status(statusSeverity, pluginId, code, getMessage(), null);
263     }
264
265     /*
266      * non java-doc for debugging only
267      */

268     public String JavaDoc toString() {
269         String JavaDoc contextString= fContext == null ? "<Unspecified context>" : fContext.toString(); //$NON-NLS-1$
270
return "\n" //$NON-NLS-1$
271
+ RefactoringStatus.getSeverityString(fSeverity) + ": " + fMessage + //$NON-NLS-1$
272
"\nContext: " + contextString + //$NON-NLS-1$
273
(fCode == NO_CODE ? "\ncode: none" : "\nplug-in id: " + fPluginId + "code: " + fCode) + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
274
"\nData: " + fData; //$NON-NLS-1$
275
}
276 }
277
Popular Tags