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