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
31 implements IJavaModelStatus, IJavaModelStatusConstants, IResourceStatus {
34 * The elements related to the failure, or <code>null</code>
35 * if no elements are involved.
37 protected IJavaElement[] fElements = new IJavaElement[0];
40 * The path related to the failure, or <code>null</code>
41 * if no path is involved.
43 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;
52 protected final static IStatus[] fgEmptyChildren = new IStatus[] {
54 protected IStatus[] fChildren = fgEmptyChildren;
57 * Shared empty collection used for efficiency.
59 protected static IJavaElement[] fgObjectEmptyChildren = new IJavaElement[]{};
65 public static final IJavaModelStatus VERIFIED_OK = new JavaModelStatus(OK, OK, Util.bind("status.OK")); //$NON-NLS-1$
68 * Constructs an Java model status with no corresponding elements.
70 public JavaModelStatus() {
71 // no code for an multi-status
72 super(ERROR, JavaCore.PLUGIN_ID, 0, "JavaModelStatus", null); //$NON-NLS-1$
75 * Constructs an Java model status with no corresponding elements.
77 public JavaModelStatus(int code) {
78 super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
79 // fElements= JavaElementInfo.fgEmptyChildren;
80 fElements = fgObjectEmptyChildren;
83 * Constructs an Java model status with the given corresponding
86 public JavaModelStatus(int code, IJavaElement[] elements) {
87 super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
92 * Constructs an Java model status with no corresponding elements.
94 public JavaModelStatus(int code, String string) {
95 this(ERROR, code, string);
98 * Constructs an Java model status with no corresponding elements.
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;
108 * Constructs an Java model status with no corresponding elements.
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;
116 * Constructs an Java model status with no corresponding elements.
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;
125 * Constructs an Java model status with the given corresponding
128 public JavaModelStatus(int code, IJavaElement element) {
129 this(code, new IJavaElement[]{element});
132 * Constructs an Java model status with the given corresponding
135 public JavaModelStatus(int code, IJavaElement element, String string) {
136 this(code, new IJavaElement[]{element});
141 * Constructs an Java model status with the given corresponding
144 public JavaModelStatus(int code, IJavaElement element, IPath path) {
145 this(code, new IJavaElement[]{element});
149 * Constructs an Java model status with no corresponding elements.
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;
156 protected int getBits() {
157 int severity = 1 << (getCode() % 100 / 33);
158 int category = 1 << ((getCode() / 100) + 3);
159 return severity | category;
164 public IStatus[] getChildren() {
168 * @see IJavaModelStatus
170 public IJavaElement[] getElements() {
174 * Returns the message that is relevant to the code of this status.
176 public String getMessage() {
177 Throwable exception = getException();
178 if (exception == null) {
180 case CORE_EXCEPTION :
181 return Util.bind("status.coreException"); //$NON-NLS-1$
183 case BUILDER_INITIALIZATION_ERROR :
184 return Util.bind("build.initializationError"); //$NON-NLS-1$
186 case BUILDER_SERIALIZATION_ERROR :
187 return Util.bind("build.serializationError"); //$NON-NLS-1$
190 return Util.bind("status.cannotUseDeviceOnPath", getPath().toString()); //$NON-NLS-1$
193 return Util.bind("status.JDOMError"); //$NON-NLS-1$
195 // case ELEMENT_DOES_NOT_EXIST:
196 // return Util.bind("element.doesNotExist",((JavaElement)fElements[0]).toStringWithAncestors()); //$NON-NLS-1$
198 case EVALUATION_ERROR :
199 return Util.bind("status.evaluationError", fString); //$NON-NLS-1$
201 case INDEX_OUT_OF_BOUNDS :
202 return Util.bind("status.indexOutOfBounds"); //$NON-NLS-1$
204 case INVALID_CONTENTS :
205 return Util.bind("status.invalidContents"); //$NON-NLS-1$
207 // case INVALID_DESTINATION:
208 // return Util.bind("status.invalidDestination", ((JavaElement)fElements[0]).toStringWithAncestors()); //$NON-NLS-1$
210 // case INVALID_ELEMENT_TYPES:
211 // StringBuffer buff= new StringBuffer(Util.bind("operation.notSupported")); //$NON-NLS-1$
212 // for (int i= 0; i < fElements.length; i++) {
214 // buff.append(", "); //$NON-NLS-1$
216 // buff.append(((JavaElement)fElements[i]).toStringWithAncestors());
218 // return buff.toString();
221 return Util.bind("status.invalidName", fString); //$NON-NLS-1$
223 case INVALID_PACKAGE :
224 return Util.bind("status.invalidPackage", fString); //$NON-NLS-1$
227 if (fString != null) {
230 return Util.bind("status.invalidPath", getPath() == null ? "null" : getPath().toString()); //$NON-NLS-1$ //$NON-NLS-2$
233 case INVALID_PROJECT :
234 return Util.bind("status.invalidProject", fString); //$NON-NLS-1$
236 case INVALID_RESOURCE :
237 return Util.bind("status.invalidResource", fString); //$NON-NLS-1$
239 case INVALID_RESOURCE_TYPE :
240 return Util.bind("status.invalidResourceType", fString); //$NON-NLS-1$
242 // case INVALID_SIBLING:
243 // if (fString != null) {
244 // return Util.bind("status.invalidSibling", fString); //$NON-NLS-1$
246 // return Util.bind("status.invalidSibling", ((JavaElement)fElements[0]).toStringWithAncestors()); //$NON-NLS-1$
250 return Util.bind("status.IOException"); //$NON-NLS-1$
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 Util.bind("operation.cannotRenameDefaultPackage"); //$NON-NLS-1$
260 // if (fString != null) {
263 // return Util.bind("status.nameCollision", ""); //$NON-NLS-1$ //$NON-NLS-2$
265 case NO_ELEMENTS_TO_PROCESS :
266 return Util.bind("operation.needElements"); //$NON-NLS-1$
269 return Util.bind("operation.needName"); //$NON-NLS-1$
272 return Util.bind("operation.needPath"); //$NON-NLS-1$
275 return Util.bind("operation.needString"); //$NON-NLS-1$
277 // case PATH_OUTSIDE_PROJECT:
278 // return Util.bind("operation.pathOutsideProject", fString, ((JavaElement)fElements[0]).toStringWithAncestors()); //$NON-NLS-1$
281 // IJavaElement element = fElements[0];
282 // String name = element.getElementName();
283 // if (element instanceof IPackageFragment && name.equals(IPackageFragment.DEFAULT_PACKAGE_NAME)) {
284 // return Util.bind("status.defaultPackageReadOnly"); //$NON-NLS-1$
286 // return Util.bind("status.readOnly", name); //$NON-NLS-1$
289 return Util.bind("operation.needAbsolutePath", getPath().toString()); //$NON-NLS-1$
291 case TARGET_EXCEPTION :
292 return Util.bind("status.targetException"); //$NON-NLS-1$
294 case UPDATE_CONFLICT :
295 return Util.bind("status.updateConflict"); //$NON-NLS-1$
297 case NO_LOCAL_CONTENTS :
298 return Util.bind("status.noLocalContents", getPath().toString()); //$NON-NLS-1$
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 Util.bind("classpath.unboundContainerPath", description); //$NON-NLS-1$
309 // case INVALID_CP_CONTAINER_ENTRY:
310 // path = this.fPath;
311 // javaProject = (IJavaProject)fElements[0];
312 // IClasspathContainer container = null;
313 // description = null;
315 // container = PHPCore.getClasspathContainer(path, javaProject);
316 // } catch(JavaModelException e){
318 // if (container == null) {
319 // initializer = PHPCore.getClasspathContainerInitializer(path.segment(0));
320 // if (initializer != null) description = initializer.getDescription(path, javaProject);
322 // description = container.getDescription();
324 // if (description == null) description = path.makeRelative().toString();
325 // return Util.bind("classpath.invalidContainer", description); //$NON-NLS-1$
327 // case CP_VARIABLE_PATH_UNBOUND:
328 // path = this.fPath;
329 // return Util.bind("classpath.unboundVariablePath", path.makeRelative().toString()); //$NON-NLS-1$
331 // case CLASSPATH_CYCLE:
332 // javaProject = (IJavaProject)fElements[0];
333 // return Util.bind("classpath.cycle", javaProject.getElementName()); //$NON-NLS-1$
335 // case DISABLED_CP_EXCLUSION_PATTERNS:
336 // path = this.fPath;
337 // return Util.bind("classpath.disabledExclusionPatterns", path.makeRelative().toString()); //$NON-NLS-1$
339 // case DISABLED_CP_MULTIPLE_OUTPUT_LOCATIONS:
340 // path = this.fPath;
341 // return Util.bind("classpath.disabledMultipleOutputLocations", path.makeRelative().toString()); //$NON-NLS-1$
343 if (fString != null) {
346 return ""; // //$NON-NLS-1$
349 String message = exception.getMessage();
350 if (message != null) {
353 return exception.toString();
358 * @see IJavaModelStatus#getPath()
360 public IPath getPath() {
364 * @see IStatus#getSeverity()
366 public int getSeverity() {
367 if (fChildren == fgEmptyChildren)
368 return super.getSeverity();
370 for (int i = 0, max = fChildren.length; i < max; i++) {
371 int childrenSeverity = fChildren[i].getSeverity();
372 if (childrenSeverity > severity) {
373 severity = childrenSeverity;
379 * @see IJavaModelStatus#getString()
382 public String getString() {
386 * @see IJavaModelStatus#isDoesNotExist()
388 public boolean isDoesNotExist() {
389 return getCode() == ELEMENT_DOES_NOT_EXIST;
392 * @see IStatus#isMultiStatus()
394 public boolean isMultiStatus() {
395 return fChildren != fgEmptyChildren;
398 * @see IStatus#isOK()
400 public boolean isOK() {
401 return getCode() == OK;
404 * @see IStatus#matches(int)
406 public boolean matches(int mask) {
407 if (!isMultiStatus()) {
408 return matches(this, mask);
410 for (int i = 0, max = fChildren.length; i < max; i++) {
411 if (matches((JavaModelStatus) fChildren[i], mask))
418 * Helper for matches(int).
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);
428 * Creates and returns a new <code>IJavaModelStatus</code> that is a
429 * a multi-status status.
431 * @see IStatus#isMultiStatus()
433 public static IJavaModelStatus newMultiStatus(IJavaModelStatus[] children) {
434 JavaModelStatus jms = new JavaModelStatus();
435 jms.fChildren = children;
439 * Returns a printable representation of this exception for debugging
442 public String toString() {
443 if (this == VERIFIED_OK) {
444 return "JavaModelStatus[OK]"; //$NON-NLS-1$
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();