001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.dialogs;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.core.runtime.IStatus;
014:
015: import org.eclipse.jdt.ui.JavaUI;
016:
017: /**
018: * A settable IStatus.
019: * Can be an error, warning, info or ok. For error, info and warning states,
020: * a message describes the problem.
021: */
022: public class StatusInfo implements IStatus {
023:
024: public static final IStatus OK_STATUS = new StatusInfo();
025:
026: private String fStatusMessage;
027: private int fSeverity;
028:
029: /**
030: * Creates a status set to OK (no message)
031: */
032: public StatusInfo() {
033: this (OK, null);
034: }
035:
036: /**
037: * Creates a status .
038: * @param severity The status severity: ERROR, WARNING, INFO and OK.
039: * @param message The message of the status. Applies only for ERROR,
040: * WARNING and INFO.
041: */
042: public StatusInfo(int severity, String message) {
043: fStatusMessage = message;
044: fSeverity = severity;
045: }
046:
047: /**
048: * Returns if the status' severity is OK.
049: */
050: public boolean isOK() {
051: return fSeverity == IStatus.OK;
052: }
053:
054: /**
055: * Returns if the status' severity is WARNING.
056: */
057: public boolean isWarning() {
058: return fSeverity == IStatus.WARNING;
059: }
060:
061: /**
062: * Returns if the status' severity is INFO.
063: */
064: public boolean isInfo() {
065: return fSeverity == IStatus.INFO;
066: }
067:
068: /**
069: * Returns if the status' severity is ERROR.
070: */
071: public boolean isError() {
072: return fSeverity == IStatus.ERROR;
073: }
074:
075: /**
076: * @see IStatus#getMessage
077: */
078: public String getMessage() {
079: return fStatusMessage;
080: }
081:
082: /**
083: * Sets the status to ERROR.
084: * @param errorMessage The error message (can be empty, but not null)
085: */
086: public void setError(String errorMessage) {
087: Assert.isNotNull(errorMessage);
088: fStatusMessage = errorMessage;
089: fSeverity = IStatus.ERROR;
090: }
091:
092: /**
093: * Sets the status to WARNING.
094: * @param warningMessage The warning message (can be empty, but not null)
095: */
096: public void setWarning(String warningMessage) {
097: Assert.isNotNull(warningMessage);
098: fStatusMessage = warningMessage;
099: fSeverity = IStatus.WARNING;
100: }
101:
102: /**
103: * Sets the status to INFO.
104: * @param infoMessage The info message (can be empty, but not null)
105: */
106: public void setInfo(String infoMessage) {
107: Assert.isNotNull(infoMessage);
108: fStatusMessage = infoMessage;
109: fSeverity = IStatus.INFO;
110: }
111:
112: /**
113: * Sets the status to OK.
114: */
115: public void setOK() {
116: fStatusMessage = null;
117: fSeverity = IStatus.OK;
118: }
119:
120: /*
121: * @see IStatus#matches(int)
122: */
123: public boolean matches(int severityMask) {
124: return (fSeverity & severityMask) != 0;
125: }
126:
127: /**
128: * Returns always <code>false</code>.
129: * @see IStatus#isMultiStatus()
130: */
131: public boolean isMultiStatus() {
132: return false;
133: }
134:
135: /*
136: * @see IStatus#getSeverity()
137: */
138: public int getSeverity() {
139: return fSeverity;
140: }
141:
142: /*
143: * @see IStatus#getPlugin()
144: */
145: public String getPlugin() {
146: return JavaUI.ID_PLUGIN;
147: }
148:
149: /**
150: * Returns always <code>null</code>.
151: * @see IStatus#getException()
152: */
153: public Throwable getException() {
154: return null;
155: }
156:
157: /**
158: * Returns always the error severity.
159: * @see IStatus#getCode()
160: */
161: public int getCode() {
162: return fSeverity;
163: }
164:
165: /**
166: * Returns always an empty array.
167: * @see IStatus#getChildren()
168: */
169: public IStatus[] getChildren() {
170: return new IStatus[0];
171: }
172:
173: /**
174: * Returns a string representation of the status, suitable
175: * for debugging purposes only.
176: */
177: public String toString() {
178: StringBuffer buf = new StringBuffer();
179: buf.append("StatusInfo "); //$NON-NLS-1$
180: if (fSeverity == OK) {
181: buf.append("OK"); //$NON-NLS-1$
182: } else if (fSeverity == ERROR) {
183: buf.append("ERROR"); //$NON-NLS-1$
184: } else if (fSeverity == WARNING) {
185: buf.append("WARNING"); //$NON-NLS-1$
186: } else if (fSeverity == INFO) {
187: buf.append("INFO"); //$NON-NLS-1$
188: } else {
189: buf.append("severity="); //$NON-NLS-1$
190: buf.append(fSeverity);
191: }
192: buf.append(": "); //$NON-NLS-1$
193: buf.append(fStatusMessage);
194: return buf.toString();
195: }
196: }
|