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