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;
17 import net.sourceforge.phpdt.internal.core.util.Util;
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;
26 * @see IJavaModelStatus
29 public class JavaModelStatus extends Status implements IJavaModelStatus,
30 IJavaModelStatusConstants, IResourceStatus {
33 * The elements related to the failure, or <code>null</code> if no
34 * elements are involved.
36 protected IJavaElement[] fElements = new IJavaElement[0];
39 * The path related to the failure, or <code>null</code> if no path is
42 protected IPath fPath;
45 * The <code>String</code> related to the failure, or <code>null</code>
46 * if no <code>String</code> is involved.
48 protected String fString;
53 protected final static IStatus[] fgEmptyChildren = new IStatus[] {};
55 protected IStatus[] fChildren = fgEmptyChildren;
58 * Shared empty collection used for efficiency.
60 protected static IJavaElement[] fgObjectEmptyChildren = new IJavaElement[] {};
65 public static final IJavaModelStatus VERIFIED_OK = new JavaModelStatus(OK,
66 OK, Util.bind("status.OK")); //$NON-NLS-1$
69 * Constructs an Java model status with no corresponding elements.
71 public JavaModelStatus() {
72 // no code for an multi-status
73 super(ERROR, JavaCore.PLUGIN_ID, 0, "JavaModelStatus", null); //$NON-NLS-1$
77 * Constructs an Java model status with no corresponding elements.
79 public JavaModelStatus(int code) {
80 super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
81 // fElements= JavaElementInfo.fgEmptyChildren;
82 fElements = fgObjectEmptyChildren;
86 * Constructs an Java model status with the given corresponding elements.
88 public JavaModelStatus(int code, IJavaElement[] elements) {
89 super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
95 * Constructs an Java model status with no corresponding elements.
97 public JavaModelStatus(int code, String string) {
98 this(ERROR, code, string);
102 * Constructs an Java model status with no corresponding elements.
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;
113 * Constructs an Java model status with no corresponding elements.
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;
122 * Constructs an Java model status with no corresponding elements.
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;
132 * Constructs an Java model status with the given corresponding element.
134 public JavaModelStatus(int code, IJavaElement element) {
135 this(code, new IJavaElement[] { element });
139 * Constructs an Java model status with the given corresponding element and
142 public JavaModelStatus(int code, IJavaElement element, String string) {
143 this(code, new IJavaElement[] { element });
148 * Constructs an Java model status with the given corresponding element and
151 public JavaModelStatus(int code, IJavaElement element, IPath path) {
152 this(code, new IJavaElement[] { element });
157 * Constructs an Java model status with no corresponding elements.
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;
166 protected int getBits() {
167 int severity = 1 << (getCode() % 100 / 33);
168 int category = 1 << ((getCode() / 100) + 3);
169 return severity | category;
175 public IStatus[] getChildren() {
180 * @see IJavaModelStatus
182 public IJavaElement[] getElements() {
187 * Returns the message that is relevant to the code of this status.
189 public String getMessage() {
190 Throwable exception = getException();
191 if (exception == null) {
194 return Util.bind("status.coreException"); //$NON-NLS-1$
196 case BUILDER_INITIALIZATION_ERROR:
197 return Util.bind("build.initializationError"); //$NON-NLS-1$
199 case BUILDER_SERIALIZATION_ERROR:
200 return Util.bind("build.serializationError"); //$NON-NLS-1$
204 "status.cannotUseDeviceOnPath", getPath().toString()); //$NON-NLS-1$
207 return Util.bind("status.JDOMError"); //$NON-NLS-1$
209 // case ELEMENT_DOES_NOT_EXIST:
211 // ProjectPrefUtil.bind("element.doesNotExist",((JavaElement)fElements[0]).toStringWithAncestors());
214 case EVALUATION_ERROR:
215 return Util.bind("status.evaluationError", fString); //$NON-NLS-1$
217 case INDEX_OUT_OF_BOUNDS:
218 return Util.bind("status.indexOutOfBounds"); //$NON-NLS-1$
220 case INVALID_CONTENTS:
221 return Util.bind("status.invalidContents"); //$NON-NLS-1$
223 // case INVALID_DESTINATION:
224 // return ProjectPrefUtil.bind("status.invalidDestination",
225 // ((JavaElement)fElements[0]).toStringWithAncestors());
228 // case INVALID_ELEMENT_TYPES:
229 // StringBuffer buff= new
230 // StringBuffer(ProjectPrefUtil.bind("operation.notSupported"));
232 // for (int i= 0; i < fElements.length; i++) {
234 // buff.append(", "); //$NON-NLS-1$
236 // buff.append(((JavaElement)fElements[i]).toStringWithAncestors());
238 // return buff.toString();
241 return Util.bind("status.invalidName", fString); //$NON-NLS-1$
243 case INVALID_PACKAGE:
244 return Util.bind("status.invalidPackage", fString); //$NON-NLS-1$
247 if (fString != null) {
252 "status.invalidPath", getPath() == null ? "null" : getPath().toString()); //$NON-NLS-1$ //$NON-NLS-2$
255 case INVALID_PROJECT:
256 return Util.bind("status.invalidProject", fString); //$NON-NLS-1$
258 case INVALID_RESOURCE:
259 return Util.bind("status.invalidResource", fString); //$NON-NLS-1$
261 case INVALID_RESOURCE_TYPE:
262 return Util.bind("status.invalidResourceType", fString); //$NON-NLS-1$
264 // case INVALID_SIBLING:
265 // if (fString != null) {
266 // return ProjectPrefUtil.bind("status.invalidSibling",
267 // fString); //$NON-NLS-1$
269 // return ProjectPrefUtil.bind("status.invalidSibling",
270 // ((JavaElement)fElements[0]).toStringWithAncestors());
275 return Util.bind("status.IOException"); //$NON-NLS-1$
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)) {
284 // ProjectPrefUtil.bind("operation.cannotRenameDefaultPackage");
288 // if (fString != null) {
291 // return ProjectPrefUtil.bind("status.nameCollision", "");
292 // //$NON-NLS-1$ //$NON-NLS-2$
294 case NO_ELEMENTS_TO_PROCESS:
295 return Util.bind("operation.needElements"); //$NON-NLS-1$
298 return Util.bind("operation.needName"); //$NON-NLS-1$
301 return Util.bind("operation.needPath"); //$NON-NLS-1$
304 return Util.bind("operation.needString"); //$NON-NLS-1$
306 // case PATH_OUTSIDE_PROJECT:
307 // return ProjectPrefUtil.bind("operation.pathOutsideProject",
309 // ((JavaElement)fElements[0]).toStringWithAncestors());
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");
320 // return ProjectPrefUtil.bind("status.readOnly", name);
325 "operation.needAbsolutePath", getPath().toString()); //$NON-NLS-1$
327 case TARGET_EXCEPTION:
328 return Util.bind("status.targetException"); //$NON-NLS-1$
330 case UPDATE_CONFLICT:
331 return Util.bind("status.updateConflict"); //$NON-NLS-1$
333 case NO_LOCAL_CONTENTS:
335 .bind("status.noLocalContents", getPath().toString()); //$NON-NLS-1$
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$
350 // case INVALID_CP_CONTAINER_ENTRY:
351 // path = this.fPath;
352 // javaProject = (IJavaProject)fElements[0];
353 // IClasspathContainer container = null;
354 // description = null;
356 // container = PHPCore.getClasspathContainer(path, javaProject);
357 // } catch(JavaModelException e){
359 // if (container == null) {
361 // PHPCore.getClasspathContainerInitializer(path.segment(0));
362 // if (initializer != null) description =
363 // initializer.getDescription(path, javaProject);
365 // description = container.getDescription();
367 // if (description == null) description =
368 // path.makeRelative().toString();
369 // return ProjectPrefUtil.bind("classpath.invalidContainer",
370 // description); //$NON-NLS-1$
372 // case CP_VARIABLE_PATH_UNBOUND:
373 // path = this.fPath;
374 // return ProjectPrefUtil.bind("classpath.unboundVariablePath",
375 // path.makeRelative().toString()); //$NON-NLS-1$
377 // case CLASSPATH_CYCLE:
378 // javaProject = (IJavaProject)fElements[0];
379 // return ProjectPrefUtil.bind("classpath.cycle",
380 // javaProject.getElementName()); //$NON-NLS-1$
382 // case DISABLED_CP_EXCLUSION_PATTERNS:
383 // path = this.fPath;
385 // ProjectPrefUtil.bind("classpath.disabledExclusionPatterns",
386 // path.makeRelative().toString()); //$NON-NLS-1$
388 // case DISABLED_CP_MULTIPLE_OUTPUT_LOCATIONS:
389 // path = this.fPath;
391 // ProjectPrefUtil.bind("classpath.disabledMultipleOutputLocations",
392 // path.makeRelative().toString()); //$NON-NLS-1$
394 if (fString != null) {
397 return ""; // //$NON-NLS-1$
400 String message = exception.getMessage();
401 if (message != null) {
404 return exception.toString();
410 * @see IJavaModelStatus#getPath()
412 public IPath getPath() {
417 * @see IStatus#getSeverity()
419 public int getSeverity() {
420 if (fChildren == fgEmptyChildren)
421 return super.getSeverity();
423 for (int i = 0, max = fChildren.length; i < max; i++) {
424 int childrenSeverity = fChildren[i].getSeverity();
425 if (childrenSeverity > severity) {
426 severity = childrenSeverity;
433 * @see IJavaModelStatus#getString()
436 public String getString() {
441 * @see IJavaModelStatus#isDoesNotExist()
443 public boolean isDoesNotExist() {
444 return getCode() == ELEMENT_DOES_NOT_EXIST;
448 * @see IStatus#isMultiStatus()
450 public boolean isMultiStatus() {
451 return fChildren != fgEmptyChildren;
455 * @see IStatus#isOK()
457 public boolean isOK() {
458 return getCode() == OK;
462 * @see IStatus#matches(int)
464 public boolean matches(int mask) {
465 if (!isMultiStatus()) {
466 return matches(this, mask);
468 for (int i = 0, max = fChildren.length; i < max; i++) {
469 if (matches((JavaModelStatus) fChildren[i], mask))
477 * Helper for matches(int).
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);
488 * Creates and returns a new <code>IJavaModelStatus</code> that is a a
489 * multi-status status.
491 * @see IStatus#isMultiStatus()
493 public static IJavaModelStatus newMultiStatus(IJavaModelStatus[] children) {
494 JavaModelStatus jms = new JavaModelStatus();
495 jms.fChildren = children;
500 * Returns a printable representation of this exception for debugging
503 public String toString() {
504 if (this == VERIFIED_OK) {
505 return "JavaModelStatus[OK]"; //$NON-NLS-1$
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();