KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > JavaModelStatus


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.core;
12
13 import org.eclipse.core.resources.IResourceStatus;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.jdt.core.ClasspathContainerInitializer;
19 import org.eclipse.jdt.core.IClasspathContainer;
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IJavaModelStatus;
22 import org.eclipse.jdt.core.IJavaModelStatusConstants;
23 import org.eclipse.jdt.core.IJavaProject;
24 import org.eclipse.jdt.core.IPackageFragment;
25 import org.eclipse.jdt.core.JavaCore;
26 import org.eclipse.jdt.core.JavaModelException;
27 import org.eclipse.jdt.internal.core.util.Messages;
28
29 /**
30  * @see IJavaModelStatus
31  */

32
33 public class JavaModelStatus extends Status implements IJavaModelStatus, IJavaModelStatusConstants, IResourceStatus {
34
35     /**
36      * The elements related to the failure, or <code>null</code>
37      * if no elements are involved.
38      */

39     protected IJavaElement[] elements = new IJavaElement[0];
40     /**
41      * The path related to the failure, or <code>null</code>
42      * if no path is involved.
43      */

44     protected IPath path;
45     /**
46      * The <code>String</code> related to the failure, or <code>null</code>
47      * if no <code>String</code> is involved.
48      */

49     protected String JavaDoc string;
50     /**
51      * Empty children
52      */

53     protected final static IStatus[] NO_CHILDREN = new IStatus[] {};
54     protected IStatus[] children= NO_CHILDREN;
55
56     /**
57      * Singleton OK object
58      */

59     public static final IJavaModelStatus VERIFIED_OK = new JavaModelStatus(OK, OK, Messages.status_OK);
60
61     /**
62      * Constructs an Java model status with no corresponding elements.
63      */

64     public JavaModelStatus() {
65         // no code for an multi-status
66
super(ERROR, JavaCore.PLUGIN_ID, 0, "JavaModelStatus", null); //$NON-NLS-1$
67
}
68     /**
69      * Constructs an Java model status with no corresponding elements.
70      */

71     public JavaModelStatus(int code) {
72         super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
73
this.elements= JavaElement.NO_ELEMENTS;
74     }
75     /**
76      * Constructs an Java model status with the given corresponding
77      * elements.
78      */

79     public JavaModelStatus(int code, IJavaElement[] elements) {
80         super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
81
this.elements= elements;
82         this.path= null;
83     }
84     /**
85      * Constructs an Java model status with no corresponding elements.
86      */

87     public JavaModelStatus(int code, String JavaDoc string) {
88         this(ERROR, code, string);
89     }
90     /**
91      * Constructs an Java model status with no corresponding elements.
92      */

93     public JavaModelStatus(int severity, int code, String JavaDoc string) {
94         super(severity, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
95
this.elements= JavaElement.NO_ELEMENTS;
96         this.path= null;
97         this.string = string;
98     }
99     /**
100      * Constructs an Java model status with no corresponding elements.
101      */

102     public JavaModelStatus(int code, Throwable JavaDoc throwable) {
103         super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", throwable); //$NON-NLS-1$
104
this.elements= JavaElement.NO_ELEMENTS;
105     }
106     /**
107      * Constructs an Java model status with no corresponding elements.
108      */

109     public JavaModelStatus(int code, IPath path) {
110         super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
111
this.elements= JavaElement.NO_ELEMENTS;
112         this.path= path;
113     }
114     /**
115      * Constructs an Java model status with the given corresponding
116      * element.
117      */

118     public JavaModelStatus(int code, IJavaElement element) {
119         this(code, new IJavaElement[]{element});
120     }
121     /**
122      * Constructs an Java model status with the given corresponding
123      * element and string
124      */

125     public JavaModelStatus(int code, IJavaElement element, String JavaDoc string) {
126         this(code, new IJavaElement[]{element});
127         this.string = string;
128     }
129
130     /**
131      * Constructs an Java model status with the given corresponding
132      * element and path
133      */

134     public JavaModelStatus(int code, IJavaElement element, IPath path) {
135         this(code, new IJavaElement[]{element});
136         this.path = path;
137     }
138
139     /**
140      * Constructs an Java model status with the given corresponding
141      * element, path and string
142      */

143     public JavaModelStatus(int code, IJavaElement element, IPath path, String JavaDoc string) {
144         this(code, new IJavaElement[]{element});
145         this.path = path;
146         this.string = string;
147     }
148
149     /**
150      * Constructs an Java model status with the given corresponding
151      * element and path
152      */

153     public JavaModelStatus(int severity, int code, IJavaElement element, IPath path, String JavaDoc msg) {
154         super(severity, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
155
this.elements= new IJavaElement[]{element};
156         this.path = path;
157         this.string = msg;
158     }
159
160     /**
161      * Constructs an Java model status with no corresponding elements.
162      */

163     public JavaModelStatus(CoreException coreException) {
164         super(ERROR, JavaCore.PLUGIN_ID, CORE_EXCEPTION, "JavaModelStatus", coreException); //$NON-NLS-1$
165
elements= JavaElement.NO_ELEMENTS;
166     }
167     protected int getBits() {
168         int severity = 1 << (getCode() % 100 / 33);
169         int category = 1 << ((getCode() / 100) + 3);
170         return severity | category;
171     }
172     /**
173      * @see IStatus
174      */

175     public IStatus[] getChildren() {
176         return children;
177     }
178     /**
179      * @see IJavaModelStatus
180      */

181     public IJavaElement[] getElements() {
182         return elements;
183     }
184     /**
185      * Returns the message that is relevant to the code of this status.
186      */

187     public String JavaDoc getMessage() {
188         Throwable JavaDoc exception = getException();
189         if (exception == null) {
190             switch (getCode()) {
191                 case CORE_EXCEPTION :
192                     return Messages.status_coreException;
193
194                 case BUILDER_INITIALIZATION_ERROR:
195                     return Messages.build_initializationError;
196
197                 case BUILDER_SERIALIZATION_ERROR:
198                     return Messages.build_serializationError;
199
200                 case DEVICE_PATH:
201                     return Messages.bind(Messages.status_cannotUseDeviceOnPath, getPath().toString());
202
203                 case DOM_EXCEPTION:
204                     return Messages.status_JDOMError;
205
206                 case ELEMENT_DOES_NOT_EXIST:
207                     return Messages.bind(Messages.element_doesNotExist, ((JavaElement)elements[0]).toStringWithAncestors());
208
209                 case ELEMENT_NOT_ON_CLASSPATH:
210                     return Messages.bind(Messages.element_notOnClasspath, ((JavaElement)elements[0]).toStringWithAncestors());
211
212                 case EVALUATION_ERROR:
213                     return Messages.bind(Messages.status_evaluationError, string);
214
215                 case INDEX_OUT_OF_BOUNDS:
216                     return Messages.status_indexOutOfBounds;
217
218                 case INVALID_CONTENTS:
219                     return Messages.status_invalidContents;
220
221                 case INVALID_DESTINATION:
222                     return Messages.bind(Messages.status_invalidDestination, ((JavaElement)elements[0]).toStringWithAncestors());
223
224                 case INVALID_ELEMENT_TYPES:
225                     StringBuffer JavaDoc buff= new StringBuffer JavaDoc(Messages.operation_notSupported);
226                     for (int i= 0; i < elements.length; i++) {
227                         if (i > 0) {
228                             buff.append(", "); //$NON-NLS-1$
229
}
230                         buff.append(((JavaElement)elements[i]).toStringWithAncestors());
231                     }
232                     return buff.toString();
233
234                 case INVALID_NAME:
235                     return Messages.bind(Messages.status_invalidName, string);
236
237                 case INVALID_PACKAGE:
238                     return Messages.bind(Messages.status_invalidPackage, string);
239
240                 case INVALID_PATH:
241                     if (string != null) {
242                         return string;
243                     } else {
244                         return Messages.bind(
245                             Messages.status_invalidPath,
246                             new String JavaDoc[] {getPath() == null ? "null" : getPath().toString()} //$NON-NLS-1$
247
);
248                     }
249
250                 case INVALID_PROJECT:
251                     return Messages.bind(Messages.status_invalidProject, string);
252
253                 case INVALID_RESOURCE:
254                     return Messages.bind(Messages.status_invalidResource, string);
255
256                 case INVALID_RESOURCE_TYPE:
257                     return Messages.bind(Messages.status_invalidResourceType, string);
258
259                 case INVALID_SIBLING:
260                     if (string != null) {
261                         return Messages.bind(Messages.status_invalidSibling, string);
262                     } else {
263                         return Messages.bind(Messages.status_invalidSibling, ((JavaElement)elements[0]).toStringWithAncestors());
264                     }
265
266                 case IO_EXCEPTION:
267                     return Messages.status_IOException;
268
269                 case NAME_COLLISION:
270                     if (elements != null && elements.length > 0) {
271                         IJavaElement element = elements[0];
272                         if (element instanceof PackageFragment && ((PackageFragment) element).isDefaultPackage()) {
273                             return Messages.operation_cannotRenameDefaultPackage;
274                         }
275                     }
276                     if (string != null) {
277                         return string;
278                     } else {
279                         return Messages.bind(Messages.status_nameCollision, ""); //$NON-NLS-1$
280
}
281                 case NO_ELEMENTS_TO_PROCESS:
282                     return Messages.operation_needElements;
283
284                 case NULL_NAME:
285                     return Messages.operation_needName;
286
287                 case NULL_PATH:
288                     return Messages.operation_needPath;
289
290                 case NULL_STRING:
291                     return Messages.operation_needString;
292
293                 case PATH_OUTSIDE_PROJECT:
294                     return Messages.bind(Messages.operation_pathOutsideProject, new String JavaDoc[] {string, ((JavaElement)elements[0]).toStringWithAncestors()});
295
296                 case READ_ONLY:
297                     IJavaElement element = elements[0];
298                     String JavaDoc name = element.getElementName();
299                     if (element instanceof IPackageFragment && name.equals(IPackageFragment.DEFAULT_PACKAGE_NAME)) {
300                         return Messages.status_defaultPackageReadOnly;
301                     }
302                     return Messages.bind(Messages.status_readOnly, name);
303
304                 case RELATIVE_PATH:
305                     return Messages.bind(Messages.operation_needAbsolutePath, getPath().toString());
306
307                 case TARGET_EXCEPTION:
308                     return Messages.status_targetException;
309
310                 case UPDATE_CONFLICT:
311                     return Messages.status_updateConflict;
312
313                 case NO_LOCAL_CONTENTS :
314                     return Messages.bind(Messages.status_noLocalContents, getPath().toString());
315
316                 case CP_CONTAINER_PATH_UNBOUND:
317                     IJavaProject javaProject = (IJavaProject)elements[0];
318                     ClasspathContainerInitializer initializer = JavaCore.getClasspathContainerInitializer(this.path.segment(0));
319                     String JavaDoc description = null;
320                     if (initializer != null) description = initializer.getDescription(this.path, javaProject);
321                     if (description == null) description = path.makeRelative().toString();
322                     return Messages.bind(Messages.classpath_unboundContainerPath, new String JavaDoc[] {description, javaProject.getElementName()});
323
324                 case INVALID_CP_CONTAINER_ENTRY:
325                     javaProject = (IJavaProject)elements[0];
326                     IClasspathContainer container = null;
327                     description = null;
328                     try {
329                         container = JavaCore.getClasspathContainer(path, javaProject);
330                     } catch(JavaModelException e){
331                         // project doesn't exist: ignore
332
}
333                     if (container == null) {
334                          initializer = JavaCore.getClasspathContainerInitializer(path.segment(0));
335                         if (initializer != null) description = initializer.getDescription(path, javaProject);
336                     } else {
337                         description = container.getDescription();
338                     }
339                     if (description == null) description = path.makeRelative().toString();
340                     return Messages.bind(Messages.classpath_invalidContainer, new String JavaDoc[] {description, javaProject.getElementName()});
341
342                 case CP_VARIABLE_PATH_UNBOUND:
343                     javaProject = (IJavaProject)elements[0];
344                     return Messages.bind(Messages.classpath_unboundVariablePath, new String JavaDoc[] {path.makeRelative().toString(), javaProject.getElementName()});
345
346                 case CLASSPATH_CYCLE:
347                     javaProject = (IJavaProject)elements[0];
348                     return Messages.bind(Messages.classpath_cycle, javaProject.getElementName());
349
350                 case DISABLED_CP_EXCLUSION_PATTERNS:
351                     javaProject = (IJavaProject)elements[0];
352                     String JavaDoc projectName = javaProject.getElementName();
353                     IPath newPath = path;
354                     if (path.segment(0).toString().equals(projectName)) {
355                         newPath = path.removeFirstSegments(1);
356                     }
357                     return Messages.bind(Messages.classpath_disabledInclusionExclusionPatterns, new String JavaDoc[] {newPath.makeRelative().toString(), projectName});
358
359                 case DISABLED_CP_MULTIPLE_OUTPUT_LOCATIONS:
360                     javaProject = (IJavaProject)elements[0];
361                     projectName = javaProject.getElementName();
362                     newPath = path;
363                     if (path.segment(0).toString().equals(projectName)) {
364                         newPath = path.removeFirstSegments(1);
365                     }
366                     return Messages.bind(Messages.classpath_disabledMultipleOutputLocations, new String JavaDoc[] {newPath.makeRelative().toString(), projectName});
367
368                 case INCOMPATIBLE_JDK_LEVEL:
369                         javaProject = (IJavaProject)elements[0];
370                         return Messages.bind(Messages.classpath_incompatibleLibraryJDKLevel, new String JavaDoc[]{
371                             javaProject.getElementName(),
372                             javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true),
373                             path.makeRelative().toString(),
374                             string,
375                         });
376
377                 case CANNOT_RETRIEVE_ATTACHED_JAVADOC :
378                     if (elements != null && elements.length == 1) {
379                         if (this.string != null) {
380                             return Messages.bind(Messages.status_cannot_retrieve_attached_javadoc, ((JavaElement)elements[0]).toStringWithAncestors(), this.string);
381                         }
382                         return Messages.bind(Messages.status_cannot_retrieve_attached_javadoc, ((JavaElement)elements[0]).toStringWithAncestors(), ""); //$NON-NLS-1$
383
}
384                     if (this.string != null) {
385                         return Messages.bind(Messages.status_cannot_retrieve_attached_javadoc, this.string, "");//$NON-NLS-1$
386
}
387                     break;
388
389                 case UNKNOWN_JAVADOC_FORMAT :
390                     return Messages.bind(Messages.status_unknown_javadoc_format, ((JavaElement)elements[0]).toStringWithAncestors());
391
392                 case DEPRECATED_VARIABLE :
393                     javaProject = (IJavaProject)elements[0];
394                     return Messages.bind(Messages.classpath_deprecated_variable, new String JavaDoc[] {path.segment(0).toString(), javaProject.getElementName(), this.string});
395             }
396             if (string != null) {
397                 return string;
398             } else {
399                 return ""; //$NON-NLS-1$
400
}
401         } else {
402             String JavaDoc message = exception.getMessage();
403             if (message != null) {
404                 return message;
405             } else {
406                 return exception.toString();
407             }
408         }
409     }
410     /**
411      * @see IJavaModelStatus#getPath()
412      */

413     public IPath getPath() {
414         return path;
415     }
416     /**
417      * @see IStatus#getSeverity()
418      */

419     public int getSeverity() {
420         if (children == NO_CHILDREN) return super.getSeverity();
421         int severity = -1;
422         for (int i = 0, max = children.length; i < max; i++) {
423             int childrenSeverity = children[i].getSeverity();
424             if (childrenSeverity > severity) {
425                 severity = childrenSeverity;
426             }
427         }
428         return severity;
429     }
430     /**
431      * @see IJavaModelStatus#getString()
432      * @deprecated
433      */

434     public String JavaDoc getString() {
435         return string;
436     }
437     /**
438      * @see IJavaModelStatus#isDoesNotExist()
439      */

440     public boolean isDoesNotExist() {
441         int code = getCode();
442         return code == ELEMENT_DOES_NOT_EXIST || code == ELEMENT_NOT_ON_CLASSPATH;
443     }
444     /**
445      * @see IStatus#isMultiStatus()
446      */

447     public boolean isMultiStatus() {
448         return children != NO_CHILDREN;
449     }
450     /**
451      * @see IStatus#isOK()
452      */

453     public boolean isOK() {
454         return getCode() == OK;
455     }
456     /**
457      * @see IStatus#matches(int)
458      */

459     public boolean matches(int mask) {
460         if (! isMultiStatus()) {
461             return matches(this, mask);
462         } else {
463             for (int i = 0, max = children.length; i < max; i++) {
464                 if (matches((JavaModelStatus) children[i], mask))
465                     return true;
466             }
467             return false;
468         }
469     }
470     /**
471      * Helper for matches(int).
472      */

473     protected boolean matches(JavaModelStatus status, int mask) {
474         int severityMask = mask & 0x7;
475         int categoryMask = mask & ~0x7;
476         int bits = status.getBits();
477         return ((severityMask == 0) || (bits & severityMask) != 0) && ((categoryMask == 0) || (bits & categoryMask) != 0);
478     }
479     /**
480      * Creates and returns a new <code>IJavaModelStatus</code> that is a
481      * a multi-status status.
482      *
483      * @see IStatus#isMultiStatus()
484      */

485     public static IJavaModelStatus newMultiStatus(IJavaModelStatus[] children) {
486         JavaModelStatus jms = new JavaModelStatus();
487         jms.children = children;
488         return jms;
489     }
490     /**
491      * Returns a printable representation of this exception for debugging
492      * purposes.
493      */

494     public String JavaDoc toString() {
495         if (this == VERIFIED_OK){
496             return "JavaModelStatus[OK]"; //$NON-NLS-1$
497
}
498         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
499         buffer.append("Java Model Status ["); //$NON-NLS-1$
500
buffer.append(getMessage());
501         buffer.append("]"); //$NON-NLS-1$
502
return buffer.toString();
503     }
504 }
505
Popular Tags