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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
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;
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;
25 * @see IJavaModelStatus
28 public class JavaModelStatus
30 implements IJavaModelStatus, IJavaModelStatusConstants, IResourceStatus {
33 * The elements related to the failure, or <code>null</code>
34 * if no elements are involved.
36 protected IJavaElement[] fElements = new IJavaElement[0];
39 * The path related to the failure, or <code>null</code>
40 * if no path is involved.
42 protected IPath fPath;
44 * The <code>String</code> related to the failure, or <code>null</code>
45 * if no <code>String</code> is involved.
47 protected String fString;
51 protected final static IStatus[] fgEmptyChildren = new IStatus[] {
53 protected IStatus[] fChildren = fgEmptyChildren;
56 * Shared empty collection used for efficiency.
58 protected static IJavaElement[] fgObjectEmptyChildren = new IJavaElement[]{};
64 public static final IJavaModelStatus VERIFIED_OK = new JavaModelStatus(OK, OK, Util.bind("status.OK")); //$NON-NLS-1$
67 * Constructs an Java model status with no corresponding elements.
69 public JavaModelStatus() {
70 // no code for an multi-status
71 super(ERROR, JavaCore.PLUGIN_ID, 0, "JavaModelStatus", null); //$NON-NLS-1$
74 * Constructs an Java model status with no corresponding elements.
76 public JavaModelStatus(int code) {
77 super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
78 // fElements= JavaElementInfo.fgEmptyChildren;
79 fElements = fgObjectEmptyChildren;
82 * Constructs an Java model status with the given corresponding
85 public JavaModelStatus(int code, IJavaElement[] elements) {
86 super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
91 * Constructs an Java model status with no corresponding elements.
93 public JavaModelStatus(int code, String string) {
94 this(ERROR, code, string);
97 * Constructs an Java model status with no corresponding elements.
99 public JavaModelStatus(int severity, int code, String string) {
100 super(severity, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
101 // fElements= JavaElementInfo.fgEmptyChildren;
102 fElements = fgObjectEmptyChildren;
107 * Constructs an Java model status with no corresponding elements.
109 public JavaModelStatus(int code, Throwable throwable) {
110 super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", throwable); //$NON-NLS-1$
111 // fElements= JavaElementInfo.fgEmptyChildren;
112 fElements = fgObjectEmptyChildren;
115 * Constructs an Java model status with no corresponding elements.
117 public JavaModelStatus(int code, IPath path) {
118 super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
119 // fElements= JavaElementInfo.fgEmptyChildren;
120 fElements = fgObjectEmptyChildren;
124 * Constructs an Java model status with the given corresponding
127 public JavaModelStatus(int code, IJavaElement element) {
128 this(code, new IJavaElement[]{element});
131 * Constructs an Java model status with the given corresponding
134 public JavaModelStatus(int code, IJavaElement element, String string) {
135 this(code, new IJavaElement[]{element});
140 * Constructs an Java model status with the given corresponding
143 public JavaModelStatus(int code, IJavaElement element, IPath path) {
144 this(code, new IJavaElement[]{element});
148 * Constructs an Java model status with no corresponding elements.
150 public JavaModelStatus(CoreException coreException) {
151 super(ERROR, JavaCore.PLUGIN_ID, CORE_EXCEPTION, "JavaModelStatus", coreException); //$NON-NLS-1$
152 // fElements= JavaElementInfo.fgEmptyChildren;
153 fElements = fgObjectEmptyChildren;
155 protected int getBits() {
156 int severity = 1 << (getCode() % 100 / 33);
157 int category = 1 << ((getCode() / 100) + 3);
158 return severity | category;
163 public IStatus[] getChildren() {
167 * @see IJavaModelStatus
169 public IJavaElement[] getElements() {
173 * Returns the message that is relevant to the code of this status.
175 public String getMessage() {
176 Throwable exception = getException();
177 if (exception == null) {
179 case CORE_EXCEPTION :
180 return Util.bind("status.coreException"); //$NON-NLS-1$
182 case BUILDER_INITIALIZATION_ERROR :
183 return Util.bind("build.initializationError"); //$NON-NLS-1$
185 case BUILDER_SERIALIZATION_ERROR :
186 return Util.bind("build.serializationError"); //$NON-NLS-1$
189 return Util.bind("status.cannotUseDeviceOnPath", getPath().toString()); //$NON-NLS-1$
192 return Util.bind("status.JDOMError"); //$NON-NLS-1$
194 // case ELEMENT_DOES_NOT_EXIST:
195 // return Util.bind("element.doesNotExist",((JavaElement)fElements[0]).toStringWithAncestors()); //$NON-NLS-1$
197 case EVALUATION_ERROR :
198 return Util.bind("status.evaluationError", fString); //$NON-NLS-1$
200 case INDEX_OUT_OF_BOUNDS :
201 return Util.bind("status.indexOutOfBounds"); //$NON-NLS-1$
203 case INVALID_CONTENTS :
204 return Util.bind("status.invalidContents"); //$NON-NLS-1$
206 // case INVALID_DESTINATION:
207 // return Util.bind("status.invalidDestination", ((JavaElement)fElements[0]).toStringWithAncestors()); //$NON-NLS-1$
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++) {
213 // buff.append(", "); //$NON-NLS-1$
215 // buff.append(((JavaElement)fElements[i]).toStringWithAncestors());
217 // return buff.toString();
220 return Util.bind("status.invalidName", fString); //$NON-NLS-1$
222 case INVALID_PACKAGE :
223 return Util.bind("status.invalidPackage", fString); //$NON-NLS-1$
226 if (fString != null) {
229 return Util.bind("status.invalidPath", getPath() == null ? "null" : getPath().toString()); //$NON-NLS-1$ //$NON-NLS-2$
232 case INVALID_PROJECT :
233 return Util.bind("status.invalidProject", fString); //$NON-NLS-1$
235 case INVALID_RESOURCE :
236 return Util.bind("status.invalidResource", fString); //$NON-NLS-1$
238 case INVALID_RESOURCE_TYPE :
239 return Util.bind("status.invalidResourceType", fString); //$NON-NLS-1$
241 // case INVALID_SIBLING:
242 // if (fString != null) {
243 // return Util.bind("status.invalidSibling", fString); //$NON-NLS-1$
245 // return Util.bind("status.invalidSibling", ((JavaElement)fElements[0]).toStringWithAncestors()); //$NON-NLS-1$
249 return Util.bind("status.IOException"); //$NON-NLS-1$
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$
259 // if (fString != null) {
262 // return Util.bind("status.nameCollision", ""); //$NON-NLS-1$ //$NON-NLS-2$
264 case NO_ELEMENTS_TO_PROCESS :
265 return Util.bind("operation.needElements"); //$NON-NLS-1$
268 return Util.bind("operation.needName"); //$NON-NLS-1$
271 return Util.bind("operation.needPath"); //$NON-NLS-1$
274 return Util.bind("operation.needString"); //$NON-NLS-1$
276 // case PATH_OUTSIDE_PROJECT:
277 // return Util.bind("operation.pathOutsideProject", fString, ((JavaElement)fElements[0]).toStringWithAncestors()); //$NON-NLS-1$
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$
285 // return Util.bind("status.readOnly", name); //$NON-NLS-1$
288 return Util.bind("operation.needAbsolutePath", getPath().toString()); //$NON-NLS-1$
290 case TARGET_EXCEPTION :
291 return Util.bind("status.targetException"); //$NON-NLS-1$
293 case UPDATE_CONFLICT :
294 return Util.bind("status.updateConflict"); //$NON-NLS-1$
296 case NO_LOCAL_CONTENTS :
297 return Util.bind("status.noLocalContents", getPath().toString()); //$NON-NLS-1$
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$
308 // case INVALID_CP_CONTAINER_ENTRY:
309 // path = this.fPath;
310 // javaProject = (IJavaProject)fElements[0];
311 // IClasspathContainer container = null;
312 // description = null;
314 // container = PHPCore.getClasspathContainer(path, javaProject);
315 // } catch(JavaModelException e){
317 // if (container == null) {
318 // initializer = PHPCore.getClasspathContainerInitializer(path.segment(0));
319 // if (initializer != null) description = initializer.getDescription(path, javaProject);
321 // description = container.getDescription();
323 // if (description == null) description = path.makeRelative().toString();
324 // return Util.bind("classpath.invalidContainer", description); //$NON-NLS-1$
326 // case CP_VARIABLE_PATH_UNBOUND:
327 // path = this.fPath;
328 // return Util.bind("classpath.unboundVariablePath", path.makeRelative().toString()); //$NON-NLS-1$
330 // case CLASSPATH_CYCLE:
331 // javaProject = (IJavaProject)fElements[0];
332 // return Util.bind("classpath.cycle", javaProject.getElementName()); //$NON-NLS-1$
334 // case DISABLED_CP_EXCLUSION_PATTERNS:
335 // path = this.fPath;
336 // return Util.bind("classpath.disabledExclusionPatterns", path.makeRelative().toString()); //$NON-NLS-1$
338 // case DISABLED_CP_MULTIPLE_OUTPUT_LOCATIONS:
339 // path = this.fPath;
340 // return Util.bind("classpath.disabledMultipleOutputLocations", path.makeRelative().toString()); //$NON-NLS-1$
342 if (fString != null) {
345 return ""; // //$NON-NLS-1$
348 String message = exception.getMessage();
349 if (message != null) {
352 return exception.toString();
357 * @see IJavaModelStatus#getPath()
359 public IPath getPath() {
363 * @see IStatus#getSeverity()
365 public int getSeverity() {
366 if (fChildren == fgEmptyChildren)
367 return super.getSeverity();
369 for (int i = 0, max = fChildren.length; i < max; i++) {
370 int childrenSeverity = fChildren[i].getSeverity();
371 if (childrenSeverity > severity) {
372 severity = childrenSeverity;
378 * @see IJavaModelStatus#getString()
381 public String getString() {
385 * @see IJavaModelStatus#isDoesNotExist()
387 public boolean isDoesNotExist() {
388 return getCode() == ELEMENT_DOES_NOT_EXIST;
391 * @see IStatus#isMultiStatus()
393 public boolean isMultiStatus() {
394 return fChildren != fgEmptyChildren;
397 * @see IStatus#isOK()
399 public boolean isOK() {
400 return getCode() == OK;
403 * @see IStatus#matches(int)
405 public boolean matches(int mask) {
406 if (!isMultiStatus()) {
407 return matches(this, mask);
409 for (int i = 0, max = fChildren.length; i < max; i++) {
410 if (matches((JavaModelStatus) fChildren[i], mask))
417 * Helper for matches(int).
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);
427 * Creates and returns a new <code>IJavaModelStatus</code> that is a
428 * a multi-status status.
430 * @see IStatus#isMultiStatus()
432 public static IJavaModelStatus newMultiStatus(IJavaModelStatus[] children) {
433 JavaModelStatus jms = new JavaModelStatus();
434 jms.fChildren = children;
438 * Returns a printable representation of this exception for debugging
441 public String toString() {
442 if (this == VERIFIED_OK) {
443 return "JavaModelStatus[OK]"; //$NON-NLS-1$
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();