Parser detects wrong include files now
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / JavaModelStatus.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import net.sourceforge.phpdt.core.IJavaElement;
14 import net.sourceforge.phpdt.core.IJavaModelStatus;
15 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
16 import net.sourceforge.phpdt.core.JavaCore;
17 import net.sourceforge.phpdt.internal.core.util.Util;
18
19 import org.eclipse.core.resources.IResourceStatus;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24
25 /**
26  * @see IJavaModelStatus
27  */
28
29 public class JavaModelStatus
30   extends Status
31   implements IJavaModelStatus, IJavaModelStatusConstants, IResourceStatus {
32
33   /**
34    * The elements related to the failure, or <code>null</code>
35    * if no elements are involved.
36    */
37   protected IJavaElement[] fElements = new IJavaElement[0];
38   
39   /**
40    * The path related to the failure, or <code>null</code>
41    * if no path is involved.
42    */
43   protected IPath fPath;
44   /**
45    * The <code>String</code> related to the failure, or <code>null</code>
46    * if no <code>String</code> is involved.
47    */
48   protected String fString;
49   /**
50    * Empty children
51    */
52   protected final static IStatus[] fgEmptyChildren = new IStatus[] {
53   };
54   protected IStatus[] fChildren = fgEmptyChildren;
55
56   /**
57          * Shared empty collection used for efficiency.
58          */
59   protected static IJavaElement[] fgObjectEmptyChildren = new IJavaElement[]{};
60   
61
62   /**
63    * Singleton OK object
64    */
65   public static final IJavaModelStatus VERIFIED_OK = new JavaModelStatus(OK, OK, Util.bind("status.OK")); //$NON-NLS-1$
66
67   /**
68    * Constructs an Java model status with no corresponding elements.
69    */
70   public JavaModelStatus() {
71     // no code for an multi-status
72     super(ERROR, JavaCore.PLUGIN_ID, 0, "JavaModelStatus", null); //$NON-NLS-1$
73   }
74   /**
75    * Constructs an Java model status with no corresponding elements.
76    */
77   public JavaModelStatus(int code) {
78     super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
79     //  fElements= JavaElementInfo.fgEmptyChildren;
80     fElements = fgObjectEmptyChildren;
81   }
82   /**
83    * Constructs an Java model status with the given corresponding
84    * elements.
85    */
86   public JavaModelStatus(int code, IJavaElement[] elements) {
87     super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
88     fElements = elements;
89     fPath = null;
90   }
91   /**
92    * Constructs an Java model status with no corresponding elements.
93    */
94   public JavaModelStatus(int code, String string) {
95     this(ERROR, code, string);
96   }
97   /**
98    * Constructs an Java model status with no corresponding elements.
99    */
100   public JavaModelStatus(int severity, int code, String string) {
101     super(severity, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
102     // fElements= JavaElementInfo.fgEmptyChildren;
103     fElements = fgObjectEmptyChildren;
104     fPath = null;
105     fString = string;
106   }
107   /**
108    * Constructs an Java model status with no corresponding elements.
109    */
110   public JavaModelStatus(int code, Throwable throwable) {
111     super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", throwable); //$NON-NLS-1$
112     //  fElements= JavaElementInfo.fgEmptyChildren;
113     fElements = fgObjectEmptyChildren;
114   }
115   /**
116    * Constructs an Java model status with no corresponding elements.
117    */
118   public JavaModelStatus(int code, IPath path) {
119     super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
120     //          fElements= JavaElementInfo.fgEmptyChildren;
121     fElements = fgObjectEmptyChildren;
122     fPath = path;
123   }
124   /**
125    * Constructs an Java model status with the given corresponding
126    * element.
127    */
128         public JavaModelStatus(int code, IJavaElement element) {
129                 this(code, new IJavaElement[]{element});
130         }
131   /**
132    * Constructs an Java model status with the given corresponding
133    * element and string
134    */
135         public JavaModelStatus(int code, IJavaElement element, String string) {
136                 this(code, new IJavaElement[]{element});
137                 fString = string;
138         }
139         
140         /**
141          * Constructs an Java model status with the given corresponding
142          * element and path
143          */
144         public JavaModelStatus(int code, IJavaElement element, IPath path) {
145                 this(code, new IJavaElement[]{element});
146                 fPath = path;
147         }       
148   /**
149    * Constructs an Java model status with no corresponding elements.
150    */
151   public JavaModelStatus(CoreException coreException) {
152     super(ERROR, JavaCore.PLUGIN_ID, CORE_EXCEPTION, "JavaModelStatus", coreException); //$NON-NLS-1$
153     //  fElements= JavaElementInfo.fgEmptyChildren;
154     fElements = fgObjectEmptyChildren;
155   }
156   protected int getBits() {
157     int severity = 1 << (getCode() % 100 / 33);
158     int category = 1 << ((getCode() / 100) + 3);
159     return severity | category;
160   }
161   /**
162    * @see IStatus
163    */
164   public IStatus[] getChildren() {
165     return fChildren;
166   }
167   /**
168    * @see IJavaModelStatus
169    */
170   public IJavaElement[] getElements() {
171     return fElements;
172   }
173   /**
174    * Returns the message that is relevant to the code of this status.
175    */
176   public String getMessage() {
177     Throwable exception = getException();
178     if (exception == null) {
179       switch (getCode()) {
180         case CORE_EXCEPTION :
181           return Util.bind("status.coreException"); //$NON-NLS-1$
182
183         case BUILDER_INITIALIZATION_ERROR :
184           return Util.bind("build.initializationError"); //$NON-NLS-1$
185
186         case BUILDER_SERIALIZATION_ERROR :
187           return Util.bind("build.serializationError"); //$NON-NLS-1$
188
189         case DEVICE_PATH :
190           return Util.bind("status.cannotUseDeviceOnPath", getPath().toString()); //$NON-NLS-1$
191
192         case DOM_EXCEPTION :
193           return Util.bind("status.JDOMError"); //$NON-NLS-1$
194
195 //                                      case ELEMENT_DOES_NOT_EXIST:
196 //                                              return ProjectPrefUtil.bind("element.doesNotExist",((JavaElement)fElements[0]).toStringWithAncestors()); //$NON-NLS-1$
197
198         case EVALUATION_ERROR :
199           return Util.bind("status.evaluationError", fString); //$NON-NLS-1$
200
201         case INDEX_OUT_OF_BOUNDS :
202           return Util.bind("status.indexOutOfBounds"); //$NON-NLS-1$
203
204         case INVALID_CONTENTS :
205           return Util.bind("status.invalidContents"); //$NON-NLS-1$
206
207           //                            case INVALID_DESTINATION:
208           //                                    return ProjectPrefUtil.bind("status.invalidDestination", ((JavaElement)fElements[0]).toStringWithAncestors()); //$NON-NLS-1$
209           //
210           //                            case INVALID_ELEMENT_TYPES:
211           //                                    StringBuffer buff= new StringBuffer(ProjectPrefUtil.bind("operation.notSupported")); //$NON-NLS-1$
212           //                                    for (int i= 0; i < fElements.length; i++) {
213           //                                            if (i > 0) {
214           //                                                    buff.append(", "); //$NON-NLS-1$
215           //                                            }
216           //                                            buff.append(((JavaElement)fElements[i]).toStringWithAncestors());
217           //                                    }
218           //                                    return buff.toString();
219
220         case INVALID_NAME :
221           return Util.bind("status.invalidName", fString); //$NON-NLS-1$
222
223         case INVALID_PACKAGE :
224           return Util.bind("status.invalidPackage", fString); //$NON-NLS-1$
225
226         case INVALID_PATH :
227           if (fString != null) {
228             return fString;
229           } else {
230             return Util.bind("status.invalidPath", getPath() == null ? "null" : getPath().toString()); //$NON-NLS-1$ //$NON-NLS-2$
231           }
232
233         case INVALID_PROJECT :
234           return Util.bind("status.invalidProject", fString); //$NON-NLS-1$
235
236         case INVALID_RESOURCE :
237           return Util.bind("status.invalidResource", fString); //$NON-NLS-1$
238
239         case INVALID_RESOURCE_TYPE :
240           return Util.bind("status.invalidResourceType", fString); //$NON-NLS-1$
241
242           //                            case INVALID_SIBLING:
243           //                                    if (fString != null) {
244           //                                            return ProjectPrefUtil.bind("status.invalidSibling", fString); //$NON-NLS-1$
245           //                                    } else {
246           //                                            return ProjectPrefUtil.bind("status.invalidSibling", ((JavaElement)fElements[0]).toStringWithAncestors()); //$NON-NLS-1$
247           //                                    }
248
249         case IO_EXCEPTION :
250           return Util.bind("status.IOException"); //$NON-NLS-1$
251
252           //                            case NAME_COLLISION:
253           //                                    if (fElements != null && fElements.length > 0) {
254           //                                            IJavaElement element = fElements[0];
255           //                                            String name = element.getElementName();
256           //                                            if (element instanceof IPackageFragment && name.equals(IPackageFragment.DEFAULT_PACKAGE_NAME)) {
257           //                                                    return ProjectPrefUtil.bind("operation.cannotRenameDefaultPackage"); //$NON-NLS-1$
258           //                                            }
259           //                                    }
260           //                                    if (fString != null) {
261           //                                            return fString;
262           //                                    } else {
263           //                                            return ProjectPrefUtil.bind("status.nameCollision", ""); //$NON-NLS-1$ //$NON-NLS-2$
264           //                                    }
265         case NO_ELEMENTS_TO_PROCESS :
266           return Util.bind("operation.needElements"); //$NON-NLS-1$
267
268         case NULL_NAME :
269           return Util.bind("operation.needName"); //$NON-NLS-1$
270
271         case NULL_PATH :
272           return Util.bind("operation.needPath"); //$NON-NLS-1$
273
274         case NULL_STRING :
275           return Util.bind("operation.needString"); //$NON-NLS-1$
276
277           //                            case PATH_OUTSIDE_PROJECT:
278           //                                    return ProjectPrefUtil.bind("operation.pathOutsideProject", fString, ((JavaElement)fElements[0]).toStringWithAncestors()); //$NON-NLS-1$
279           //
280           //                            case READ_ONLY:
281           //                                    IJavaElement element = fElements[0];
282           //                                    String name = element.getElementName();
283           //                                    if (element instanceof IPackageFragment && name.equals(IPackageFragment.DEFAULT_PACKAGE_NAME)) {
284           //                                            return ProjectPrefUtil.bind("status.defaultPackageReadOnly"); //$NON-NLS-1$
285           //                                    }
286           //                                    return  ProjectPrefUtil.bind("status.readOnly", name); //$NON-NLS-1$
287
288         case RELATIVE_PATH :
289           return Util.bind("operation.needAbsolutePath", getPath().toString()); //$NON-NLS-1$
290
291         case TARGET_EXCEPTION :
292           return Util.bind("status.targetException"); //$NON-NLS-1$
293
294         case UPDATE_CONFLICT :
295           return Util.bind("status.updateConflict"); //$NON-NLS-1$
296
297         case NO_LOCAL_CONTENTS :
298           return Util.bind("status.noLocalContents", getPath().toString()); //$NON-NLS-1$
299
300           //                            case CP_CONTAINER_PATH_UNBOUND:
301           //                                    IPath path = this.fPath;
302           //                                    IJavaProject javaProject = (IJavaProject)fElements[0];
303           //                                    ClasspathContainerInitializer initializer = PHPCore.getClasspathContainerInitializer(path.segment(0));
304           //                                    String description = null;
305           //                                    if (initializer != null) description = initializer.getDescription(path, javaProject);
306           //                                    if (description == null) description = path.makeRelative().toString();
307           //                                    return ProjectPrefUtil.bind("classpath.unboundContainerPath", description); //$NON-NLS-1$
308           //
309           //                            case INVALID_CP_CONTAINER_ENTRY:
310           //                                    path = this.fPath;
311           //                                    javaProject = (IJavaProject)fElements[0];
312           //                                    IClasspathContainer container = null;
313           //                                    description = null;
314           //                                    try {
315           //                                            container = PHPCore.getClasspathContainer(path, javaProject);
316           //                                    } catch(JavaModelException e){
317           //                                    }
318           //                                    if (container == null) {
319           //                                             initializer = PHPCore.getClasspathContainerInitializer(path.segment(0));
320           //                                            if (initializer != null) description = initializer.getDescription(path, javaProject);
321           //                                    } else {
322           //                                            description = container.getDescription();
323           //                                    }
324           //                                    if (description == null) description = path.makeRelative().toString();
325           //                                    return ProjectPrefUtil.bind("classpath.invalidContainer", description); //$NON-NLS-1$
326           //
327           //                    case CP_VARIABLE_PATH_UNBOUND:
328           //                                    path = this.fPath;
329           //                                    return ProjectPrefUtil.bind("classpath.unboundVariablePath", path.makeRelative().toString()); //$NON-NLS-1$
330           //                                    
331           //                    case CLASSPATH_CYCLE: 
332           //                                    javaProject = (IJavaProject)fElements[0];
333           //                                    return ProjectPrefUtil.bind("classpath.cycle", javaProject.getElementName()); //$NON-NLS-1$
334
335           //                    case DISABLED_CP_EXCLUSION_PATTERNS:
336           //                                    path = this.fPath;
337           //                                    return ProjectPrefUtil.bind("classpath.disabledExclusionPatterns", path.makeRelative().toString()); //$NON-NLS-1$
338           //
339           //                    case DISABLED_CP_MULTIPLE_OUTPUT_LOCATIONS:
340           //                                    path = this.fPath;
341           //                                    return ProjectPrefUtil.bind("classpath.disabledMultipleOutputLocations", path.makeRelative().toString()); //$NON-NLS-1$
342       }
343       if (fString != null) {
344         return fString;
345       } else {
346         return ""; // //$NON-NLS-1$
347       }
348     } else {
349       String message = exception.getMessage();
350       if (message != null) {
351         return message;
352       } else {
353         return exception.toString();
354       }
355     }
356   }
357   /**
358    * @see IJavaModelStatus#getPath()
359    */
360   public IPath getPath() {
361     return fPath;
362   }
363   /**
364    * @see IStatus#getSeverity()
365    */
366   public int getSeverity() {
367     if (fChildren == fgEmptyChildren)
368       return super.getSeverity();
369     int severity = -1;
370     for (int i = 0, max = fChildren.length; i < max; i++) {
371       int childrenSeverity = fChildren[i].getSeverity();
372       if (childrenSeverity > severity) {
373         severity = childrenSeverity;
374       }
375     }
376     return severity;
377   }
378   /**
379    * @see IJavaModelStatus#getString()
380    * @deprecated
381    */
382   public String getString() {
383     return fString;
384   }
385   /**
386    * @see IJavaModelStatus#isDoesNotExist()
387    */
388   public boolean isDoesNotExist() {
389     return getCode() == ELEMENT_DOES_NOT_EXIST;
390   }
391   /**
392    * @see IStatus#isMultiStatus()
393    */
394   public boolean isMultiStatus() {
395     return fChildren != fgEmptyChildren;
396   }
397   /**
398    * @see IStatus#isOK()
399    */
400   public boolean isOK() {
401     return getCode() == OK;
402   }
403   /**
404    * @see IStatus#matches(int)
405    */
406   public boolean matches(int mask) {
407     if (!isMultiStatus()) {
408       return matches(this, mask);
409     } else {
410       for (int i = 0, max = fChildren.length; i < max; i++) {
411         if (matches((JavaModelStatus) fChildren[i], mask))
412           return true;
413       }
414       return false;
415     }
416   }
417   /**
418    * Helper for matches(int).
419    */
420   protected boolean matches(JavaModelStatus status, int mask) {
421     int severityMask = mask & 0x7;
422     int categoryMask = mask & ~0x7;
423     int bits = status.getBits();
424     return ((severityMask == 0) || (bits & severityMask) != 0)
425       && ((categoryMask == 0) || (bits & categoryMask) != 0);
426   }
427   /**
428    * Creates and returns a new <code>IJavaModelStatus</code> that is a
429    * a multi-status status.
430    *
431    * @see IStatus#isMultiStatus()
432    */
433   public static IJavaModelStatus newMultiStatus(IJavaModelStatus[] children) {
434     JavaModelStatus jms = new JavaModelStatus();
435     jms.fChildren = children;
436     return jms;
437   }
438   /**
439    * Returns a printable representation of this exception for debugging
440    * purposes.
441    */
442   public String toString() {
443     if (this == VERIFIED_OK) {
444       return "JavaModelStatus[OK]"; //$NON-NLS-1$
445     }
446     StringBuffer buffer = new StringBuffer();
447     buffer.append("Java Model Status ["); //$NON-NLS-1$
448     buffer.append(getMessage());
449     buffer.append("]"); //$NON-NLS-1$
450     return buffer.toString();
451   }
452 }